180 likes | 336 Views
Parsing. Ambiguous grammars. For instance , consider the sentential form E + E * E It has two derivations from E : E -> E + E -> E + E * E E -> E * E -> E + E * E Draw the parse trees for these derivations.
E N D
Ambiguous grammars • For instance , consider the sentential form • E + E * E • It has two derivations from E : • E -> E + E • ->E + E * E • E -> E *E • ->E + E * E • Draw the parse trees for these derivations. • Example : 1 + 2 * 3, the 1st expression suggests that 1 + 2 *3 would be grouped 1 + (2 * 3) = 7 , where as 2nd derivation suggests the same expression should be grouped (1+2) * 3 = 9. Consider our previous example: the expression grammar A CFG for simple expression: • E -> I • E -> E + E • E -> E * E • E -> ( E ) • I -> a • I -> b • I -> Ia • I -> Ib • I -> I0 • I -> I1
Ambiguous grammars(p-205) • If each string has at most one parse tree in the grammar, then the grammar is unambiguous. • Draw the parse trees for the following expression : a + a * a
Leftmost derivation • A string is aa. • S -> aS|Sa|a This is an ambiguous grammar. But for S->aS|a Grammar can be ambiguous, but language not. (page 90)
Chomsky Normal Form • A context-free grammar G = (V,Σ,P,S) is in Chomsky normal form if each rule has one of the following forms: • A -> BC • A -> a • S -> λ Where B,C Є V-{S}
CNF Attributes • A -> BC • A -> a • S -> λ • The start symbol of G is non-recursive • G does not contain lambda rules other than S -> λ • G does not contain chain rules • G does not contain useless symbols (page 134)
CNF examples • A-> bDcF • Step 1: • A -> B’DC’F • B’->b • C’->c • Step 2: • A -> B’T1 • T1->DT2 • T2->C’F • B’->b • C’->c
CNF examples • Another example • S->aABC|a • A->aA|a • B->bcB|bc • C->cC|c
CNF Examples • Solutions • G’->A’T1|a • A’->a • T1->AT2 • T2->BC • A->A’A|a • B->B’T3|B’C’ • T3->C’B • C->C’C|c • B’->b • C’->c
CNF Examples • Another Example • S-> A+T|b|(A) • A-> A+T|b|(A) • T-> b|(A)
CNF Examples • Solution • S -> AY|b|LZ • A -> AR • A-> AY|b|LZ • T-> b|LZ • Y->PT • P->+ • L->( • R->)
Removal of Direct Left recursion • Consider the grammar for ba* • A->Aa|b • A ->Aa -> Aaa -> Aaaa ->baaa
Rules to remove DLR • Grammar with left recursion • A->Aa|b • Rules to change we have no business with non-recurring part (here b) • Step 1: remove the recurrence (here A) • Step 2: put a non-terminal on the right-end (here Z) • A-> b|bZ
Rules to remove DLR • Step 3: make the new non-terminal (here Z) right recursive with the right part of recurrence in the main grammar. • Step 4: put the terminal condition without recursion • Z->aZ|a • Solution is • A-> bZ|b • Z->aZ|a
Generalization of the rule • Grammar with DLR • A -> Au1|Au2| … | Auj • A-> v1|v2| … |vk • Grammar after removal of DLR • A-> v1|v2| … |vk | v1Z|v2Z| … |vkZ • Z -> u1 Z|u2 Z| … | uj Z| u1 |u2 | … | uj
Examples of Removal of DLR • Remove the DLR from the following grammar • A -> Aa|Ab|b|c • Solution • A->bZ|cZ|b|c • Z->aZ|bZ|a|b
Examples of Removal of DLR • Remove the DLR from the following grammar • A -> AB|BA|a • B->b|c • Solution • A->BAZ|aZ|BA|a • Z->BZ|B • B->b|c
Examples of Removal of DLR • Remove the DLR from the following grammar • A -> Aa|Aab|bb|b • Solution • A-> bb|b|bbZ|bZ • Z->aZ|abZ|a|ab