View
234
Download
0
Category
Preview:
Citation preview
Context Free Grammar
A → 0A1A → BB → #
A 0A1 00A11 000A111
000B111 000#111
derivation = a sequence of productions that results a string
productions
variablesterminals
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}
L1 = {wywR : y, w ∈ *}
L2 = {ai bj : i < j}
Context Free Grammar
• L1 = {wywR : y, w ∈ *}• Consider a simpler CFG L1’ = {wwR : w ∈ *}
• e.g. z = aabaabaa• First character and the last character are the same
z = aabaabaa = ax1a
x1 = abaaba = ax2a
x2 = baab = bx3b
x3 = aa = ax4a
S → aSa | bSb ? How to stop? base case?S → aSa | bSb |
recursive
Context Free Grammar
• L1 = {wywR : y, w ∈ *}• Consider a simpler CFG L1’ = {wwR : w ∈ *}
S → aSa | bSb | How to turn it into a CFG for L1?
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 | TT → aT | bT |
Context Free Grammar
• L2 = {ai bj : i < j}• Consider a simpler CFG L2’ = {aibj : i = j}
• e.g. z = aaaabbbb• First character must be a and the last character must be b
z = aaaabbbb = ax1b
x1 = aaabbb = ax2b
x2 = aabb = ax3b
x3 = ab = ax4b
S → aSb ? Base case again…S → aSb |
recursive
Context Free Grammar
• L2 = {ai bj : i < j}• Consider a simpler CFG L2’ = {wwR : w ∈ *}
S → aSb | How to turn it into a CFG for L2? 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 {bi : i > 0}? T → bT | bReplace by T → bT | b
S → aSb | | TT → bT | b
Parse Trees
• Representation of derivationse.g. S → AB | ba
A → aA | aB → 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) | NA → A + A | NN → 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) | NA → A + A | NN → 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 /
E E
2
N
+A A
1
N
2
N
A( )
Parsing - Preprocessing
1. Eliminate productions2. 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 QIf 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’ → SS → XYX → Y →
*
Repeat the following:If X , push X into QIf X YZ…W and XZ…W are all in Q, push X into Q
Parsing - Preprocessing
1. Eliminate productionsii. Remove nullable variablesRepeat the following until Q is empty:
Let N be the first element in Qa) 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
S → ASA | aBA → B | SB → b |
•B → , Q = (B)•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 QIf X YZ…W and XZ…W are all in Q, push X into Q
*
Parsing - Preprocessing
ii. Remove nullable variablesRepeat the following until Q is empty:
Let N be the first element in Qa) For each X → N, add X → b) Remove all N → Remove N from Q.
Parsing - Preprocessing
ii. Remove nullable variablesRepeat the following until Q is empty:
Let N be the first element in Qa) For each X → N, add X → b) Remove all N → Remove N from Q.
Parsing - Preprocessing
i. If there is a cycle of unit productionsA → B → ... → C → A
delete it and replace everything with A
CYK Algorithm
1. Eliminate productions2. Eliminate unit productions
• CYK Algorithm1. More preprocessing – Chomsky normal form2. Dynamic Programming
CYK Algorithm - Preprocessing
1. Chomsky normal formevery production has the form
A → BC or A → aAllow S → for start variable
A → BCDEC → c
A → BXX → CDEC → c
A → BXX → CYY → DEC → c
A → BcDE
Parsing
• CYK algorithmDynamic Programming (taught in CSCI3160)Let s = s1s2s3s4…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 algorithmDynamic 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 CFGS → ASA | aBA → B | SB → b |
Cocke-Younger-Kasami algorithm
ba a bb
ABSAU SAU ABAB-- ---- ---
-
S,A,U can derive aA,B can derive b
Cocke-Younger-Kasami algorithm
ba a bb
ABSAU SAU ABAB-SAX SAXSA-- ---
-
If we can derive“a”(SAU) and “b”(AB), thenwe can derive “ab”.
SAU derives “a”, AB derives “b”.Look for variables that produceSA, SB, AA, AB, UA, or UBS → SA, A → SA, X → SA
similarly for “bb”, “ba”, “ab”
Cocke-Younger-Kasami algorithm
ba a bb
ABSAU SAU ABAB-SAX SAXSA
SASAX 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 produceSA, SB, AA, AB, XA, or XBS → SA, A → SA, X → SA
similarly for “bba” and “bab”
Cocke-Younger-Kasami algorithm
ba a bb
ABSAU SAU ABAB-SAX SAXSA
SASAX SAXSAXSAX
-
If we can derive“a”(SAU) and “bba”(SA), or“ab”(SAX) and “ba”(SA), or“abb”(SAX) and “a”(SAU), thenwe can derive “abba”.
SAX derives “ab”, SA derives “ba”.Look for variables that produceSS, SA, AA, AS, XS, or XAS → SA, A → SA, X → SA
similarly for “bbab”
Cocke-Younger-Kasami algorithm
ba a bb
ABSAU SAU ABAB-SAX SAXSA
SASAX SAXSAXSAX
SAX
If we can derive“a”(SAU) and “bbab”(SAX), or“ab”(SAX) and “bab”(SAX), or“abb”(SAX) and “ab”(SAX), thenwe can derive “abbab”.
SAU derives “a”, AB derives “bbab”.Look for variables that produceSA, SB, AA, AB, UA, or UBS → SA, A → SA, X → SA
If S is on the top left cell, thenx can be derived.
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 l1, l2
such thats(i, k) = s(i, l1) + s(l1+1, k)
s(i, k) = s(k+1, l2) + s(l2+1, j)
Do the rest recursively.
Parse tree reconstruction
ba a bb
ABSAU SAU ABAB-SAX SAXSA
SASAX SAXSAXSAX
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 l1, l2
such thats(i, k) = s(i, l1) + s(l1+1, k)
s(i, k) = s(k+1, l2) + s(l2+1, j)
Do the rest recursively.
Parse tree reconstruction
ba a bb
ABSAU SAU ABAB-SAX SAXSA
SASAX SAXSAXSAX
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 l1, l2
such thats(i, k) = s(i, l1) + s(l1+1, k)
s(i, k) = s(k+1, l2) + s(l2+1, j)
Do the rest recursively.
Parse tree reconstruction
ba a bb
ABSAU SAU ABAB-SAX SAXSA
SASAX SAXSAXSAX
SAX
Recommended