240 likes | 345 Views
LING 388 Language and Computers. Lecture 11 10/7 /03 Sandiway FONG. Administrivia. Computer Lab Double Class: Thursday 9th and Tuesday 14th Location: SBS 224 TA Office Hours Change in time and location Now Tuesdays after class 12:15 pm - 1:15 pm SBS 224. Review. Chomsky Hierarchy:
E N D
LING 388Language and Computers Lecture 11 10/7/03 Sandiway FONG
Administrivia • Computer Lab • Double Class: • Thursday 9th and Tuesday 14th • Location: • SBS 224 • TA Office Hours • Change in time and location • Now Tuesdays after class 12:15 pm - 1:15 pm • SBS 224
Review • Chomsky Hierarchy: • Type-0 General rewrite rules • Type-1 Context-sensitive rules • anbncn • Implementation: type-2 rules + counter • Type-2 Context-free rules • anbn • Implementation: type-2 rules or type-3 rules + counter • Type-3 Regular grammar rules • a+b+ • Implementation: type-3 rules
Review • Production rule formats: • Type-1: • Next slide… • Type-2: • A -> a • Type-3: • A -> Bc A -> c or • A -> cB A -> c • where … • A e VN, c e VT and a e (VN u VT)*
Context-sensitive Grammars • Type-2 and 3 grammars may only have a single non-terminal on the left. • Type-1 (context-sensitive) grammars extend what’s allowed on the left. • Production rules have the format: • a -> b such that |b| >= |a| • where … • a e (VN u VT)+ Note: a should not be comprised of just terminal symbols • b e (VN u VT)* • Notes: • Length constraint means a rule like A -> l is not permitted
Context-sensitive Grammars • (Almost equivalent) alternative definition: • Production rules have the format: • aAb -> agb • where … • A e VN • a, b, g e (VN u VT)* • Notes: • a and b are “copied” over from the left to the right side unchanged • a..b constitutes the (left and right) contexts for non-terminal A • i.e. A -> g in context a__b • However, • … will be more convenient (for our purposes) to use the first definition
Context-sensitive Grammars • Equivalence (informal) • Can transform a context-sensitive rule of form: • AB -> CD • where A,B,C,D are all different non-terminals into rules respecting the form: • aAb -> agb • Invent non-terminals A’ and B’ • Conversion: • AB -> A’B (left context a empty, right context b = B) • A’B -> A’B’ (left context a = A’, right context b empty) • A’B’ -> CB’ (left context a empty, right context b = B’) • CB’ -> CD (left context a = C, right context b empty) • Note: • All four rules respect aAb -> agb, yet clearly AB =>+ CD
Context-sensitive Grammars • Example: • Gabc is a type-1 grammar such that L(Gabc) = {anbncn | n >=1 } • Gabc has 4 production rules: • S -> aSBc • S -> abc • cB -> Bc • bB -> bb • Notes: • 1st two rules context-free • 3rd/4th rules context-sensitive • c (resp. b) left context for non-terminal B in cB -> Bc (bB -> bb) • Length restriction is respected
Context-sensitive Grammars • Compare Gabc to DCG (based on type-2 rules) shown in Lecture 10: • s --> [a],t(1),[c]. • t(N) --> [a],{M is N+1},t(M),[c]. • t(N) --> u(N). • u(N) --> {N > 1}, [b],{M is N-1},u(M). • u(1) --> [b].
Context-sensitive Grammars • How does Gabc work? • Basic Idea: • Gabc has two stages: • Build an equal number of as, bs and cs • Re-arrange them into the correct linear order • We have 4 production rules: • S -> aSBc Stage 1: Build • S -> abc • cB -> Bc Stage 2: Re-arrange • bB -> bb
Context-sensitive Grammars • Build Stage: • S -> aSBc • S -> abc • Sentential Forms: • S • aSBc Using 1st rule • aaSBcBc Using 1st rule • aaabcBcBc Using 2nd rule • Notes: • same number of as, bs (counting b and B together) and cs at each sentential form
Context-sensitive Grammars • Re-arrangement Stage: • cB -> Bc • bB -> bb • Example Sentential Form: • aaabcBcBc • Question: What can we do? • Answer: • Basic Idea 1: Re-arrange the order of Bs and cs • want to move the Bs to the left • want to move the cs to the right • Question: How do we know when to stop re-arranging? • Answer: • Basic Idea 2: Stop when a B comes into contact with a b
Context-sensitive Grammars • Re-arrangement Stage: • cB -> Bc re-arrange c and B • bB -> bbstopping condition • Example Sentential Form: • aaabcBcBc • Basic Idea 1: Re-arrange the order of Bs and cs • cB -> Bc • “If a c precedes a B, the order is wrong, let’s flip them” • Example of Derivation: • aaabcBcBc • aaabBccBc • aaabBcBcc • aaabBBccc
Context-sensitive Grammars • Re-arrangement Stage: • cB -> Bc re-arrange c and B • bB -> bbstopping condition • Example Sentential Form: • aaabBBccc • We still have non-terminals in the sentential string • so we’re not done yet • Apply stopping condition: • bB -> bb • Example Derivation: • aaabBBccc • aaabbBccc • aaabbbccc (a3b3c3) • Final sentential form contains no non-terminals, so we’re done!
Grammar Rule Implementations • Chomsky Hierarchy: • Type-0 General rewrite rules • Type-1 Context-sensitive rules • anbncn • Implementation: type-2 rules + counter or type-1 rules • Type-2 Context-free rules • anbn • Implementation: type-2 rules or type-3 rules + counter • Type-3 Regular grammar rules • a+b+ • Implementation: type-3 rules
Type-0 Grammars • General rewrite rule system • Rule format: • a -> b • where • a e (VN u VT)+ • b e (VN u VT)*
Equivalent Automata Implementations • Chomsky Hierarchy: • Type-0 General rewrite rules • Implementation: Turing Machine (TM) • Type-1 Context-sensitive rules • anbncn • Implementation: Linear Bounded Automata (LBA) • Type-2 Context-free rules • anbn • Implementation: Non-deterministic Push-Down Automata (NPDA) • Type-3 Regular grammar rules • a+b+ • Implementation: Finite State Automata (FSA)
Equivalent Automata Implementations • Machine Characteristics: • All the machine types have a finite state core • However, they differ with respect to working memory • The difference in expressive power can be traced to limitations on the working memory…
DCG and Context-sensitive Grammars • DCG formalism is based on type-2 (context-free) grammars • It is powerful enough to encode type-1 (context-sensitive) grammars and beyond… • Example: • in Lecture 10, we exhibited a DCG for anbncn • … based on type-2 rules plus a counter
FSA Regular Expressions Regular Grammars = Type-3 DCG and Chomsky Hierarchy Type-2 Type-1 DCG = Type-0
DCG and Context-sensitive Grammars • However, we cannot write DCG rules for context-sensitive rules directly • Example: • can’t write [c],b --> b, [c]. • for cB -> Bc • Note: • we can write Prolog code that takes a context-sensitive grammar and interprets it • Possible term programming project?
This concludes our tour of the grammar hierarchy for this course • But before we leave …
Extra Credit Homework Question • We now know • {anbncn | n >=1 } is a context-sensitive (not context-free) language • {anbn | n >=1 } is a context-free (not regular) language • How about? • Labcd = {anbncndn | n >=1 } • Write a grammar for Labcd • Notes: • Not a computer lab homework • Extra credit question is entirely optional • A chance to make up some ground if you lost points on Homeworks 1 or 2 • Hand in your solution at the same time as Homework 3