我写了以下语法,Bison 警告我有关归约/归约冲突。
\n\nparser.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]\nRun Code Online (Sandbox Code Playgroud)\n\n如何检测语法的哪一部分产生了冲突?Bison 是否生成了日志,可以在其中查看冲突?还有,我该如何解决这个问题?
\n\n语法:
\n\n%left TK_OC_OR TK_OC_AND\n%left \'<\' \'>\' TK_OC_LE TK_OC_GE TK_OC_EQ TK_OC_NE\n%left \'+\' \'-\'\n%left \'*\' \'/\'\n\n%nonassoc LOWER_THAN_ELSE\n%nonassoc TK_PR_ELSE\n\n%start s\n\n%type<symbol> decl_var\n%type<symbol> cabecalho\n\n%%\n\ns: decl_global s\n | def_funcao s\n |\n ;\n\ndecl_global: decl_var \';\'\n | decl_vetor \';\'\n | decl_var {error("Faltando o \';\' no final do comando.", $1->line); return IKS_SYNTAX_ERRO;}\n ;\n\ndecl_local: decl_var \';\' decl_local\n |\n ;\n\ndecl_var\n : tipo_var TK_IDENTIFICADOR {$$ = $2;}\n ;\n\ndecl_vetor\n : tipo_var TK_IDENTIFICADOR \'[\' TK_LIT_INT \']\'\n ;\n\n\ntipo_var: TK_PR_INT\n | TK_PR_FLOAT\n …Run Code Online (Sandbox Code Playgroud) 我回来了,现在正在编写我自己的语言和我的操作系统,但是由于我现在开始开发自己的开发语言,因此在使用Bison时我遇到了一些错误,我不知道如何解决它们.这是我的*.y文件代码:
input:
| input line
;
line: '\n'
| exp '\n' { printf ("\t%.10g\n", $1); }
;
exp: NUM { $$ = $1; }
| exp exp '+' { $$ = $1 + $2; }
| exp exp '-' { $$ = $1 - $2; }
| exp exp '*' { $$ = $1 * $2; }
| exp exp '/' { $$ = $1 / $2; }
/* Exponentiation */
| exp exp '^' { $$ = pow ($1, …Run Code Online (Sandbox Code Playgroud) 我在我的yacc文件中有这个.
var_declaration : type_specifier ID ';' {$2->args = ""; $2->value = 0; $2->arraysize = 0; $2->type = "variable";}
Run Code Online (Sandbox Code Playgroud)
以上一切都有效.
我想把它添加到它.
fn($2);
Run Code Online (Sandbox Code Playgroud)
从函数内部,我想做这样的事情.
fn(struct symtab sp)
{
sp->value = 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译程序时,我收到此错误:
错误:' - >'的无效类型参数(有'struct symtab')
我想从字符串而不是文件解析.我知道v可以使用yy_scan_string fn来做它.但对我来说它不能正常工作所以请帮助我
lex和yacc应该一起使用.
扫描仪是哪一个,解析器是哪一个?
哪一个创建扫描仪,哪一个创建解析器?
如何匹配aa的数量应该是10分钟的aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ...
我的意思是我知道这种方式:
[a][a][a][a][a][a][a][a][a][a][a][a][a]a*b
Run Code Online (Sandbox Code Playgroud)
但是必须有一个更好的优雅方法,如果我的最小数量变为100 ...
它是什么?我试图匹配(a ^ n)b类似于n可以是任何东西的东西
编辑:
我忘了提到这是使用lex和yacc来完成的...其中lex必须将一个令牌返回给yacc.
%{
#include "y.tab.h"
%}
%%
aaaaaaaaaa[a]*b {return ok;}
\n {return '\n';}
. {return 0;}
%%
Run Code Online (Sandbox Code Playgroud) 我一直试图使用YACC(Bison)将中缀表达式转换为后缀表达式但没有成功.我想知道怎么做?示例代码将是真棒:)
我在尝试将Flex和Bison一起使用时遇到了问题.当我使用gcc命令(gcc -c y.tab.c lex.yy.c)进行编译时,我不断收到flex文件的错误说
error: expected identifier or ‘(’ before string constant
Run Code Online (Sandbox Code Playgroud)
这是代码:
FLEX(文件名为arxeioflex.l):
%{
#include "y.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
%}
%option noyywrap
id [a-zA-Z][a-zA-Z0-9]*
num [0-9]*
%%
%%
%%
"extern" {return EXTERN;}
"void" {return VOID;}
"(" {return LP;}
")" {return RP;}
"int" {return INT;}
"bool" {return BOOL;}
"string" {return STRING;}
";" {return SC;}
"," {return S;}
"&" {return DEC;}
"begin" {return BEGIN;}
"end" {return END;}
"{" {return LB;}
"}" {return RB;}
"if" …Run Code Online (Sandbox Code Playgroud) 我有一个 lex 程序如下。我遇到错误
在动作 LEX 程序中遇到 EOF
%{
#include<stdio.h>
#include<math.h>
#include "y.tab.h"
%}
%%
[ \t]+ ;
[0-9]+ {yylval = atoi(yytext);
return INTEGER;}
[-+*/] {return *yytext;}
"(" {return *yytext;}
")" {return *yytext;}
\n {return *yytext;}
. {char msg[25];
sprintf(msg,"%s <%s>","invalid character",yytext);
yyerror(msg);}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?
我真的更喜欢一个有效的例子来解释.无论我到目前为止在Bison的文档网站上所阅读的内容都与Flex所说的相矛盾.有人说要宣布yylex为
int yylex (yyscan_t yyscanner);
Run Code Online (Sandbox Code Playgroud)
另一个人希望它是:
int yylex(YYSTYPE *lvalp, YYLTYPE *llocp);
Run Code Online (Sandbox Code Playgroud)
我真正需要的是位置信息.我现在还不确定是否需要YYSTYPE(我现在没有使用这些信息,但将来我可能会这样做).
与上述无关,作为奖励,我很有兴趣知道为什么这个基础设施如此糟糕.这似乎是一件非常直截了当的事情,但它却非常糟糕.它从不适用于默认值.即使编写一个最简单的教科书计算器示例,也需要多天修复配置错误...为什么?
这里给出了完整的例子:
import ply.lex as lex
import Property
# List of token names. This is always required
tokens = [
'CheckupInformation',
'Introduction',
'Information',
'perfect',
'sick',
'LPAREN',
'RPAREN',
'CHAR',
'NUMBER'
]
def t_CheckupInformation(t) : 'CheckupInformation' ; return t
def t_Introduction(t) : 'Introduction' ; return t
def t_Information(t) : 'Information' ; return t
def t_perfect(t): 'perfect'; return t
def t_sick(t) : 'sick'; return t
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_CHAR = r'[a-zA-Z_][a-zA-Z0-9_\-]*'
t_ignore = " \t"
# Define a rule so we …Run Code Online (Sandbox Code Playgroud)