this is my bison code:
%} %union { int int_val; } %left '+' '-' %nonassoc '(' ')' %token INTEGER PRINT %type <int_val> expr_int INTEGER %% program: command '\n' { return 0; } ; command: print_expr ; print_expr: PRINT expr_int { cout<<$2<<endl; } expr_int: expr_int '+' expr_int { $$ = $1 + $3; } | expr_int '-' expr_int { $$ = $1 - $3; } | '(' expr_int ')' { $$ = $2; } | INTEGER ; and this is the flex code:
%} INTEGER [1-9][0-9]*|0 BINARY [-+] WS [ \t]+ BRACKET [\(\)] %% print{WS} { return PRINT; } {INTEGER} { yylval.int_val=atoi(yytext); return INTEGER; } {BINARY}|\n { return *yytext; } {BRACKET} { return *yytext; } {WS} {} . { return *yytext; } %% ////////////////////////////////////////////////// int yywrap(void) { return 1; } // Callback at end of file Invalid inputs for the program are:
print 5 output:
5 input:
print (1+1) output:
2 But for some reason, for the following inputs I do not get immediate error:
print (1+1)) output:
2 some error input:
print 5! output:
5 some error I would like an error to be printed immediately, without commiting the print command and then throwing an error.
How should I change the program so it will not print errornous inputs?