0
从我的bison/flex文件生成的程序对第一个输入(yyparse()的第一次运行)正常工作,但在尝试运行时会抛出错误这是第二次。当我的语言输入第二个输入时,我收到一个错误
例如:
[email protected]:~/4850/hw$ ./calc
HORN 3+4*6-11+33*2 ?
= 82
HORN 1+1 ?
error: syntax error
基本上, 'HORN' 之间存在的命令 '?'
这是我的.Y文件:
%{
#include <stdlib.h>
#include <stdio.h>
void yyerror(const char *str);
%}
%union
{
int ival;
float fval;
char* word;
}
%start line
%type <word> wexp
%type <ival> iexp
%type <fval> fexp
%left PLUS MINUS
%left STAR DIV MOD
%left POW
%token GT
%token LT
%token ASGN
%token LP
%token RP
%token LB
%token RB
%token NOT
%token GTEQ
%token LTEQ
%token EQTO
%token NOTEQ
%token HORN
%token QMARK
%token <word> WORD
%token <fval> FLOAT
%token <ival> INTEGER
%%
line : HORN wexp QMARK { printf("=\t%s\n", $2); }
| HORN iexp QMARK { printf("=\t%d\n", $2); }
| HORN fexp QMARK { printf("=\t%f\n", $2); }
;
iexp : iexp MINUS iexp { $$ = $1 - $3; }
| iexp PLUS iexp { $$ = $1 + $3; }
| iexp STAR iexp { $$ = $1 * $3; }
| iexp DIV iexp { $$ = $1/$3; }
| INTEGER { $$ = $1; }
;
fexp : FLOAT { $$ = $1; }
;
wexp : WORD { $$ = $1; }
;
%%
int main(){
yyparse();
}
void yyerror(const char *str)
{
printf("error: %s\n",str);
}
感谢任何投入!
语法需要换行。 – BLUEPIXY 2014-11-06 10:14:20
@BLUEPIXY:我已经在我的.l flex文件中作为正则表达式[\ t \ n] {;} – 2014-11-06 11:06:55
您应该使用flex-lexer标记而不是flex。我已经为你编辑了这个。 – 2014-11-06 15:07:48