Plan for This Today
Our Next Class of Languages
Context Free Grammars
– model for specifying programming languages
– why not just use regular expressions?
– example grammar
– derivations
Context-Free Languages
n n
{a b }
Parse trees
{ww R }
Syntax-directed translation
– using syntax-directed translation to interpret MiniSVG
Regular Languages
( a + b) *
a *b *
Top-down Predictive Parsing
CS453 Lecture
Context Free Grammar Intro
1
Context-Free Languages
Example
A context-free grammar
Context-Free
Grammars
Pushdown
Automata
S "aSb
S "#
A derivation:
We will start here
stack
automaton
G:
S ! aSb ! aaSbb ! aabb
Another derivation:
! ! aaaSbbb ! aaabbb
S ! aSb ! aaSbb
An Application of This Language
S " aSb
S "!
Context-Free Languages
Gave a
grammar
for:
n n
L(G ) = {a b : n ! 0}
(((( ))))
Describes parentheses:
Example
A context-free grammar
A derivation:
G : S "aSa
S "bSb
S "#
S ! aSa ! abSba ! abba
Another derivation: !
Deriving another grammar
S ! aSa ! abSba ! abaSaba ! abaaba
Can we derive a
Grammar for:
{a nb n }
{ww R }
Regular Languages
Representing All Properly Nested
Parentheses
S " aSb
S "!
L(G ) = {a nb n : n ! 0}
Describes parentheses:
(((( ))))
Can we build a grammar to include any valid
combination of ()? For example ( () (()) )
The Possible Grammar
A context-free grammar
Context-Free Grammars
G : S "(S)
S "SS
S "#
A derivation:
Grammar
Productions of the form:
Another derivation:
!
Terminals Start
symbol
Nonterminals
S " SS " (S)S " ()S " ()
G = (V , T , S , P )
A! x
S " SS " (S)S "!()S " ()(S) " ()()
Nonterminal String of symbols,
Nonterminals and terminals
!
Derivation Order
Derivation Order
Given a grammar with rules:
1. S ! AB
2. A "aaA
3. A "#
Given a grammar with rules:
4. B "Bb
5. B "#
Always expand the leftmost non-terminal
Leftmost derivation:
!
1
2
3
! 4
2. A "aaA
3. A "#
1. S ! AB
4. B "Bb
5. B "#
Always expand the rightmost non-terminal
5
S ! AB ! aaAB ! aaB ! aaBb ! aab
!
Rightmost derivation:
1
4
5
!
2
3
S ! AB ! ABb ! Ab ! aaAb ! aab
Grammar
String
Parse Trees
Stm --> id := Exp
Exp --> num
Exp --> ( Stm, Exp )
a := ( b := ( c := 3, 2 ), 1 )
S ! AB
A "aaA | #
B "Bb | #
S ! AB
Leftmost derivation:
Stm ==> a := Exp ==> a := ( Stm, Exp ) ==> a := ( b := Exp, Exp )
==> a := ( b := ( Stm, Exp ), Exp ) ==> a := ( b := ( c := Exp, Exp ), Exp )
==> a := ( b := ( c := 3, Exp ), Exp ) ==> a := ( b := ( c := 3, 2), Exp )
==> a := ( b := ( c := 3, 2), 1)
S
!
!
A
B
Rightmost derivation:
Stm ==> a := Exp ==> a := ( Stm, Exp ) ==> a := ( Stm, 1)
==>
Parse Trees
Parse Trees
S ! AB
A "aaA | #
B "Bb | #
S ! AB
S ! AB ! aaAB
a
!
A
a
A
B
B "Bb | #
S ! AB ! aaAB ! aaABb
S
!
A "aaA | #
S
!
a
!
A
a
A
B
B
b
Parse Trees
Parse Trees
S ! AB
A "aaA | #
B "Bb | #
S ! AB ! aaAB ! aaABb ! aaBb
S
!
a
!
A
a
A
b
A "aaA | #
B "Bb | #
S ! AB ! aaAB ! aaABb ! aaBb ! aab
S
!
B
B
S ! AB
!
A
a
a
"
B
A
B
"
"
yield
aa""b
= aab
b
!
!
S ! AB
!
Sentential forms
A "aaA | #
B "Bb | #
!
S ! AB ! aaAB
Partial parse tree
S
! S ! AB
!
Partial parse tree
A
S
A
B
a
sentential
form
a
B
A
Sometimes, derivation order doesn’t matter
Does it matter here?
Leftmost:
S ! AB ! aaAB ! aaB ! aaBb ! aab
Rightmost:
S ! AB ! ABb ! Ab ! aaAb ! aab
a := ( b := ( c := 3, 2 ), 1 )
A
a
a
!
Grammar
Stm --> id := Exp
Exp --> num
Exp --> ( Stm, Exp )
String
S
Same parse tree
Parse Tree
B
A
B
"
"
b
CS453 Lecture
Context Free Grammar Intro
22
!
How about here?
Syntax Directed Translation or Interpretation
Use parse tree to …
– Translate one language to another
– Create a data structure of the program
– Interpret, or evaluate, the program
Grammar
(1) exp --> exp * exp
(2) exp --> exp + exp
(3) exp --> NUM
Works conceptually by…
– Parser discovers the parse tree
– Parser executes certain actions while “traversing” the parse tree using a
depth-first, post-order traversal
String
42 + 7 * 6
CS453 Lecture
Context Free Grammar Intro
23
CS453 Lecture
Context Free Grammar Intro
24
MiniSVG Grammar
Example Parse Tree for MiniSVG
svg -> SVG_START elem_list SVG_END
elem_list -> elem elem_list
|
epsilon
elem -> RECT_START KW_X EQ NUM KW_Y EQ NUM KW_WIDTH
EQ NUM KW_HEIGHT EQ NUM KW_FILL EQ COLOR ELEM_END
| CIRCLE_START KW_CX EQ NUM KW_CY EQ NUM KW_R EQ NUM
KW_FILL EQ COLOR ELEM_END
| LINE_START KW_X1 EQ NUM KW_Y1 EQ NUM KW_X2 EQ NUM
KW_Y2 EQ NUM KW_STROKE EQ COLOR ELEM_END
CS453 Lecture
Context Free Grammar Intro
Interpret this program
25
Context Free Grammar Intro
26
Context Free Grammar Intro
28
How about here?
Parse Tree
Grammar
Stm --> id := Exp
Exp --> num
Exp --> ( Stm, Exp )
Grammar
(1) exp --> exp * exp
(2) exp --> exp + exp
(3) exp --> NUM
String
String
a := ( b := ( c := 3, 2 ), 1 )
CS453 Lecture
CS453 Lecture
42 + 7 * 6
Context Free Grammar Intro
27
CS453 Lecture
Example:
input
string
Parser
Parser
derivation
grammar
input
aabb
S "SS
S "aSb
S "bSa
derivation
?
S "#
!
Exhaustive Search
S "SS | aSb | bSa | #
Phase 1:
!
S " SS
S " aSb
Find derivation of
S " SS
S " aSb
aabb
S " bSa
S "#
S " bSa
S "#
All possible derivations of length 1
!
!
aabb
Phase 2
S "SS | aSb | bSa | #
S ! SS ! SSS
S ! SS ! aSbS
Phase 1 ! S ! SS ! bSaS
S ! SS
S ! SS ! S
S ! aSb S ! aSb ! aSSb
S ! SS ! SSS
aabb
S ! aSb ! aaSbb
S ! aSb ! abSab
S ! aSb ! ab
Final result of exhaustive search
(top-down parsing)
Parser
input
aabb
aabb
S ! SS ! aSbS
S ! SS !!
S
S ! aSb ! aSSb
S ! aSb ! aaSbb
Phase 3
S ! aSb ! aaSbb ! aabb
Time complexity of exhaustive search
Suppose there are no productions of the form
S " SS
S " aSb
S " bSa
S "#
A "#
A! B
derivation
!
S "SS | aSb | bSa | #
Phase 2
S ! aSb ! aaSbb ! aabb
Number !
of phases for string
w:
2| w|
For grammar with
k rules
Time for phase 1:
k
Time for phase 2:
k2
k2
k possible derivations
possible derivations
Total time needed for string
Time for phase
2 | w |: k 2|w|
k 2|w| possible derivations
w:
k + k 2 + ! + k 2|w|
phase 1
phase 2
phase 2|w|
Extremely bad!!!
For general context-free grammars:
Example Predictive Parser
(1) start -> mesh EOF
(2) mesh -> NUM nodelist NUM elemlist
(3a & b) nodelist -> ϵ | node nodelist
(4) node -> NODE NUM REAL REAL
// node_id, x, y
(5a & b) elemlist -> ϵ | elem elemlist
(6a) elem -> TRI NUM NUM NUM NUM
// elem_id, 3 node ids
(6b) elem -> SQR NUM NUM NUM NUM NUM //elem_id,4 node ids
There exists a parsing algorithm
that parses a string | w |
in time | w |3
For LL(1) grammars we can use
Predictive parsing and parse in | w |
void start() { switch(m_lookahead) {
case NUM:
mesh(); match(Token.Tag.EOF); break;
default:
throw new ParseException(…);
}}
void mesh() { switch(this.m_lookahead) {
case NUM:
num_nodes = ((Num)m_lookahead).value; match(NUM); nodelist(); num_elem = ((Num)m_lookahead).value; match(NUM); elemlist(); break;
default:
throw new ParseException(…);
}}
void nodelist() { switch(m_lookahead) {
case NUM:
break;
// nodelist -> epsilon
case NODE: node(); nodelist(); break;
// nodelist -> node nodelist
default:
throw new ParseException(…);
}}
CS453 Lecture
CS453 Lecture
Context Free Grammar Intro
43
Context Free Grammar Intro
42
© Copyright 2026 Paperzz