c - Why is the result of the bison is not displayed? -
i tested example of teamwork flex , bison, result of calculation not displayed in console. test.l:
%{ #include "parser.tab.h" %} %option yylineno %option noyywrap %% [/][/].*\n ; // comment [0-9]+ { yylval = atoi(yytext); return num; } [ \t\r\n] ; // whitespace . { return *yytext; } %%
parser.y:
%{ #include <stdio.h> void yyerror(char *s) { fprintf (stderr, "%s\n", s); } %} %token num %start evaluate %% evaluate: expr {printf("=%d\n", $$);} ; expr: expr '+' term { $$ = $1 + $3; } | expr '-' term { $$ = $1 - $3; } | term ; term: term '*' num { $$ = $1 * $3; } | term '/' num { $$ = $1 / $3; } | num ; %% int main() { return yyparse(); }
but if add getchar(), after enter additional character same calculation result displayed. why not change(evaluate: expr{printf("=%d\n", $$); getchar();} ;
) , not see result? sorry english.
you parsing input coming stdin
, "stream". before stream terminated, parser cannot know complete parse tree. example, if enter expression 1+1
, complete input 1+11
, 1+1-1
or 1+11*4
- different expressions lead different parse trees result.
you can create terminated input doing 1 of following:
- pressing ctrl+d after typing in input (on unix shells)
- piping input:
echo "1+1" | ./parser
- reading input file
inputfile.txt
containing input1+1
:
./parser < inputfile.txt
Comments
Post a Comment