09 CFL
09 CFL
COMP1600 / COMP6260
Semester 2, 2023
Formal Languages – A Reminder of Terminology
Notation.
Σ∗ is the set of all strings over Σ.
Therefore, every language with alphabet Σ is some subset of Σ∗ .
1 / 59
Specifying Languages
Grammar.
a concept that has been invented in linguistics to describe natural
languages
describes how strings are constructed rather than how membership
can be checked (e.g. by an automaton)
the main tool to describe syntax.
2 / 59
Grammars in general
α→β
where
I α ∈ V ∗ Vn V ∗ (i.e. at least one non-terminal in α)
I β ∈ V ∗ (i.e. β is any list of symbols)
3 / 59
Example
The grammar
G = h{a, b}, {S, A}, S, {S → aAb, aA → aaAb, A → }i
has the following components:
Terminals: {a, b} Productions:
Non-terminals: {S, A} S → aAb
Start symbol: S aA → aaAb
A→
Notation.
Often, we just list the productions P, as all other components can be
inferred (S is the standard notation for the start symbol)
The notation α → β1 | · · · | βn abbreviates the set of productions
α → β1 , α → β2 , ..., α → βn
(like for inductive data types)
4 / 59
Derivations
Intuition.
A production α → β tells you what you can “make” if you have α:
you can turn it into β.
The production α → β allows us to re-write any string γαρ to γβρ.
Notation: γαρ ⇒ γβρ
Derivations.
α ⇒∗ β if α can be re-written to β in 0 or more steps
so ⇒∗ is the reflexive transitive closure of ⇒.
Sentential Forms of a grammar.
informally: all strings (may contain non-terminals) that can be
generated from S
formally: S(G ) = {w ∈ V ∗ | S ⇒∗ w }.
Language of a grammar.
informally: all strings of terminal symbols that can be generated from
the start symbol S
formally: L(G ) = {w ∈ Vt∗ | S ⇒∗ w } ⊆ S(G )
5 / 59
Example
Productions of the grammar G .
S → aAb, aA → aaAb, A → .
Example Derivation.
6 / 59
Example
Productions of the grammar G .
S → aAb, aA → aaAb, A → .
Example Derivation.
Language of grammar G .
L(G ) = {an b n | n ∈ N, n ≥ 1}
Immediate Fact.
Every language of type n + 1 is also of type n.
8 / 59
Example — language {an b n | n ∈ N, n ≥ 1}
Different grammars for this language
Unrestricted (type 0):
S → aAb
aA → aaAb
A→
S → ab
S → aSb
A → aB or A→a or A→
A → Ba or A→a or A → .
right and left linear grammars are equivalent: they generate the same
languages
we focus on right linear (for no deep reason)
i.e. one symbol is generated at a time (cf. DFA/NFA!)
termination with terminal symbols or
So far.
have seen that NFAs and DFAs generate the same languages (subset
construction)
have hinted at regular expressions and NFAs generate the same
languages
Goal. Show that NFAs and right-linear grammars generate the same
languages.
11 / 59
From NFAs to Right-linear Grammars
Given. Take an NFA A = (Σ, S, s0 , F , R).
alphabet, state set, initial state, final states, transition relation
13 / 59
NFAs to Right-linear Grammars - Example
S → aS S2 → cS2
S → aS1 S2 → cS3
S1 → bS1 S3 → ε
S1 → bS2
Exercise. Convince yourself that the NFA accepts precisely the words that
the grammar generates.
13 / 59
From Right-linear Grammars to NFAs
Given. Right-linear grammar (Vt , Vn , S, P)
terminals, non-terminals, start symbol, productions
Sf
0
1
T 0, 1
Exercise. Convince yourself that the NFA accepts precisely the words that
the grammar generates.
15 / 59
Context-Free (Type 2) Grammars (CFGs)
αAβ → αw β
16 / 59
Example
Goal. Design a CFG for the language
L = {am b n c m−n | m ≥ n ≥ 0}
Grammar
S → aSc | T
T → aTb |
S ⇒ aSc
⇒ aTc
⇒ aaTbc
⇒ aaaTbbc
⇒ aaabbc
18 / 59
The Power of Context-Free Grammars
Note that the generator doesn’t seem to work anymore, but they
also link similar services. And maybe you can find a working version
of it somewhere.
19 / 59
Parse Trees
Idea. Represent derivation as tree rather than as list of rule applications
describes where and how productions have been applied
generated word can be collected at the leaves
Example for the grammar that we have just constructed
S
a S c
a T b
a T b
20 / 59
Parse Trees Carry Semantics
Take the code
Two Readings.
and
22 / 59
Example: If-Then and If-Then-Else
where bexp and prog stand for boolean expressions and (if-statement
free) programs respectively, defined elsewhere.
The string if e1 then if e2 then s1 else s2 then has two parse trees:
S S
x " x "
(
if e1 then S if e1 then S else S
t | " x
if e2 then S else S if e2 then S s2
s1 s2 s1
23 / 59
Example: If-Then and If-Then-Else
S → if bexp then S | T
T → if bexp then T else S | prog
There is now only one parse for if e1 then if e2 then s1 else s2.
This is given on the next slide:
24 / 59
Example: If-Then and If-Then-Else
S
x "
if e1 then S
T
t | "
if e2 then T else S
s1 T
s2
You cannot parse this string as if e1 then ( if e2 then s1 ) else s2.
25 / 59
Reflecting on This Example
Observation.
more than one grammar for a language
some are ambiguous, others are not
ambiguity is a property of grammars
Reasoning.
Suppose that the above production was used
we can then expand either T or S first.
Complete Grammar.
S → T | W
T → UV W → XY
U → aUb | X → aX |
V → cV | Y → bYc |
28 / 59
Inherently Ambiguous Languages
Problem. Both left part ai b i c k and right part ai b j c j has non-empty
intersection: ai b i c i
S S
T W
} ! | "
U V X Y
~ ! ! ~ |
a U b c V a X b Y c
30 / 59
Example: Subtraction
Example.
S → S − S | int
int stands for integers
the intended meaning of − is subtraction
Ambiguity.
S S
S − 1 5 − S
5 − 3 3 − 1
Evaluation.
left parse tree evaluates to 1
right parse tree evaluates to 3
so ambiguity matters! (As we also saw for the if/else statements.)
31 / 59
Technique 1: Associativity
Idea for ambiguity induced by binary operator (think: −)
prescribe “implicit parentheses”, e.g. a − b − c ≡ (a − b) − c
make operator associate to the left or the right
Left Associativity.
S → S − int | int
Result.
5 − 3 − 1 can only be read as (5 − 3) − 1
this is left associativity
Right Associativity.
S → int − S | int
S → S ∗ S | S + S | int
Ambiguity.
1 + 2 ∗ 3 can be read as (1 + 2) ∗ 3 and 1 + (2 ∗ 3) with different results
also 1 + 2 + 3 is ambiguous – but this doesn’t matter here.
33 / 59
Technique 2: Precedence
Example Grammar giving ∗ higher precedence:
S → S +T | T
T → T ∗ int | int
Given e.g. 1 + 2 ∗ 3 or 2 ∗ 3 + 1
forced to expand + first: otherwise only ∗
so + will be last operation evaluated
Example. Derivation of 1 + 2 ∗ 3
suppose we start with S ⇒ T ⇒ T ∗ int
stuck, as cannot generate 1 + 2 from T
S → S +T | S −T | T
T → T ∗ U | T /U | U
U → (S) | int
Main Differences.
have parentheses to break operator priorities, e.g. (1 + 2) ∗ 3
parentheses at lowest level, so highest priority
lower-priority operator can be inside parentheses
expressions of arbitrary complexity (no nesting in previous examples)
35 / 59
Example: Balanced Brackets
S → | (S) | SS
Ambiguity.
associativity: create brackets from left or from right (as before)
. . . two ways of generating (): S ⇒ SS ⇒ S ⇒ (S) ⇒ ()
indeed, any expression has infinitely many parse trees
36 / 59
Technique 3: Controlling
S→ | T
T → TU | U
U → () | (T )
37 / 59
From Grammars to Automata
So Far.
regular languages correspond to regular grammars.
38 / 59
General Structure of Automata
a0 a1 a2 ... .... an
Finite Auxiliary
State
Memory
Control
a0 a1 a2 ... .... an
Finite
State
zk
Control
stack
memory
z2
z1
40 / 59
PDAs ctd.
Actions of a push-down automaton
change of internal state
pushing or popping the stack
advance to next input symbol
Variation.
PDAs can equivalently be defined without final states F .
Then, acceptance condition is having an empty stack
(after the input word was completely read)
41 / 59
Example
L = {an b n | n ≥ 1}
42 / 59
Deterministic PDA – Definition
[label=important]
Definition. A deterministic PDA has the form (S, s0 , F , Σ, Γ, Z , δ), where
S is the finite set of states, s0 ∈ S is the initial state and F ⊆ S are
the final states;
Σ is the finite alphabet, or set of input symbols;
Γ is the finite set of stack symbols, and Z ∈ Γ is the initial stack
symbol;
δ is a (partial) transition function
δ : S × (Σ ∪ {}) × Γ 9 S × Γ∗
δ : (state, input token or , top-of-stack) 9 (new state, new stack)
δ : S × (Σ ∪ {}) × Γ 9 S × Γ∗
δ : (state, input token or , top-of-stack) 9 (new state, stack string)
Notation.
write δ(s, a, γ) = s 0 /σ
σ is a string that replaces top stack symbol
final states are usually underlined (s)
Rationale.
replacing top stack symbol gives just one operation for push and pop
pop: δ(s, a, γ) = s 0 /
push: δ(s, a, γ) = s 0 /w γ
44 / 59
Two types of PDA transition
Non-consuming transitions
δ contains (s1 , , γ) 7→ s2 /σ
Recall that for the pair s1 , γ, we can’t have any other entry (s1 , a, γ)
with a ∈ Σ to stay deterministic! (See slide 43)
independent of input symbol
can happen any time and does not consume input symbol
45 / 59
Example ctd.
Language L = {an b n | n ≥ 1}
Push-down automaton
starts with Z (initial stack symbol) on stack
final state is S3 (underlined)
transition function (partial) given by
δ(S0 , a, Z ) 7→ S1 /aZ push first a
δ(S1 , a, a) 7→ S1 /aa push further a’s
δ(S1 , b, a) 7→ S2 / start popping a’s
δ(S2 , b, a) 7→ S2 / pop further a’s
δ(S2 , , Z ) 7→ S3 / accept
a, a/aa b, a/
a, Z /aZ b, a/
start S0 S1 S2
, Z /
S3
47 / 59
Example ctd. — PDA Trace
PDA configurations
triples: (state, remaining input, stack)
top of stack on the left (by convention)
Example Execution.
PDA execution.
Non-accepting execution.
No transition possible, stuck without reaching final state
rejection happens when transition function is undefined for current
configuration (state, input, top of stack)
49 / 59
Example: Palindromes with ‘Centre Mark’
Example Language.
50 / 59
Non-Deterministic PDAs
Deterministic PDAs
transitions are a partial function
δ : S × (Σ ∪ {}) × Γ 9 S × Γ∗
δ : (state, input token or , top-of-stack) 9 (new state, stack string)
side condition about -transitions
Non-Deterministic PDAs
transitions given by relation
δ ⊆ S × (Σ ∪ {}) × Γ × S × Γ∗
no side condition (at all).
Main differences
for deterministic PDA: at most one transition possible
for non-deterministic PDA: zero or more transitions possible
51 / 59
Non-Deterministic PDAs ctd.
Finite Automata
non-determinism is convenient
but doesn’t give extra power (subset construction)
can convert every NFA to an equivalent DFA
Push-down automata.
non-determinism gives extra power
cannot convert every non-deterministic PDA to deterministic PDA
there are context free languages that can only be recognised by
non-deterministic PDA
intuition: non-determinism allows “guessing”
Intuition
“guess” (non-deterministically) whether we need to enter
“match-and-pop”-state
automaton gets stuck if guess is not correct (no harm done)
automaton accepts if guess is correct
53 / 59
Grammars and PDAs
54 / 59
From CFG to PDA
Given. Context-Free Grammar G = (Vt , Vn , S, P)
Stack Alphabet. Γ = Vt ∪ Vn ∪ {Z }
Initialisation.
push start symbol S onto stack, enter working state Q1
δ(Q0 , , Z ) 7→ Q1 /SZ
Termination.
if the stack is empty (i.e. just contains Z ), terminate
δ(Q1 , , Z ) 7→ Q2 /
55 / 59
From CFGs to PDAs: working state
Idea.
build the derivation on the stack by expanding non-terminals
according to productions productions
if a terminal appears that matches the input, pop it
terminate, if the entire input has been consumed
Expand Non-Terminals.
non-terminals on the stack are replaced by right hand side of
productions
δ(Q1 , , A) 7→ Q1 /α for all productions A → α
Pop Terminals.
terminals on the stack are popped if they match the input
δ(Q1 , t, t) 7→ Q1 / for all terminals t
S → S +T | T
T → T ∗U | U
U → (S) | int
1. Initialise:
δ(Q0 , , Z ) 7→ Q1 /SZ
2. Expand non-terminals:
δ(Q1 , , S) 7→ Q1 /S + T δ(Q1 , , T ) 7→ Q1 /U
δ(Q1 , , S) 7→ Q1 /T δ(Q1 , , U) 7→ Q1 /(S)
δ(Q1 , , T ) 7→ Q1 /T ∗ U δ(Q1 , , U) 7→ Q1 /int
57 / 59
CFG to PDA ctd.
δ(Q1 , +, +) 7→ Q1 /
δ(Q1 , ∗, ∗) 7→ Q1 /
δ(Q1 , int, int) 7→ Q1 /
δ(Q1 , (, () 7→ Q1 /
δ(Q1 , ), )) 7→ Q1 /
4. Terminate:
δ(Q1 , , Z ) 7→ Q2 /
58 / 59
Example Trace
59 / 59