Logic grammars - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Logic grammars

Description:

e(V) -- t(V1), [-], e(V2), {V is V1 - V2}. e(V) -- t(V) ... t(V) -- f(V). f(V) -- {integer(V)}. Larger example. Compiler for a small imperative language ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 15
Provided by: timokn
Category:

less

Transcript and Presenter's Notes

Title: Logic grammars


1
Logic grammars
  • Parsing traditional application area of LP
  • Context-free (CF) grammars
  • G (N,T,P,S)
  • N nonterminal symbols, T terminal symbols
  • P set of productions N ? (N ? T)
  • S starting nonterminal
  • derivation ?A? ?_G ??? iff A ? ? ? P
  • L(G) w ? T S ?_G w
  • Formalize L(G) as a logic program
  • define ? and its transitive closure
  • e.g. g_prod(N, Beta)

2
w ? L in LP
  • in_lang(W) ? g_init(S), derives(S, W).
  • derives(W, W).
  • derives(A, W) ? direct(A, B), derives(B, W).
  • direct(N, M) ? append3(Alpha, A, Gamma, N),
  • g_prod(A, Beta), append3(Alpha, Beta, Gamma,
    M).

3
Example grammar (anbn)
  • Terminals nonterminals
  • terminal(a). terminal(b).
  • nonterminal(s). nonterminal(t).
  • Initial symbol
  • g_init(s).
  • Productions
  • g_prod(s, a,t,b).
  • g_prod(t, ).
  • g_prod(t, a,t,b).
  • Test
  • ? in_lang(a,a,b,b).

4
Some optimizations
  • We have to expand all nonterminals
  • so, the order does not matter (as in LP
    derivations)
  • ? choose always the leftmost nonterminal
  • direct(NNs, M) ? nonterminal(N), g_prod(N,
    Beta), append(Beta, Ns, M).
  • direct(NNs, NMs) ? terminal(N),
  • direct(Ns, Ms).
  • direct(, ).

5
Logic grammars
  • More efficient way to code CF parsers
  • and any applications built on them
  • Result of unfolding the general simulator
  • For each N ? A1,,Am output
  • nt_N(S0)? B1,,Bm where
  • Bi
  • nt_Ai(Beta_i), append(Beta_i, Si, Si-1) or
  • cons(Ai, Si, Si-1) for terminals
  • Note
  • Beta_i part of the parsed word corresponding to
    Ai
  • Si remaining part of the word (to be parsed by
    Ai1Am)

6
Example N ? ? aNb
  • nt_N().
  • nt_N(S0) ? cons(a, S1, S0), nt_N(BN),
    append(BN, S2, S1), cons(b, , S2).

7
Change lists to d-lists
  • nt_N(L) ? dl_empty(L).
  • nt_N(S0) ? dl_cons(a, S1, S0), nt_N(BN),
    dl_append(BN, S2, S1), dl_empty(S3),
    dl_cons(b, S3, S2).

8
Some transformations
  • Unfold dl_append, dl_empty, dl_cons
  • In the result below, we denote Si with Fi Li
  • nt_N(L-L).
  • nt_N(aF1-L3)? nt_N(F1-bL3).
  • Replace -/2 with 2 arguments
  • nt_N(L, L).
  • nt_N(aF1, L3)?nt_N(F1, bL3).
  • Usage
  • ? nt_N(Word, ). implicit list2dl

9
General transformation procedure
  • Production N ? A1,,Am
  • nt_N(F0, Lm)
  • chain difference list ends/heads L_(i-1)F_i
  • (result of unfolding calls to append_dl)
  • for each Ai
  • nonterminal nt_Ai(L_(i-1), L_i)
  • terminal L_(i-1) AiFi - Fi
  • Automatic transformation possible!

10
Definite Clause Grammars
  • Compact notation for grammars
  • Translated automatically to the unfolded
    dl-version by the Prolog interpreter
  • Example grammar
  • nt_N --gt .
  • nt_N --gt a, nt_N, b.
  • Extension of CF
  • nonterminals can have arguments
  • nonterminals are unified with the productions
  • not just matched as letters as in normal grammars

11
Derivation in DCG
  • G (N,T,P)
  • ? p(s1,,sn) ? derives to (? ? ?)?
  • P contains a production p(t1,,tn) ? ?
  • p(s1,,sn) and p(t1,,tn) unify with mgu ?
  • Example (note a non-CF grammar)
  • abc --gt a(N), b(N), c(N).
  • a(0) --gt .
  • a(s(N)) --gt a, a(N).
  • sim. for b and c

12
Operational nonterminals
  • Nonterminals that
  • do not directly relate to the parsing itself
  • do not consume the input
  • Example
  • a2nb2nc2n --gt a(N),b(N),c(N), even(N).
  • even(0) --gt .
  • even(s(s(N))) --gt even(N).
  • even/1 produces always the empty string
  • Special notation (/1)
  • tells the DCG translator not to translate
    anything inside

13
Example DCG
  • value(E, V) ? e(V, E, ).
  • e(V) --gt t(V1), , e(V2), V is V1 V2.
  • e(V) --gt t(V1), -, e(V2), V is V1 - V2.
  • e(V) --gt t(V).
  • t(V) --gt f(V1), , t(V2), V is V1 V2.
  • t(V) --gt f(V1), /, t(V2), V is V1 / V2.
  • t(V) --gt f(V).
  • f(V) --gt integer(V).

14
Larger example
  • Compiler for a small imperative language
  • Parser implemented using a DCG
  • Translation to assembly code
  • Final executable
  • memory allocation addressing
  • (sort of) pipeline optimization
Write a Comment
User Comments (0)
About PowerShow.com