Presentation is loading. Please wait.

Presentation is loading. Please wait.

LING 388 Language and Computers Lecture 9 9/30/03 Sandiway FONG.

Similar presentations


Presentation on theme: "LING 388 Language and Computers Lecture 9 9/30/03 Sandiway FONG."— Presentation transcript:

1 LING 388 Language and Computers Lecture 9 9/30/03 Sandiway FONG

2 Administrivia Reminder Reminder  Homework 2 due this Thursday  No late submissions please  Need help?  Talk to Charles or me after the class to set up an appointment  We’re here to help you understand the concepts

3 Recap Regular Grammars FSA Regular Expressions DCG

4 Recap Extensions to regular grammars Extensions to regular grammars 1. Use left and right recursive rules 2. Use multiple terminal/non-terminals on the right (Not a problem for DCG) => Last time, a DCG for the non-regular (context-free) language L n ={a n b n | n>=0} using 1. Now, an alternative DCG for L n using 2.

5 Multiple Non-Terminals/Terminals on the Right Free from the single non-terminal/terminal symbol restriction imposed by regular grammars… Free from the single non-terminal/terminal symbol restriction imposed by regular grammars…  we can encode L n ={a n b n | n>=0 } using just 2 context-free rules as follows:  S -> s --> [].  S -> aSbs --> [a],s,[b].

6 Multiple Non-Terminals/Terminals on the Right Reminder: Reminder:  This rather simple-looking grammar cannot be encoded with a FSA  Requires “working memory” unavailable to the FSA  Intuitively, given … s --> [a],s,[b].s --> [a],s,[b]. recall the regular grammar -> FSA construction idea S a How to keep track of the bs as we read the as?

7 Multiple Non-Terminals/Terminals on the Right However, G n can be simulated using a Push-Down Automata (PDA) However, G n can be simulated using a Push-Down Automata (PDA)  i.e. a FSA plus a stack bb…bbb…b S a / push b B b / pop b Stack

8 Multiple Non-Terminals/Terminals on the Right DCG rule mapping to Prolog clauses: DCG rule mapping to Prolog clauses:  s --> [].  s(L,L).  s --> [a],s,[b].  s(L1,L4) :- ‘C’(L1,a,L2), s(L2,L3), ‘C’(L3,b,L4). L1 L2 L3 L4 Prolog query ?- s(X,[]).

9 Multiple Non-Terminals/Terminals on the Right Informally, we have the following Prolog sentential forms… Informally, we have the following Prolog sentential forms…  ?- s([a,a,a,b,b,b],[]).  a s([a,a,b,b,b],X) b a a s([a,b,b,b],X’) b ba a s([a,b,b,b],X’) b b –a a a s([b,b,b],X”) b b b –a a a s([b,b,b],[b,b,b]) b b b –Note: [b,b,b] - [b,b,b] = [] a a s([a,b,b,b],[b,b]) b ba a s([a,b,b,b],[b,b]) b b Note: [a,b,b,b] – [b,b] = [a,b]Note: [a,b,b,b] – [b,b] = [a,b]  a s([a,a,b,b,b],[b]) b  Note: [a,a,b,b,b] – [b] = [a,a,b,b]

10 So far… We have encoded L n = { a n b n | n >= 0 } using two different DCGs containing: We have encoded L n = { a n b n | n >= 0 } using two different DCGs containing: 1. Left and right recursive rules 2. Three symbols on the right  Now let’s examine one more technique:  Modifying regular grammar G ab where L(G ab ) = { a + b + } without changing the rule format  i.e. without using either 1. or 2.

11 More on the capabilities of DCG rules To do this, we’ll need two more things … To do this, we’ll need two more things …  The ability to call Prolog predicates from DCG rules, and  The capability for non-terminals to be more than just simple names  general structures like: a(X) b(X,Y)a(X) b(X,Y)

12 Calling Prolog Predicates We can insert calls to any Prolog predicate, either built-in or user- defined, anywhere using the notation { … } We can insert calls to any Prolog predicate, either built-in or user- defined, anywhere using the notation { … } Example: Example:  s --> [a], { playChess }, [b], { playGo }.  Perhaps a slightly extreme example … playChess/0 and playGo/0 are not SWI-Prolog built-insplayChess/0 and playGo/0 are not SWI-Prolog built-ins  but the point is that this capability gives the DCG formalism the expressive power of Turing Machines (Turing, 1949) aka “general computing power”  Somewhat more down-to-earth and relevant for L n …  we can use { … } to do arithmetic

13 Non-terminals as structures In general, non-terminals need not be limited to names only In general, non-terminals need not be limited to names only Some instances where we’ll find non-terminals as structures useful: Some instances where we’ll find non-terminals as structures useful:  Subject/Inflection agreement  John likes Mary[ IP [ NP John ][ I’ [ Infl ] …]]  I like Mary[ IP [ NP I ][ I’ [ Infl ] …]]  John is tired  We are tired  IP -> NP(A) I’(A) A a variable controlling agreement between the subject and inflectionA a variable controlling agreement between the subject and inflection

14 Non-terminals as structures In Prolog In Prolog  IP -> NP(A) I’(A) =>  ip --> np(A), ibar(A). A a variable controlling agreement between the subject and inflectionA a variable controlling agreement between the subject and inflection  Simple implementation: np(sg) --> [john]. np(pl) --> [we].np(sg) --> [john]. np(pl) --> [we]. propagates subject number (sg/pl) into non- terminal ibarpropagates subject number (sg/pl) into non- terminal ibar

15 Non-terminals as structures Another use, to build parse trees as Prolog structures… Another use, to build parse trees as Prolog structures…  s(s(NP,VP)) --> np(NP), vp(VP).  np(np(N)) --> pronoun(N).  np(np(det(the),n(ball))) --> [the,ball].  pronoun(i) --> [i].pronoun(we) --> [we].  vp(vp(V)) --> unergative(V).  vp(vp(V,NP)) --> transitive(V), np(NP).  unergative(v(ran)) --> [ran].  transitive(v(hit)) --> [hit]. Prolog query Prolog query  ?- s(X,[i,hit,the,ball],[]).  Note: extra parameter precedes difference list Answer Answer  X = s(np(i),vp(v(hit),np(det(the),n(ball))))

16 From a + b + to a n b n Define G ab such that L(G ab ) = a + b + Define G ab such that L(G ab ) = a + b + We have seen this grammar before as: We have seen this grammar before as:  s --> [a], b.  b --> [a], b.  b --> [b], c.  b --> [b].  c --> [b], c.  c --> [b].

17 From a + b + to a n b n Idea: Idea:  Add a parameter as a counter to the non-terminals to count the number of as and bs  Input stringCounter Value  a1  aa2  aaa3  aaab 2  aaabb 1  aaabbb 0  So:  every time we see an a, we add 1 to the counter  every time we see a b, we subtract 1 from the counter  This way we can be sure the numbers of as and bs match

18 From a + b + to a n b n Idea: Idea:  Add a parameter to the non-terminals to count the number of as and bs  s --> [a], b(1).1 means we have seen one a  b(N) --> [a], b(M).where M is N+1  b(N) --> [b], c(M).where M is N-1  b(N) --> [b].N must be 1  c(N) --> [b], c(M).where M is N-1  c(N) --> [b].N must be 1

19 From a + b + to a n b n DCG rules: DCG rules:  s --> [a], b.s --> [a], b(1).  b --> [a], b.b(N) --> [a], {M is N+1}, b(M).  b --> [b], c.b(N) --> [b], {M is N-1}, c(M).  b --> [b].b(1) --> [b].  c --> [b], c.c(N) --> [b], {M is N-1}, c(M).  c --> [b].c(1) --> [b].

20 Context-Free Grammars Upgrading from regular to context-free grammars: Upgrading from regular to context-free grammars:  By removing all restrictions from the right side of the rule  X ->   { V N u V T } *  How powerful are context-free grammars?

21 Context-Free Grammars The Expressive Power of Formal Grammars The Expressive Power of Formal Grammars Chomsky Hierarchy: Chomsky Hierarchy:  Type-0 General rewrite rules  Type-1 Context-sensitive rules  a n b n c n Type-2 Context-free rulesType-2 Context-free rules a n b n a n b n –Type-3 Regular grammar rules – a + b +

22 DCG and Chomsky Hierarchy Regular Grammars = Type-3 FSA Regular Expressions DCG = Type-0 Type-2 Type-1

23 DCG is more powerful than Context-Free Next time… Next time…  We can write a DCG for the context- sensitive language L abc = { a n b n c n | n >= 0 }  But: No context-free grammar can encode L abcNo context-free grammar can encode L abc


Download ppt "LING 388 Language and Computers Lecture 9 9/30/03 Sandiway FONG."

Similar presentations


Ads by Google