1 / 10

Fixing non- ll (1) grammars

Fixing non- ll (1) grammars. Module 07.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez. Problems with model grammar Fixing common prefices Fixing left-recursion New grammar: LL(1). Topics. S → begin SL end {begin} → id := E; {id} SL → SL S {begin,id}

gcarlos
Télécharger la présentation

Fixing non- ll (1) grammars

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. Fixing non-ll(1) grammars Module 07.2COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

  2. Problems with model grammar Fixing common prefices Fixing left-recursion New grammar: LL(1) Topics

  3. S →begin SL end {begin} → id := E; {id} SL → SL S {begin,id} →S {begin,id} E → E+T {(, id} → T {(, id} T → P*T {(, id} → P {(, id} P → (E) {(} → id {id} Model grammar Problems: • SL is left recursive. • E is left recursive. • T → P * T have common → P prefices. Showing a grammar is not LL(1): easy. PL grammars: “mostly” LL(1). This is our “model” PL grammar. We’ll use it throughout.

  4. Change: T → P * T { (, id } → P { (, id } to: T → P X { (, id } X →* T { * } → { +, ; , ) } Follow(X) ⊇ Follow(T) due to T → P X ⊇ Follow(E)due to E → E+T , E → T = { +, ;, ) } due to E →E+T, S→id := E ; and P → (E) Fixing common prefices

  5. In general, change A → 1 → 2 . . . → n to A → X X →1 . . . →n Fixing common prefices • Hopefully all the’sbegin with different symbols. If not, repeat !

  6. We have (…((( T + T) + T) + T)…) Instead, we want (T) (+T) (+T) … (+T) Change: E → E + T { (, id } → T { (, id } To: E → T Y { (, id } Y → + T Y { + } → { ; , ) } Follow(Y)  Follow(E) = { ; , ) } Fixing left recursion of E Yikes: destroyed the left associatvity of ‘+’ ! Will fix this later. No longer contains ‘+’: we eliminated E → E + T

  7. In general, Change:A → A1 A →  1 . . . . . . → An → m to:A → 1 X X → 1X . . . . . . →m X→nX → Fixing left recursion • The ’s don’t begin with A.

  8. We have (…(((S)S)S)…) Instead, we want (S)(S) …(S) Change:SL → SL S { begin, id } → S { begin, id } To:SL → S Z { begin, id } Z →S Z { begin, id } → { end } Fixing left recursion of SL Destroyed the left associatvity of ‘ ’. Fixable, but won’t matter.

  9. Modified grammar S → begin SL end {begin} → id := E ; {id} SL → S Z {begin,id} Z →S Z {begin,id} → {end} E → T Y {(,id} Y → + T Y {+} → {;,)} T → P X {(,id} X → * T {*} → {;,+,)} P → (E) {(} → id {id} Grammar is LL(1) !

  10. summary • Problems with model grammar • Fixing common prefices • Fixing left-recursion • New grammar is LL(1)

More Related