Lecture 4
Lecture 4
Adnane Saoud
1
Summary of lecture 3
Lecture 3
§ From finite automata to regular expressions
§ Proving some languages are not regular
§ Context free grammars
Lecture4
§ Context free languages (CFLs)
§ Pushdown automata (PDA)
§ Converting CFGs to PDAs
2
Context-free
Languages
3
Context Free Grammars (CFGs)
𝐺! Shorthand:
S → 0S1
S→R S → 0S1 | R
R→ε R→ε
Recall that a CFG has terminals, variables, and rules.
0
𝑀1 1
0,1
1
𝑞1 𝑞2 𝑞3
0
5
CFG – Formal Definition
For 𝑢, 𝑣 ∈ 𝑉 ∪ Σ ∗ write
1) 𝑢 ⇒ 𝑣 if we can go from 𝑢 to 𝑣 with one substitution step in 𝐺 (𝑢 𝑦𝑖𝑒𝑙𝑑𝑠 𝑣)
∗
2) 𝑢 ⇒ 𝑣 if can go from 𝑢 to 𝑣 with some number of substitution steps in 𝐺
𝑢 ⇒ 𝑢" ⇒ 𝑢# ⇒ ⋯ ⇒ 𝑢$ = 𝑣 is called a derivation of 𝑣 from 𝑢.
(𝑢 𝑑𝑒𝑟𝑖𝑣𝑒𝑠 𝑣)
∗
𝐿 𝐺 = 𝑤 𝑤 ∈ Σ ∗ and 𝑆 ⇒ 𝑤}
Definition: 𝐴 is a Context Free Language (CFL) if 𝐴 = 𝐿(𝐺) for some CFG 𝐺.
6
CFG - Example
a) 𝐶" only
b) 𝐶# only
c) Both 𝐶" and 𝐶#
d) Neither
7
CFG – Example
𝐺#
E → E+T | T Resulting
Parse E E
T → T×F | F tree string
E+T E+T
F→(E)|a
T T×F T+T×F
𝑉= {E, T, F} F F a F+F×a
Σ= {+, ×, (, ), a}
𝑅= the 6 rules above a a a a+a×a ∈ 𝐿 𝐺#
𝑆= E
Generates a+a×a, (a+a)×a, a, a+a+a, etc.
If a string has two different parse trees then it is derived ambiguously
and we say that the grammar is ambiguous.
This means that we can generate the same string in different ways (using
different parse trees)
Ambiguity
A case with ambiguous grammar
𝐺# 𝐺$
E → E+T | T E → E+E | E×E | ( E ) | a
T → T×F | F
F→(E)|a
E
Both 𝐺# and 𝐺$ recognize the same language, i.e., E E
𝐿 𝐺# = 𝐿 𝐺$ .
However 𝐺# is an unambiguous CFG and 𝐺$ is E E
ambiguous. a + a × a
E E
E E
E
Chomsky normal form
Definition: A context-free grammar is in Chomsky normal form if every rule is of the form
A → BC
A→a
where a is any terminal and A, B, and C are any variables (except that B and C may not be
the start variable). In addition, we permit the rule S → ε, where S is the start variable.
Conversion algorithm:
• Add a new start variable S0 and the rule S0 → S, where S was the original start variable
(we guarantee that the start variable does not occur on the right-hand side of a rule).
• Remove an ε-rule A → ε, where A is not the start variable. Then for each occurrence of
an A on the right-hand side of a rule, add a new rule with that occurrence deleted (if R
→ uAv is a rule in which u and v are strings of variables and terminals, add rule R → uv)
• Remove a unit rule A → B. Then, whenever a rule B → u appears, we add the rule A →u.
• Replace each rule A → u1u2 · · · uk, where k ≥ 3 and each ui is a variable or terminal
symbol, with the rules A → u1A1, A1 → u2A2, A2 → u3A3, . . . , and Ak−2 → uk−1uk. The
Ai ’s are new variables. Replace any terminal ui in the preceding rule with the new
variable Ui and add the rule Ui → ui .
Pushdown
Automatas
12
Pushdown Automata (PDA)
“head”
a b a b a … a
Finite
control input appears on a “tape”
c Schematic diagram for DFA or NFA
(pushdown)
d
d
stack Schematic diagram for PDA
15
Converting CFGs to PDAs
IDEA: PDA begins with starting variable and guesses substitutions.
It keeps intermediate generated strings on stack. When done, compare with input.
a + a × a 𝐺# E → E+T | T
Input:
T → T×F | F
F→(E)|a
E E T T
+ + + E E
T T T
× E+T E + T
F
T+T×F T T× F
F+F×a F F a
Problem! Access below the top of stack is cheating! a a a
a+a×a
Instead, only substitute variables when on the top of stack.
If a terminal is on the top of stack, pop it and compare with input. Reject if ≠.
16
Converting CFGs to PDAs
Theorem: If 𝐴 is a CFL then some PDA 𝐺# E → E+T | T
recognizes 𝐴 T → T×F | F
Proof : Convert the CFG for 𝐴 to the following PDA. F→(E)|a
1) Push the start symbol on the stack. E
E
2) If the top of stack is E+T
E + T
Variable: replace with right hand side of rule T+T×F
(nondet choice). T T× F
Terminal: pop it and match with next input F+F×a
F F a
symbol. a+a×a
a a a
3) If the stack is empty, accept.
Example:
a + a × a
E E T F a + T T
+ + + + T ×
T T T T F
17
Equivalence of CFGs and PDAs
Recognizer Generator
Regular Regular
DFA or NFA
language expression
Context Free Context Free
PDA
language Grammar
Context Free
languages
Regular
languages
Summary
19
Summary
20