Adding two tokens together (integers). Flex/Bison












0















I have a simple Bison file with some simple grammar. I am taking an unending list of expressions, and my goal is to make a single definition for the expression that adds the values of two tokens together.



At line 20, there is a grammar rule that shows what I am trying to achieve. Unfortunately, it does not work. How can I achieve this functionality?



Bison File



%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
void yyerror(const char* msg);
%}

%token INT
%token PLUS
%token END


%%
expr_list: END
{ exit(0); }
| expr
| expr expr_list
;

expr: INT PLUS INT
{ printf("%dn", ($1 + $3)); }
;

%%


int main(int argc, char** argv) {
yyparse();
return 0;
}


void yyerror(const char* msg) {
fprintf(stderr, "ERROR! %sn", msg);
}




Flex File



%{
#include "adder.tab.h"
%}

%%
(+) return PLUS;
[0-9]+ return INT;
(END) return END;
%%









share|improve this question

























  • please copy the file here instead of in an external link. questions should be self-contained

    – phuclv
    Nov 27 '18 at 3:37











  • @phuclv added bison and flex source.

    – Farid Karadsheh
    Nov 27 '18 at 3:43











  • Can you clarify "does not work"? In what way does it not work? Do you get an error? An unexpected result? Something else? In either case, what's your input, expected output and actual output or error message?

    – sepp2k
    Nov 27 '18 at 7:35











  • @sepp2k Everything gets up and running fine. When I type in "END" the program exits. When I type in 10 + 5, 0 is printed. This is likely due to a poorly defined grammar rule.

    – Farid Karadsheh
    Nov 27 '18 at 14:31
















0















I have a simple Bison file with some simple grammar. I am taking an unending list of expressions, and my goal is to make a single definition for the expression that adds the values of two tokens together.



At line 20, there is a grammar rule that shows what I am trying to achieve. Unfortunately, it does not work. How can I achieve this functionality?



Bison File



%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
void yyerror(const char* msg);
%}

%token INT
%token PLUS
%token END


%%
expr_list: END
{ exit(0); }
| expr
| expr expr_list
;

expr: INT PLUS INT
{ printf("%dn", ($1 + $3)); }
;

%%


int main(int argc, char** argv) {
yyparse();
return 0;
}


void yyerror(const char* msg) {
fprintf(stderr, "ERROR! %sn", msg);
}




Flex File



%{
#include "adder.tab.h"
%}

%%
(+) return PLUS;
[0-9]+ return INT;
(END) return END;
%%









share|improve this question

























  • please copy the file here instead of in an external link. questions should be self-contained

    – phuclv
    Nov 27 '18 at 3:37











  • @phuclv added bison and flex source.

    – Farid Karadsheh
    Nov 27 '18 at 3:43











  • Can you clarify "does not work"? In what way does it not work? Do you get an error? An unexpected result? Something else? In either case, what's your input, expected output and actual output or error message?

    – sepp2k
    Nov 27 '18 at 7:35











  • @sepp2k Everything gets up and running fine. When I type in "END" the program exits. When I type in 10 + 5, 0 is printed. This is likely due to a poorly defined grammar rule.

    – Farid Karadsheh
    Nov 27 '18 at 14:31














0












0








0








I have a simple Bison file with some simple grammar. I am taking an unending list of expressions, and my goal is to make a single definition for the expression that adds the values of two tokens together.



At line 20, there is a grammar rule that shows what I am trying to achieve. Unfortunately, it does not work. How can I achieve this functionality?



Bison File



%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
void yyerror(const char* msg);
%}

%token INT
%token PLUS
%token END


%%
expr_list: END
{ exit(0); }
| expr
| expr expr_list
;

expr: INT PLUS INT
{ printf("%dn", ($1 + $3)); }
;

%%


int main(int argc, char** argv) {
yyparse();
return 0;
}


void yyerror(const char* msg) {
fprintf(stderr, "ERROR! %sn", msg);
}




Flex File



%{
#include "adder.tab.h"
%}

%%
(+) return PLUS;
[0-9]+ return INT;
(END) return END;
%%









share|improve this question
















I have a simple Bison file with some simple grammar. I am taking an unending list of expressions, and my goal is to make a single definition for the expression that adds the values of two tokens together.



At line 20, there is a grammar rule that shows what I am trying to achieve. Unfortunately, it does not work. How can I achieve this functionality?



Bison File



%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
void yyerror(const char* msg);
%}

%token INT
%token PLUS
%token END


%%
expr_list: END
{ exit(0); }
| expr
| expr expr_list
;

expr: INT PLUS INT
{ printf("%dn", ($1 + $3)); }
;

%%


int main(int argc, char** argv) {
yyparse();
return 0;
}


void yyerror(const char* msg) {
fprintf(stderr, "ERROR! %sn", msg);
}




Flex File



%{
#include "adder.tab.h"
%}

%%
(+) return PLUS;
[0-9]+ return INT;
(END) return END;
%%






bison






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 5:56







Farid Karadsheh

















asked Nov 27 '18 at 3:35









Farid KaradshehFarid Karadsheh

218210




218210













  • please copy the file here instead of in an external link. questions should be self-contained

    – phuclv
    Nov 27 '18 at 3:37











  • @phuclv added bison and flex source.

    – Farid Karadsheh
    Nov 27 '18 at 3:43











  • Can you clarify "does not work"? In what way does it not work? Do you get an error? An unexpected result? Something else? In either case, what's your input, expected output and actual output or error message?

    – sepp2k
    Nov 27 '18 at 7:35











  • @sepp2k Everything gets up and running fine. When I type in "END" the program exits. When I type in 10 + 5, 0 is printed. This is likely due to a poorly defined grammar rule.

    – Farid Karadsheh
    Nov 27 '18 at 14:31



















  • please copy the file here instead of in an external link. questions should be self-contained

    – phuclv
    Nov 27 '18 at 3:37











  • @phuclv added bison and flex source.

    – Farid Karadsheh
    Nov 27 '18 at 3:43











  • Can you clarify "does not work"? In what way does it not work? Do you get an error? An unexpected result? Something else? In either case, what's your input, expected output and actual output or error message?

    – sepp2k
    Nov 27 '18 at 7:35











  • @sepp2k Everything gets up and running fine. When I type in "END" the program exits. When I type in 10 + 5, 0 is printed. This is likely due to a poorly defined grammar rule.

    – Farid Karadsheh
    Nov 27 '18 at 14:31

















please copy the file here instead of in an external link. questions should be self-contained

– phuclv
Nov 27 '18 at 3:37





please copy the file here instead of in an external link. questions should be self-contained

– phuclv
Nov 27 '18 at 3:37













@phuclv added bison and flex source.

– Farid Karadsheh
Nov 27 '18 at 3:43





@phuclv added bison and flex source.

– Farid Karadsheh
Nov 27 '18 at 3:43













Can you clarify "does not work"? In what way does it not work? Do you get an error? An unexpected result? Something else? In either case, what's your input, expected output and actual output or error message?

– sepp2k
Nov 27 '18 at 7:35





Can you clarify "does not work"? In what way does it not work? Do you get an error? An unexpected result? Something else? In either case, what's your input, expected output and actual output or error message?

– sepp2k
Nov 27 '18 at 7:35













@sepp2k Everything gets up and running fine. When I type in "END" the program exits. When I type in 10 + 5, 0 is printed. This is likely due to a poorly defined grammar rule.

– Farid Karadsheh
Nov 27 '18 at 14:31





@sepp2k Everything gets up and running fine. When I type in "END" the program exits. When I type in 10 + 5, 0 is printed. This is likely due to a poorly defined grammar rule.

– Farid Karadsheh
Nov 27 '18 at 14:31












1 Answer
1






active

oldest

votes


















1














In your parser you have this:



expr:         INT PLUS INT
{ printf("%dn", ($1 + $3)); }


This takes the semantic values of the two INT tokens and adds them, which is fine. If this always produces 0, that means there must be something wrong with the tokens' semantic values. So let's look at the corresponding lexer action:



[0-9]+      return INT;


Here you never assign anything to yylval, so $1 and $3 in your parser rule will be unassigned. To assign a meaningful semantic value you can use strtol to convert the string in yytext to an integer:



[0-9]+      yylval = strtol(yytext, NULL, 10); return INT;


For proper error handling for numbers outside of int's range, you should check errno after calling strtol.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492354%2fadding-two-tokens-together-integers-flex-bison%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    In your parser you have this:



    expr:         INT PLUS INT
    { printf("%dn", ($1 + $3)); }


    This takes the semantic values of the two INT tokens and adds them, which is fine. If this always produces 0, that means there must be something wrong with the tokens' semantic values. So let's look at the corresponding lexer action:



    [0-9]+      return INT;


    Here you never assign anything to yylval, so $1 and $3 in your parser rule will be unassigned. To assign a meaningful semantic value you can use strtol to convert the string in yytext to an integer:



    [0-9]+      yylval = strtol(yytext, NULL, 10); return INT;


    For proper error handling for numbers outside of int's range, you should check errno after calling strtol.






    share|improve this answer






























      1














      In your parser you have this:



      expr:         INT PLUS INT
      { printf("%dn", ($1 + $3)); }


      This takes the semantic values of the two INT tokens and adds them, which is fine. If this always produces 0, that means there must be something wrong with the tokens' semantic values. So let's look at the corresponding lexer action:



      [0-9]+      return INT;


      Here you never assign anything to yylval, so $1 and $3 in your parser rule will be unassigned. To assign a meaningful semantic value you can use strtol to convert the string in yytext to an integer:



      [0-9]+      yylval = strtol(yytext, NULL, 10); return INT;


      For proper error handling for numbers outside of int's range, you should check errno after calling strtol.






      share|improve this answer




























        1












        1








        1







        In your parser you have this:



        expr:         INT PLUS INT
        { printf("%dn", ($1 + $3)); }


        This takes the semantic values of the two INT tokens and adds them, which is fine. If this always produces 0, that means there must be something wrong with the tokens' semantic values. So let's look at the corresponding lexer action:



        [0-9]+      return INT;


        Here you never assign anything to yylval, so $1 and $3 in your parser rule will be unassigned. To assign a meaningful semantic value you can use strtol to convert the string in yytext to an integer:



        [0-9]+      yylval = strtol(yytext, NULL, 10); return INT;


        For proper error handling for numbers outside of int's range, you should check errno after calling strtol.






        share|improve this answer















        In your parser you have this:



        expr:         INT PLUS INT
        { printf("%dn", ($1 + $3)); }


        This takes the semantic values of the two INT tokens and adds them, which is fine. If this always produces 0, that means there must be something wrong with the tokens' semantic values. So let's look at the corresponding lexer action:



        [0-9]+      return INT;


        Here you never assign anything to yylval, so $1 and $3 in your parser rule will be unassigned. To assign a meaningful semantic value you can use strtol to convert the string in yytext to an integer:



        [0-9]+      yylval = strtol(yytext, NULL, 10); return INT;


        For proper error handling for numbers outside of int's range, you should check errno after calling strtol.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 27 '18 at 14:53

























        answered Nov 27 '18 at 14:45









        sepp2ksepp2k

        296k38596613




        296k38596613
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492354%2fadding-two-tokens-together-integers-flex-bison%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks