CSCI 3130: Formal languages and automata theory Tutorial 4 Chin.

Slides:



Advertisements
Similar presentations
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Advertisements

Closure Properties of CFL's
Context Free Grammars.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory VII.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki.
Theory Of Automata By Dr. MM Alam
Transforming Context-Free Grammars to Chomsky Normal Form 1 Roger L. Costello April 12, 2014.
CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.
CS5371 Theory of Computation
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 11, 2006.
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
1 CSC 3130: Automata theory and formal languages Tutorial 4 KN Hung Office: SHB 1026 Department of Computer Science & Engineering.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
Tutorial CSC3130 : Formal Languages and Automata Theory Tu Shikui ( ) SHB 905, Office hour: Thursday 2:30pm-3:30pm
1 Module 32 Chomsky Normal Form (CNF) –4 step process.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
Context-Free Grammars
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
CONVERTING TO CHOMSKY NORMAL FORM
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
CSCI 2670 Introduction to Theory of Computing September 20, 2005.
Normal Forms for Context-Free Grammars Definition: A symbol X in V  T is useless in a CFG G=(V, T, P, S) if there does not exist a derivation of the form.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Ambiguity.
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
Chapter 5 Context-Free Grammars
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
Context Free Grammars.
Lecture 11 Theory of AUTOMATA
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
Lecture # 26 Theory Of Automata By Dr. MM Alam 1.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1 Chapter 6 Simplification of CFGs and Normal Forms.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
CSCI 2670 Introduction to Theory of Computing September 14, 2005.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Normal forms.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
Exercises on Chomsky Normal Form and CYK parsing
Chomsky Normal Form.
Theory of Languages and Automata By: Mojtaba Khezrian.
Theory of Computation Automata Theory Dr. Ayman Srour.
Recap lecture 31 Context Free Grammar, Terminals, non- terminals, productions, CFG, context Free language, examples.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
Context Free Grammars Chapter 12.
CONTEXT-FREE LANGUAGES
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
Context-Free Grammars: an overview
Formal Language & Automata Theory
Ambiguity Parsing algorithms
Complexity and Computability Theory I
7. Properties of Context-Free Languages
Recap Lecture 34 Example of Ambiguous Grammar, Example of Unambiguous Grammer (PALINDROME), Total Language tree with examples (Finite and infinite trees),
Theory Of Automata By Dr. MM Alam
NORMAL FORMS FDP ON THEORY OF COMPUTING
7. Properties of Context-Free Languages
CHAPTER 2 Context-Free Languages
Normal forms and parsing
Presentation transcript:

CSCI 3130: Formal languages and automata theory Tutorial 4 Chin

Reminder Homework 3 is due on next Thursday. You can get back homework 1 after this tutorial.

Context Free Grammar A → 0A1 A → B B → # A  0A1  00A11  000A111  000B111  000#111 derivation = a sequence of productions that results a string productions variables terminals start variable

Context Free Grammar Regular languages are context free Context free languages NOT necessarily regular CFG describes the recursive structure of languages

Context Free Grammar Design a CFG that represents the following.  = {a, b} L 1 = {wyw R : y, w ∈  *} L 2 = {a i b j : i < j}

Context Free Grammar L 1 = {wyw R : y, w ∈  *} Consider a simpler CFG L 1 ’ = {ww R : w ∈  *} e.g. z = aabaabaa First character and the last character are the same z = aabaabaa = ax 1 a x 1 = abaaba = ax 2 a x 2 = baab = bx 3 b x 3 = aa = ax 4 a S → aSa | bSb ? How to stop? base case? S → aSa | bSb |  recursive

Context Free Grammar L 1 = {wyw R : y, w ∈  *} Consider a simpler CFG L 1 ’ = {ww R : w ∈  *} S → aSa | bSb |  How to turn it into a CFG for L 1 ? e.g. aabaaababaa? S → aSa | bSb |  < change this How to write a CFG for {y : y ∈  *}? T → aT | bT |  Replace  by T → aT | bT |  S → aSa | bSb | T T → aT | bT | 

Context Free Grammar L 2 = {a i b j : i < j} Consider a simpler CFG L 2 ’ = {a i b j : i = j} e.g. z = aaaabbbb First character must be a and the last character must be b z = aaaabbbb = ax 1 b x 1 = aaabbb = ax 2 b x 2 = aabb = ax 3 b x 3 = ab = ax 4 b S → aSb ? Base case again… S → aSb |  recursive

Context Free Grammar L 2 = {a i b j : i < j} Consider a simpler CFG L 2 ’ = {ww R : w ∈  *} S → aSb |  How to turn it into a CFG for L 2 ? need at least 1 more b in bb…b e.g. aaaabbbbbb? S → aSb |  < change this. insert b’s in front of bb…b How to write a CFG for {b i : i > 0}? T → bT | b Replace  by T → bT | b S → aSb |  | T T → bT | b

Parse Trees Representation of derivations e.g.S → AB | ba A → aA | a B → b Derivation of aab S → AB → aAB → aaB → aab S B b a Aa A

