杰森:减少实际上没有冲突的冲突 [英] Jison: Reduce Conflict where actually no conflict is

查看:120
本文介绍了杰森:减少实际上没有冲突的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个小型JavaScript解析器,该解析器还包括一个小型项目的类型化变量.

I'm trying to generate a small JavaScript parser which also includes typed variables for a small project.

幸运的是,jison已经提供了一个jscore.js,我刚刚对其进行了调整以满足自己的需求.添加类型后,我遇到了减少冲突的问题.我将问题最小化到最小的JISON:

Luckily, jison already provides a jscore.js which I just adjusted to fit my needs. After adding types I ran into a reduce conflict. I minimized to problem to this minimum JISON:

吉森:

%start SourceElements
%%

// This is up to become more complex soon
Type
    : VAR
    | IDENT
    ;

// Can be a list of statements
SourceElements
    : Statement
    | SourceElements Statement
    ;

// Either be a declaration or an expression
Statement
    : VariableStatement
    | ExprStatement
    ;

// Parses something like: MyType hello;
VariableStatement
    : Type IDENT ";"
    ;

// Parases something like hello;
ExprStatement
    : PrimaryExprNoBrace ";"
    ;

// Parses something like hello;
PrimaryExprNoBrace
    : IDENT
    ;

实际上,此脚本除了解析两个语句外什么也不做:

Actually this script does nothing than parsing two statements:

VariableStatement

IDENT IDENT ";"

ExpStatement

IDENT ";"

由于这是一个极端最小化的JISON脚本,所以我不能简单地将"Type"替换为"IDENT"(这确实起作用).

As this is a extremly minimized JISON Script, I can't simply replace "Type" be "IDENT" (which btw. worked).

生成解析器会引发以下冲突:

Generating the parser throws the following conflicts:

Conflict in grammar: multiple actions possible when lookahead token is IDENT in state 8
- reduce by rule: PrimaryExprNoBrace -> IDENT
- reduce by rule: Type -> IDENT
Conflict in grammar: multiple actions possible when lookahead token is ; in state 8
- reduce by rule: PrimaryExprNoBrace -> IDENT
- reduce by rule: Type -> IDENT

States with conflicts:
State 8
  Type -> IDENT . #lookaheads= IDENT ;
  PrimaryExprNoBrace -> IDENT . #lookaheads= IDENT ;

是否有任何技巧可以解决此冲突?

Is there any trick to fix this conflict?

非常感谢您! 〜本杰明

Thank you in advanced! ~Benjamin

推荐答案

对我来说,这似乎是一个Jison错误.在这两个令牌序列的情况下,它抱怨模棱两可:

This looks like a Jison bug to me. It is complaining about ambiguity in the cases of these two sequences of tokens:

  1. IDENT IDENT
  2. IDENT;"

有问题的状态是在转移第一个IDENT令牌后达到的状态. Jison注意到它需要减少该令牌,并且(声称)它不知道是减少为Type还是PrimaryExpressionNoBrace.

The state in question is that reached after shifting the first IDENT token. Jison observes that it needs to reduce that token, and that (it claims) it doesn't know whether to reduce to a Type or to a PrimaryExpressionNoBrace.

但是Jison应该能够基于下一个标记进行区分:如果它是第二个IDENT,则仅将其简化为Type可以导致有效的解析,而如果为;"那么只有将其简化为PrimaryExpressionNoBrace才能导致有效的解析.

But Jison should be able to distinguish based on the next token: if it is a second IDENT then only reducing to a Type can lead to a valid parse, whereas if it is ";" then only reducing to PrimaryExpressionNoBrace can lead to a valid parse.

您确定给定的输出符合给定的语法吗?添加规则或修改给定规则以产生诸如所描述的歧义是可能的.这看起来像是一个简单的案例,令我惊讶的是Jison弄错了.但是,如果确实如此,那么您应该考虑提交错误报告.

Are you sure the given output goes with the given grammar? It would be possible either to add rules or to modify the given ones to produce an ambiguity such as the one described. This just seems like such a simple case that I'm surprised Jison gets it wrong. If it in fact does, however, then you should consider filing a bug report.

这篇关于杰森:减少实际上没有冲突的冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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