- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,当我运行以下 Bison 时,我发现 $$ 没有返回类型:
%{
#include <stdio.h>
#include <string.h>
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap()
{
return 1;
}
main()
{
yyparse();
}
%}
%start query
%token AND OR GREATER LESS COLON TO
%union
{
int number;
char *string;
}
%token <number> VALUE
%token <string> WORD
%%
query: /* empty */
| query expression { }
;
expression:
term
|expression AND term {$$=$1}
|expression OR term {}
;
term:
WORD { printf("Term:%s\t",$1);}
| VALUE
;
以下是我的 flex:
%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
%}
%%
":" return COLON;
"and"|"&"|"&&" return AND;
"or"|"|"|"||" return OR;
".." return TO;
">" return GREATER;
"<" return LESS;
\n /* ignore end of line */;
\t /* ignore end of line */;
%%
这个错误是什么意思?我该如何解决这个问题?
最佳答案
这是因为您缺少声明 %type <...> expression
在语法的第一部分,...
是在您的 %union
中声明的某种类型陈述。没有这个,您将无法访问 $$
在任何 expression
行动。此外,如果您设置 $$
在非终端的任何操作中,您需要在该非终端的所有操作中设置它。
一般来说,您需要为所有非终结符决定它们是否产生可在使用该非终结符的规则中使用的值。如果他们确实产生了这样的值(value),他们需要一个 %type
声明和每个 Action 都需要设置$$
到某事。如果他们不这样做,则没有任何操作可以使用 $1
(或其他)访问不存在的值。
所有终端都需要相同,除了终端,您使用 %token
而不是 %type
, 你需要设置 yylval
在你的词法分析器中,因为这相当于 $$
.
在您的特定示例中,您处理了终端但忽略了非终端。根据您要执行的操作,您可能需要 %type
对于expression
和 term
, 但可能不想要一个 query
.这意味着每个 term
和 expression
规则需要设置 $$
的操作
关于c - Bison 警告 “… has no declared type”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10232142/
我有一个使用 Yacc 编写的语法。语法的相关部分摘录在这里 postfix : primary | postfix '[' expr ']' | postfix '[' ex
如何在 bison/yacc 中使用继承的属性? 说,我有这样的语法 - 程序 -> stmts 我想做的是在 Bison 身上: 程序:stmts {$$.next = newLabel(); $1
有什么方法可以更改 Bison 错误消息的格式?例如,不要说 syntax error, unexpected T_ID, expected ';' or T_IMPORT说 unexpected i
我的语法有这两个标记声明: %token RP %token ELSE 而这两条规则: Statement : IF LP Exp RP Statement; Statement : IF LP
如果有一个表单的输入文件: BEGIN stuff.... END BEGIN stuff ... END 我的 .y 文件是这样的 %token BEGIN %token END %star
我希望为现有语言创建一个 JavaScript 解析器,该语言目前具有笨拙的“手工制作”C# 和 Java 解析器。我想使用 Jison,并且也一直在尝试学习 Bison 的基础知识。 我不确定如何回
目前我正在研究一个源到源编译器,我已经编写了一个野牛解析器,可以正确地为输入创建 AST。我现在需要对语法树进行多次转换,因此我需要向树中插入许多节点。 我可以手动创建所有要添加到语法树中的结构体/联
如果有一个表单的输入文件: BEGIN stuff.... END BEGIN stuff ... END 我的 .y 文件是这样的 %token BEGIN %token END %star
我正在寻找为现有语言创建 JavaScript 解析器的方法,该语言目前具有笨拙的“手工制作”C# 和 Java 解析器。想用Jison,也在努力学习Bison的基础。 一个我不确定如何回答的问题是
我正在使用 flex/bison 编写解析器(我可以用 Python 编写解析器,但我总是更喜欢经典。) 当我用这个编译代码时: gcc -lfl -ly chance.tab.c lex.yy.c
正在使用 flex/bison 开发编译器。我的构建输出中有此警告。 警告:在默认操作中键入冲突 ('s' '') 请问有什么帮助吗? 最佳答案 它似乎与源中的 %token 和 %type 声明有关
考虑这个 lex.l 文件: %{ #include "y.tab.h" %} digit [0-9] letter [a-zA-Z] %% "+"
我正在为 bison 中的一个简单的 Pascal 编译器编写语法,我想可视化我在 pascal.y 中指定的语法的解析树。是否可以基于我的 pascal.y 文件以图形方式显示语法树? 最佳答案 野
我正在尝试编写一个能够使用以下输入的语法: begin #this is a example x = 56; while x > 0 do begin point 15
我正在尝试使用 BNF 语法编写 Flex/Bison 文件。但是,当我尝试编译时出现错误,而且我不确定如何调试它们。 BNF语法: ::= | head() ::=:: | @ | tail() |
我正在用 Flex/Bison 编写一个小程序来标记/解析我创建的查询语言。 我想知道是否有任何方法可以在 Flex/Bison 中创建任何关键字。 我的意思是:flex 将输入分解成一个标记列表,但
我正在尝试使用 flex 和 bison 创建过滤器,因为我想从复杂的语言中获取某些语法元素。我的计划是使用 flex + bison 来识别语法,并转储出感兴趣元素的位置。 (然后使用脚本根据转储的
我正在尝试实现一个可以进行浮点运算的 Flex/bison 计算器。我的 Flex 代码如下所示 %{ #include "calc.tab.h" #include void yyerror(cha
我正在尝试使用 FLEX 和 BISON 进行一些练习。 这是我写的代码: calc_pol.y %{ #define YYSTYPE double #include "calc_pol.tab.h"
我有一个使用括号和方括号作为分隔符的语法。当由 bison 生成的解析器输入带有不平衡分隔符的输入时,传递给 yyerror 的 YYLTYPE* 中的错误位置是输入的结尾。所以,例如,在输入 x
我是一名优秀的程序员,十分优秀!