问题描述
- ubuntu下bison编写语法分析器的问题
-
用flex和bison编写语法分析器,但是有一行提示语法错误,涉及到“REALNUM”和“INTEGER”的产生式都会报错,求解答。
测试内容为:
main()
{
int a;
double b;
a=true;
a=false;
}
此时能通过,但是测试内容为
main()
{
int a;
double b;
a=1;
b=1.4;
}
就会报第5行第6行语法错误。求解。
相关代码如下:
flex.l中:
IDENTIFIER[a-zA-Z_$][a-zA-Z0-9_$]*
INTEGER ^-?[0-9]+$
REALNUM ^(-?[0-9]+)(.[0-9]+)?$
…………
{IDENTIFIER} {ids++; yylval.id=(char*)strdup(yytext);
return IDENTIFIER;}
{INTEGER} {ints++; yylval.interval = atoi(yytext);
return INTEGER;}
{REALNUM} {reals++;yylval.realval = atof(yytext);
return REALNUM;}
{WHITESPACE} {}
{NEWLINE} {lineno++;}
syntax.y中
program:
MAIN '(' ')' SBLOCK {printf("(%d)t program ->MAIN() SBLOCKn",linenum++);}
;SBLOCK:
'{' DECLS STMTS '}' {printf("(%d)t SBLOCK -> { DECLS STMTS }n",linenum++);}
;DECLS:
DECLS DECL {printf("(%d)tDECLS -> DECLS DECLn",linenum++);}
| {printf("(%d)tDECLS -> en",linenum++);}
;DECL:
INT IDENTIFIER ';' {printf("(%d)tDECL -> INT %sn",linenum++,$2);}
|REAL IDENTIFIER ';' {printf("(%d)tDECL -> REAL %sn",linenum++,$2);}
;STMTS:
STMTS STMT {printf("(%d)tSTMTS -> STMTS STMTn",linenum++);}
| {printf("(%d)tSTMTS -> en",linenum++);}
;STMT:
IDENTIFIER ASSGNOP BEXP ';' {printf("(%d)t STMT -> %s = BEXPn",linenum++,$1);}
|IF '(' BEXP ')' STMT %prec IF_THEN {printf("(%d)t STMT -> IF ( BEXP ) STMTn",linenum++);}
|IF '(' BEXP ')' STMT ELSE STMT {printf("(%d)t STMT -> IF ( BEXP ) STMT ELSE STMTn",linenum++);}
|WHILE '(' BEXP ')' STMT {printf("(%d)t STMT -> WHILE ( BEXP ) SBLOCKn",linenum++);}
|DO STMT WHILE '(' BEXP ')' ';' {printf("(%d)t STMT -> DO SBLOCK WHILE ( BEXP ) ;n",linenum++);}
;BEXP:
BEXP OR JOIN {printf("(%d)tBEXP -> BEXP || JOINn",linenum++);}
|JOIN {printf("(%d)tBEXP -> JOIN n",linenum++);}
;
JOIN:
JOIN AND EQEXP {printf("(%d)tJOIN -> JOIN && EQEXPn",linenum++);}
|EQEXP {printf("(%d)tJOIN -> EQEXPn",linenum++);}
;
EQEXP:
EQEXP EQ REL {printf("(%d)tEQEXP -> EQEXP == RELn",linenum++);}
|EQEXP NE REL {printf("(%d)tEQEXP -> EQEXP != RELn",linenum++);}
|REL {printf("(%d)tEQEXP -> RELn",linenum++);}
;
REL:
EXP GT EXP {printf("(%d)tREL -> EXP > EXPn",linenum++);}
|EXP LT EXP {printf("(%d)tREL -> EXP < EXPn",linenum++);}
|EXP GE EXP {printf("(%d)tREL -> EXP >= EXPn",linenum++);}
|EXP LE EXP {printf("(%d)tREL -> EXP <= EXPn",linenum++);}
|EXP {printf("(%d)tREL -> EXPn",linenum++);}
;
EXP:
EXP '+' TERM {printf("(%d)tEXP -> EXP + TERMn",linenum++);}
|EXP '-' TERM {printf("(%d)tEXP -> EXP - TERMn",linenum++);}
|TERM {printf("(%d)tEXP -> TERMn",linenum++);}
;
TERM:
TERM '*' UNARY {printf("(%d)tTERM -> TERM * UNARYn",linenum++);}
|TERM '/' UNARY {printf("(%d)tTERM -> TERM / UNARYn",linenum++);}
|UNARY {printf("(%d)tTERM -> UNARYn",linenum++);}
;
UNARY:
NOT UNARY {printf("(%d)tUNARY -> ! UNARYn",linenum++);}
//|'-' UNARY {printf("(%d)tUNARY -> - UNARYn",linenum++);}
|FACTOR {printf("(%d)tUNARY -> FACTORn",linenum++);}
;
FACTOR:
'(' BEXP ')' {printf("(%d)tFACTOR -> ( BEXP )n",linenum++);}
|INTEGER {printf("(%d)tFACTOR -> %dn",linenum++,$1);}
|REALNUM {printf("(%d)tFACTOR -> %fn",linenum++,$1);}
|IDENTIFIER {printf("(%d)tFACTOR -> %sn",linenum++,$1);}
|TRUE {printf("(%d)tFACTOR -> truen",linenum++);}
|FALSE {printf("(%d)tFACTOR -> falsen",linenum++);}
;