所以,在组合语法分析方面我还很初级。当我减少班次-减少冲突时,我需要帮助来解决Menhir报告的冲突。
以这个小语法为例:
(* {2 Tokens } *)
%token EOF
%token COLON PIPE SEMICOLON
%token <string> COUNT
%token <string> IDENTIFIER
%start <AST.t> script
%start <AST.statement> statement
%%
(* {2 Rules } *)
script:
| it = separated_list(break, statement); break?; EOF { { statements = it } }
;
statement:
| COLON*; count = COUNT?; cmd = command { AST.make_statement ~count ~cmd }
;
command:
| it = IDENTIFIER { it }
;
break:
| SEMICOLON { }
;
%%
Menhir的--explain
标志产生了对由此产生的移位/减少冲突的描述。不幸的是,我无法做出这样的决定:
** Conflict (shift/reduce) in state 3.
** Token involved: SEMICOLON
** This state is reached from script after reading:
statement
** The derivations that appear below have the following common factor:
** (The question mark symbol (?) represents the spot where the derivations begin to differ.)
script
(?)
** In state 3, looking ahead at SEMICOLON, shifting is permitted
** because of the following sub-derivation:
loption(separated_nonempty_list(break,statement)) option(break) EOF
separated_nonempty_list(break,statement)
statement break separated_nonempty_list(break,statement)
. SEMICOLON
** In state 3, looking ahead at SEMICOLON, reducing production
** separated_nonempty_list(break,statement) -> statement
** is permitted because of the following sub-derivation:
loption(separated_nonempty_list(break,statement)) option(break) EOF // lookahead token appears because option(break) can begin with SEMICOLON
separated_nonempty_list(break,statement) // lookahead token is inherited
statement .
我花了整整一个晚上来尝试查找有关转变/减少冲突实际上是什么的文档,但是我不得不承认我很难理解我的意思。正在阅读。有人可以给我一个简单的(尽可能多的)解释移位/减少冲突的方法吗?具体使用上面示例的上下文?
答案 0 :(得分:1)
问题在于,在查看SEMICOLON时,解析器无法决定是应该使用EOF还是使用其余列表。原因是您使用break
作为可选终止符,而不是分隔符。
我建议您更改主要规则:
script:
| it = optterm_list(break, statement); EOF { { statements = it } }
;
并使用以下内容自行定义optterm_list
组合器:
optterm_list(separator, X):
| separator? {[]}
| l=optterm_nonempty_list(separator, X) { l }
optterm_nonempty_list(separator, X):
| x = X separator? { [ x ] }
| x = X
separator
xs = optterm_nonempty_list(separator, X)
{ x :: xs }