Earley cannot handle epsilon-states already contained in chart

天涯浪子 提交于 2019-12-12 05:33:38

问题


I have implemented the Earley parser using a queue to process states. The queue is seeded with the top-level rule. For each state in the queue, one of the operations (prediction, scanning, completion) is performed by adding new states to the queue. Duplicate states are not added.

The problem I am having is best described with the following grammar:

When parsing A, the following happens:

As you can tell, A will not be fully resolved. This is because the completion with the epsilon state will only happen once as it is not added to the queue.

How can I adapt my algorithm to support these epsilon-states?

Edit: Note that this is not an issue when using terminals as a new chart set will be created to insert the scanned state. As the state does not exist there already, it will be processed.


回答1:


In the paper "Practical Earley Parsing" by John a Aycock and R. Nigel Horspool hidden in section 4 is a statement on how to handle nullable rules:

If [A→ ... •B ..., j] is in Si, add [B→ • a, i]
to Si for all rules B → a.
If B is nullable, also add [A → ... B • ..., j] to Si

So in your example, in the prediction of A→ • B B the following rules would be produced:

(1) B → •
(2) A → B • B
(3) A → B B •

The key is this happens in the prediction phase. During the prediction phase if the 'post dot' symbol is nullable (both directly and through transference) then move the dot right and add that rule as well.

So basically:

A → • B B produces (B → • and A → B • B) each being queued and processed
A → B • B produces (A → B B •) which is queued and processed



来源:https://stackoverflow.com/questions/35524183/earley-cannot-handle-epsilon-states-already-contained-in-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!