prolog can be used for parsing context-free grammars

13
53 Prolog can be used for parsing context-free grammars Here is a simple grammar: s -> 'a' 'b' s -> 'a' 'c' s -> s s Terminals: a, b Non-terminals: s

Upload: others

Post on 10-Feb-2022

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Prolog can be used for parsing context-free grammars

53

Prolog can be used for parsing context-free grammars

Here is a simple grammar:

s -> 'a' 'b's -> 'a' 'c's -> s s

Terminals: a, bNon-terminals: s

Page 2: Prolog can be used for parsing context-free grammars

54

Parsing by consumption

Write a predicate for each non-terminal which consumes as much as the first list

which is necessary to match the non-terminal and returns the remaining

elements in the second list

e.g.s([a,b],[]), s([a,b,c,d],[c,d])

Page 3: Prolog can be used for parsing context-free grammars

55

A Prolog program which accepts sentences from our

grammar

c([X|T],X,T).

s(In,Out) :- c(In,a,In2), c(In2,b,Out).s(In,Out) :- c(In,a,In2), c(In2,c,Out).s(In,Out) :- s(In,In2), s(In2,Out).

s -> 'a' 'b's -> 'a' 'c's -> s s

Page 4: Prolog can be used for parsing context-free grammars

56

Prolog provides a shortcut syntax for this

s --> [a],[b].s --> [a],[c].

s --> s,s.

This will both test and generate: s([a,c,a,b],[]) or s(A,[]).

s -> 'a' 'b's -> 'a' 'c's -> s s

Page 5: Prolog can be used for parsing context-free grammars

57

Building a parse tree

c([X|T],X,T).

s(ab,In,Out) :- c(In,a,In2), c(In2,b,Out).s(ac,In,Out) :- c(In,a,In2), c(In2,c,Out).s(t(A,B),In,Out) :- s(A,In,In2), s(B,In2,Out).

:- s(Result,[a,c,a,b,a,b],[]).

Page 6: Prolog can be used for parsing context-free grammars

58

Building a parse tree

s(ab) --> [a],[b].s(ac) --> [a],[c].

s(t(A,B)) --> s(A),s(B).

Page 7: Prolog can be used for parsing context-free grammars

59

Parsing Natural Language(back to Prolog's roots)

s --> np,vp.np --> det,n.vp --> v.vp --> v,np.

n --> [cat].n --> [dog].v --> [eats].det --> [the].

This is a very limited grammar of English. Things get complicated very quickly – for more see the Natural Language Processing course next year (Prolog is not a pre-requisite)

Page 8: Prolog can be used for parsing context-free grammars

60

the dog eats the cat

det noun

verb

det noun

np

np

vp

s

Page 9: Prolog can be used for parsing context-free grammars

61

We can also handle agreements(N) --> np(N),vp(N).np(N) --> det,n(N).vp(N) --> v(N).vp(N) --> v(N),np(_).

n(s) --> [cat].n(s) --> [dog].n(p) --> [cats].v(s) --> [eats].v(p) --> [eat].det --> [the].

We consider onlythird-person constructions

Page 10: Prolog can be used for parsing context-free grammars

62

the cats eat the dog

det n(p)

verb(p)

det n(s)

np(p)

np(s)

vp(p)

s

Page 11: Prolog can be used for parsing context-free grammars

63

Things get much more complicated very quickly

Ambiguities, special cases and noise all make this hard to scale up to a full

language

Page 12: Prolog can be used for parsing context-free grammars

64

Closing Remarks● Declarative programming is different to Functional or

Procedural programming

– Foundations of Computer Science & Programming in Java

● Prolog is built on logical deduction

– formal explanation in Logic & Proof

● It can provide concise implementations of algorithms such as sorting or graph search

– Algorithms I & Algorithms II

Page 13: Prolog can be used for parsing context-free grammars

65

Closing Remarks● Foundations of Functional Programming (Part IB)

– Building computation from first principles

● Databases (Part 1B)

– Find out more about representing data and SQL

● Artificial Intelligence (Part 1B)

– Search, constraint programming and more

● C & C++ (Part 1B)

– Doing useful stuff in the real world

● Natural Language Processing (Part II)

– Parsing natural language