Download - Context Free Grammer
-
7/31/2019 Context Free Grammer
1/20
Context Free Grammars
1
-
7/31/2019 Context Free Grammer
2/20
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 Vn that starts all derivations Also called goal symbol
P, a finite set of productions (rewriting rules) of the
form A X1X2 Xm AVn, Xi VnVt, 1i m
A is a valid production
2
-
7/31/2019 Context Free Grammer
3/20
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
,,, denote strings in V*
u,v,w, denote strings in Vt*
3
-
7/31/2019 Context Free Grammer
4/20
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)
-
7/31/2019 Context Free Grammer
5/20
Context-Free Grammars: Concepts and
Notation (Contd)
Derivation One step derivation
If A , then A
One or more steps derivation
Zero or more steps derivation
If S , then is said to be sentential formof the CFG
SF(G) is the set of sentential forms of grammar G L(G) = {x Vt
*|S x}
L(G)=SF(G)Vt* (A Sentence)
5
-
7/31/2019 Context Free Grammer
6/20
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 lmlmlmlm
-
7/31/2019 Context Free Grammer
7/20
Context-Free Grammars: Concepts and
Notation (Contd)
Right-most derivation (canonical derivation) rm , rm , rm Buttom-up parsers
7
-
7/31/2019 Context Free Grammer
8/20
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
-
-
7/31/2019 Context Free Grammer
9/20
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)
-
7/31/2019 Context Free Grammer
10/20
Ambiguity
Grammer produces more than one tree
More than one leftmost/rightmost
derivations for the same sentence
10
-
7/31/2019 Context Free Grammer
11/20
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
Thehandle of a sentential form is the left-
most simple phrase
11
-
7/31/2019 Context Free Grammer
12/20
Context-Free Grammars: Concepts and
Notation (Contd)
Regular grammars
is of CFGs
Limited to productions of the form
A aB
C
12
-
7/31/2019 Context Free Grammer
13/20
Errors in Context-Free Grammars
CFGs are a definitional mechanism. Theymay have errors, just as programs may.
Flawed CFG
1. Useless nonterminals Unreachable
Derive no terminal string
13
SA|BAa
BBb
Cc
Nonterminal C cannot be reached form S
Nonterminal B derives no terminal string
S is the start symbol.
-
7/31/2019 Context Free Grammer
14/20
Errors in Context-Free Grammars
Another example Ambiguous:
Grammars that allow different parse trees for the sameterminal string
It is impossible to decide whether a given CFG isambiguous
14
-
7/31/2019 Context Free Grammer
15/20
Errors in Context-Free Grammars
It is impossible to decide whether a given
CFG is ambiguous
For certain grammar classes, we can prove thatconstituent grammars are unambiguous
A general comparison algorithm applicable
to all CFGs is known to be impossible
15
-
7/31/2019 Context Free Grammer
16/20
Parsers and Recognizers
Two general approaches to parsing
Top-down parser
Expanding the parse tree (via predictions) in adepth-first manner
Preorder traversal of the parse tree
Predictive in nature
lm
LL
16
-
7/31/2019 Context Free Grammer
17/20
Parsers and Recognizers (Contd)
Buttom-down parser
Beginning at its bottom (the leaves of the tree,
which are terminal symbols) and determining theproductions used to generate the leaves
Postorder traversal of the parse tree
rm
LR
17
-
7/31/2019 Context Free Grammer
18/20
Parsers and Recognizers (Contd)
18
To parsebegin SimpleStmt; SimpleStmt; end $
-
7/31/2019 Context Free Grammer
19/20
19
-
7/31/2019 Context Free Grammer
20/20
20