ll_grammar

ll_grammar - Arrange the nonterminals in some order A1, A2,...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
COP4020 Programming Languages Writing LL(1) grammar Prof. Xin Yuan
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 2 02/20/12 Overview Writing LL(1) grammar.
Background image of page 2
LL(1) grammar: A grammar is LL(1) iff for each set of A productions, where The following conditions hold: Ambiguous grammar are not LL(1). LL(1) parsing won’t work on certain type of grammars A grammar with left recursion cannot be LL(1): A->Ab | a A grammar with left common factor: A-> a B | a C Eliminating left recursion and left common factor is essential for developing LL(1) grammar n A α | ... | | 2 1 j i and n j 1 and n i 1 when {}, ) ( ) ( = j i First First j. i when {}, Follow(A) ) First( (b) j i when e, no, (a) the , if j = ε j i n
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Eliminating Left Recursion Immediate Left Recursion Replace A->Aa | b with A->bA’ and A’->aA’ | e Example: E->E+T | T T->T*F | F F->(E) | id In general, Can be replaced by n m A A A A β α | ... | | | | ... | | 2 1 2 1 ε | ' | ... | ' | ' ' ' | ... | ' | ' 2 1 2 1 A A A A A A A A m n
Background image of page 4
Left recursion may not be immediate A -> Ba B -> Cd | b C-> Ac | c Algorithm for eliminating all left recursions:
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Arrange the nonterminals in some order A1, A2, , An for i = 1 to n do begin for j = 1 to I-1 do begin expand production of the form Ai ->Aj w end for eliminate the immediate left recursion among Ai productions. End for (the algorithm can fail if the grammar has a cycle (A==> A), or A->e) Example 1: S->Aa | b A->Ac | Sd | e Example 2: X->YZ | a Y->ZX |Xb Z->XY | ZZ | a Left factoring (to produce a grammar suitable for predictive parsing) replace productions by m n A | ... | | | ... | | 1 2 1 n m A A A | ... | ' | ... | | ' 1 1 Example: S->iEtS | iEtSeS|a E->b Is a non ambiguous grammar LL(1) after eliminating common left factors and left recursion? When is a grammar LL(1)? COP4020 Spring 2011 8 02/20/12...
View Full Document

Page1 / 8

ll_grammar - Arrange the nonterminals in some order A1, A2,...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online