SlideShare uma empresa Scribd logo
1 de 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)
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.pptxPEzhumalai
 
ContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptxContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptxviswanath kani
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)bolovv
 
Finals-review.pptx
Finals-review.pptxFinals-review.pptx
Finals-review.pptxamara jyothi
 
TOC Solutions-Adi.pdf
TOC Solutions-Adi.pdfTOC Solutions-Adi.pdf
TOC Solutions-Adi.pdfAdiseshaK
 
TOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdfTOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdfAdiseshaK
 
Pushdown AutomataChapter 12Recognizing Context-F.docx
Pushdown AutomataChapter 12Recognizing Context-F.docxPushdown AutomataChapter 12Recognizing Context-F.docx
Pushdown AutomataChapter 12Recognizing Context-F.docxwoodruffeloisa
 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionEelco 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 3Srimatre K
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Iffat Anjum
 
inbound6417574391896884649.pptx
inbound6417574391896884649.pptxinbound6417574391896884649.pptx
inbound6417574391896884649.pptxFarjanaParvin9
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy 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

The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 

Último (20)

The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 

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.