Ambiguity A CFG is ambiguous if the deviation of some string has two different parse trees. Removing ambiguity is impossible for some CFG.

Ambiguity Show that the following CFG is ambiguous. E → E * E | E / E | (A) | N A → A + A | N N → 6 | 1 | 2 Find a string in the CFG and show that it has two different parse trees.

Ambiguity E → E * E | E / E | (A) | N A → A + A | N N → 6 | 1 | 2 6 / 2 * (1 + 2) * E + A A 1 N 2 N E / E E 6 N 2 N A E ( ) E E * 6 N E / EE 2 N + A A 1 N 2 N A ( )

Parsing - Preprocessing 1.Eliminate  productions 2.Eliminate unit productions Do the steps in order (1 then 2)

Parsing - Preprocessing 1.Eliminate  productions i. Identify nullable variables (N   ) Let Q be the queue containing the nullable variables. Repeat the following: If X  , push X into Q If X  YZ…W and XZ…W are all in Q, push X into Q If start variable S nullable, add S’ → S |  *

Parsing - Preprocessing 1.Eliminate  productions i. Identify nullable variables (N   ) e.g.S → XY X →  Y →  Q = (X, Y, S) but not (S, X, Y) S’ → S S → XY X →  Y →  * Repeat the following: If X  , push X into Q If X  YZ…W and XZ…W are all in Q, push X into Q

Parsing - Preprocessing 1.Eliminate  productions ii. Remove nullable variables Repeat the following until Q is empty: Let N be the first element in Q a) For each X →  N , add X →  b) Remove all N →  Remove N from Q Caution!: S → NTN becomes S → NTN | NT | TN | T

Parsing - Preprocessing 1.Eliminate  productions ii. Remove nullable variables e.g.Q = (Y, X) S → XY X → XY | YZY | Y Y →  After one step, Q = (X) S → XY | X X → XY | X | YZY | YZ | ZY | Z | Y |  Y → 

Parsing - Preprocessing Eliminate  productions for the following CFG S → ASA | aB A → B | S B → b | 

Parsing - Preprocessing S → ASA | aB A → B | S B → b |  1.B → , Q = (B) 2.A → B and B is in Q, Q = (B, A) i. Identify nullable variables (N   ) Let Q be the queue containing the nullable variables. Repeat the following: If X  , push X into Q If X  YZ…W and XZ…W are all in Q, push X into Q *

Parsing - Preprocessing S → ASA | aB A → B | S B → b |  Q = (B, A) N = B S → ASA | aB | a A → B | S |  B → b |  ii. Remove nullable variables Repeat the following until Q is empty: Let N be the first element in Q a) For each X →  N , add X →  b) Remove all N →  Remove N from Q.

Parsing - Preprocessing S → ASA | aB A → B | S B → b |  Q = (A) N = A S → ASA | aB | a | SA | AS | S A → B | S |  B → b ii. Remove nullable variables Repeat the following until Q is empty: Let N be the first element in Q a) For each X →  N , add X →  b) Remove all N →  Remove N from Q.

Parsing - Preprocessing 2.Eliminate unit productions i. If there is a cycle of unit productions A → B →... → C → A delete it and replace everything with A e.g.S → T | X | Y T S → U | X | Y U S → S | Y | a

Parsing - Preprocessing 2.Eliminate unit productions ii. Replace every chain A → B →... → C →  by A → , B → ,..., C →  e.g.S → T AX | a | XY | YZ T → U AX | a | BY U → AX | a

Parsing - Preprocessing S → ASA | aB | a | SA | AS | S A → B | S B → b S → ASA | aB | a | SA | AS | S A → B | S B → b i. If there is a cycle of unit productions A → B →... → C → A delete it and replace everything with A

Parsing - Preprocessing S → ASA | aB | a | SA | AS A → B | S B → b S → ASA | aB | a | SA | AS A → B b | S ASA | aB | a | SA | AS B → b ii. Replace every chain A → B →... → C →  by A → , B → ,..., C → 

Parsing - Preprocessing Eliminate  productions and unit productions for the following CFG S → ASA | aB A → B | S B → b |  Ans:S → ASA | aB | a | SA | AS A → b | ASA | aB | a | SA | AS B → b

CYK Algorithm 1.Eliminate  productions 2.Eliminate unit productions CYK Algorithm 1. More preprocessing – Chomsky normal form 2. Dynamic Programming

CYK Algorithm - Preprocessing 1.Chomsky normal form every production has the form A → BC orA → a Allow S →  for start variable A → BCDE C → c A → BX X → CDE C → c A → BX X → CY Y → DE C → c A → BcDE

Parsing - Preprocessing Convert the CFG to Chomsky normal form S → ASA | aB | a | SA | AS A → b | ASA | aB | a | SA | AS B → b

Parsing - Preprocessing S → ASA | aB | a | SA | AS A → b | ASA | aB | a | SA | AS B → b S → AX | UB | a | SA | AS X → SA U → a A → b | ASA | aB | a | SA | AS B → b

Parsing - Preprocessing S → AX | UB | a | SA | AS X → SA U → a A → b | ASA | aB | a | SA | AS B → b S → AX | UB | a | SA | AS A → b | AX | UB | a | SA | AS X → SA U → a B → b

Parsing - Preprocessing Convert the CFG to Chomsky normal form S → ASA | aB | a | SA | AS A → b | ASA | aB | a | SA | AS B → b Ans:S → AX | UB | a | SA | AS A → b | AX | UB | a | SA | AS X → SA U → a B → b

Parsing CYK algorithm Dynamic Programming (taught in CSCI3160) Let s = s 1 s 2 s 3 s 4 …s n be a string Let s(i, j) be the substring s i …s j of s e.g.s = abcde, s(2,4) = bcd s(i, j) can be construct by s(i, k) + s(k + 1, j) for some k. e.g.s = abcde s = “a” + “bcde”, “ab” + “cde”, …, “abcd” + “e”

Parsing CYK algorithm Dynamic Programming (taught in CSCI3160) s(i, j) can be construct by s(i, k) + s(k + 1, j) for some k. Main Idea: If A derives s(i, k) and B derives s(k + 1, j), and S → AB. Then S derives s(i, j) e.g. A derives “ab”, B derives “cde”, S → AB. Then S derives “abcde”

Cocke-Younger-Kasami algorithm Use the CYK algorithm to parse abbab for the CFG S → ASA | aB A → B | S B → b | 

Cocke-Younger-Kasami algorithm baabb Chomsky Normal Form: S → AX | UB | SA | AS | a A → AX | UB | SA | AS | a | b X → SA U → a B → b x = abbab

Cocke-Younger-Kasami algorithm baabb ABSAU AB S,A,U can derive a A,B can derive b S → AX | UB | SA | AS | a A → AX | UB | SA | AS | a | b X → SA U → a B → b

Cocke-Younger-Kasami algorithm baabb ABSAU AB -SAX SA If we can derive “a”(SAU) and “b”(AB), then we can derive “ab”. SAU derives “a”, AB derives “b”. Look for variables that produce SA, SB, AA, AB, UA, or UB S → SA, A → SA, X → SA similarly for “bb”, “ba”, “ab” S → AX | UB | SA | AS | a A → AX | UB | SA | AS | a | b X → SA U → a B → b

Cocke-Younger-Kasami algorithm baabb ABSAU AB -SAX SA SAX -- - If we can derive “ab”(SAX) and “b”(AB), or “a”(SAU) and “bb”(-), then we can derive “abb”. 1. SAU derives “a”, nothing derives “b” 2. SAX derives “ab”, AB derives “b”. Look for variables that produce SA, SB, AA, AB, XA, or XB S → SA, A → SA, X → SA similarly for “bba” and “bab” S → AX | UB | SA | AS | a A → AX | UB | SA | AS | a | b X → SA U → a B → b

Cocke-Younger-Kasami algorithm baabb ABSAU AB -SAX SA SAX - If we can derive “a”(SAU) and “bba”(SA), or “ab”(SAX) and “ba”(SA), or “abb”(SAX) and “a”(SAU), then we can derive “abba”. SAX derives “ab”, SA derives “ba”. Look for variables that produce SS, SA, AA, AS, XS, or XA S → SA, A → SA, X → SA similarly for “bbab” S → AX | UB | SA | AS | a A → AX | UB | SA | AS | a | b X → SA U → a B → b

Cocke-Younger-Kasami algorithm baabb ABSAU AB -SAX SA SAX If we can derive “a”(SAU) and “bbab”(SAX), or “ab”(SAX) and “bab”(SAX), or “abb”(SAX) and “ab”(SAX), then we can derive “abbab”. SAU derives “a”, AB derives “bbab”. Look for variables that produce SA, SB, AA, AB, UA, or UB S → SA, A → SA, X → SA If S is on the top left cell, then x can be derived. S → AX | UB | SA | AS | a A → AX | UB | SA | AS | a | b X → SA U → a B → b

Starting from S in the top left, there must be some k such that s(i, j) = s(i, k) + s(k+1, j) Then there must be some l 1, l 2 such that s(i, k) = s(i, l 1 ) + s(l 1 +1, k) s(i, k) = s(k+1, l 2 ) + s(l 2 +1, j) Do the rest recursively. Parse tree reconstruction baabb ABSAU AB -SAX SA SAX

Starting from S in the top left, there must be some k such that s(i, j) = s(i, k) + s(k+1, j) Then there must be some l 1, l 2 such that s(i, k) = s(i, l 1 ) + s(l 1 +1, k) s(i, k) = s(k+1, l 2 ) + s(l 2 +1, j) Do the rest recursively. Parse tree reconstruction baabb ABSAU AB -SAX SA SAX

Starting from S in the top left, there must be some k such that s(i, j) = s(i, k) + s(k+1, j) Then there must be some l 1, l 2 such that s(i, k) = s(i, l 1 ) + s(l 1 +1, k) s(i, k) = s(k+1, l 2 ) + s(l 2 +1, j) Do the rest recursively. Parse tree reconstruction baabb ABSAU AB -SAX SA SAX

End Questions?