flex lexer - Bison, simple calculator program -
i want make parser simple calculator cant uderstand whu error simple input. flex file looks this
%{ #include "exp.tab.h" #include <string.h> %} blanks [ \t\n]+ %% {blanks} { /* ignore */ } [0-9]+ {yylval= strtol(yytext, null, 10); return(numb);} %% bison file looks this:
%{ #include <stdio.h> %} %token numb %left '+' %% exp: numb { $$ = $1; } | exp '+' exp { $$ = $1 + $3; } %% int yyerror(char *s) { printf("yyerror : %s\n",s); } int main(void) { yyparse(); } for input
123 + 12 i error message.why happening?
your lexer missing rule match/return '+' token. try adding @ end:
. { return *yytext; } /* other single character returns */ the default lexer action if text doesn't match rule print , skip it. error parser because gets 123 12 input, causes syntax error.
Comments
Post a Comment