SLIDE 37 Grammars and Parsing
75
length of the input. Even with this worst-case analysis, PROLOG-based grammars can be quite efficient in practice. It is also possible to insert chart-like mechanisms to improve the efficiency of a grammar, although then the simple correspondence between context-free rules and PROLOG rules is lost. Some of these issues will be discussed in the next chapter. It is worthwhile to try some simple grammars written in PROLOG to better understand top-down, depth-first search. By turning on the tracing facility, you can obtain a trace similar in content to that shown in Figure 3.30.
1. s(P1, P3) :– np(P1, P2), vp(P2, P3) 2. np(P1, P3) :– art(P1, P2), n(P2, P3) 3. np(P1, P3) :– name(P1, P3) 4. pp(P1, P3) :– p(P1, P2), np(P2, P3) 5. vp(P1, P2) :– v(P1, P2) 6. vp(P1, P3) :– v(P1, P2), np(P2, P3) 7. vp(P1, P3) :– v(P1, P2), pp(P2, P3)
Figure 3.29 A PROLOG-based representation of Grammar 3.4
Step Current State Backup States Comments
1. s(1, 5) 2. np(1, P2) vp(P2, 5) 3. art(1, P2´) n(P2´, P2) vp(P2, 5) name(1, P2) vp(P2, 5) fails as no ART at position 1 4. name(1, P2) vp(P2, 5) 5. vp(2, 5) name(1, 2) proven 6. v(2, 5) v(2, P2) np(P2, 5) fails as no verb spans v(2, P2) pp(P2, 5) positions 2 to 5 7. v(2, P2) np(P2, 5) v(2, P2) pp(P2, 5) 8. np(3, 5) v(2, P2) pp(P2, 5) v(2, 3) proven 9. art(3, P2) n(P2, 5) name(3, 5) v(2, P2) pp(P2, 5) 10. n(4, 5) name(3, 5) art(3, 4) proven v(2, P2) pp(P2, 5) 11. √ proof succeeds name(3, 5) n(4, 5) proven v(2, P2) pp(P2, 5)
Figure 3.30 A trace of a PROLOG-based parse of John ate the cat