I don't know what I'm doing wrong, perhapes I don't know what i'm doing... I could really use some help. This is my first compiler and I'm trying to design something very simple.
here's my flex file
Code:
%{
#include <stdlib.h>
#include "compile.h"
%}
%%
[ \t]+
"+" {return PLUS;}
"-" {return MINUS;}
"*" {return TIMES;}
"/" {return DIVIDE;}
"(" {return LEFT;}
")" {return RIGHT;}
"\n" {return END;}
[0-9]+ {yylval=atof(yytext); return NUMBER;}
%%
yywrap() {
return 1;
}
and here's my bison file
Code:
%{
#define YYSTYPE double
#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
%left PLUS
%left MINUS
%left TIMES
%left DIVIDE
%start Input
%%
Input:
| Input Line
;
Line: '\n'
| Expression '\n' { printf ("\t%.10g\n", $1); }
;
Expression:
NUMBER { $$=$1; }
| Expression PLUS Expression { $$=$1+$3; printf("test");}
| Expression MINUS Expression { $$=$1-$3; }
| Expression TIMES Expression { $$=$1*$3; }
| Expression DIVIDE Expression { $$=$1/$3; if ($1 = 0) printf("testagain");}
| LEFT Expression RIGHT { $$=$2; }
;
%%
int yyerror(char *errMessage) {
printf("Trouble: %s\n",errMessage);
}
main() {
yyparse();
}
So you can see I'm trying to impliment a very simple calculator, I go through the cmds
flex lang.lex
bison -d -o compile.c land.y
gcc -o compile compile.c lex.yy.c
compile < test.txt (which is say "5+6") and I get a parse error
whats going on?