1 / 18

Parsing

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.

natan
Download Presentation

Parsing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Parsing

  2. 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

  3. 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

  4. 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)

  5. 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}

  6. 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)

  7. 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

  8. CNF examples • Another example • S->aABC|a • A->aA|a • B->bcB|bc • C->cC|c

  9. 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

  10. CNF Examples • Another Example • S-> A+T|b|(A) • A-> A+T|b|(A) • T-> b|(A)

  11. CNF Examples • Solution • S -> AY|b|LZ • A -> AR • A-> AY|b|LZ • T-> b|LZ • Y->PT • P->+ • L->( • R->)

  12. Removal of Direct Left recursion • Consider the grammar for ba* • A->Aa|b • A ->Aa -> Aaa -> Aaaa ->baaa

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

More Related