Aad*_*hah 4 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;"]
]
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,它给我以下错误:
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
Run Code Online (Sandbox Code Playgroud)
但是它最终仍能产生正确的输出.例如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;"]
]
}
}
Run Code Online (Sandbox Code Playgroud)
据说这个语法只允许一个可选的换行符跟随二元运算符.如何重写它以允许任意数量的换行符跟随二元运算符?还必须有一些方法,我不必为每个运算符编写2条规则.
我对Jison并不完全熟悉,但看起来你正在定义一个看起来像这样的规则:
expression ::= number;
expression ::= expression binary expression;
Run Code Online (Sandbox Code Playgroud)
考虑表达式1 - 2 - 3.这可以解释为(1 - 2) - 3或1 - (2 - 3).这是什么?你的语法含糊不清.正常的数学规则说它应该是左联想的.你需要让你的语法反映出来:
expression ::= number;
expression ::= expression binary number;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
713 次 |
| 最近记录: |