javascript - 这个语法怎么有歧义?

标签 javascript parsing bison parser-generator jison

我正在用 Jison 编写一个简单的表达式解析器。这是我的语法:

{
    "operators": [
        ["left", "+", "-"],
        ["left", "*", "/", "%"]
    ],
    "bnf": {
        "program": [
            ["statement EOF", "return $1;"]
        ],
        "statement": [
            ["expression NEWLINE", "$$ = $1 + ';';"]
        ],
        "expression": [
            ["NUMBER",                       "$$ = yytext;"],
            ["expression binary expression", "$$ = $1 + $2 + $3;"]
        ],
        "binary": [
            ["+",              "$$ = ' + ';"],
            ["-",              "$$ = ' - ';"],
            ["*",              "$$ = ' * ';"],
            ["/",              "$$ = ' / ';"],
            ["%",              "$$ = ' % ';"],
            ["binary NEWLINE", "$$ = $1;"]
        ]
    }
}

当我尝试运行它时,出现以下错误:

Conflict in grammar: multiple actions possible when lookahead token is + in state
13
- reduce by rule: expression -> expression binary expression
- shift token (then go to state 8)
Conflict in grammar: multiple actions possible when lookahead token is - in state
13
- reduce by rule: expression -> expression binary expression
- shift token (then go to state 9)
Conflict in grammar: multiple actions possible when lookahead token is * in state
13
- reduce by rule: expression -> expression binary expression
- shift token (then go to state 10)
Conflict in grammar: multiple actions possible when lookahead token is / in state
13
- reduce by rule: expression -> expression binary expression
- shift token (then go to state 11)
Conflict in grammar: multiple actions possible when lookahead token is % in state
13
- reduce by rule: expression -> expression binary expression
- shift token (then go to state 12)

States with conflicts:
State 13
  expression -> expression binary expression . #lookaheads= NEWLINE + - * / %
  expression -> expression .binary expression
  binary -> .+
  binary -> .-
  binary -> .*
  binary -> ./
  binary -> .%
  binary -> .binary NEWLINE

但是它最终仍然会产生正确的输出。例如,2 + 3 * 5/7 % 11 正确翻译为 2 + 3 * 5/7 % 11;

在我看来,我的语法似乎没有歧义,那么 Jison 为什么要提示呢?

更新:正如@icktoofay 所解释的那样,这是一个运算符关联性问题。通过将运算符解析为非终端符号,运算符优先级和关联性信息将丢失。因此我解决了如下问题:

{
    "operators": [
        ["left", "+", "-"],
        ["left", "*", "/", "%"]
    ],
    "bnf": {
        "program": [
            ["statement EOF", "return $1;"]
        ],
        "statement": [
            ["expression NEWLINE", "$$ = $1 + ';';"]
        ],
        "expression": [
            ["NUMBER",                          "$$ = yytext;"],
            ["expression + expression",         "$$ = $1 + ' + ' + $3;"],
            ["expression - expression",         "$$ = $1 + ' - ' + $3;"],
            ["expression * expression",         "$$ = $1 + ' * ' + $3;"],
            ["expression / expression",         "$$ = $1 + ' / ' + $3;"],
            ["expression % expression",         "$$ = $1 + ' % ' + $3;"],
            ["expression + NEWLINE expression", "$$ = $1 + ' + ' + $4;"],
            ["expression - NEWLINE expression", "$$ = $1 + ' - ' + $4;"],
            ["expression * NEWLINE expression", "$$ = $1 + ' * ' + $4;"],
            ["expression / NEWLINE expression", "$$ = $1 + ' / ' + $4;"],
            ["expression % NEWLINE expression", "$$ = $1 + ' % ' + $4;"]
        ]
    }
}

也就是说,这个语法只允许一个可选的换行符跟在二元运算符之后。我如何重写它以允许任意数量的换行符跟在二元运算符之后?此外,必须有一些方法可以让我不必为每个运算符编写 2 条规则。

最佳答案

我对 Jison 并不完全熟悉,但看起来你正在定义一个如下所示的规则:

expression ::= number;
expression ::= expression binary expression;

考虑表达式 1 - 2 - 3。这可以解释为 (1 - 2) - 31 - (2 - 3)。是哪个?你的语法有歧义。正常的数学规则说它应该是左关联的。你需要让你的语法反射(reflect):

expression ::= number;
expression ::= expression binary number;

关于javascript - 这个语法怎么有歧义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15801592/

相关文章:

javascript 解析纽约时区的日期时间字符串

java - 解析包含单行和多行数据的文件

c++ - 解析自定义类型的数学函数

javascript - 幻灯片弹出时内容移动

javascript - 如何在 producthunt 之类的 NodeJS 中定义快速路由以逐日获取数据?

javascript - 将信息发送到数据库而无需重新加载页面

javascript - 显示图像直到滚动?

java - Jackson无法解析json,返回NPE

c++ - Bison 更好的内存管理

c - 如何为错误创建语法规则?