如何避免 LALR 语法中用于解析嵌套列表的移位归约冲突?
我想创建一个 LALR 语法来解析嵌套列表,但我总是遇到移位/归约冲突。
我有 list1,它是 type1 项目的列表和 list2:
<list1> ::= <type1> | <type1> <list1> ;
<type1> ::= A | B | <list2> ;
我有一个 list2,它是 type2 项目的列表:
<list2> ::= <type2> | <type2> <list2> ;
<type2> ::= X | Y ;
此语法会产生移位/归约错误。我怎样才能避免它?
这是具体的 Bigloo 来源:
(lalr-grammar
(comment
new-line
text-chunk
white-space)
(input
(() '())
((node-list) node-list))
(node-list
((node) (cons node '()))
((node node-list) (cons node node-list)))
(node
((comment) (cons 'comment comment))
((new-line) (cons 'new-line new-line))
((text) (cons 'text text))
((white-space) (cons 'white-space white-space)))
(text
((text-chunk) (cons 'text text-chunk))
((text-chunk text) (cons 'text (string-append text-chunk text)))))
终端有:注释、换行、文本块和空白。 非终结符是:输入、节点列表、节点和文本。
Bigloo 抱怨文本到文本块的缩减规则:
*** WARNING:bigloo:lalr-grammar
** Shift/Reduce conflict:
- shift to state 2
- reduce rule (text --> text-chunk)
on token `text-chunk'
但我不认为这是 Bigloo 的问题。看起来像是语法问题。
I would like to create a LALR grammar to parse nested lists, but I get always a shift/reduce conflict.
I have the list1 which is a list of type1 items and list2:
<list1> ::= <type1> | <type1> <list1> ;
<type1> ::= A | B | <list2> ;
And I have a list2 which is a list of type2 items:
<list2> ::= <type2> | <type2> <list2> ;
<type2> ::= X | Y ;
This grammar produces a shift/reduce error. How can I avoid it?
This is the concrete Bigloo source:
(lalr-grammar
(comment
new-line
text-chunk
white-space)
(input
(() '())
((node-list) node-list))
(node-list
((node) (cons node '()))
((node node-list) (cons node node-list)))
(node
((comment) (cons 'comment comment))
((new-line) (cons 'new-line new-line))
((text) (cons 'text text))
((white-space) (cons 'white-space white-space)))
(text
((text-chunk) (cons 'text text-chunk))
((text-chunk text) (cons 'text (string-append text-chunk text)))))
The terminals are: comment, new-line, text-chunk and white-space.
The non terminals are: input, node-list, node and text.
Bigloo complains about the reduce rule for text to text-chunk:
*** WARNING:bigloo:lalr-grammar
** Shift/Reduce conflict:
- shift to state 2
- reduce rule (text --> text-chunk)
on token `text-chunk'
But I do not think that this is a Bigloo problem. It looks like a grammar problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该语法实际上是不明确的,因为
type2
实例的序列可以在list2
级别上累积,但它也可以被视为type1
的序列代码>实例。例如,此输入
可以解析为
,也可以解析为
在
list1
级别引入分隔符怎么样?这样就可以解决问题了。The grammar is in fact ambiguous, because a sequence of
type2
instances can be accumulated onlist2
level, but it can also be seen as a sequence oftype1
instances.E.g. this input
can be parsed as
but also as
What about introducing a delimiter on
list1
level? This would solve the problem.