Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CSE, 2nd Year, 4th Semester Copyright to: Abhishek Mukhopadhyay 3/25/2014 7:47:05 PM
Introduction
If we follow the Chomsky classification of language, according the Type-II grammar,
But there is no restriction in the right side. We saw in regular grammar that certain restriction on grammatical forms; eliminating rules of the form and make the arrangement easier.
Parse Tree
Definition: Let G = (V, T, P, S) be a CFG. A tree is a derivation (or parse) tree if:
Every vertex has a label from V U T U {} The label of the root is S If a vertex with label A has children with labels X1, X2,, Xn, from left to right, then A > X1, X2,, Xn must be a production in P If a vertex has label , then that vertex is a leaf and the only child of its parent
More Generally, a derivation tree can be defined with any non-terminal as the root.
a
yield = aAab Notes:
Root can be any non-terminal Leaf nodes can be terminals or non-terminals A derivation tree with root S shows the productions used to obtain a sentential form
yield = aaAA
Observation:Every derivation corresponds to one derivation tree. S => AB => aAAB => aaAB => aaaB => aaab S A a A a A a B b
Definition: A derivation is leftmost (rightmost) if at each step in the derivation a production is applied to the leftmost (rightmost) non-terminal in the sentential form.
The first derivation above is leftmost, second is rightmost, the third is neither.
6
Observation: Every derivation tree corresponds to exactly one leftmost (and rightmost) derivation.
S A a A a A a B b
Observation: Let G be a CFG. Then there may exist a string x in L(G) that has more than 1 leftmost (or rightmost) derivation. Such a string will also have more than 1 derivation tree.
S A a A a A a B b
S
A a a A A B b
a The string has two left-most derivations, and therefore has two distinct parse trees.
8
Ambiguity in CFG
the production S > A clearly plays no role, as A cannot be transformed into a terminal string. While A can occur in a string derived from S, this can never lead to a sentence. Removing this production leaves the language unaffected and is a simplification by any definition.
Reduced Grammar
Remove -productions
Algorithm. Remove -productions from grammars for langauges without . 1. Find nonterminals that derive . 2. For each production A w construct all productions A w where w is obtained from w by removing one or more occurrences of the nonterminals from Step 1. 3. Combine the original productions with those of step 2 and eliminate any -productions.
13
Example
Remove -productions from the grammar S ABc A aA | B bB | . Solution. Step 1: The nonterminals A and B derive . Step 2: From the production S ABc we construct S Bc | Ac | c. From the production A aA we construct A a. From the production B bB we construct B b. Step 3: S ABc | Bc | Ac | c A aA | a B bB | b.
14
Problem(Remove -productions )
Remove -productions from S ABc | Ab | c A ABa | B Bbc | . Solution. S ABc | Ab | c| Bc | Ac | b A ABa | Ba | Aa | a B Bbc | bc.
15
by productions A1 ,... , Ak
Example
S 0S1 | 1S0S1 |R| R 0SR S 0S1 | 1S0S1 | 0SR | R 0SR