|
-
February 27th, 2011, 02:51 PM
#1
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
-
February 27th, 2011, 06:51 PM
#2
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?
-
February 28th, 2011, 03:20 AM
#3
Re: Help I can't get this simple flex/bison compiler to work
 Originally Posted by Amleto
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 %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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|