CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2011
    Posts
    2

    Help I can't get this simple flex/bison compiler to work

    I just want it to perform simple arithmetic, but I'm doing something wrong

    This is the lex file

    %{
    #include <stdlib.h>
    #include "compile.h"
    %}

    %%
    "+" {return PLUS;}
    "-" {return MINUS;}
    "*" {return TIMES;}
    "/" {return DIVIDE;}
    "(" {return LEFT;}
    ")" {return RIGHT;}
    "\n" {return END;}
    [0-9]+ {yylval=atoi(yytext); return NUMBER;}
    [ \t]+

    %%
    yywrap() {
    return 1;
    }

    and this is the bison file

    %{
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "compile.h"
    %}

    %token NUMBER
    %token PLUS
    %token MINUS
    %token TIMES
    %token DIVIDE
    %token LEFT
    %token RIGHT
    %token END

    %start Input
    %%

    Input:

    | Input Line
    ;

    Line:
    END
    | Expression END { printf("Result: %f\n", $1); }
    ;


    Expression:
    NUMBER { $$=$1; }
    | Expression PLUS Expression { $$=$1+$3; }
    | Expression MINUS Expression { $$=$1-$3; }
    | Expression TIMES Expression { $$=$1*$3; }
    | Expression DIVIDE Expression { $$=$1/$3; }
    | LEFT Expression RIGHT { $$=$2; }
    ;

    %%

    int yyerror(char *errMessage) {
    printf("Trouble: %s\n",errMessage);
    }

    main() {
    yyparse();
    }

    I think you can see what I'm tring to do, but when I try to compile I get "16 shift reduce conflicts" and the trouble parse error when I test it

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    Re: Help I can't get this simple flex/bison compiler to work

    there isnt much c++ code there.

    the only thing your main does is call yyparse(), which isnt anywhere in you poorly formatted code. Please use code tags.

    What is your c++ question?

  3. #3
    Join Date
    Oct 2006
    Posts
    616

    Re: Help I can't get this simple flex/bison compiler to work

    Quote Originally Posted by Amleto View Post
    the only thing your main does is call yyparse(), which isnt anywhere in you poorly formatted code.
    This is because this is not C++ or C code at all. This is bison(or yacc) BNF input format.
    Maybe this yacc calculator example will prove handy ?
    I am absolutely *not* the master of bison(or yacc), but there is at least one thing I noticed you failed in doing: defining operator precedences (using &#37;left, %right).

    Regards,
    Zachm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured