CSCI 3130 Formal languages and automata theory Tutorial
- Slides: 46
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 start variable A → 0 A 1 A→B B→# variables terminals productions A 0 A 1 00 A 11 000 A 111 000 B 111 000#111 derivation = a sequence of productions that results a string
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. • S = {a, b} L 1 = {wyw. R : y, w ∈ S*} L 2 = {ai bj : i < j}
Context Free Grammar • L 1 = {wyw. R : y, w ∈ S*} • Consider a simpler CFG L 1’ = {ww. R : w ∈ S*} • 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 recursive S → a. Sa | b. Sb ? How to stop? base case? S → a. Sa | b. Sb |
Context Free Grammar • L 1 = {wyw. R : y, w ∈ S*} • Consider a simpler CFG L 1’ = {ww. R : w ∈ S*} S → a. Sa | b. Sb | How to turn it into a CFG for L 1? e. g. aababaa? S → a. Sa | b. Sb | < change this How to write a CFG for {y : y ∈ S*}? T → a. T | b. T | Replace by T → a. T | b. T | S → a. Sa | b. Sb | T T → a. T | b. T |
Context Free Grammar • L 2 = {ai bj : i < j} • Consider a simpler CFG L 2’ = {aibj : 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 → a. Sb ? Base case again… S → a. Sb | recursive
Context Free Grammar • L 2 = {ai bj : i < j} • Consider a simpler CFG L 2’ = {ww. R : w ∈ S*} S → a. Sb | How to turn it into a CFG for L 2? need at least 1 more b in bb…b e. g. aaaabbbbbb? S → a. Sb | < change this. insert b’s in front of bb…b How to write a CFG for {bi : i > 0}? T → b. T | b Replace by T → b. T | b S → a. Sb | | T T → b. T | b
Parse Trees • Representation of derivations e. g. S → AB | ba A → a. A | a B→b Derivation of aab • S → AB → aa. B → aab S A a B A a b
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 / E | (A) | N E * E A→A+A|N N→ 6|1|2 E / E ( A ) • 6 / 2 * (1 + 2) N N A + A 6 2 N N 1 2 E E / E N E * E 6 N ( A ) 2 A + A N N 1 2
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. Repeat the following: S → XY If X , push X into Q X→ If X YZ…W and XZ…W are all in Q, Y→ push X into Q Q = (X, Y, S) but not (S, X, Y) S’ → S S → XY X→ Y→
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 | a. B A→B|S B→b|
Parsing - Preprocessing S → ASA | a. B A→B|S B→b| • • * ) 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 B → , Q = (B) A → B and B is in Q, Q = (B, A)
Parsing - Preprocessing S → ASA | a. B 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. Q = (B, A) N=B S → ASA | a. B | a A→B|S| B→b|
Parsing - Preprocessing S → ASA | a. B 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. Q = (A) N=A S → ASA | a. B | a | SA | AS | S A→B|S| B→b
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 TS→U|X|Y US→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 | a. B | a | SA | AS | S i. If there is a cycle of unit productions A→B|S A → B →. . . → C → A B→b delete it and replace everything with A S → ASA | a. B | a | SA | AS | S A→B|S B→b
Parsing - Preprocessing S → ASA | a. B | a | SA | AS ii. Replace every chain A→B|S A → B →. . . → C → B→b by A → , B → , . . . , C → S → ASA | a. B | a | SA | AS A → B b | S ASA | a. B | a | SA | AS B→b
Parsing - Preprocessing • Eliminate productions and unit productions for the following CFG S → ASA | a. B A→B|S B→b| Ans: S → ASA | a. B | a | SA | AS A → b | ASA | a. B | 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 or A → a Allow S → for start variable A → Bc. DE A → BCDE C→c A → BX X → CY Y → DE C→c
Parsing - Preprocessing • Convert the CFG to Chomsky normal form S → ASA | a. B | a | SA | AS A → b | ASA | a. B | a | SA | AS B→b
Parsing - Preprocessing S → ASA | a. B | a | SA | AS A → b | ASA | a. B | a | SA | AS B→b S → AX | UB | a | SA | AS X → SA U→a A → b | ASA | a. B | a | SA | AS B→b
Parsing - Preprocessing S → AX | UB | a | SA | AS X → SA U→a A → b | ASA | a. B | 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 | a. B | a | SA | AS A → b | ASA | a. B | 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 CSCI 3160) Let s = s 1 s 2 s 3 s 4…sn be a string Let s(i, j) be the substring si…sj 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 CSCI 3160) 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 | a. B A→B|S B→b|
Cocke-Younger-Kasami algorithm Chomsky Normal Form: S → AX | UB | SA | AS | a A → AX | UB | SA | AS | a | b X → SA U→a B→b x = abbab - - - a b b a b
Cocke-Younger-Kasami algorithm S → AX | UB | SA | AS | a A → AX | UB | SA | AS | a | b X → SA U→a B→b S, A, U can derive a A, B can derive b SAU AB AB SAU AB a b b a b
Cocke-Younger-Kasami algorithm S → AX | UB | SA | AS | a A → AX | UB | SA | AS | a | b X → SA U→a B→b 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” SAX SAU AB a b b a b
Cocke-Younger-Kasami algorithm S → AX | UB | SA | AS | a A → AX | UB | SA | AS | a | b X → SA U→a B→b 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” SAX SAU SA AB SAX SAU AB a b b a b
Cocke-Younger-Kasami algorithm S → AX | UB | SA | AS | a A → AX | UB | SA | AS | a | b X → SA U→a B→b 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, AS, XS, or XA S → SA, A → SA, X → SA similarly for “bbab” SAX SAX SAU SAX SA AB SAX SAU AB a b b a b
Cocke-Younger-Kasami algorithm S → AX | UB | SA | AS | a A → AX | UB | SA | AS | a | b X → SA U→a B→b 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”. SAX SAU derives “a”, AB derives “bbab”. SAX Look for variables that produce SAX SA, SB, AA, AB, UA, or UB S → SA, A → SA, X → SA SAX SAU If S is on the top left cell, then x can be derived. a SAX SA AB SAX SAU AB b b a b
Parse tree reconstruction 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. SAX SAX SAU SAX SA AB SAX SAU AB a b b a b
Parse tree reconstruction 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. SAX SAX SAU SAX SA AB SAX SAU AB a b b a b
Parse tree reconstruction 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. SAX SAX SAU SAX SA AB SAX SAU AB a b b a b
End • Questions?
- Formal languages and automata theory tutorial
- Formal languages and automata theory tutorial
- Formal languages and automata theory tutorial
- Csci 3130
- Csci 3130
- Csci 3130
- Csci 3130
- Csci 3130
- An introduction to formal languages and automata
- Automata theory tutorial
- Heather lipford
- Ece 3130
- Ece 3130
- Ece 3130
- Cisc 3130
- Difference between formal and informal language
- Characteristics of formal language
- Automata
- Study and central concepts of automata theory
- Formal relational query languages
- Find the id name dept_name
- Grouping relational algebra
- Generalized transition graph
- Bidirectional transducers in automata theory
- Automata theory
- Automata theory
- Automata theory
- Length of a string in automata theory
- Aaaaaabb
- Why we study automata theory
- Reverse of a string in automata theory
- Theory of machines
- Non formal education examples
- Unit 3 formal informal and nonformal education
- Difference between formal education and als
- Csci 4211
- Csci 530
- Csci 530 security systems
- Csci 530 security systems
- Csci 430 usc
- Contoh knapsack problem
- Csci 420
- Csci 2720
- Csci 1951a
- Csci 2670
- Csci 2670
- Csci 1730 uga