Definite Clause Grammars - PowerPoint PPT Presentation

About This Presentation
Title:

Definite Clause Grammars

Description:

Title: Logic Programming Author: T. K. Prasad Last modified by: test Created Date: 9/30/1996 6:28:10 PM Document presentation format: On-screen Show (4:3) – PowerPoint PPT presentation

Number of Views:121
Avg rating:3.0/5.0
Slides: 32
Provided by: TK181
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Definite Clause Grammars


1
Definite Clause Grammars
  • t.k.prasad_at_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.

L1 L2
a,b,cT T
a,b,c,dT dT
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)

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

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

6
DCG Example Syntax
  • sentence --gt noun_phrase, verb_phrase.
  • noun_phrase --gt determiner, noun.
  • verb_phrase --gt verb, noun_phrase.
  • determiner --gt a.
  • determiner --gt the.
  • determiner --gt many.
  • noun --gt president.
  • noun --gt cat.
  • noun --gt cats.
  • verb --gt has.
  • verb --gt have.

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(aR,R).
  • determiner(theR,R).
  • determiner(manyR,R).
  • noun(presidentR,R).
  • noun(catR,R).
  • noun(catsR,R).
  • verb(hasR,R).
  • verb(haveR,R).

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.

9
DCG Example Number Agreement
  • sentence --gt noun_phrase(N),verb_phrase(N).
  • noun_phrase(N) --gt determiner(N), noun(N).
  • verb_phrase(N) --gt verb(N), noun_phrase(_).
  • determiner(sgular) --gt a.
  • determiner(_) --gt the.
  • determiner(plural) --gt many.
  • noun(sgular) --gt president.
  • noun(sgular) --gt cat.
  • noun(plural) --gt cats.
  • verb(sgular) --gt has.
  • verb(plural) --gt have.

10
Extension AST plus Number agreement
  • sentence(s(NP,VP)) --gt noun_phrase(N,
    NP),verb_phrase(N, VP).
  • noun_phrase(N, np(D,NT)) --gt determiner(N, D),
    noun(N, NT).
  • verb_phrase(N, vp(V,NP)) --gt verb(N, V),
    noun_phrase(_, NP).
  • determiner(sgular, dt(a)) --gt a.
  • determiner(_, dt(the)) --gt the.
  • determiner(plural, dt(many)) --gt many.
  • noun(sgular, n(president)) --gt president.
  • noun(sgular, n(cat)) --gt cat.
  • noun(plural, n(cats)) --gt cats.
  • verb(sgular, v(has)) --gt has.
  • verb(plural, v(have)) --gt have.

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, presidentX,
    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.

12
Prefix Expression DCG
  • expr --gt if, expr, then, expr,
  • else, expr.
  • expr --gt , expr, expr.
  • expr --gt , expr, expr.
  • expr --gt m.
  • expr --gt n.
  • expr --gt a.
  • expr --gt b.

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, ).

14
Prefix Expression DCG Type Checking Version
  • tExpr(T) --gt if, tExpr(bool), then, tExpr(T),
  • else, tExpr(T).
  • tExpr(T) --gt , tExpr(T), tExpr(T).
  • tExpr(T) --gt , tExpr(T), tExpr(T).
  • tExpr(int) --gt m.
  • tExpr(int) --gt n.
  • tExpr(bool) --gt a.
  • tExpr(bool) --gt b.
  • Assume that and are overloaded for int and
    bool.

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, ).

16
Prefix Expression DCG Type Checking and
Evaluation Version
  • evalExpr(V) --gt etExpr(V,_).
  • etExpr(V,T) --gt if, etExpr(B,bool), then,
    etExpr(V1,T), else, etExpr(V2,T),
  • Btrue -gt V V1 V V2.
  • etExpr(V,bool) --gt ,
  • etExpr(V1,bool), etExpr(V2,bool),
  • or(V1,V2,V).
  • etExpr(V,int) --gt ,
  • etExpr(V1,int), etExpr(V2,int),
  • V is V1 V2.

17
(contd)
  • etExpr(V,bool) --gt ,
  • etExpr(V1,bool), etExpr(V2,bool),
  • and(V1,V2,V).
  • etExpr(V,bool) --gt ,
  • etExpr(V1,int), etExpr(V2,int),
  • V is V1 V2.
  • etExpr(V,int) --gt m, value(m,V).
  • etExpr(V,int) --gt n, value(n,V).
  • etExpr(V,bool) --gt a, value(a,V).
  • etExpr(V,bool) --gt b, value(b,V).

18
(contd)
  • 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).

19
Prefix Expression DCG AST Version
  • treeExpr(V) --gt trExpr(V,_).
  • trExpr(cond(B,V1,V2),T) --gt
  • if, trExpr(B,bool), then,
    trExpr(V1,T), else, trExpr(V2,T).
  • trExpr(or(V1,V2),bool) --gt ,
  • trExpr(V1,bool), trExpr(V2,bool).
  • trExpr(plus(V1,V2),int) --gt ,
  • trExpr(V1,int), trExpr(V2,int).

20
(contd)
  • trExpr(and(V1,V2),bool) --gt ,
  • trExpr(V1,bool), trExpr(V2,bool).
  • trExpr(mul(V1,V2),int) --gt ,
  • trExpr(V1,int), trExpr(V2,int).
  • trExpr(m,int) --gt m.
  • trExpr(n,int) --gt n.
  • trExpr(a,bool) --gt a.
  • trExpr(b,bool) --gt b.

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/At
    tribute-Grammars.pdf

22
Variation on Expression Grammars
  • Inefficient
  • Backtracking Parser Exists
  • Unsuitable Grammar
  • E -gt T E T
  • T -gt F T F
  • F -gt (E)
  • x
  • y
  • E -gt E E
  • E E
  • x
  • y

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
  • Attributes A(X)
  • Synthesized S(X)
  • Inherited I(X)
  • Attribute computation rules (Semantic functions)
  • X0 -gt 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) )

26
Information Flow
inherited
computed
available
synthesized
...
...
27
  • Synthesized Attributes
  • Pass information up the parse tree
  • Inherited 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
An Extended Example
  • Distinct identifiers in a straight-line
    program.
  • BNF
  • ltexpgt ltvargt ltexpgt ltexpgt
  • ltstmgt ltvargt ltexpgt ltstmgt ltstmgt
  • Attributes
  • ltvargt ? id
  • ltexpgt ? ids
  • ltstmgt ? ids ? num
  • Semantics specified in terms of sets (of
    identifiers).

29
  • ltexpgt ltvargt
  • ltexpgt.ids ltvargt.id
  • ltexpgt ltexp1gt ltexp2gt
  • ltexpgt.ids ltexpgt.ids U ltexpgt.ids
  • ltstmgt ltvargt ltexpgt
  • ltstmgt.ids ltvargt.id U ltexpgt.ids
  • ltstmgt.num ltstmgt.ids
  • ltstmgt ltstm1gt ltstm2gt
  • ltstmgt.ids ltstm1gt.ids U ltstm2gt.ids
  • ltstmgt.num ltstmgt.ids

30
Alternative Semantics using lists
  • Attributes
  • ? envi list of vars in preceding context
  • ? envo list of vars for following context
  • ? dnum number of new variables
  • ltexpgt ltvargt
  • ltexpgt.envo
  • if member(ltvargt.id,ltexpgt.envi)
  • then ltexpgt.envi
  • else cons(ltvargt.id,ltexpgt.envi)

31
Attribute Computation Rules
  • ltexpgt ltexp1gt ltexp2gt
  • ? envi ? envi ? envi
  • ? envo ? envo ? envo
  • ? dnum ? dnum ? dnum
  • ltexp1gt.envi ltexpgt.envi
  • ltexp2gt.envi ltexp1gt.envo
  • ltexpgt.envo ltexp2gt.envo
  • ltexpgt.dnum length(ltexpgt.envo)
Write a Comment
User Comments (0)
About PowerShow.com