Lecture 2 Parse Tree
Lecture 2 Parse Tree
Lecture 2 Parse Tree
--Sakshi Surve
Basics of Grammar :
A language is set of strings over a set of symbols
Words
Grammar
Sentences
Example :
If we want English statement “Dog Runs” ….We may use following rules :
<Sentence> <Noun> <Verb>
<Noun> Dog
< Verb > Runs
These rules indicate how the sentence of the form ‘Noun’ followed by
‘Verb’ can be generated.
There are many such rules of the language and they are collectively called
the Grammar for the language
Constituents of Grammar :
Two Symbols :
Terminals
Non Terminals
Two Constituents :
Terminals
Non terminals
S = Sentence
V = { Noun , Verb}
T = {Dog , Runs }
P
<Sentence> <Noun> <Verb>
<Noun> Dog
< Verb > Runs
Example :
G = { S, V, P, T }
P
S ANVN
A A | An | The
N Man | Book
V Reads
Derive string “ The Man Reads Book”
Derivation …. S ANVN
A A | An | The
S ANVN N Man | Book
The NVN V Reads
The Man VN
The Man Reads Book
The Man Reads N
The Man Reads Book
Leftmost Derivation
Derivation …. S ANVN
A A | An | The
S ANVN
N Man | Book
ANV Book V Reads
AN Reads Book
A Man Reads Book The Man Reads Book
The Man Reads Book
Rightmost Derivation
∑ = { a , b }
L = { w € L | w begins with a }
L = { a , aa, ab, aab, aba, aaa, ………}
S -> aA
A -> aA | bA | €
For ‘a’
Derivation of ‘a’
S aA
S a
a A
€
S -> aA
A -> aA | bA | €
For generating ‘aa’
S
a A
Derivation of ‘aa’
S aA
a A aaA
aa
€
S -> aA Derivation of ‘aba’
A -> aA | bA | € S aA
abA
For generating ‘aba’ abaA
aba
S
a A
b A
In this grammar, the tuples are :
a A V = {S, A}
T = { a,b }
€ P
S= S
Parser :
Parser is a component of a Compiler or Interpreter that
breaks data into smaller elements
aaabbabbba
S → aB
→ ab (Using B → b)
aaabbabbba
S → aB
→ a bS (Using B → bS)
→ abbA (Using S → bA)
→ abba (Using A → a)
S → aB aaabbabbba
→ aaBB (Using B → aBB)
→ aaBbS (Using B → bS)
→ aaBbbA (Using S → bA)
→ aaBbba (Using A → a )
→ aaaBBbba (Using B → aBB)
→ aaaBbbba (Using B → b )
→ aaabSbbba (Using B → bS)
→ aaabbAbbba (Using S → bA)
→ aaabbaSbbba (Using A → aS)
1. Leftmost derivation
2. Rightmost derivation
3. Parse Tree
Solution :
1. Leftmost Derivation- 2. Rightmost Derivation-
S → bB S → bB
→ bbBB (Using B → bBB) → bbBB (Using B → bBB)
→ bbaB (Using B → a) → bbBaS (Using B → aS)
→ bbBabB (Using S → bB)
→ bbaaS (Using B → aS)
→ bbBabaS (Using B → aS)
→ bbaabB (Using S → bB) → bbBababB (Using S → bB)
→ bbaabaS (Using B → aS) → bbBababa (Using B → a)
→ bbaababB (Using S → bB) → bbaababa (Using B → a)
→ bbaababa (Using B → a)
Example 3 :
Consider the grammar-
S → A1B
A → 0A / ∈
B → 0B / 1B / ∈
For the string w = 00101, find-
Leftmost derivation
Rightmost derivation
Parse Tree
Solution-
1. Leftmost Derivation- 2. Rightmost Derivation-
S → A1B S → A1B
→ 0A1B (Using A → 0A) → A10B (Using B → 0B)
→ 00A1B (Using A → 0A) → A101B (Using B → 1B)
→ 001B (Using A → ∈) → A101 (Using B → ∈)
→ 0010B (Using B → 0B) → 0A101 (Using A → 0A)
→ 00101B (Using B → 1B) → 00A101 (Using A → 0A)
→ 00101 (Using B → ∈) → 00101 (Using A → ∈)
Parse Tree :
Example :
Let any set of production rules in a CFG be
X → X+X | X*X |X| a
over an alphabet {a}.