+ All Categories
Home > Documents > Definite Clause Grammars

Definite Clause Grammars

Date post: 21-Mar-2016
Category:
Upload: blaine
View: 30 times
Download: 0 times
Share this document with a friend
Description:
Definite Clause Grammars. [email protected] http://www.knoesis.org/tkprasad/. Review : Difference Lists. Represent list L as a difference of two lists L1 and L2 E.g., consider L = [a,b,c] and various L1-L2 combinations given below. Review: Append using Difference Lists. - PowerPoint PPT Presentation
32
cs7120 (Prasad) L21-DCG 1 Definite Clause Grammars [email protected] http://www.knoesis.org/ tkprasad/
Transcript
Page 1: Definite Clause Grammars

cs7120 (Prasad) L21-DCG 1

Definite Clause Grammars

[email protected]://www.knoesis.org/tkprasad/

Page 2: Definite Clause Grammars

Review : Difference Lists • Represent list L as a difference of two lists

L1 and L2– E.g., consider L = [a,b,c] and various L1-L2

combinations given below.

cs7120 (Prasad) L21-DCG 2

L1 L2[a,b,c|T] T

[a,b,c,d|T] [d|T]

Page 3: Definite Clause Grammars

Review: Append using Difference Lists

append(X-Y, Y-Z, X-Z).• Ordinary append complexity = O(length of first list)• Difference list append complexity = O(1)

cs7120 (Prasad) L21-DCG 3

X

Y

Z

X-Y

Y-Z

Y

Z

Z

X-Z

Page 4: Definite Clause Grammars

Running append goal

cs7120 (Prasad) L21-DCG 4

Page 5: Definite Clause Grammars

DCGs

• Mechanize attribute grammar formalism (a generalization of CFG)– Executable specification

• Use difference lists for efficiency

• Translation from DCGs to Prolog clauses is automatic

cs7120 (Prasad) L21-DCG 5

Page 6: Definite Clause Grammars

Sample Applications of DCGs

• Coding recursive descent backtracking parser

• Encoding and checking context-sensitive constraints

• Simple NLP

• In general, enabling syntax directed translation

• E.g., VHDL Parser-Pretty Printer

cs7120 (Prasad) L21-DCG 6

Page 7: Definite Clause Grammars

DCG Example : Syntaxsentence --> noun_phrase, verb_phrase.noun_phrase --> determiner, noun.verb_phrase --> verb, noun_phrase.determiner --> [a].determiner --> [the].determiner --> [many].noun --> [president].noun --> [cat].noun --> [cats].verb --> [has].verb --> [have].

cs7120 (Prasad) L21-DCG 7

Page 8: Definite Clause Grammars

DCG to Ordinary Prolog Syntaxsentence(S,R) :-

noun_phrase(S,T), verb_phrase(T,R).noun_phrase(S,T) :- determiner(S,N), noun(N,T).verb_phrase(T,R) :- verb(T,N), noun_phrase(N,R).determiner([a|R],R).determiner([the|R],R).determiner([many|R],R).noun([president|R],R).noun([cat|R],R).noun([cats|R],R).verb([has|R],R).verb([have|R],R).

cs7120 (Prasad) L21-DCG 8

Page 9: Definite Clause Grammars

Queries?- sentence([the, president, has, a, cat], []).

?- sentence([the, cats, have, a, president], []).

?- sentence([a, cats, has, the, cat, president], [president]).

?- sentence([a, cats, has, the, cat, President], [President]).

• Each non-terminal takes two lists as arguments. • In difference list representation, they together

stand for a single list.

cs7120 (Prasad) L21-DCG 9

Page 10: Definite Clause Grammars

DCG Example: Number Agreement

sentence --> noun_phrase(N),verb_phrase(N).noun_phrase(N) --> determiner(N), noun(N).verb_phrase(N) --> verb(N), noun_phrase(_).

determiner(sgular) --> [a].determiner(_) --> [the].determiner(plural) --> [many].

noun(sgular) --> [president].noun(sgular) --> [cat].noun(plural) --> [cats].verb(sgular) --> [has].verb(plural) --> [have].

cs7120 (Prasad) L21-DCG 10

Page 11: Definite Clause Grammars

Extension: AST plus Number agreement

sentence(s(NP,VP)) --> noun_phrase(N, NP),verb_phrase(N, VP).

noun_phrase(N, np(D,NT)) --> determiner(N, D), noun(N, NT).

verb_phrase(N, vp(V,NP)) --> verb(N, V), noun_phrase(_, NP).

determiner(sgular, dt(a)) --> [a].

determiner(_, dt(the)) --> [the].

determiner(plural, dt(many)) --> [many].

noun(sgular, n(president)) --> [president].

noun(sgular, n(cat)) --> [cat].

noun(plural, n(cats)) --> [cats].

verb(sgular, v(has)) --> [has].

verb(plural, v(have)) --> [have].

cs7120 (Prasad) L21-DCG 11

Page 12: Definite Clause Grammars

Queries?- sentence(T,[the, president, has, a, cat], []).

T = s(np(dt(the), n(president)), vp(v(has), np(dt(a), n(cat)))) ;

?- sentence(T,[the, cats, have, a, president|X], X).

?- sentence(T,[a, cats, has, the, cat, preside], [preside]).

• Each non-terminal takes two lists as arguments for input sentences, and additional arguments for the static semantics (e.g., number and AST).

• Number disagreement causes the last query to fail.

cs7120 (Prasad) L21-DCG 12

Page 13: Definite Clause Grammars

Prefix Expression DCG

expr --> [if], expr, [then], expr,

[else], expr.

expr --> [’+’], expr, expr.

expr --> [’*’], expr, expr.

expr --> [m].

expr --> [n].

expr --> [a].

expr --> [b].

cs7120 (Prasad) L21-DCG 13

Page 14: Definite Clause Grammars

Queries

?-expr([’*’, m, n], []).

?-expr([m, ’*’, n], []).

?-expr([’*’, m, ’+’, ’a’, n, n], [n]).

?-expr([if, a, then, m, else, n], []).

?-expr([if, a, then, a, else, ’*’, m, n], []).

cs7120 (Prasad) L21-DCG 14

Page 15: Definite Clause Grammars

Prefix Expression DCG : Type Checking Version

tExpr(T) --> [if], tExpr(bool), [then], tExpr(T),

[else], tExpr(T).

tExpr(T) --> [’+’], tExpr(T), tExpr(T).

tExpr(T) --> [’*’], tExpr(T), tExpr(T).

tExpr(int) --> [m].

tExpr(int) --> [n].

tExpr(bool) --> [a].

tExpr(bool) --> [b].

• Assume that + and * are overloaded for int and bool.

cs7120 (Prasad) L21-DCG 15

Page 16: Definite Clause Grammars

Queries

?-tExpr(T,[’*’, m, n], []).

?-tExpr(T,[m, ’*’, n], []).

?-tExpr(T,[’*’, m, ’+’, ’a’, n, n], [n]).

?-tExpr(T,[if, a, then, m, else, n], []).

T = int ;

?-tExpr(T,[if, a, then, b, else, ’*’, m, n], []).

cs7120 (Prasad) L21-DCG 16

Page 17: Definite Clause Grammars

Prefix Expression DCG : Type Checking and Evaluation Version

evalExpr(V) --> etExpr(V,_).

etExpr(V,T) --> [if], etExpr(B,bool), [then],

etExpr(V1,T), [else], etExpr(V2,T),

{B==true -> V = V1 ; V = V2}.

etExpr(V,bool) --> [’+’],

etExpr(V1,bool), etExpr(V2,bool),

{or(V1,V2,V)}.

etExpr(V,int) --> [’+’],

etExpr(V1,int), etExpr(V2,int),

{V is V1 + V2}.

cs7120 (Prasad) L21-DCG 17

Page 18: Definite Clause Grammars

(cont’d)

etExpr(V,bool) --> [’*’],

etExpr(V1,bool), etExpr(V2,bool),

{and(V1,V2,V)}.

etExpr(V,bool) --> [’*’],

etExpr(V1,int), etExpr(V2,int),

{V is V1 * V2}.

etExpr(V,int) --> [m], {value(m,V)}.

etExpr(V,int) --> [n], {value(n,V)}.

etExpr(V,bool) --> [a], {value(a,V)}.

etExpr(V,bool) --> [b], {value(b,V)}.

cs7120 (Prasad) L21-DCG 18

Page 19: Definite Clause Grammars

(cont’d)

value(m,10). value(n,5).value(a,true).value(b,false).

and(true,true,true). and(true,false,false).and(false,true,false).

and(false,false,false).

or(true,true,true). or(true,false,true).or(false,true,true). or(false,false,false).

cs7120 (Prasad) L21-DCG 19

Page 20: Definite Clause Grammars

Prefix Expression DCG : AST Version

treeExpr(V) --> trExpr(V,_).

trExpr(cond(B,V1,V2),T) -->

[if], trExpr(B,bool), [then], trExpr(V1,T),

[else], trExpr(V2,T).

trExpr(or(V1,V2),bool) --> [’+’],

trExpr(V1,bool), trExpr(V2,bool).

