无法弄清楚为什么Bison会抛出“由于冲突而在解析器中无用的规则". [英] Can't figure out why Bison is throwing "Rules useless in parser due to conflicts"

查看:568
本文介绍了无法弄清楚为什么Bison会抛出“由于冲突而在解析器中无用的规则".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一种非常简单的编程语言编写BNF语法,并使用Flex和Bison进行编译.
我只有3种变量和常量类型:实数,整数,字符串.
我的.l文件具有"ID"的令牌定义,如下所示:

I'm writing a BNF grammar for a very simple programming language and using Flex and Bison to compile.
I only have 3 variable and constant types: real, integer, string.
My .l file has a token definition for "ID" as follows:

DIGIT [0-9]
LETTER [a-zA-Z]
ID {LETTER}({LETTER}|{DIGIT})*


我的.y文件具有标识符的定义,如下所示:

identifier:
ID;

现在,我想使用标识符定义来构建变量和常量名称.但我也想将赋值限制为相同类型的数据(例如,我不希望将字符串分配给整数变量).因此,我创建了一些规​​则来分隔每种变量和常量:

Now, I want to use the identifier definition to build variable and constant names. But I also want to limit assignment to data of the same type (e.g., I don't want a string assigned to an integer variable). So I created a few rules to separate each kind of variable and constant:

id_variable_string:
identifier;

id_variable_integer:
identifier;

id_variable_real:
identifier;

我对常量也做同样的事情. 现在,用我的语言,我有一个用于常量声明/定义的部分,然后有一个用于变量声明的部分.也就是说,常量是在分配常量的同时声明的(类似于"myConstant = 123"),但必须先声明变量,然后在专门为语句设计的部分中为其赋值.
例如,如果我想要一个整数和一个字符串变量,则必须先声明它们:
STRING myStrVariable;
INTEGER myIntVariable;
然后,在为语句保留的区域中,我可以选择执行一个赋值(一条语句可以是赋值,决定,选择,输出等):

I did the same for constants. Now, in my language I have a section for constant declaration/definition and then a section for variable declaration. That is, constants are declared at the same time as they're assigned (something like "myConstant = 123") but variables have to be declared first, and then assigned a value in the section specifically designed for statements.
E.g., if I want an integer and a string variable, I'd have to declare them first:
STRING myStrVariable;
INTEGER myIntVariable;
And then, in the zone reserved for statements, I can choose to do an assignment (a statement can be an assignment, a decision, a selection, an output, etc.):

assignment: 
        id_variable_string ASSIGN_OPERATOR literal_string
        | id_variable_string ASSIGN_OPERATOR id_const_string 
        | id_variable_string ASSIGN_OPERATOR id_variable_string 
        | id_variable_string ASSIGN_OPERATOR concatenacion  
        | id_variable_integer ASSIGN_OPERATOR id_const_integer 
        | id_variable_integer ASSIGN_OPERATOR id_variable_integer  
        | id_variable_integer ASSIGN_OPERATOR expression 
        | id_variable_integer ASSIGN_OPERATOR literal_integer
        | id_variable_real ASSIGN_OPERATOR id_variable_real
        | id_variable_real ASSIGN_OPERATOR id_const_real
        | id_variable_real ASSIGN_OPERATOR expression
        | id_variable_real ASSIGN_OPERATOR literal_real
        ;

我在这里要明确地说一个字符串变量只能分配一个字符串文字,一个字符串的串联(使用+),一个字符串常量或另一个字符串变量.对于整数变量和实数变量相同,仅是不能为它们分配串联,而是为表达式分配(数学运算).
串联定义如下:

What I intend here is to explicitly say that a string variable can only be assigned a string literal, a concatenation of strings (using +), a string constant or another string variable. The same for integer variables and then for real variables, only that they can't be assigned a concatenation but an expression instead (math operations).
Concatenation is defined as follows:

concatenation:
        id_variable_string ADD_OPERATOR id_variable_string 
        | id_variable_string ADD_OPERADOR literal_string 
        | literal_string ADD_OPERADOR id_variable_string 
        | literal_string ADD_OPERADOR literal_string
        | id_const_string ADD_OPERADOR id_const_string  
        | id_const_string ADD_OPERADOR id_variable_string 
        | id_const_string ADD_OPERADOR literal_string 
        | literal_string ADD_OPERADOR id_const_string  
        | id_variable_string ADD_OPERADOR id_const_string
        ;

表达式定义为:

expression: 
        expression ADD_OPERATOR term
        | expression SUBST_OPERADOR term
        | term
        ;

term:
        term MULTIP_OPERATOR factor
        | term DIVISION_OPERATOR factor
        | factor
        ;

factor:     
        id_variable_integer
        | id_variable_real
        | id_const_integer
        | id_const_real
        | literal_integer
        | literal_real
        | PARENTHESIS_OPEN expression PARENTHESIS_CLOSE
        ;

现在,这是野牛所说的:

Now, this is what Bison is saying:


55个作业:id_variable_integer ASSIGN_OPERATOR id_const_integer
56 | id_variable_integer ASSIGN_OPERATOR id_variable_integer
58 | id_variable_integer ASSIGN_OPERATORliteral_integer
59 | id_variable_real ASSIGN_OPERATOR id_variable_real
60 | id_variable_real ASSIGN_OPERATOR id_const_real
62 | id_variable_real ASSIGN_OPERATORliteral_real


55 assignment: id_variable_integer ASSIGN_OPERATOR id_const_integer
56 | id_variable_integer ASSIGN_OPERATOR id_variable_integer
58 | id_variable_integer ASSIGN_OPERATOR literal_integer
59 | id_variable_real ASSIGN_OPERATOR id_variable_real
60 | id_variable_real ASSIGN_OPERATOR id_const_real
62 | id_variable_real ASSIGN_OPERATOR literal_real


状态50冲突:1转换/减少
状态76冲突:14转换/减少
状态130冲突:2转换/减少
状态131冲突:1转换/减少
状态133冲突:1转换/减少
状态134冲突:1转换/减少
状态135冲突:1转换/减少
状态137冲突:1转换/减少
状态138冲突:1转换/减少


State 50 conflicts: 1 shift/reduce
State 76 conflicts: 14 shift/reduce
State 130 conflicts: 2 shift/reduce
State 131 conflicts: 1 shift/reduce
State 133 conflicts: 1 shift/reduce
State 134 conflicts: 1 shift/reduce
State 135 conflicts: 1 shift/reduce
State 137 conflicts: 1 shift/reduce
State 138 conflicts: 1 shift/reduce


我假设语法中的某些内容是错误的,但是我不确定到底是什么.


I'm assuming something in my grammar is wrong but I'm not sure what exactly.

推荐答案

您说的是

因此,我创建了一些规​​则来分隔每种变量和常量:

So I created a few rules to separate each kind of variable and constant:

id_variable_string:
identifier;

id_variable_integer:
identifier;

id_variable_real:
identifier;

这是你的问题.语法上没有什么可区分id_variable_stringid_variable_integer的,所以您有(至少两个)浪费的规则.这就是它所抱怨的.当它得到identifier时,它是否应该将其视为id_variable_stringid_variable_integer.

And this was your problem. There is nothing syntactically to distinguish an id_variable_string from an id_variable_integer, so you have (at least two) wasted rules. This is what it is complaining about. It has no clue when it gets an identifier whether it should be treating it as an id_variable_string or an id_variable_integer.

您必须以不同的方式处理类型冲突-语义检查(不是语法检查),即与标识符关联的类型与表达式中其他标识符的类型一致.

You have to handle the type conflicts differently — a semantic check (not a syntactic check) that the type associated with the identifier is consistent with the types of the other identifiers in the expression.

这篇关于无法弄清楚为什么Bison会抛出“由于冲突而在解析器中无用的规则".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