作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 bison 解析器。
要在错误消息中获取预期和意外的 token 名称,需要在生成的 y.go 文件中设置变量 YYERROR_VERBOSE
。如何从 .y 文件中设置它?
最佳答案
这是一段语法
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <strings.h>
#include "calc.h"
int yylex(void);
void yyerror(char *s);
symrec * symTable;
/**
temporary variable declarations
*/
linkedList * temp_list = NULL;
linkedList * programRoutineList = NULL;
linkedList * argList = NULL;
program * prg;
%}
%error-verbose
%union {
int iValue;
symrec * sRec;
nodeType *nPtr;
basicType basic;
parameter *parameter;
routine * routine;
linkedList *list;
};
%token <iValue> INTEGER
%token <sRec> VARIABLE
%token WHILE IF PRINT FOR TO
%nonassoc IFX
%nonassoc ELSE
…
…
这是 yacc 文件的开头。作为 yacc 源文件的一部分,这是声明部分,如您所见,用户可以在其中定义几乎任何类型的有用内容(C 兼容)。
作为指令的一部分,您可以看到 %error-verbose
指令,它被 yacc 解释为在发出错误信号时对“用户”错误更加友好的指令。有关此类指令的更详细 View ,请查看 error-verbose the bison manual (记得使用谷歌:它知道很多东西;))
有关完整示例,请查看 this error tracker example .你需要 error_tracker_lexer.l 和 error_tracker.y 来让它工作。您可以在同一个目录中找到它们。编译后,您可以将其用作 my_exec < input_error_tracker ...预期的输出是
at line 2 follows detail on columns--> 2.3-2.4: division by zero
returning 1 as default action
好的是,您可以构建自己的文件,其中包含例如:89 + 广告
输出将是
syntax error, unexpected '\n', expecting NUM or '-' or '('
希望这会有所帮助..
关于bison - 如何在 .y 文件中定义 YYERROR_VERBOSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33430619/
我是一名优秀的程序员,十分优秀!