SlideShare uma empresa Scribd logo
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)
Recall that a CFG has terminals, variables, and rules.
Grammars generate strings
1. Write down start variable
2. Replace any variable according to a rule
Repeat until only terminals remain
3. Result is the generated string
4. 𝐿(𝐺) is the language of all generated strings
5. We call 𝐿(𝐺) a Context Free Language.
Example of 𝐺1 generating a string
S → 0S1
S → R
R → ε
𝐺1
S
0 S 1
0 S 1
R
ε
S
0S1
00S11
00R11
0011
𝐿 𝐺1 = 0𝑘
1𝑘
𝑘 ≥ 0}
Tree of
substitutions
“parse tree”
Resulting
string
∈ 𝐿 𝐺1
S → 0S1 | R
R → ε
Shorthand:
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
1) 𝑢 ⇒ 𝑣 if can go from 𝑢 to 𝑣 with one substitution step in 𝐺
2) 𝑢 ⇒ 𝑣 if can go from 𝑢 to 𝑣 with some number of substitution steps in 𝐺
𝑢 ⇒ 𝑢1 ⇒ 𝑢2 ⇒ ⋯ ⇒ 𝑢𝑘 = 𝑣 is called a derivation of 𝑣 from 𝑢.
If 𝑢 = 𝑆 then it is a derivation of 𝑣.
𝐿 𝐺 = 𝑤 𝑤 ∈ Σ∗ and 𝑆 ⇒ 𝑤}
Defn: 𝐴 is a Context Free Language (CFL) if 𝐴 = 𝐿(𝐺) for some CFG 𝐺.
∗
∗
Check-in 4.1
Check-in 4.1
Which of these are valid CFGs?
a) 𝐶1 only
b) 𝐶2 only
c) Both 𝐶1 and 𝐶2
d) Neither
B → 0B1 | ε
B1 → 1B
0B → 0B
𝐶1: S → 0S | S1
R → RR
𝐶2:
3
CFG – Example
𝑉 = {E, T, F}
Σ = {+, ×, (, ), a}
𝑅 = the 6 rules above
𝑆 = E
Observe that the parse tree contains additional information,
such as the precedence of × over + .
If a string has two different parse trees then it is derived ambiguously
and we say that the grammar is ambiguous.
E → E+T | T
T → T×F | F
F → ( E ) | a
𝐺2 Parse
tree
∈ 𝐿 𝐺2
Generates a+a×a, (a+a)×a, a, a+a+a, etc.
E
E+T
T+T×F
F+F×a
a+a×a
E
E + T
T T × F
F F a
a a a
Resulting
string
Check-in 4.2
Check-in 4.2
How many reasonable distinct meanings
does the following English sentence have?
The boy saw the girl with the mirror.
(a) 1
(b) 2
(c) 3 or more
4
Both 𝐺2 and 𝐺3 recognize the same language, i.e., 𝐿 𝐺2 = 𝐿 𝐺3 .
However 𝐺2 is an unambiguous CFG and 𝐺3 is ambiguous.
Ambiguity
E → E+T | T
T → T×F | F
F → ( E ) | a
𝐺2
E → E+E | E×E | ( E ) | a
𝐺3
E
E E
E E
a + a × a
E E
E E
E
5
Pushdown Automata (PDA)
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)
c
d
d
(pushdown)
stack
Finite
control
a b a b a a
…
input appears on a “tape”
Schematic diagram for DFA or NFA
Schematic diagram for PDA
Operates like an NFA except can write-add or read-remove symbols
from the top of stack.
push pop
“head”
6
PDA – Formal Definition
Defn: A Pushdown Automaton (PDA) is a 6-tuple (𝑄, Σ, Γ, 𝛿, 𝑞0, 𝐹)
Σ input alphabet
Γ stack alphabet
𝛿: Q × Σ𝜀 × Γ𝜀 → 𝒫(𝑄 × Γ𝜀)
𝛿 𝑞, a, c = 𝑟1, d , 𝑟2, e
Example: PDA for 𝐵 = {𝑤𝑤ℛ| 𝑤 ∈ 0,1 ∗ }
1) Read and push input symbols.
Nondeterministically either repeat or go to (2).
2) Read input symbols and pop stack symbols, compare.
If ever ≠ then thread rejects.
3) Enter accept state if stack is empty. (do in “software”)
Accept if some thread is in the accept state
at the end of the input string.
The nondeterministic forks replicate the stack.
This language requires nondeterminism.
Our PDA model is nondeterministic.
0 1 1 1 1 0
Sample input:
7
Converting CFGs to PDAs
Theorem: If 𝐴 is a CFL then some PDA recognizes 𝐴
Proof: Convert 𝐴’s CFG to a PDA
IDEA: PDA begins with starting variable and guesses substitutions.
It keeps intermediate generated strings on stack. When done, compare with input.
Problem! Access below the top of stack is cheating!
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 ≠.
…
PDA
E → E+T | T
T → …
F → … CFG
E
E
E + T
T T × F
F F a
a a a
E
E+T
T+T×F
F+F×a
a+a×a
E → E+T | T
T → T×F | F
F → ( E ) | a
𝐺2
E
+
T
T
+
T
T
+
T
×
F
a + a × a
Input:
8
Theorem: If 𝐴 is a CFL then some PDA recognizes 𝐴
Proof construction: Convert the CFG for 𝐴 to the following PDA.
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.
3) If the stack is empty, accept.
Example:
E
E + T
T T × F
F F a
a a a
E
E+T
T+T×F
F+F×a
a+a×a
E E
+
T
F
+
T
Converting CFGs to PDAs (contd)
T
+
T
a
+
T
+
T
T T
×
F
a + a × a
E → E+T | T
T → T×F | F
F → ( E ) | a
𝐺2
9
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 (←).
Equivalence of CFGs and PDAs
Check-in 4.3
Check-in 4.3
Is every Regular Language also
a Context Free Language?
(a) Yes
(b) No
(c) Not sure
10
Recap
Recognizer Generator
Regular
language
DFA or NFA
Regular
expression
Context Free
language
PDA
Context Free
Grammar
Regular
languages
Context Free
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 Theory of Computation
Fall 2020
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.

