我正在使用Jison(Bison)創建簡單的標記語言。我顯然對此很陌生,但輕微的變化非常有效。我只是不明白S/R衝突的來源。語法規格解析Shift/Reduce衝突
兩個詞法分析器操作(具有不同的開始條件)返回'文本'似乎並不重要,我喜歡這樣,因爲它似乎允許語法具有較少的規則,並且因爲錯誤消息給用戶是一致的。我已經試過使'文本'規則不受上下文影響,我也試着給每個令牌一個不同的名字,但是當它們在一起時,它似乎對S/R衝突沒有任何影響。
解析器被SUPPOSED創建一個json對象與純文本,子陣列和各種特殊節點。
規格:
/* lexical grammar */
%lex
%s bracketed
%%
<bracketed>(\\.|[^\\\,\[\]])+ { yytext = yytext.replace(/\\(.)/g, '$1'); return 'Text'; }
<INITIAL>(\\.|[^\\\[])+ { yytext = yytext.replace(/\\(.)/g, '$1'); return 'Text'; }
"[" { this.begin('bracketed'); return '['; }
"]" { this.popState(); return ']'; }
"," return ','
<<EOF>> return 'END'
/lex
%start template
%%
template
: sentence END
;
sentence
: /* empty */
| sentence Text
| sentence '[' ']'
| sentence '[' dynamic ']'
;
dynamic
: sentence
/*| dynamic ',' sentence*/
;
警告:
Conflict in grammar: multiple actions possible when lookahead token is ] in state 5
- reduce by rule: sentence ->
- shift token (then go to state 6)
States with conflicts:
State 5
sentence -> sentence [ .] #lookaheads= END Text [ ]
sentence -> sentence [ .dynamic ] #lookaheads= END Text [ ]
dynamic -> .sentence #lookaheads= ]
sentence -> . #lookaheads= ] Text [
sentence -> .sentence Text
sentence -> .sentence [ ]
sentence -> .sentence [ dynamic ]
不同的生成算法都有或多或少的麻煩,但他們似乎都有些問題。
謝謝!
很好的答案。我喜歡你添加的建議 - 這些打開了我的想法,在這些行動中更好地處理,沒有想到這一點。儘管如此,我仍在努力做到這一點。規則出現的順序是否重要? –
你也幫助我認識到,這些行動並不是解決衝突討論所必需的。 –
我更新了語法 - 我仍然無法看到它。 –