Flex/Bison not evaluating properly
For some reason or another, bison doesn't want to do any evaluation. Compilation of all files goes smoothly and the program runs. When I enter the expression 4+5
and press return, it creates tokens for 4
+
5
respectively. I can even put in some printf
into the places where bison recognizes the attributes of each token including the plus (43).
However the program never evaluates this production expr '+' term { $$ = $1 + $3; }
expr '+' term { $$ = $1 + $3; }
. It's simply never called at least to my knowledge and even if it was this production assign 'n' { printf("%dn", $1); }
assign 'n' { printf("%dn", $1); }
never prints out the value. Upon ^D
to quit, it fires void yyerror(const char *)
.
Any help on this matter is much appreciated. Thanks!
//FLEX
%{
//#include <stdio.h>
#include "y.tab.h"
%}
%option noyywrap
letter [A-Za-z]
digit [0-9]
space [ t]
var {letter}
int {digit}+
ws {space}+
%%
{var} { yylval = (int)yytext[0]; return VAR; }
{int} { yylval = atoi(yytext); return CONST; }
{ws} { }
. { return (int)yytext[0]; }
%%
/* nothing */
.
//BISON
%{
//INCLUDE
//#include <ctype.h>
//DEFINE
#define YYDEBUG 1
//PROTOTYPE
void yyerror(const char *);
void print_welcome();
int get_val(int);
void set_val(int, int);
%}
%token CONST
%token VAR
%%
session
: { print_welcome(); }
eval
;
eval
: eval line
|
;
line
: assign 'n' { printf("%dn", $1); }
;
assign
: VAR '=' expr { set_val($1, $3); $$ = $3; }
| expr { $$ = $1; }
;
expr
: expr '+' term { $$ = $1 + $3; }
| expr '-' term { $$ = $1 - $3; }
| term { $$ = $1; }
;
term
: term '*' factor { $$ = $1 * $3; }
| term '/' factor { $$ = $1 / $3; }
| term '%' factor { $$ = $1 % $3; }
| factor { $$ = $1; }
;
factor
: '(' expr ')' { $$ = $2; }
| CONST { $$ = $1; }
| VAR { $$ = get_val($1); }
;
%%
void yyerror(const char * s)
{
fprintf(stderr, "%sn", s);
}
void print_welcome()
{
printf("Welcome to the Simple Expression Evaluator.n");
printf("Enter one expression per line, end with ^Dnn");
}
static int val_tab[26];
int get_val(int var)
{
return val_tab[var - 'A'];
}
void set_val(int var, int val)
{
val_tab[var - 'A'] = val;
}
.
//MAIN
//PROTOTYPE
int yyparse();
int main()
{
extern int yydebug;
yydebug = 0;
yyparse();
return 0;
}
Your lex
file does not have any rule which matches n
, because in lex
/ flex
, .
matches any character except line-end. The default rule for lex
(or flex
) echoes and otherwise ignores the matched character, so that's what happens to the n
. Since the parser won't be able to accept a line
unless it sees a n
token, it will eventually be forced to present you with a syntax error.
So you need to change the rule
. { return (int)yytext[0]; }
to
.|n { return (int)yytext[0]; }
(I wouldn't have bothered with the cast to int
but it's certainly not doing any harm, so I left it in.)
上一篇: Flex和Bison工具
下一篇: Flex / Bison没有正确评估