Mais conteúdo relacionado

Semelhante a a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx

ContextFreeGrammars.pptx
ContextFreeGrammars.pptxContextFreeGrammars.pptx
ContextFreeGrammars.pptx
PEzhumalai
 
ContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptxContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptx
viswanath kani
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)bolovv
 
Finals-review.pptx
Finals-review.pptxFinals-review.pptx
Finals-review.pptx
amara jyothi
 
FPGA - Programmable Logic Design
FPGA - Programmable Logic DesignFPGA - Programmable Logic Design
FPGA - Programmable Logic Design
Dr. Shivananda Koteshwar
 
Lecture4 lexical analysis2
Lecture4 lexical analysis2Lecture4 lexical analysis2
Lecture4 lexical analysis2
Mahesh Kumar Chelimilla
 
TOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdfTOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdf
Prof. Dr. K. Adisesha
 
TOC Solutions-Adi.pdf
TOC Solutions-Adi.pdfTOC Solutions-Adi.pdf
TOC Solutions-Adi.pdf
AdiseshaK
 
TOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdfTOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdf
AdiseshaK
 
Pushdown AutomataChapter 12Recognizing Context-F.docx
Pushdown AutomataChapter 12Recognizing Context-F.docxPushdown AutomataChapter 12Recognizing Context-F.docx
Pushdown AutomataChapter 12Recognizing Context-F.docx
woodruffeloisa
 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint Resolution
Eelco Visser
 
0015.register allocation-graph-coloring
0015.register allocation-graph-coloring0015.register allocation-graph-coloring
0015.register allocation-graph-coloringsean chen
 
Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3
Srimatre K
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
Iffat Anjum
 
Lex analysis
Lex analysisLex analysis
Lex analysis
Suhit Kulkarni
 
TOC question bank.pdf
TOC question bank.pdfTOC question bank.pdf
TOC question bank.pdf
MrRRajasekarCSE
 
inbound6417574391896884649.pptx
inbound6417574391896884649.pptxinbound6417574391896884649.pptx
inbound6417574391896884649.pptx
FarjanaParvin9
 
Unit iv
Unit ivUnit iv
Unit iv
TPLatchoumi
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
Kuppusamy P
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauagesdanhumble
 

Semelhante a a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx (20)

ContextFreeGrammars.pptx
ContextFreeGrammars.pptxContextFreeGrammars.pptx
ContextFreeGrammars.pptx
 
ContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptxContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptx
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
 
Finals-review.pptx
Finals-review.pptxFinals-review.pptx
Finals-review.pptx
 
FPGA - Programmable Logic Design
FPGA - Programmable Logic DesignFPGA - Programmable Logic Design
FPGA - Programmable Logic Design
 
Lecture4 lexical analysis2
Lecture4 lexical analysis2Lecture4 lexical analysis2
Lecture4 lexical analysis2
 
TOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdfTOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdf
 
TOC Solutions-Adi.pdf
TOC Solutions-Adi.pdfTOC Solutions-Adi.pdf
TOC Solutions-Adi.pdf
 
TOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdfTOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdf
 
Pushdown AutomataChapter 12Recognizing Context-F.docx
Pushdown AutomataChapter 12Recognizing Context-F.docxPushdown AutomataChapter 12Recognizing Context-F.docx
Pushdown AutomataChapter 12Recognizing Context-F.docx
 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint Resolution
 
0015.register allocation-graph-coloring
0015.register allocation-graph-coloring0015.register allocation-graph-coloring
0015.register allocation-graph-coloring
 
Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
Lex analysis
Lex analysisLex analysis
Lex analysis
 
TOC question bank.pdf
TOC question bank.pdfTOC question bank.pdf
TOC question bank.pdf
 
inbound6417574391896884649.pptx
inbound6417574391896884649.pptxinbound6417574391896884649.pptx
inbound6417574391896884649.pptx
 
Unit iv
Unit ivUnit iv
Unit iv
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 

Último

LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdf
Kamal Acharya
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
bim.edu.pl
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
RCC Institute of Information Technology
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
wendy cai
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
AbrahamGadissa
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
VishalDeshpande27
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
jeevanprasad8
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
Emre Günaydın
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
Kamal Acharya
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
Kamal Acharya
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
Kamal Acharya
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
Kamal Acharya
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 

Último (20)

LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdf
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 

