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