trExpr(plus(V1,V2),int) --> [’+’],

trExpr(V1,int), trExpr(V2,int).

cs7120 (Prasad) L21-DCG 20

Page 21: Definite Clause Grammars

(cont’d)

trExpr(and(V1,V2),bool) --> [’*’],

trExpr(V1,bool), trExpr(V2,bool).

trExpr(mul(V1,V2),int) --> [’*’],

trExpr(V1,int), trExpr(V2,int).

trExpr(m,int) --> [m].

trExpr(n,int) --> [n].

trExpr(a,bool) --> [a].

trExpr(b,bool) --> [b].

cs7120 (Prasad) L21-DCG 21

Page 22: Definite Clause Grammars

Other Compiler Operations• From parse tree and type information, one

can:– compute (stack) storage requirements for

variables and for expression evaluation– generate assembly code (with coercion

instructions if necessary)– transform/simplify expression

• Ref: http://www.cs.wright.edu/~tkprasad/papers/Attribute-Grammars.pdf

cs7120 (Prasad) L21-DCG 22

Page 23: Definite Clause Grammars

Variation on Expression GrammarsInefficient

Backtracking Parser Exists

E -> T + E | T T -> F * T | F F -> (E) | x | y

Unsuitable Grammar

E -> E + E | E * E | x | y

cs7120 (Prasad) L21-DCG 23

Page 24: Definite Clause Grammars

RELATIONSHIP TO ATTRIBUTE GRAMMARS

4

Page 25: Definite Clause Grammars

5

Attribute Grammars

• Formalism for specifying semantics based on context-free grammars (BNF) Static semantics (context-sensitive aspects)

Type checking and type inference Compatibility between procedure definition and call

Dynamic semantics

• Associate attributes with terminals and non-terminals

• Associate attribute computation rules with productions

Page 26: Definite Clause Grammars

cs7120 (Prasad) L21-DCG 26

• AttributesAttributes A(X)– Synthesized S(X)– Inherited I(X)

• Attribute computation rulesAttribute computation rules (Semantic functions)

X0 -> X1 X2 … Xn

S(X0) = f( I(X0), A(X1), A(X2), …, A(Xn) )I(Xj) = Gj( I(X0), A(X1), A(X2), …, A(Xj-1)) for all j in 1..n P( A(X0), A(X1), A(X2), …, A(Xn) )

Page 27: Definite Clause Grammars

cs7120 (Prasad) L21-DCG 27

Information Flowinherited

synthesized

computedcomputed

availableavailable

Page 28: Definite Clause Grammars

cs7120 (Prasad) L21-DCG 28

• Synthesized AttributesSynthesized Attributes Pass information up the parse tree

• Inherited AttributesInherited Attributes Pass information down the parse tree or

from left siblings to the right siblings• Attribute values assumed to be available from the

context.• Attribute values computed using the semantic rules

provided. The constraints on the attribute evaluation rules

permit top-down left-to-right (one-pass) traversal of the parse tree to compute the meaning.

Page 29: Definite Clause Grammars

cs7120 (Prasad) L21-DCG 29

An Extended Example• Distinct identifiers in a straight-line program.BNF<exp> ::= <var> | <exp> + <exp><stm> ::= <var> := <exp> | <stm> ; <stm>

Attributes <var> id <exp> ids <stm> ids num

• Semantics specified in terms of sets (of identifiers).

Page 30: Definite Clause Grammars

cs7120 (Prasad) L21-DCG 30

<exp> ::= <var><exp>.ids = { <var>.id } <exp> ::= <exp1> + <exp2><exp>.ids = <exp>.ids U <exp>.ids <stm> ::= <var> := <exp><stm>.ids ={ <var>.id } U <exp>.ids<stm>.num = | <stm>.ids | <stm> ::= <stm1> ; <stm2><stm>.ids = <stm1>.ids U <stm2>.ids<stm>.num = | <stm>.ids |

Page 31: Definite Clause Grammars

cs7120 (Prasad) L21-DCG 31

Alternative Semantics using lists• Attributes

envi : list of vars in preceding context envo : list of vars for following context dnum : number of new variables

<exp> ::= <var><exp>.envo = if member(<var>.id,<exp>.envi) then <exp>.envi else cons(<var>.id,<exp>.envi)

Page 32: Definite Clause Grammars

cs7120 (Prasad) L21-DCG 32

<exp> ::= <exp1> + <exp2>

envi envi envi envo envo envo dnum dnum dnum

<exp1>.envi = <exp>.envi <exp2>.envi = <exp1>.envo <exp>.envo = <exp2>.envo <exp>.dnum = length(<exp>.envo)

Attribute Computation Rules


Recommended