Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...
Theory of computation Lec6
1. Lecture 6
Parsing trees and Ambiguity
Dr. Hussien Sharaf
Computer Science Department
dr.sharaf@from-masr.com
2. Parsing
CFG grammar is about categorizing
the statements of a language.
Parsing using CFG means
categorizing a certain statements
into categories defined in the CFG.
Parsing can be expressed using a
special type of graph called Trees
where no cycles exist.
A parse tree is the graph
representation of a derivation.
Dr. Hussien M. Sharaf 2
3. Parse tree
Dr. Hussien M. Sharaf 3
(1)A vertex with a label which is a
Non-terminal symbol is a parse tree.
(2) If A → y1 y2 … yn is a rule in R,
then the tree
A
y1 y2 yn. . .
is a parse tree.
4. Ambiguity
A grammar can generate the same
string in different ways.
Ambiguity occurs when a string has
two or more leftmost derivations for
the same CFG.
There are ways to eliminate
ambiguity such as using Chomsky
Normal Form (CNF) which does n’t
use Λ.
Λ cause ambiguity.
Dr. Hussien M. Sharaf 4
5. Ex 1
i.e. {Λ, ab, abbaabba,… }
S → aSa| bSb| Λ Derive abaaba
Dr. Hussien M. Sharaf 5
S
S
Λ
aa
S bb
S aa
6. Example2
Deduce CFG of a addition and parse
the following expression 2+3+5
1] S→S+S|N
2] N→1|2|3|4|5|6|7|8|9|0
Dr. Hussien M. Sharaf 6
S
S+N
S
+
N
5
S
+
3
N
2
N
Can u make
another parsing
tree ?
7. Example3
Deduce CFG of a
addition/multiplication and parse the
following expression 2+3*5
1] S→S+S|S*S|N
2] N→1|2|3|4|5|6|7|8|9|0| N
Dr. Hussien M. Sharaf 7
S
S*S
S
*
N
5
S
+
3
N
2
N
Can u make
another parsing
tree ?
8. Ex4 CFG without ambiguity
Deduce CFG of a addition/multiplication
and parse the following expression 2*3+5
1] S→ Term|Term + S
2] Term → N|N * Term
3] N→1|2|3|4|5|6|7|8|9|0
Dr. Hussien M. Sharaf 8
S
S+N
S
+
N
5
S
*
3
N
2
N
Can u make
another parsing
tree ?
9. Example 5
Dr. Hussien M. Sharaf 9
S A | A B
A Λ| a | A b | A A
B b | b c | B c | b B
Sample derivations:
S AB AAB
aAB aaB aabB aabb
S AB AbB Abb AAbb
Aabb aabb
S
A B
AA Bb
a a b
S
BA
b
A
b
AA
a a
10. Example 6
Dr. Hussien M. Sharaf 10
S A | A B
A Λ | a | A b | A A
B b | b c | B c | b B
S
A B
AA b
a
a
bA
S
A
A A
AA bA
a
a
bA
S
A B
AA Bb
a a b
11. Dr. Hussien M. Sharaf 11
Eliminate ambiguity.
Eliminate “useless” variables.
Eliminate Λ-productions: A Λ.
Eliminate unit productions: A B.
Eliminate redundant productions.
Avoid left recursion by trading left-
and right-recursion.
13. PDA
A Pushdown Automata (PDA ) is an NFA
equipped with a single stack.
Dr. Hussien M. Sharaf 13
1
4
3
2
14. PDA as a deterministic machine
Its inputs are
1. The current state (of its “NFA”),
2. The current input symbol (or
Λ), and
3. The current symbol on top of its
stack.
Dr. Hussien M. Sharaf 14
1
4
3
2
Its actions are:
1. Change state.
2. Change stack if needed.