MIT18 404f20 Lec4
MIT18 404f20 Lec4
840 Lecture 4
Last time:
- Finite automata → regular expressions
- Proving languages aren’t regular
- Context free grammars
1
Context Free Grammars (CFGs)
#% Shorthand:
S → 0S 1
S→R S → 0S 1 | R
R→ε R→ε
Recall that a CFG has terminals, variables, and rules.
Example of #% generating a string
Grammars generate strings
1. Write down start variable Tree of S S Resulting
2. Replace any variable according to a rule substitutions string
Repeat until only terminals remain “parse tree” 0S1 0S1
3. Result is the generated string
0S1 00S11
4. !(#) is the language of all generated strings
5. We call !(#) a Context Free Language. R 00R11
ε 0011 ∈ ! #%
! #% = 0* 1* , ≥ 0}
2
CFG – Formal Definition
Defn: A Context Free Grammar (CFG) ! is a 4-tuple (#, Σ, &, ')
# finite set of variables
Σ finite set of terminal symbols
∗
& finite set of rules (rule form: # → # ∪ Σ )
' start variable
For ,, - ∈ # ∪ Σ ∗ write
Check-in 4.1
1) , ⇒ - if can go from , to - with one substitution step in !
∗ Which of these are valid CFGs?
2) , ⇒ - if can go from , to - with some number of substitution steps in !
90 : B → 0B1 | ε 91 : S → 0S | S1
, ⇒ ,0 ⇒ ,1 ⇒ ⋯ ⇒ ,3 = - is called a derivation of - from ,.
B1 → 1B R → RR
If , = ' then it is a derivation of -. 0B → 0B
5 ! = 6 6 ∈ Σ ∗ and ' ⇒ 6}
∗ a) 90 only
b) 91 only
Defn: 8 is a Context Free Language (CFL) if 8 = 5(!) for some CFG !.
c) Both 90 and 91
d) Neither
3 Check-in 4.1
CFG – Example
'( Parse E E Resulting
E → E+T | T tree string
T → T×F | F E+T E+T
!" !#
E → E+T | T E → E+E | E×E | ( E ) | a
T → T×F | F
F→(E)|a
E
E E
E E
E
5
Pushdown Automata (PDA)
“head”
a b a b a … a
Finite
control input appears on a “tape”
6
PDA – Formal Definition
Defn: A Pushdown Automaton (PDA) is a 6-tuple (", Σ, Γ, &, '0, ))
Σ input alphabet
Γ stack alphabet
&: Q×Σ. ×Γ. → 0("×Γ. ) Accept if some thread is in the accept state
& ', a, c = 45 , d , 47 , e at the end of the input string.
7
Converting CFGs to PDAs
Theorem: If ! is a CFL then some PDA recognizes !
Proof: Convert !’s CFG to a PDA
…
E → E+T | T PDA
T→…
F→… CFG
$% E → E+T | T
IDEA: PDA begins with starting variable and guesses substitutions.
T → T×F | F
It keeps intermediate generated strings on stack. When done, compare with input.
E E T T F→(E)|a
+ + + Input: a + a × a
T T T E E
×
E+ T E+T
F
T + T× F T T×F
Problem! Access below the top of stack is cheating!
F+F×a F F 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 ≠. a+a×a a a a
8
Converting CFGs to PDAs (contd)
#$ E → E+T | T
Theorem: If ! is a CFL then some PDA recognizes ! T → T×F | F
Proof construction: Convert the CFG for ! to the following PDA. F→(E)|a
1) Push the start symbol on the stack.
2) If the top of stack is
Variable: replace with right hand side of rule (nondet choice).
Terminal: pop it and match with next input symbol.
E E
3) If the stack is empty, accept.
E+T E+T
a + a × a T+T×F T T×F
Example:
E E F T a + T F+F×a F F a
T
+ + + + T × a+a×a a a a
T T T T F
9
Equivalence of CFGs and PDAs
Theorem: ! is a CFL iff* some PDA recognizes !
Done.
In book. You are responsible for knowing
it is true, but not for knowing the proof.
* “iff” = “if an only if” means the implication goes both ways.
So we need to prove both directions: forward (→) and reverse (←).
Check-in 4.3
Is every Regular Language also
a Context Free Language?
(a) Yes
(b) No
(c) Not sure Check-in 4.3
10
Recap
Recognizer Generator
Regular Regular
DFA or NFA
language expression
Context Free Context Free
PDA
language Grammar
Context Free
languages
Regular
languages
11
Quick review of today
12
MIT OpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.