如何避免语法冲突

时间:2020-12-12 04:17:26

I have a grammar file — https://github.com/itrelease/fubar-script/blob/jsast/src/grammar.js but I get conflicts and I don't really know how to solve this. If someone could explain me it would be helpful.

我有一个语法文件 - https://github.com/itrelease/fubar-script/blob/jsast/src/grammar.js但我遇到了冲突,我真的不知道如何解决这个问题。如果有人能解释我会有所帮助。

This rules produce conflicts:

这条规则产生了冲突:

ParamVar: [
  ['Identifier', '$$ = $Identifier;'],
  ['THIS', '$$ = new yy.ThisExpression();']
],

PrimaryExpression: [
  ['THIS', '$$ = new yy.ThisExpression();'],
  ['Literal', '$$ = $Literal;'],
  ['ArrayLiteral', '$$ = $ArrayLiteral;'],
  ['Identifier', '$$ = $Identifier;'],
  ['ObjectLiteral', '$$ = $ObjectLiteral;'],
  ['( Expression )', '$$ = $Expression;']
],

1 个解决方案

#1


2  

Your current grammar does not have a PrimaryExpressionNoBrace, but I presume that the problem occurred with this older version of the grammar.

您当前的语法没有PrimaryExpressionNoBrace,但我认为这个旧版本的语法出现了问题。

The conflict is caused by this production:

冲突是由这种生产造成的:

MemberExpression: [
      ['PrimaryExpression', '$$ = $PrimaryExpression;'],
      ['ArrowFunctionExpression', '$$ = $ArrowFunctionExpression'],
      ...

where

哪里

  • a PrimaryExpression derives to a PrimaryExpressionNoBrace which has '( Expression )'

    PrimaryExpression派生到具有'(Expression)'的PrimaryExpressionNoBrace

  • Expression again derives to PrimaryExpressionNoBrace which has IDENTIFIER or THIS alternatives

    表达式再次派生到具有IDENTIFIER或THIS替代品的PrimaryExpressionNoBrace

  • an ArrowFunctionExpression has '( FormalParameterList ) => Block'

    一个ArrowFunctionExpression有'(FormalParameterList)=> Block'

  • FormalParameterList has IDENTIFIER or THIS alternatives, too.
  • FormalParameterList也有IDENTIFIER或THIS替代品。

Thus the input of a left parenthesis, followed by an IDENTIFIER or THIS takes us into an LR-state that cannot decide between reducing to PrimaryExpressionNoBrace or to FormalParameterList, i.e. it has reduce-reduce conflicts on common legal followers (e.g. a right parenthesis). A single token lookahead is not suffient here, and there is no support for more.

因此,左括号的输入,后跟IDENTIFIER或THIS将我们带入LR状态,该状态不能在减少到PrimaryExpressionNoBrace或FormalParameterList之间做出决定,即它在普通合法追随者(例如右括号)上具有减少 - 减少冲突。单个令牌前瞻在这里是不够的,并且不支持更多。

#1


2  

Your current grammar does not have a PrimaryExpressionNoBrace, but I presume that the problem occurred with this older version of the grammar.

您当前的语法没有PrimaryExpressionNoBrace,但我认为这个旧版本的语法出现了问题。

The conflict is caused by this production:

冲突是由这种生产造成的:

MemberExpression: [
      ['PrimaryExpression', '$$ = $PrimaryExpression;'],
      ['ArrowFunctionExpression', '$$ = $ArrowFunctionExpression'],
      ...

where

哪里

  • a PrimaryExpression derives to a PrimaryExpressionNoBrace which has '( Expression )'

    PrimaryExpression派生到具有'(Expression)'的PrimaryExpressionNoBrace

  • Expression again derives to PrimaryExpressionNoBrace which has IDENTIFIER or THIS alternatives

    表达式再次派生到具有IDENTIFIER或THIS替代品的PrimaryExpressionNoBrace

  • an ArrowFunctionExpression has '( FormalParameterList ) => Block'

    一个ArrowFunctionExpression有'(FormalParameterList)=> Block'

  • FormalParameterList has IDENTIFIER or THIS alternatives, too.
  • FormalParameterList也有IDENTIFIER或THIS替代品。

Thus the input of a left parenthesis, followed by an IDENTIFIER or THIS takes us into an LR-state that cannot decide between reducing to PrimaryExpressionNoBrace or to FormalParameterList, i.e. it has reduce-reduce conflicts on common legal followers (e.g. a right parenthesis). A single token lookahead is not suffient here, and there is no support for more.

因此,左括号的输入,后跟IDENTIFIER或THIS将我们带入LR状态,该状态不能在减少到PrimaryExpressionNoBrace或FormalParameterList之间做出决定,即它在普通合法追随者(例如右括号)上具有减少 - 减少冲突。单个令牌前瞻在这里是不够的,并且不支持更多。