简体   繁体   English

Sablecc移位/减少带有标识符的产品上的冲突

[英]Sablecc shift/reduce conflicts on productions with identifiers

I'm trying to write a specification file for sablecc for a version of minipython (with postfix/prefix increment and decrement operators), and some productions naturally need to use identifiers, but i get these conflicts during parsing: 我正在尝试为minipython版本(带有后缀/前缀递增和递减运算符)的sablecc编写规范文件,某些产品自然需要使用标识符,但是在解析过程中遇到了这些冲突:

shift/reduce conflict in state [stack: TPrint TIdentifier *] on TPlusPlus in {
    [ PMultiplication = TIdentifier * ] followed by TPlusPlus (reduce),
    [ PPostfix = TIdentifier * TPlusPlus ] (shift)
}

shift/reduce conflict in state [stack: TPrint TIdentifier *] on TMinusMinus in {
    [ PMultiplication = TIdentifier * ] followed by TMinusMinus (reduce),
    [ PPostfix = TIdentifier * TMinusMinus ] (shift)
}

shift/reduce conflict in state [stack: TPrint TIdentifier *] on TLPar in {
    [ PFunctionCall = TIdentifier * TLPar PArglist TRPar ] (shift),
    [ PFunctionCall = TIdentifier * TLPar TRPar ] (shift),
    [ PMultiplication = TIdentifier * ] followed by TLPar (reduce)
}

shift/reduce conflict in state [stack: TPrint TIdentifier *] on TLBr in {
    [ PExpression = TIdentifier * TLBr PExpression TRBr ] (shift),
    [ PMultiplication = TIdentifier * ] followed by TLBr (reduce),
    [ PPostfix = TIdentifier * TLBr PExpression TRBr TMinusMinus ] (shift),
    [ PPostfix = TIdentifier * TLBr PExpression TRBr TPlusPlus ] (shift)
}
java.lang.RuntimeException:

I started by following a given bnf of the language and got to this. 我从遵循给定的bnf语言开始,并做到了这一点。 Here is the grammar file: 这是语法文件:

Productions
goal = {prgrm}program* ;

program = {func}function | {stmt}statement;

function = {func}def identifier l_par argument? r_par semi statement ;

argument = {arg} identifier assign_value? subsequent_arguments* ;

assign_value = {assign} eq value ;

subsequent_arguments = {more_args} comma identifier assign_value? ;

statement = {case1}tab* if comparison semi statement
          | {case2}tab* while comparison semi statement
          | {case3}tab* for [iterator]:identifier in [collection]:identifier semi statement
          | {case4}tab* return expression
          | {case5}tab* print expression more_expressions
          | {simple_equals}tab* identifier eq expression
          | {add_equals}tab* identifier add_eq expression
          | {minus_equals}tab* identifier sub_eq expression
          | {div_equals}tab* identifier div_eq expression
          | {case7}tab* identifier l_br [exp1]:expression r_br eq [exp2]:expression
          | {case8}tab* function_call;

comparison = {less_than} comparison less relation
           | {greater_than} comparison great relation
           | {rel} relation;

relation = {relational_value} relational_value
         | {logic_not_equals} relation logic_neq relational_value
         | {logic_equals} relation logic_equals relational_value;

relational_value = {expression_value} expression_value
      | {true} true
      | {false} false;

expression = {case1} arithmetic_expression
           | {case2} prefix
           | {case4} identifier l_br expression r_br
           | {case9} l_br more_values r_br;

more_expressions = {more_exp} expression subsequent_expressions*;

subsequent_expressions = {more_exp} comma expression;

arithmetic_expression = {plus} arithmetic_expression plus multiplication
         | {minus} arithmetic_expression minus multiplication
         | {multiplication} multiplication ;

multiplication = {expression_value} expression_value
         | {div} multiplication div expression_value
         | {mult} multiplication mult expression_value;

expression_value = {exp} l_par expression r_par
                 | {function_call} function_call
                 | {value} value
                 | {identifier} identifier ;

prefix = {pre_increment} plus_plus prepost_operand
       | {pre_decrement} minus_minus prepost_operand
       | {postfix} postfix;

postfix = {post_increment} prepost_operand plus_plus
        | {post_decrement} prepost_operand minus_minus;  

prepost_operand = {value} identifier l_br expression r_br
                 | {identifier} identifier;

function_call = {args} identifier l_par arglist? r_par;

arglist = {arglist} more_expressions ;

value = {number} number
      | {string} string ;

more_values = {more_values} value subsequent_values* ;

subsequent_values = comma value ;

number = {int} numeral              
       | {float} float_numeral ;

where identifier is of course a token, and the problematic productions where it can be found are function_call, prepost_operand, expression_value. 其中,标识符当然是一个令牌,可以找到它的有问题的结果是function_call,prepost_operand,expression_value。 I experimentally removed prefix/postfix and prepost_operand to see if the conflicts would at least change a little, but that just leaves the two last conflicts. 我实验性地删除了prefix / postfix和prepost_operand来查看冲突是否至少会有所改变,但这只剩下最后两个冲突。 Is there any way i can resolve these conflicts without changing the grammar much, or have i gone down a completely wrong path? 我有什么办法可以解决这些冲突而又不用太多改变语法,或者我走了一条完全错误的道路?

The problem is the production whose right-hand side is: 问题在于生产的右侧是:

print expression more_expressions

more_expressions matches a list of expressions (so it probably should be called expression_list to be less confusing). more_expressions匹配一个表达式列表(因此应该将其称为expression_list ,以减少混乱)。 Two consecutive expression s in a rule is obviously ambiguous (if you could have two expressions, would 1+1+1 be 1+1 followed by +1 or 1 followed by +1+1 ?). 规则中两个连续的expression s显然是模棱两可的(如果可以有两个表达式,则1+1+11+1然后是+1还是1然后是+1+1 ?)。 What you want is just 你想要的就是

print more_expressions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM