|
-
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
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
|