Vous êtes sur la page 1sur 20

Context Free Grammars

1
Context-Free Grammars: Concepts and
Notation
A context-free grammar G = (Vt, Vn, S, P)
A finite terminal vocabulary Vt
The token set produced by scanner
A finite set of nonterminal vacabulary Vn
Intermediate symbols
A start symbol S eVn that starts all derivations
Also called goal symbol
P, a finite set of productions (rewriting rules) of the
form AX
1
X
2
.X
m
AeVn, X
i
e VnVt, 1si sm
A is a valid production
2
Context-Free Grammars: Concepts and
Notation (Contd)
Other notations
Vacabulary V of G,
V= VnVt
L(G), the set of string s derivable from S
Context-free language of grammar G
Notational conventions
a,b,c, . denote symbols in Vt
A,B,C, . denote symbols in Vn
U,V,W, . denote symbols in V
o,|,,. denote strings in V
*
u,v,w, . denote strings in V
t
*
3
Example:
E E + E | E * E | (E)| -E| id
4
The String (id+id) is a sentence of the above grammer
E -E
-(E) -(E+E) -(id+E) -(id+id)
Context-Free Grammars: Concepts and
Notation (Contd)
Derivation
One step derivation
If A, then oA|o|
One or more steps derivation
+
Zero or more steps derivation
-
If S
-
|, then | is said to be sentential form
of the CFG
SF(G) is the set of sentential forms of grammar G
L(G) = {x eV
t
*
|S
+
x}
L(G)=SF(G)V
t
*
(A Sentence)
5
Context-Free Grammars: Concepts and
Notation (Contd)
Left-most derivation, a top-down parsers

lm
,
+
lm
,
-
lm
6
E -E
-(E) -(E+E) -(id+E) -(id+id)
lm
lm lm
lm lm
Context-Free Grammars: Concepts and
Notation (Contd)
Right-most derivation (canonical derivation)

rm
,
+
rm
,
rm
-
Buttom-up parsers
7
Context-Free Grammars: Concepts and
Notation (Contd)
A parse tree
rooted by the start symbol
Its leaves are grammar symbols or
8
Parse tree for (id+id)
E
E
E
E E
(
+
)
id
id
-
Two Parse trees for id+id*id
E
E + E
id E * E
id id
9
(a)
E
E * E
E + E id
id id
(b)
Ambiguity
Grammer produces more than one tree
More than one leftmost/rightmost
derivations for the same sentence
10
Context-Free Grammars: Concepts and
Notation (Contd)
A phase of a sentential form is a sequence of
symbols descended from a single nonterminal
in the parse tree
Simple or prime phrase
The handle of a sentential form is the left-
most simple phrase
11
Context-Free Grammars: Concepts and
Notation (Contd)
Regular grammars
is of CFGs
Limited to productions of the form
AaB
C
12
Errors in Context-Free Grammars
CFGs are a definitional mechanism. They
may have errors, just as programs may.
Flawed CFG
1. Useless nonterminals
Unreachable
Derive no terminal string
13
SA|B
Aa
BBb
Cc
Nonterminal C cannot be reached form S
Nonterminal B derives no terminal string
S is the start symbol.
Errors in Context-Free Grammars
Another example
Ambiguous:
Grammars that allow different parse trees for the same
terminal string
It is impossible to decide whether a given CFG is
ambiguous
14
Errors in Context-Free Grammars
It is impossible to decide whether a given
CFG is ambiguous
For certain grammar classes, we can prove that
constituent grammars are unambiguous
A general comparison algorithm applicable
to all CFGs is known to be impossible
15
Parsers and Recognizers
Two general approaches to parsing
Top-down parser
Expanding the parse tree (via predictions) in a
depth-first manner
Preorder traversal of the parse tree
Predictive in nature
lm
LL
16
Parsers and Recognizers (Contd)
Buttom-down parser
Beginning at its bottom (the leaves of the tree,
which are terminal symbols) and determining the
productions used to generate the leaves
Postorder traversal of the parse tree
rm
LR
17
Parsers and Recognizers (Contd)
18
To parse
begin SimpleStmt; SimpleStmt; end $
19
20

Vous aimerez peut-être aussi