CSc 372 - Comparative Programming Languages
27 : Prolog -- Grammars

Christian Collberg

Department of Computer Science

University of Arizona

1 Prolog Grammar Rules

2 Prolog Grammar Rules...


\begin{gprogram}
s \xx --> np, vp.\\
vp \xx --> v, np.\\
vp \xx --> v.\\
np \...
...}\\
\x yes \\
\redtt{?- s([kissed, john, lisa], []).} \\
\x no
\end{gprogram}

3 Prolog Grammar Rules...


\begin{gprogram}
\redtt{?- s(A, []).}\\
\x A = [john,died,john] ; \\
\x A = [j...
...use] ; \\
\x A = [lisa,kissed,house] ; \\
\x A = [lisa,died] ;
\end{gprogram}

4 Implementing Prolog Grammar Rules

5 Implementing Prolog Grammar Rules...


\begin{gprogram}
s(Z) :- np(X), vp(Y), append(X,Y,Z). \\
np(Z) :- n(Z). \\
vp(...
...\\
\x S = [john,died,john] ;\\
\x S = [john,died,lisa] ; \ldots
\end{gprogram}

6 Implementing Prolog Grammar Rules...

7 Implementing Prolog Grammar Rules...


\begin{gprogram}
s(A,B) \xxx :- np(A,C), vp(C,B).\\
np(A,B) \xxx :- n(A,B).\\
...
...kissed\vert R], []).}\\
\x R = [john] ;\\
\x R = [lisa] ;\ldots
\end{gprogram}

8 Generating Parse Trees

9 Generating Parse Trees...


\begin{gprogram}
s(s(NP,VP)) \xxxxx --> np(NP), vp(VP).\\
vp(vp(V, NP)) \xxxxx ...
...(n(died)) \xxxxx --> [died].\\
v(n(kissed)) \xxxxx --> [kissed].
\end{gprogram}

10 Generating Parse Trees...


\begin{gprogram}
\redtt{?- s(S, [john, kissed, lisa], []).} \\
S=s(np(n(john)),...
... died, lisa], []).} \\
S=s(np(n(john)),vp(n(died),np(n(lisa))))
\end{gprogram}

11 Generating Parse Trees...


\begin{gprogram}
\redtt{?- s(s(np(n(john)),vp(n(kissed),} \\
\xxx \redtt{np(n(lisa)))), S, []).} \\
\xx S=[john, kissed, lisa]
\end{gprogram}

12 Ambiguity

Lexical ambiguity:

homographic
polysemous
homophonic

13 Ambiguity...

Syntactic ambiguity:

14 Ambiguity...


\begin{gprogram}
s(s(NP,VP)) --> np(NP), vp(VP).\\
vp(vp(V, NP)) --> v(V), np(N...
...her)) --> [her].\\
\\
\redtt{?- s(S, [i, saw, her, duck], []).}
\end{gprogram}

15 Pascal Declarations


\begin{gprogram}
\redtt{?- decl([const, a, =, 5, ;,} \\
\xxx \redtt{var, x, :, ...
...-> \xxx const\_decl, type\_decl, \\
\xxx var\_decl, proc\_decl.
\end{gprogram}

16 Pascal Declarations


\begin{gprogram}
\% Constant declarations \\
const\_decl --> [ ]. \\
const\_de...
...], \{atom(X)\}. \\
constant --> [X], \{(integer(X); float(X))\}.
\end{gprogram}

17 Pascal Declarations...


\begin{gprogram}
\% Type declarations \\
type\_decl --> [ ]. \\
type\_decl -->...
... type --> ['REAL']. \\
type --> ['BOOLEAN']. type --> ['CHAR'].
\end{gprogram}

18 Pascal Declarations...


\begin{gprogram}
\% Variable decleclarations \\
var\_decl --> [ ]. \\
var\_dec...
...st --> identifier. \\
id\_list --> identifier, [','], id\_list.
\end{gprogram}

19 Pascal Declarations...


\begin{gprogram}
\% Procedure declarations \\
proc\_decl --> [ ]. \\
proc\_dec...
...\_params --> var\_def. \\
variable\_params --> [var], var\_def.
\end{gprogram}

20 Pascal Declarations - Building Trees


\begin{gprogram}
decl(decl(C, T, V, P)) --> \\
\x const\_decl(C), type\_decl(T)...
...D, Ds)) --> \\
\x [const], const\_def(D), [;], const\_defs(Ds).
\end{gprogram}

21 Pascal Declarations - Building Trees...


\begin{gprogram}
const\_defs(null) --> [ ]. \\
const\_defs(const(D, Ds)) --> \\...
...atom(X)\}. \\
const(num(X)) --> [X], \{(integer(X); float(X))\}.
\end{gprogram}

22 Pascal Declarations - Example Parse

Pascal1

23 Pascal Declarations - Example Parse...


\begin{gprogram}
\redtt{?- decl(S, [const, a, =, 5, ;, x, =, 3.14, ;], []).} \\ ...
...(def(id(x),num(3.14)),\\
\xxxxxx null)),\\
\xxx null,null,null)
\end{gprogram}

24 Number Conversion


\begin{gprogram}
\redtt{?- number(V, [sixty, three], []).} \\
\x V = 63 \\
\re...
...
\x V = 999 \\
\redtt{?- number(V, [fifty, ten], []).} \\
\x no
\end{gprogram}

25 Number Conversion...


\begin{gprogram}
number(0) --> [zero]. \\
number(N) --> xxx(N). \\
\\
xxx(N)...
...is T+N1\}. \\
\\
rest\_xx(0) --> [ ]. rest\_xx(N) --> digit(N).
\end{gprogram}

26 Number Conversion...


\begin{gprogram}
digit(1) --> [one]. \xxxxxxxx teen(10) --> [ten]. \\
digit(2) ...
...ty]. \\
tens(80) --> [eighty] .\xxxxxxxx tens(90) --> [ninety].
\end{gprogram}

27 Expression Evaluation


\begin{gprogram}
\redtt{?- expr(X, ''234+345*456'', []).} \\
\x X = 157554 \\
...
... num(X), ''/'', term(Y), \{Z is X /Y \}. \\
term(Z) --> num(Z).
\end{gprogram}

28 Expression Evaluation...


\begin{gprogram}
num(C) --> ''+'', num(C). \\
num(C) --> ''-'', num(X), \{C is ...
...\
digit(X) --> [C], \{''0'' =< C, C =< ''9'',X is C-''0''\}. \\
\end{gprogram}

29 Summary

30 Summary...

31 Homework


\begin{gprogram}
digit(1) --> [one]. .... \\
digit(9) --> [nine]. \\
teen(10) ...
...een]. \\
tens(20) --> [twenty]. .... \\
tens(90) --> [ninety].
\end{gprogram}


\begin{gprogram}
\redtt{?- time(T, [eight, am], []).} \\
\x T = 8:0 \% Or, better, 8:00 \\
\end{gprogram}

32 Homework...


\begin{gprogram}
\redtt{?- time(T, [eight, thirty, am], []).} \\
\x T = 8:30 \\...
...
\redtt{?- time(T,[twelve,thirty,am],[]).} \\
\x T = 0:30 \% !!!
\end{gprogram}

33 Homework...


\begin{gprogram}
\redtt{?- time(T,[eleven,thirty,pm],[]).} \\
\x T = 23:30 \\
...
...\
\redtt{?- time(T,[half,past,four,pm],[]).} \\
\x T = 16:30\\
\end{gprogram}



Christian S. Collberg
2005-11-07