Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs7120 (Prasad)L21-DCG1 Definite Clause Grammars

Similar presentations


Presentation on theme: "Cs7120 (Prasad)L21-DCG1 Definite Clause Grammars"— Presentation transcript:

1 cs7120 (Prasad)L21-DCG1 Definite Clause Grammars t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/

2 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-DCG2 L1L2 [a,b,c|T]T [a,b,c,d|T][d|T]

3 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-DCG3 X Y Z X-Y Y-Z Y Z Z X-Z

4 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-DCG4

5 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-DCG5

6 DCG Example : Syntax sentence --> 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-DCG6

7 DCG to Ordinary Prolog Syntax sentence(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-DCG7

8 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-DCG8

9 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-DCG9

10 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-DCG10

11 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, AST, etc). Number disagreement causes the last query to fail. cs7120 (Prasad)L21-DCG11

12 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-DCG12

13 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-DCG13

14 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-DCG14

15 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-DCG15

16 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-DCG16

17 (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-DCG17

18 (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-DCG18

19 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-DCG19

20 (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-DCG20

21 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.pdfhttp://www.cs.wright.edu/~tkprasad/papers/Attribute-Grammars.pdf cs7120 (Prasad)L21-DCG21

22 Variation on Expression Grammars Inefficient 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-DCG22

23 RELATIONSHIP TO ATTRIBUTE GRAMMARS 

24  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

25 cs7120 (Prasad)L21-DCG25 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 ) = G j ( I( X0 ), A( X1 ), A( X2 ), …, A( Xj-1 )) for all j in 1..n P ( A( X0 ), A( X1 ), A( X2 ), …, A( Xn ) )

26 cs7120 (Prasad)L21-DCG26 Information Flow inherited synthesized  computed  available

27 cs7120 (Prasad)L21-DCG27 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.

28 cs7120 (Prasad)L21-DCG28 An Extended Example Distinct identifiers in a straight-line program. BNF ::= | + ::= := | ; Attributes  id  ids  ids  num Semantics specified in terms of sets (of identifiers).

29 cs7120 (Prasad)L21-DCG29 ::=.ids = {.id } ::= +.ids =.ids U.ids ::= :=.ids ={.id } U.ids.num = |.ids | ::= ;.ids =.ids U.ids.num = |.ids |

30 cs7120 (Prasad)L21-DCG30 Alternative Semantics using lists Attributes  envi : list of vars in preceding context  envo : list of vars for following context  dnum : number of new variables ::=.envo = if member(.id,.envi) then.envi else cons(.id,.envi)

31 cs7120 (Prasad)L21-DCG31 ::= +  envi  envi  envi  envo  envo  envo  dnum  dnum  dnum.envi =.envi.envi =.envo.envo =.envo.dnum = length(.envo) Attribute Computation Rules


Download ppt "Cs7120 (Prasad)L21-DCG1 Definite Clause Grammars"

Similar presentations


Ads by Google