a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx

  • 1. 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
  • 2. Context Free Grammars (CFGs) Recall that a CFG has terminals, variables, and rules. Grammars generate strings 1. Write down start variable 2. Replace any variable according to a rule Repeat until only terminals remain 3. Result is the generated string 4. 𝐿(𝐺) is the language of all generated strings 5. We call 𝐿(𝐺) a Context Free Language. Example of 𝐺1 generating a string S → 0S1 S → R R → ε 𝐺1 S 0 S 1 0 S 1 R ε S 0S1 00S11 00R11 0011 𝐿 𝐺1 = 0𝑘 1𝑘 𝑘 ≥ 0} Tree of substitutions “parse tree” Resulting string ∈ 𝐿 𝐺1 S → 0S1 | R R → ε Shorthand: 2
  • 3. 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 1) 𝑢 ⇒ 𝑣 if can go from 𝑢 to 𝑣 with one substitution step in 𝐺 2) 𝑢 ⇒ 𝑣 if can go from 𝑢 to 𝑣 with some number of substitution steps in 𝐺 𝑢 ⇒ 𝑢1 ⇒ 𝑢2 ⇒ ⋯ ⇒ 𝑢𝑘 = 𝑣 is called a derivation of 𝑣 from 𝑢. If 𝑢 = 𝑆 then it is a derivation of 𝑣. 𝐿 𝐺 = 𝑤 𝑤 ∈ Σ∗ and 𝑆 ⇒ 𝑤} Defn: 𝐴 is a Context Free Language (CFL) if 𝐴 = 𝐿(𝐺) for some CFG 𝐺. ∗ ∗ Check-in 4.1 Check-in 4.1 Which of these are valid CFGs? a) 𝐶1 only b) 𝐶2 only c) Both 𝐶1 and 𝐶2 d) Neither B → 0B1 | ε B1 → 1B 0B → 0B 𝐶1: S → 0S | S1 R → RR 𝐶2: 3
  • 4. CFG – Example 𝑉 = {E, T, F} Σ = {+, ×, (, ), a} 𝑅 = the 6 rules above 𝑆 = E Observe that the parse tree contains additional information, such as the precedence of × over + . If a string has two different parse trees then it is derived ambiguously and we say that the grammar is ambiguous. E → E+T | T T → T×F | F F → ( E ) | a 𝐺2 Parse tree ∈ 𝐿 𝐺2 Generates a+a×a, (a+a)×a, a, a+a+a, etc. E E+T T+T×F F+F×a a+a×a E E + T T T × F F F a a a a Resulting string Check-in 4.2 Check-in 4.2 How many reasonable distinct meanings does the following English sentence have? The boy saw the girl with the mirror. (a) 1 (b) 2 (c) 3 or more 4
  • 5. Both 𝐺2 and 𝐺3 recognize the same language, i.e., 𝐿 𝐺2 = 𝐿 𝐺3 . However 𝐺2 is an unambiguous CFG and 𝐺3 is ambiguous. Ambiguity E → E+T | T T → T×F | F F → ( E ) | a 𝐺2 E → E+E | E×E | ( E ) | a 𝐺3 E E E E E a + a × a E E E E E 5
  • 6. Pushdown Automata (PDA) 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) c d d (pushdown) stack Finite control a b a b a a … input appears on a “tape” Schematic diagram for DFA or NFA Schematic diagram for PDA Operates like an NFA except can write-add or read-remove symbols from the top of stack. push pop “head” 6
  • 7. PDA – Formal Definition Defn: A Pushdown Automaton (PDA) is a 6-tuple (𝑄, Σ, Γ, 𝛿, 𝑞0, 𝐹) Σ input alphabet Γ stack alphabet 𝛿: Q × Σ𝜀 × Γ𝜀 → 𝒫(𝑄 × Γ𝜀) 𝛿 𝑞, a, c = 𝑟1, d , 𝑟2, e Example: PDA for 𝐵 = {𝑤𝑤ℛ| 𝑤 ∈ 0,1 ∗ } 1) Read and push input symbols. Nondeterministically either repeat or go to (2). 2) Read input symbols and pop stack symbols, compare. If ever ≠ then thread rejects. 3) Enter accept state if stack is empty. (do in “software”) Accept if some thread is in the accept state at the end of the input string. The nondeterministic forks replicate the stack. This language requires nondeterminism. Our PDA model is nondeterministic. 0 1 1 1 1 0 Sample input: 7
  • 8. Converting CFGs to PDAs Theorem: If 𝐴 is a CFL then some PDA recognizes 𝐴 Proof: Convert 𝐴’s CFG to a PDA IDEA: PDA begins with starting variable and guesses substitutions. It keeps intermediate generated strings on stack. When done, compare with input. Problem! Access below the top of stack is cheating! 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 ≠. … PDA E → E+T | T T → … F → … CFG E E E + T T T × F F F a a a a E E+T T+T×F F+F×a a+a×a E → E+T | T T → T×F | F F → ( E ) | a 𝐺2 E + T T + T T + T × F a + a × a Input: 8
  • 9. Theorem: If 𝐴 is a CFL then some PDA recognizes 𝐴 Proof construction: Convert the CFG for 𝐴 to the following PDA. 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. 3) If the stack is empty, accept. Example: E E + T T T × F F F a a a a E E+T T+T×F F+F×a a+a×a E E + T F + T Converting CFGs to PDAs (contd) T + T a + T + T T T × F a + a × a E → E+T | T T → T×F | F F → ( E ) | a 𝐺2 9
  • 10. 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 (←). Equivalence of CFGs and PDAs Check-in 4.3 Check-in 4.3 Is every Regular Language also a Context Free Language? (a) Yes (b) No (c) Not sure 10
  • 11. Recap Recognizer Generator Regular language DFA or NFA Regular expression Context Free language PDA Context Free Grammar Regular languages Context Free languages 11
  • 12. 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
  • 13. MIT OpenCourseWare https://ocw.mit.edu 18.404J Theory of Computation Fall 2020 For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.

Notas do Editor

  1. Type equation here.
  2. Type equation here.
  3. Type equation here.
  4. Type equation here.
  5. Type equation here.
  6. Type equation here.
  7. Type equation here.
  8. Type equation here.
  9. Type equation here.
  10. Type equation here.
  11. Type equation here.