0% found this document useful (0 votes)
31 views13 pages

MIT18 404f20 Lec4

Uploaded by

Zuci Dark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views13 pages

MIT18 404f20 Lec4

Uploaded by

Zuci Dark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

18.404/6.

840 Lecture 4

Last time:
- Finite automata → regular expressions
- Proving languages aren’t regular
- Context free grammars

Today: (Sipser §2.2)


- Context free grammars (CFGs) – definition
- Context free languages (CFLs)
- Pushdown automata (PDA)
- Converting CFGs to PDAs

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

F→(E)|a T T×F T+T×F


F F a F+F×a
!= {E, T, F}
Σ= {+, ×, (, ), a} a a a a+a×a ∈ * '(
$= the 6 rules above Check-in
Generates a+a×a , (a+a)×a, 4.2 etc.
a, a+a+a,
%= E
How many reasonable distinct meanings
does the following English sentence have?
Observe that the parse tree contains additional information,
such as the precedence of × over + . The boy saw the girl with the mirror.
(a) 1
If a string has two different parse trees then it is derived ambiguously
and we say that the grammar is ambiguous. (b) 2
(c) 3 or more
4 Check-in 4.2
Ambiguity

!" !#
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 ambiguous. E E
a + a × a

E E
E E
E
5
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

Operates like an NFA except can write-add or read-remove symbols


from the top of stack.
push pop

Example: PDA for ! = 0$ 1$ & ≥ 0


1) Read 0s from input, push onto stack until read 1.
2) Read 1s from input, while popping 0s from stack.
3) Enter accept state if stack is empty. (note: acceptance only at end of input)

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.

Example: PDA for 9 = {;; ℛ | ; ∈ 0,1 ∗ } Sample input: 0 1 1 1 1 0


1) Read and push input symbols.
Nondeterministically either repeat or go to (2).
The nondeterministic forks replicate the stack.
2) Read input symbols and pop stack symbols, compare.
If ever ≠ then thread rejects. This language requires nondeterminism.
3) Enter accept state if stack is empty. (do in “software”) Our PDA model is nondeterministic.

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

1. Defined Context Free Grammars (CFGs)


and Context Free Languages (CFLs)
2. Defined Pushdown Automata(PDAs)
3. Gave conversion of CFGs to PDAs.

12
MIT OpenCourseWare
https://ocw.mit.edu

18.404J / 18.4041J / 6.840J Theory of Computation


Fall 2020

For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy