[英]Is this grammar not LR(1)?
I'm working on parse generator for PHP . 我正在为PHP解析生成器 。 Currently I'm trying to implement canonical LR(1) parser , but it outputs reduce-reduce conflict on following grammar. 目前我正在尝试实现规范的LR(1)解析器 ,但它在后续语法上输出reduce-reduce冲突。 Is this grammar not LR(1)? 这个语法不是LR(1)吗? Or should I recheck my algorithms? 或者我应该重新检查我的算法?
Grammar in Bison(-like) notation: Bison中的语法(类似)表示法:
syntax : toplevels rules ;
toplevels
: toplevel
| toplevel toplevels
;
optsem : ';' | /* nothing */ ;
toplevel
: 'grammar' backslash_separated_name optsem
| 'option' options optsem
| '@' period_separated_name '{' CODE '}' optsem
;
period_separated_name
: ID '.' period_separated_name
| ID
;
backslash_separated_name
: ID '\\' backslash_separated_name
| ID
;
options
: single_option
| '(' more_options ')'
;
more_options
: single_option
| single_option ';'
| single_option ';' more_options
;
single_option
: period_separated_name '=' STRING
| period_separated_name '=' '{' CODE '}'
;
rules
: rule
| rule rules
;
rule
: ID ':' expressions ';'
;
expressions
: expression
| expression '|' expressions
;
expression
: terms
| terms '{' CODE '}'
;
terms
: /* nothing */
| term
| term terms
;
term
: ID
| STRING
;
EDIT: 编辑:
Computed table: 计算表:
|$en| ; |gra|opt| @ | { |COD| } |ID | . | \ | ( | ) | = |STR| : | | |syn|top|rul|top|opt|bac|opt|per|sin|mor|rul|exp|exp|ter|ter|
--------------------------------------------------------------------------------------------------------------------------------
0 ( , , s4, s5, s6, , , , , , , , , , , , | 1, 2, , 3, , , , , , , , , , , )
1 (acc, , , , , , , , , , , , , , , , | , , , , , , , , , , , , , , )
2 ( , , , , , , , , s9, , , , , , , , | , , 7, , , , , , , , 8, , , , )
3 ( , , s4, s5, s6, , , , r2, , , , , , , , | , 10, , 3, , , , , , , , , , , )
4 ( , , , , , , , ,s12, , , , , , , , | , , , , , 11, , , , , , , , , )
5 ( , , , , , , , ,s16, , ,s17, , , , , | , , , , , , 13, 14, 15, , , , , , )
6 ( , , , , , , , ,s19, , , , , , , , | , , , , , , , 18, , , , , , , )
7 ( r1, , , , , , , , , , , , , , , , | , , , , , , , , , , , , , , )
8 (r20, , , , , , , , s9, , , , , , , , | , , 20, , , , , , , , 8, , , , )
9 ( , , , , , , , , , , , , , , ,s21, | , , , , , , , , , , , , , , )
10 ( , , , , , , , , r3, , , , , , , , | , , , , , , , , , , , , , , )
11 ( ,s23, r5, r5, r5, , , , r5, , , , , , , , | , , , , 22, , , , , , , , , , )
12 ( ,r12, , , , , , , , ,s24, , , , , , | , , , , , , , , , , , , , , )
13 ( ,s23, r5, r5, r5, , , , r5, , , , , , , , | , , , , 25, , , , , , , , , , )
14 ( , , , , , , , , , , , , ,s26, , , | , , , , , , , , , , , , , , )
15 ( ,r13, , , , , , , , , , , , , , , | , , , , , , , , , , , , , , )
16 ( , , , , , , , , ,s27, , , ,r10, , , | , , , , , , , , , , , , , , )
17 ( , , , , , , , ,s16, , , , , , , , | , , , , , , , 28, 29, 30, , , , , )
18 ( , , , , ,s31, , , , , , , , , , , | , , , , , , , , , , , , , , )
19 ( , , , , ,r10, , , ,s32, , , , , , , | , , , , , , , , , , , , , , )
20 (r21, , , , , , , , , , , , , , , , | , , , , , , , , , , , , , , )
21 ( ,r27, , , ,r27, , ,s37, , , , , ,s38, ,r27| , , , , , , , , , , , 33, 34, 35, 36)
22 ( , , r6, r6, r6, , , , r6, , , , , , , , | , , , , , , , , , , , , , , )
23 ( , , r4, r4, r4, , , , r4, , , , , , , , | , , , , , , , , , , , , , , )
24 ( , , , , , , , ,s12, , , , , , , , | , , , , , 39, , , , , , , , , )
25 ( , , r7, r7, r7, , , , r7, , , , , , , , | , , , , , , , , , , , , , , )
26 ( , , , , ,s40, , , , , , , , ,s41, , | , , , , , , , , , , , , , , )
27 ( , , , , , , , ,s16, , , , , , , , | , , , , , , , 42, , , , , , , )
28 ( , , , , , , , , , , , , ,s43, , , | , , , , , , , , , , , , , , )
29 ( ,s44, , , , , , , , , , ,r15, , , , | , , , , , , , , , , , , , , )
30 ( , , , , , , , , , , , ,s45, , , , | , , , , , , , , , , , , , , )
31 ( , , , , , ,s46, , , , , , , , , , | , , , , , , , , , , , , , , )
32 ( , , , , , , , ,s19, , , , , , , , | , , , , , , , 47, , , , , , , )
33 ( ,s48, , , , , , , , , , , , , , , | , , , , , , , , , , , , , , )
34 ( ,r23, , , , , , , , , , , , , , ,s49| , , , , , , , , , , , , , , )
35 ( ,r25, , , ,s50, , , , , , , , , , ,r25| , , , , , , , , , , , , , , )
36 ( ,r27, , , ,r27, , ,s37, , , , , ,s38, ,r27| , , , , , , , , , , , , , 51, 36)
37 ( ,r30, , , ,r30, , ,r30, , , , , ,r30, ,r30| , , , , , , , , , , , , , , )
38 ( ,r31, , , ,r31, , ,r31, , , , , ,r31, ,r31| , , , , , , , , , , , , , , )
39 ( ,r11, , , , , , , , , , , , , , , | , , , , , , , , , , , , , , )
40 ( , , , , , ,s52, , , , , , , , , , | , , , , , , , , , , , , , , )
41 ( ,r18, , , , , , , , , , , , , , , | , , , , , , , , , , , , , , )
42 ( , , , , , , , , , , , , , r9, , , | , , , , , , , , , , , , , , )
43 ( , , , , ,s53, , , , , , , , ,s54, , | , , , , , , , , , , , , , , )
44 ( , , , , , , , ,s16, , , ,r16, , , , | , , , , , , , 28, 29, 55, , , , , )
45 ( ,r14, , , , , , , , , , , , , , , | , , , , , , , , , , , , , , )
46 ( , , , , , , ,s56, , , , , , , , , | , , , , , , , , , , , , , , )
47 ( , , , , , r9, , , , , , , , , , , | , , , , , , , , , , , , , , )
48 (r22, , , , , , , ,r22, , , , , , , , | , , , , , , , , , , , , , , )
49 ( ,r27, , , ,r27, , ,s37, , , , , ,s38, ,r27| , , , , , , , , , , , 57, 34, 35, 36)
50 ( , , , , , ,s58, , , , , , , , , , | , , , , , , , , , , , , , , )
51 ( ,r29, , , ,r29, , , , , , , , , , ,r29| , , , , , , , , , , , , , , )
52 ( , , , , , , ,s59, , , , , , , , , | , , , , , , , , , , , , , , )
53 ( , , , , , ,s60, , , , , , , , , , | , , , , , , , , , , , , , , )
54 ( ,r18, , , , , , , , , , ,r18, , , , | , , , , , , , , , , , , , , )
55 ( , , , , , , , , , , , ,r17, , , , | , , , , , , , , , , , , , , )
56 ( ,s23, r5, r5, r5, , , , r5, , , , , , , , | , , , , 61, , , , , , , , , , )
57 ( ,r24, , , , , , , , , , , , , , , | , , , , , , , , , , , , , , )
58 ( , , , , , , ,s62, , , , , , , , , | , , , , , , , , , , , , , , )
59 ( ,r19, , , , , , , , , , , , , , , | , , , , , , , , , , , , , , )
60 ( , , , , , , ,s63, , , , , , , , , | , , , , , , , , , , , , , , )
61 ( , , r8, r8, r8, , , , r8, , , , , , , , | , , , , , , , , , , , , , , )
62 ( ,r26, , , , , , , , , , , , , , ,r26| , , , , , , , , , , , , , , )
63 ( ,r19, , , , , , , , , , ,r19, , , , | , , , , , , , , , , , , , , )
and conflicts: 和冲突:
conflict: state = 36, terminal = ; , production = terms -> . /* empty production */
conflict: state = 36, terminal = { , production = terms -> . /* empty production */
conflict: state = 36, terminal = | , production = terms -> . /* empty production */
The parser is stumped by the terms
rule: 解析器被terms
规则困扰:
terms
: /* nothing */
| term
| term terms
;
Since terms
can mean 'nothing', there would be cases where the input to be parsed can map to either the grammar term
or term terms
, thus causing reduce-reduce conflict (which means there are two way to reduce the same input, thus the parser can't make the decision). 由于terms
可以表示“无”,因此有些情况下要解析的输入可以映射到语法term
或term terms
,从而导致减少 - 减少冲突(这意味着有两种方法可以减少相同的输入,因此解析器无法做出决定)。
One way to fix this is to remove the /* nothing */
clause but that would certainly change your grammar although I doubt you would want to allow terms
to be 'nothing' since you would be allowing double |
解决这个问题的一种方法是删除/* nothing */
子句,但这肯定会改变你的语法,虽然我怀疑你会想让terms
成为'没有',因为你会允许双重|
in the expressions
rule. 在expressions
规则中。
If you still want to maintain the grammar, you need to break the rule into pieces, for example: 如果您仍想维护语法,则需要将规则分解为多个部分,例如:
expression
: terms_or_nothing
| terms_or_nothing '{' CODE '}'
;
terms_or_nothing
: /* nothing */
| terms
terms
: term
| term terms
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.