ll_grammar

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

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

COP4020 Programming Languages Writing LL(1) grammar Prof. Xin Yuan

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

View Full Document
COP4020 Spring 2011 2 02/20/12 Overview Writing LL(1) grammar.
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

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

View Full Document
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
Left recursion may not be immediate A -> Ba B -> Cd | b C-> Ac | c Algorithm for eliminating all left recursions:

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

View Full Document

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

View Full Document
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

## This note was uploaded on 02/20/2012 for the course COP 4020 taught by Professor Engelen during the Spring '11 term at FSU.

### 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
Ask a homework question - tutors are online