Professional Documents
Culture Documents
Syntax Analysis
Syntax Analysis
Outline
• Context free grammar
• Top down Parsing
– Unpredictive Parsing
• Backtracking
– Predictive parsing
• Recursive Desenct
• Table driven
• Bottom up Parsing
CONTEXT FREE GRAMMAR
INTRODUCTION
• A grammar is a set of rules by which the valid sentences in a
language are constructed.
6
Example.
12. → The itchy bear verb article adjective noun Rule 7
13. → The itchy bear hugs article adjective noun Rule 8
14. → The itchy bear hugs the adjective noun Rule 10
15. → The itchy bear hugs the jumpy noun Rule 9
16. → The itchy bear hugs the jumpy dog Rule 7
8
Syntax and Semantics.
10
Grammar.
• A grammar can be represented by four tuple:
G = ( Σ, N, S, P)
– Σ is a finite non-empty set called alphabets.
• The elements of Σ is called terminals and usually represented by lower-case
letters .i.e. a, b, c etc.
11
Types of Grammar.
12
Type -0 grammar.
• Type – 0 grammar is also called phrase structure grammar or un-
restricted grammar.
13
Example
• Let
Now to derive cad:
Σ = {a, c, d} 1. S → aBCc
N = {A, B, C} 2. aBCc → cadCc
S = {A} 3. cadCc → cad
A → aBCc1. S → aBCc
15
Type – 2 grammar.
• This is the most suitable grammar for computer languages and almost all
computer programming languages are CFG.
16
Type – 3 grammar.
17
Right Linear.
20
Example 1
• Let the only terminal be a.
• Let the production be:
Prod 1 S aS
Prod 2 S ε
• If we apply prod 1 six times and then apply prod 2, we generate the
following:
S aS
aaS
aaaS
aaaaS
aaaaaS
aaaaaaS
aaaaaa
• This is a derivation of a6 in this CFG.
21
Example 1
• The string an comes from n times application of prod 1 followed by one
application of prod 2.
• If we apply prod 2 with out prod 1, we find that the null string is itself in
the language of this CFG.
22
Example 2
23
Example 3
24
Example 4
∑ = {a, b}
productions:
1. S → SS
2. S → XS
3. S → YSY
4. X → aa
5. X → bb
6. Y → ab
7. Y → ba
productions:
1. S → aB
2. S → bA
3. A → a
4. A → aS
5. A → bAA
6. B → b
7. B → bS
8. B → aBB
This grammar generates the language EQUAL(The
language of strings, with number of a’s equal to number
of b’s).
Example 6
productions:
S → aSa|bSb|a|b|
S → aSb|ab
Trees
As in English language any sentence can be expressed by parse
tree, so any word generated by the given CFG can also be
expressed by the parse tree, e.g.
consider the following CFG
S AA
A AAA|bA|Ab|a
A A
Now let the left A be replaced by bA and the right one
by Ab then the tree will be
A A
b AA b
Trees continued …
A A
b AA b
a a
Trees continued …
aa aXX
bX
aXb
abX abb
bab bb
aabb
aabX aXab
abab abb
then following will be the total language tree of the above CFG
S
Note: It is to be X b
noted that the
only word in
this language aX
is b.
aaX
…
aaa …aX
RE and CFG.
38
RE and CFG.
39
Example 1
a
ε
a b b
1 2 3 4
ε
b
A1 aA1
A1 bA1
A1 aA2
A2 bA3
A2 A4
A3 bA4
A4 A2
A4 ε
41
Example 2
a,b
b a
A a
S- B+
43
Regular Grammar
Definition:
A CFG is said to be a regular grammar if it
generates the regular language i.e. a CFG is said to be
a regular grammar in which each production is one
of the two forms
Nonterminal semiword
Nonterminal word
Examples
nonterminal semiword
nonterminal word
bb
S- +
52
Types of Derivation.
• Left-most Derivation.
– The derivation in which only the left-most non-terminal in
any sentential form is expanded at each step is called left
most derivation.
• Right-most Derivation.
– The derivation in which only the right-most non-terminal
in any sentential form is expanded at each step is called left
most derivation.
53
Example.
E (E)
(E+T)
(E+F)
(E+a)
(T+a)
(F+a)
(a+a)
55
Using Left-most Derivation.
E (E)
(E+T)
(T+T)
(F+T)
(a+T)
(a+T)
(a+a)
56
Example.
57
Backus-Naur Form.
• BNF stands for Backus-Naur Form or Backus Normal Form.
58
Backus-Naur Form.
• In BNF:
– A non-terminal and terminal are usually given some
descriptive names.
59
Example.
60
Problems of a CFG.
61
Ambiguity.
62
Example.