【发布时间】:2020-03-11 04:23:04
【问题描述】:
如标题中所述,我正在使用 Bison 和 Flex 来获取解析器,但我需要处理错误并在找到错误后继续。因此我使用:
Stmt: Reference '=' Expr ';' { printf(" Reference = Expr ;\n");}
| '{' Stmts '}' { printf("{ Stmts }");}
| WHILE '(' Bool ')' '{' Stmts '}' { printf(" WHILE ( Bool ) { Stmts } ");}
| FOR NAME '=' Expr TO Expr BY Expr '{' Stmts '}' { printf(" FOR NAME = Expr TO Expr BY Expr { Stmts } ");}
| IF '(' Bool ')' THEN Stmt { printf(" IF ( Bool ) THEN Stmt ");}
| IF '(' Bool ')' THEN Stmt ELSE Stmt { printf(" IF ( Bool ) THEN Stmt ELSE Stmt ");}
| READ Reference ';' { printf(" READ Reference ;");}
| WRITE Expr ';' { printf(" WRITE Expr ;");}
| error ';' { yyerror("Statement is not valid"); yyclearin; yyerrok;}
;
但是,我总是收到一条消息“语法错误”,我不知道它来自哪里以及如何防止它,以便执行我自己的“错误代码”。 我正在尝试在这里进行错误恢复,以便我的解析器将继续解析输入直到 EOF。
【问题讨论】:
标签: error-handling compiler-construction bison flex-lexer yacc