Context-Free Grammars
Using grammars in parsers
Chapter 3 Context-free
21/10/2021 Grammar 1
Outline
Parsing Process
Grammars
Context-free grammar
Backus-Naur Form (BNF)
Parse Tree and Abstract Syntax Tree
Ambiguous Grammar
Extended Backus-Naur Form (EBNF)
21/10/2021 Chapter 3 Context-free Grammar 2
Parsing Process
Call the scanner to get tokens
Build a parse tree from the stream of t
okens
A parse tree shows the syntactic structure
of the source program.
Add information about identifiers in the
symbol table
Report error, when found, and recover
from the error
21/10/2021 Chapter 3 Context-free Grammar 3
Grammar
a quintuple (V, T, P, S) where
V is a finite set of nonterminals, containing S,
T is a finite set of terminals,
P is a set of production rules in the form of β w
here and β are strings over V UT , and
S is the start symbol.
Example
G= ({S, A, B, C}, {a, b, c}, P, S)
P= { SSABC
SAbc
S ab}
21/10/2021 Chapter 3 Context-free Grammar 4
Context-Free Grammar
a quintuple (V, T, P, S) where
V is a finite set of nonterminals, containin
g S,
T is a finite set of terminals,
P is a set of production rules in the form o
f β where is in V and β is in (V UT )*, a
nd
S is the start symbol.
Any string in (V U T)* is called a senten
tial form.
21/10/2021 Chapter 3 Context-free Grammar 5
Examples
EEOE S SS
E (E) S (S)S
E id S ()
O+ S
O-
O*
O/
21/10/2021 Chapter 3 Context-free Grammar 6
Backus-Naur Form (BNF)
Nonterminals are in < >.
Terminals are any other symbols.
::= means .
| means or.
Examples:
<E> ::= <E><O><E>| (<E>) | ID
<O> ::= + | - | * | /
<S> ::= <S><S> | (<S>)<S> | () |
21/10/2021 Chapter 3 Context-free Grammar 7
Derivation
A sequence of replacement of a substri
ng in a sentential form.
Definition
Let G = (V, T, P, S ) be a CFG, , , b
e strings in (V U T)* and A is in V.
21/10/2021 Chapter 3 Context-free Grammar 8
Examples
S SS | (S)S | () | E E O E | (E) | id
S O+|-|*|/
SS E
(S)SS EOE
(S)S(S)S (E) O E
(S)S(())S (E O E) O E
((S)S)S(())S ((E O E) O E) O E
((S)())S(())S ((id O E)) O E) O E
((())())S(())S ((id + E)) O E) O E
((())()) (())S ((id + id)) O E) O E
((())())(()) ((id + id) * id) + id
21/10/2021 Chapter 3 Context-free Grammar 9
Leftmost Derivation Rightmost Derivation
Each step of the derivat Each step of the derivat
ion is a replacement of ion is a replacement of t
the leftmost nontermin he rightmost nontermin
als in a sentential form. als in a sentential form.
E E
EOE EOE
(E) O E E O id
(E O E) O E E * id
(id O E) O E (E) * id
(id + E) O E (E O E) * id
(id + id) O E (E O id) * id
(id + id) * E (E + id) * id
(id + id) * id (id + id) * id
21/10/2021 Chapter 3 Context-free Grammar 10
Language Derived from Grammar
Let G = (V, T, P, S ) be a CFG.
A string w in T * is derived from G if S *
Gw.
A language generated by G, denoted b
y L(G), is a set of strings derived from
G.
L(G) = {w| S *
G w}.
21/10/2021 Chapter 3 Context-free Grammar 11
Right/Left Recursive
A grammar is a left r A grammar is a right
ecursive if its produc recursive if its produ
tion rules can gener ction rules can gene
ate a derivation of th rate a derivation of t
e form A * A X. he form A * X A.
Examples: Examples:
E E O id | (E) | id E id O E | (E) | id
E F + id | (E) | id E id + F | (E) | id
F E * id | id F id * E | id
E
F + id E
id + F
E * id + id
id + id * E
21/10/2021 Chapter 3 Context-free Grammar 12
Parse Tree
A labeled tree in which
the interior nodes are labeled by nontermi
nals
leaf nodes are labeled by terminals
the children of an interior node represent
a replacement of the associated nontermi
nal in a derivation
corresponding toEa derivation
id + F
i * E
d
i
21/10/2021 Chapter 3 Context-free Grammar 13
Parse Trees and Derivations
E 1
E
E+E (1)
E 2 E 3
id + E (2)
+
E 4
E 5
id + E * E (3)
i *
d i i
id + id * E (4)
d numberid
Preorder
id + id * id (5)
ng E1 E
E+E (1)
E 2
E+E*E (2)
E 5
+
E + E * id (3)
i E 4
* E 3
E + id * id (4)
d i i
id + id * id (5)
d
Reverse of postorder d
numbering
21/10/2021 Chapter 3 Context-free Grammar 14
Grammar: Example
List of parameters in: <Fdef> function id ( <argList> )
Function definition <argList> id , <arglist> | id
function sub(a,b,c) <Fcall> id ( <parList> )
<parList> <par> ,<parlist>| <par>
Function call
<par> id | const
sub(a,1,2)
<argList>
id , <arglist> <Fdef> function id ( <argList> )
id, id , <arglist> <argList> <arglist> , id | id
… (id ,)* id <Fcall> id ( <parList> )
<argList> <parList> <parlist> ,<par>| <par>
<arglist> , id <par> id | const
<arglist> , id, id
… id (, id )*
21/10/2021 Chapter 3 Context-free Grammar 15
Grammar: Example
List of parameters <Fdef> function id ( <argList> )|
If zero parameter is function id ( )
allowed, then ? <argList> id , <arglist> | id
<Fcall> id ( <parList> ) | id ( )
<parList> <par> ,<parlist>| <par>
<par> id | const
<Fdef> function id ( <argList> )
<argList> id , <arglist> | id |
<Fcall> id ( <parList> )
<parList> <par> ,<parlist>| <par>
<par> id | const
21/10/2021 Chapter 3 Context-free Grammar 16
Abstract Syntax Tree
Representation of actual source tokens
Interior nodes represent operators.
Leaf nodes represent operands.
21/10/2021 Chapter 3 Context-free Grammar 17
Abstract Syntax Tree for Expression
E
+
E + E
id1 *
E E
id * id id3
1 id id 2
2 3
21/10/2021 Chapter 3 Context-free Grammar 18
Abstract Syntax Tree for If Statement
st
ifStatement
if
if ( ex ) st elsePa
rt tru st return
p
else st e
tru
e
return
21/10/2021 Chapter 3 Context-free Grammar 19
Ambiguous Grammar
A grammar is ambiguous if it can gene
rate two different parse trees for one st
ring.
Ambiguous grammars can cause incon
sistency in parsing.
21/10/2021 Chapter 3 Context-free Grammar 20
Example: Ambiguous Grammar
E E+E
E E-E
E E*E
E E/E
E id
E E
E + E *
E E
E E E E
id1 * + id3
id id3 id id2
2 1
21/10/2021 Chapter 3 Context-free Grammar 21
Ambiguity in Expressions
Which operation is to be done first?
solved by precedence
An operator with higher precedence is done bef
ore one with lower precedence.
An operator with higher precedence is placed in
a rule (logically) further from the start symbol.
solved by associativity
If an operator is right-associative (or left-associ
ative), an operand in between 2 operators is as
sociated to the operator to the right (left).
Right-associated : W + (X + (Y + Z))
Left-associated : ((W + X) + Y) + Z
21/10/2021 Chapter 3 Context-free Grammar 22
Precedence
E
EE+E E
EE-E E + E E * E
EE*E E E
E E id3
id1 * +
EE/E
E id id id3 id id2
2 1
EE+E E
EE-E E + E
EF F
F
FF*F
id F * F
FF/F 1
F id id id
2 3
21/10/2021 Chapter 3 Context-free Grammar 23
Precedence (cont’d)
EE+E|E-E|F E
FF*F|F/F|X F
X ( E ) | id F * F
X F * F
( E ) X X
(id1 + id2) * id3 * id4
id id
E + E
3 4
F F
X X
id id
1 2
21/10/2021 Chapter 3 Context-free Grammar 24
Associativity
E
Left-associative operat F
ors
F / X
EE+F|E-F|F
FF*X|F/X|X F * X id4
X ( E ) | id X id3
( E )
E + F
(id1 + id2) * id3 / id4
F X
= (((id1 + id2) * id3) / id4
) X id2
id1
21/10/2021 Chapter 3 Context-free Grammar 25
Ambiguity in Dangling Else
St IfSt | ...
IfSt if ( E ) St | if ( E ) St else St
{ if (0)
E0|1|… { if (1) St }
{ if (0) else St }
{ if (1) St else St } } IfSt
IfSt
if ( E ) St if ( E ) St else St
0 IfSt
0 IfSt
if ( E ) St else St if ( E ) St
1 1
21/10/2021 Chapter 3 Context-free Grammar 26
Disambiguating Rules for Dangling E
lse
St
MatchedSt | UnmatchedSt
UnmatchedSt
if (E) St |
if (E) MatchedSt else Unmatched St
St
MatchedSt
if (E) MatchedSt else MatchedSt UnmatchedSt
|
... if ( E ) S
E t
0|1 MatchedSt
if (0) if (1) St else St
= if (0) if ( E )MatchedStelseMatchedSt
if (1) St else St Chapter 3 Context-free Grammar
21/10/2021 27