Lecture 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Theory of computation

Lecture 4: Automata and Regular languages

Adnane Saoud

Tuesday, September 25, 2023

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.

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.
From DFAs to CFGs

0
𝑀1 1
0,1
1
𝑞1 𝑞2 𝑞3
0

5
CFG – Formal Definition

Definition: 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 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

Which of these are valid CFGs?


𝐶" : B → 0B1 | ε 𝐶# : S → 0S | S1
B1 → 1B R → RR
0B → 0B

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.

Theorem: Any context-free language is generated by a context-free grammar in Chomsky


normal form.

Why Chomsky’s normal form?


The main advantage is that in Chomsky Normal Form, every derivation of a string of n
letters has exactly 2n − 1 steps.
Thus: one can determine if a string is in the language by exhaustive search of all
derivations.
Chomsky normal form

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

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)
PDA – Formal Definition
Definition: A Pushdown Automaton (PDA) is a 6-tuple
(𝑄, Σ, Γ, 𝛿, 𝑞0, 𝐹)
Σ input alphabet
Γ stack alphabet Accept if some thread is in the accept state
𝛿: Q×Σ% ×Γ% → 𝒫(𝑄×Γ% ) at the end of the input string.
𝛿 𝑞, a, c = 𝑟" , d , 𝑟# , e
Example: PDA for 𝐵 = {𝑤𝑤 ℛ | 𝑤 ∈ 0,1 ∗ }
1) Read and push input symbols. Sample input: 0 1 1 1 1 0
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.
The nondeterministic forks replicate the stack.

This language requires nondeterminism.


Our PDA model is nondeterministic. 14
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

IDEA: PDA begins with starting variable and guesses substitutions.


It keeps intermediate generated strings on stack.

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

Theorem: 𝐴 is a CFL iff* some PDA recognizes 𝐴

Recognizer Generator

Regular Regular
DFA or NFA
language expression
Context Free Context Free
PDA
language Grammar
Context Free
languages
Regular
languages
Summary

19
Summary

1. Defined Context Free Grammars (CFGs) and Context Free


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

20

You might also like