DOC PREVIEW
Berkeley COMPSCI 164 - Resolving Conflicts in LALR

This preview shows page 1-2-3-4 out of 13 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Resolving Conflicts in LALR(1)Finding LookaheadsExample of LALR(1) LookaheadsLALR(1) and SLR(1)Resolving Conflicts in LALR(1)• In certain LR(0) states, multiple reductions may be possible.• Also, reduction and shift might both be possible:– Is it OK to take the shift when possible, or is there a problemwith the grammar?• Resolve by computinglookahead setsfor each item in state thatrepre sents possible reduction (dot at end).• Lookahead set consis ts of terminal symbols that could legally comenext after taking the reduction.• For item ‘Q → a•’ in state S, is set of all terminal s that can followQin an i nput that puts machine in stateS.• More precise than FOLLOW set from LL(1) parsing .Last modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14 1Finding Lookaheads• Idea: try possible reductions and see wh at shifts are the n possible.• Suppose that in state S1, two p roductions Q → a and R → a areboth possible.• Look at what will happen if you reduce with Q → a:– Tra ce backwards from S1on the a arc, and then forward on Q.– Goes to new state S2.• What s hifts a re possible from S2? Add these to thelookahead setfor Q → a in S1.• Also consider further reductions from S2.• Do for all states and reduction sequences.• If lookaheads for Q → a and R → a are di stinct, we can decidewhich production to take.Last modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14 2Example of LALR(1) LookaheadsConsider this (silly) grammar and its LR(0 ) machi ne (terminals are a, x,y, a):P → S aS → Q xS → R yQ → aR → aP →•S aLast modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14 3Example of LALR(1) LookaheadsConsider this (silly) grammar and its LR(0 ) machi ne (terminals are a, x,y, a):P → S aS → Q xS → R yQ → aR → aP →•S aS →•Q xS →•R yLast modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14 3Example of LALR(1) LookaheadsConsider this (silly) grammar and its LR(0 ) machi ne (terminals are a, x,y, a):P → S aS → Q xS → R yQ → aR → aP →•S aS →•Q xS →•R yQ →•aR →•aLast modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14 3Example of LALR(1) LookaheadsConsider this (silly) grammar and its LR(0 ) machi ne (terminals are a, x,y, a):P → S aS → Q xS → R yQ → aR → aP →•S aS →•Q xS →•R yQ →•aR →•aP → S•aSP → S a•aLast modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14 3Example of LALR(1) LookaheadsConsider this (silly) grammar and its LR(0 ) machi ne (terminals are a, x,y, a):P → S aS → Q xS → R yQ → aR → aP →•S aS →•Q xS →•R yQ →•aR →•aP → S•aSP → S a•aS → Q•xQS → Q x•xS → R•yRS → R y•yLast modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14 3Example of LALR(1) LookaheadsConsider this (silly) grammar and its LR(0 ) machi ne (terminals are a, x,y, a):P → S aS → Q xS → R yQ → aR → aP →•S aS →•Q xS →•R yQ →•aR →•aP → S•aSP → S a•aS → Q•xQS → Q x•xS → R•yRS → R y•yQ → a•R → a•aLast modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14 3Example of LALR(1) LookaheadsConsider this (silly) grammar and its LR(0 ) machi ne (terminals are a, x,y, a):P → S aS → Q xS → R yQ → aR → aP →•S aS →•Q xS →•R yQ →•aR →•aP → S•aSP → S a•aS → Q•xQS → Q x•xS → R•yRS → R y•yQ → a•R → a•aLast modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14 3Example of LALR(1) LookaheadsConsider this (silly) grammar and its LR(0 ) machi ne (terminals are a, x,y, a):P → S aS → Q xS → R yQ → aR → aP →•S aS →•Q xS →•R yQ →•aR →•aP → S•aSP → S a•aS → Q•xQS → Q x•xS → R•yRS → R y•yQ → a•, xR → a•aLast modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14 3Example of LALR(1) LookaheadsConsider this (silly) grammar and its LR(0 ) machi ne (terminals are a, x,y, a):P → S aS → Q xS → R yQ → aR → aP →•S aS →•Q xS →•R yQ →•aR →•aP → S•aSP → S a•aS → Q•xQS → Q x•xS → R•yRS → R y•yQ → a•, xR → a•aLast modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14 3Example of LALR(1) LookaheadsConsider this (silly) grammar and its LR(0 ) machi ne (terminals are a, x,y, a):P → S aS → Q xS → R yQ → aR → aP →•S aS →•Q xS →•R yQ →•aR →•aP → S•aSP → S a•aS → Q•xQS → Q x•xS → R•yRS → R y•yQ → a•, xR → a•, yaLast modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14 3LALR(1) and SLR (1)• LALR(1) lookahead sets resemble FOLLOW sets.• In pr eceding grammar, in fact, could us e FOLLOW sets to choosereduction—grammar is SLR(1).• Not always true. This grammar:S → A a | b A c | dc | bdaA → dhas no reduce/reduce conflict, but in the stateb d . cMust you shift or can you also reduce to A (meaning there is shi ft/ reduceconflict in the grammar)?• SLR(1) construction won’t tell you. LALR(1) will.Last modified: Fri Feb 18 10:58:58 2005 CS164: Lecture #14


View Full Document

Berkeley COMPSCI 164 - Resolving Conflicts in LALR

Documents in this Course
Lecture 8

Lecture 8

40 pages

Load more
Download Resolving Conflicts in LALR
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Resolving Conflicts in LALR and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Resolving Conflicts in LALR 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?