douba7784 2011-03-08 01:18
浏览 6
已采纳

如何使用PHP preg_match_all实现简单的CFG解析器?

I am using preg_match_all to make a simple parser. Note that since it will parse only few sentences, the performance does not matter. Would it be possible to make a parser which parse through below Context free grammer?

S -> NP VP
PP -> P NP
NP -> 'the' N | N PP | 'the' N PP
VP -> V NP | V PP | V NP PP
N -> 'cat'
N -> 'dog'
N -> 'rug'
V -> 'chased'
V -> 'sat'
P -> 'in'
P -> 'on'

The problem here that I couldn't resolve was loop.

For example, do you see loop where there can be PP -> NP -> PP and so on?

Is there anything in PHP that works like Push-down automata that can solve this problem?

Example input: 'the cat chased the dog'

Example output:

(S (NP the (N cat)) (VP (V chased) (NP the (N dog))))

Example input: 'the cat chased the dog on the rug'

Example output(s):

(S (NP the (N cat)) (VP (V chased) (NP the (N dog) (PP (P on) (NP the (N rug))))))

(S (NP the (N cat)) (VP (V chased) (NP the (N dog)) (PP (P on) (NP the (N rug)))))

  • 写回答

1条回答 默认 最新

  • douyi4991 2011-03-08 01:51
    关注

    The usual approach here is to write a predictive parser. For you, this could mean using regular expressions to match either a noun, verb or predicate and then deciding what production to use. You are correct that parsing a grammar requires the computational power of a push-down automata (ie more than what a regular expression alone can achieve). Simulating a push-down automaton is one approach and is what parser generators like yacc/bison often do. For a small grammar like that though, you can use the call stack implicitly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