- Sample
- Model-theort and implementation of property grammars with features.pdf
- 00329b35761ebb8cefec388d4423481dc836486f
- Ss and os manual.pdf
- CD Labmanual
- Msc [Cs] Syllabus_2016-2017 [II Years] New
- Parsing
- Earley Cyk Review
- CCode Generation
- Javacc Faq
- MELJUN CORTES MANUAL Structured Programming Language CSCI07
- Cscc24 Midterm Notes
- CC Viva Questions
- lesson 6 rubric website roger11-02
- xmlIO
- PCD Full
- HOL Informatica DataQuality 9.1
- 10 Resources free of charge Computer system Shows
- 3000COMP AS3 Specification
- 2210_s15_qp_22
- B4A StringFunctions
- erp2005_ehp_06_fr_4b_ad468617fd11d28a360000e829fbbd_content
- ME 102-2013-14-AraSınav
- [Sigdial10]Dialogue Management Based on Entities and Constraints
- Competencias Program Ac i on Inter Colegiales
- lect10
- 123045820348502398450982304209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 e
- Lecture 3-1 Parsing
- Compiler 1
- Sinumerik 840D ShopMill
- w000784
- y000033
- w000784
- y000062
- y000031
- abszeitungWEB
- y000033
- y000033
- w000779
- k000148
- y000062
- y000031
- w000779
- w000784
- y000031
- w000793
- w000795
- w000801
- w000738
- w000672
- w000672
- w000738
- w000793
- w000672
- w000804
- w000738
- w000795
- w000793
- w000801
- w000795

Handout 17

Section Handout: LR(1) and Precedence

October 19, 2007

**Problem 1: LR(1) Configurating Sets Given the following already augmented grammar:
**

S' → S S → AB | AA | bC A → bCa | b B → Bd | ε C→c

Draw the goto graph including just those states of an LR(1) parser that are pushed on the parse stack during a parse of the input bdd (this will be a subset of all configurating sets). If you accidentally construct irrelevant states, cross them out. Do not add or change any productions in the grammar. Problem 2: Understanding Ambiguity in yacc A CS143 student tests the "classical" desk calculator written in yacc. This desk calculator reads integer NUMBERs as well as the characters '+', '-', '*', '/', '(', ')', and '\n' as tokens from the input. It uses semantic actions to compute and output the value of simple arithmetic expressions entered by the user. Unfortunately, the student made a mistake when entering the yacc description for the calculator: he simply forgets to assign a precedence to '/'. Here is the (buggy) code:

%{ #include <ctype.h> #include <stdio.h> #define YYSTYPE double %} %token NUMBER %left '+' '-' %left '*' %right UnaryMinus %% lines : lines expr '\n' | lines '\n' | /* empty */ ; expr : | | | | expr '+' expr '-' expr '*' expr '/' '(' expr expr expr expr expr ')'

// so division doesn’t truncate

{ printf("%g\n", $2); }

{ { { { {

$$ $$ $$ $$ $$

= = = = =

$1 + $3; $1 - $3; $1 * $3; $1 / $3; $2; }

} } } }

2

| '-' expr %prec UnaryMinus { $$ = -$2; } | NUMBER ; %%

Considering yacc’s rules for using precedence and associativity, and its default conflict resolution strategy of preferring shift to reduce, what does this buggy version of the calculator output for the following inputs? • • • •

3-5*2/2 12/4/2 -16/4-2 16*-4/2-3

Assume now that '/' has been given the same precedence as '*', but that all references to UnaryMinus have been removed.

%{ #include <ctype.h> #include <stdio.h> #define YYSTYPE double %} %token NUMBER %left '+' '-' %left '*' '/' %% lines : lines expr '\n' | lines '\n' | /* empty */ ; expr : | | | | | | ; expr '+' expr '-' expr '*' expr '/' '(' expr '-' expr NUMBER expr expr expr expr ')'

// so division doesn’t truncate

{ printf("%g\n", $2); }

{ { { { { { {

$$ $$ $$ $$ $$ $$ $$

= = = = = = =

$1 + $3; $1 - $3; $1 * $3; $1 / $3; $2; } -$2; } $1; }

} } } }

**%% What does the calculator output for: • •
**

10-8*-3 12*-2/8

**3 Solution 1: LR(1) Configurating Sets Given the following already augmented grammar:
**

S' → S S → AB | AA | bC A → bCa | b B → Bd | ε C→c

Draw the goto graph including just those states of an LR(1) parser that are pushed on the parse stack during a parse of the input bdd (this will be a subset of all configurating sets). If you accidentally construct irrelevant states, cross them out. Do not add or change any productions in the grammar.

S' → •S, $ S → •AB, $ S → •AA, $ S → •bC, $ A → •bCa, b/d/$ A → •b, b/d/$

A

S → A•B, $ S → A•A, $ A → •bCa, $ A → •b, $ B → •Bd, $/d B → •ε, $/d

B

S → AB•, $ B → B•d, $/d

d

B → Bd•, $/d

b

S → b•C, $ A → b•Ca, b/d/$ A → b•, b/d/$ C → •c, a/$

S

S' → S•, $

4

**Solution 2: Understanding Ambiguity in yacc What happens when precedence information for '/' is missing?
**

• 3+5*2/2

evaluates to 8

Because * has higher precedence than +, five tokens are shifted, so that the state of the stack looks like:

3+5*2

Here yacc could reduce, or it can shift in the / and reduce later. By omitting precedence information, we’re forcing yacc to resolve a shift-reduce conflict, and we know full well that it always chooses the shift. That means everything the entire expression gets shifted in before any significant reductions take place (other than the expr → number reductions, which aren’t computationally interesting here)

3+5*2/2 3+5*1 3+5 8

(reduce expr → expr / expr: replace 2 / 2 with 1) (reduce expr → expr * expr: replace 5 * 1 with 5) (reduce expr → expr + expr: replace 3 + 5 with 8) (reduce lines → expr '\n': print 8)

**So, in this case, the decisions yacc makes for us don’t interfere with the evaluation.
**

• 12/4/2

evaluates to 6

Again, no precedence or associativity information on / translates into a preference for a shift over a reduction. Everything gets shifted in before any interesting reductions take place:

12 / 4 / 2 12 / 2 6

(reduce expr → expr / expr: replace 4 / 2 with 2) (reduce expr → expr / expr: replace 12 / 2 with 6) (reduce lines → expr '\n': print 6)

•

-16/4-2

evaluates to –8 (reduce expr → expr - expr: replace 4 - 2 with 2) (reduce expr → expr / expr: replace 16 / 2 with 8) (reduce expr → - expr: replace - 8 with -8) (reduce lines → expr '\n': print -8)

- 16 / 4 - 2 - 16 / 2 -8 -8

5 Note that the negation doesn’t actually take place until the very end; at no point during the parse is there ever a yylval value of –16 anywhere on the stack. •

16*-4/2-3

evaluates to 64 (reduce expr → expr - expr: replace 2 - 3 with -1) (reduce expr → expr / expr: replace 4 / -1 with -4) (reduce expr → - expr: replace - -4 with 4) (reduce expr → expr * expr: replace 16 * 4 with 64) (reduce lines → expr '\n': print 64)

16 * - 4 / 2 - 3 16 * - 4 / -1 16 * - -4 16 * 4 64

be careful to distinguish between a lexical minus sign (which has no meaning all by itself) and a negative number (where unary minus is understood to mean negative) What happens when precedence information for unary minus is missing? Unfortunately, precedence policy is then dictated by the precedence and associativity rules of binary infix minus. •

10-8*-3

evaluates to 34 (* is shift character, and has higher precedence than – on stack, so reduction possibility is ignored) (- is shift character, but there’s no conflict, because top of parse stack isn’t reducible; no choice but shift and then shift again) (reduce expr → - expr: replace - 3 with -3) (reduce expr → expr * expr: replace 8 * -3 with -24) (reduce expr → expr - expr: replace 10 - -24 with 34) (reduce lines → expr '\n': print 34)

10 - 8 10 - 8 *

10 - 8 * - 3 10 - 8 * -3 10 - -24 34

•

12*-2/8

evaluates to -3 (no interesting reduction possibilities until the 2 has been shifted in. Now (sadly) the lookahead character of / is compared to the rightmost token of the reducible handle, and / wins, because the – token has lower precedence; we shift (and then shift again)) (reduce expr → expr / expr: replace 2 / 8 with .25) (reduce expr → - expr: replace - .25 with -.25) (reduce expr → expr * expr: replace 12 * -.25 with -3) (reduce lines → expr '\n': print -3)

12 * - 2

12 * - 2 / 8 12 * - .25 12 * -.25 -3

- SampleUploaded bykrishnaoca
- Model-theort and implementation of property grammars with features.pdfUploaded bykak_e
- 00329b35761ebb8cefec388d4423481dc836486fUploaded byaditya00012
- Ss and os manual.pdfUploaded byShiv Mangal Singh Rajawat
- CD LabmanualUploaded byNidhi Gondalia
- Msc [Cs] Syllabus_2016-2017 [II Years] NewUploaded bysania2011
- ParsingUploaded byNgô Đạt
- Earley Cyk ReviewUploaded byp_thanh
- CCode GenerationUploaded byabhishek gera
- Javacc FaqUploaded byFrederick Bejarano Sanchez
- MELJUN CORTES MANUAL Structured Programming Language CSCI07Uploaded byMELJUN CORTES, MBA,MPA
- Cscc24 Midterm NotesUploaded byDeveloperAtWork
- CC Viva QuestionsUploaded bySaraah Ghori
- lesson 6 rubric website roger11-02Uploaded byapi-239839952
- xmlIOUploaded byLeonardo Priori
- PCD FullUploaded byJeevan Kapaganty
- HOL Informatica DataQuality 9.1Uploaded byvvmex
- 10 Resources free of charge Computer system ShowsUploaded byhobbssnfvypljfl
- 3000COMP AS3 SpecificationUploaded byEddie McArdle
- 2210_s15_qp_22Uploaded byashok jaipal
- B4A StringFunctionsUploaded byLéo Júnior
- erp2005_ehp_06_fr_4b_ad468617fd11d28a360000e829fbbd_contentUploaded byadelzaker1373
- ME 102-2013-14-AraSınavUploaded byyuyuyu tututu
- [Sigdial10]Dialogue Management Based on Entities and ConstraintsUploaded bylogostream
- Competencias Program Ac i on Inter ColegialesUploaded byJonathan Vargas-Rodríguez
- lect10Uploaded by858014
- 123045820348502398450982304209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 209385082340958203948502983459823094509238549382509283405820934502398450923845823045023495 eUploaded bypetrenk
- Lecture 3-1 ParsingUploaded byAbhinav Kumar
- Compiler 1Uploaded byvasuki1964
- Sinumerik 840D ShopMillUploaded byJay Amar

- w000784Uploaded byaptureinc
- y000033Uploaded byaptureinc
- w000784Uploaded byaptureinc
- y000062Uploaded byaptureinc
- y000031Uploaded byaptureinc
- abszeitungWEBUploaded byaptureinc
- y000033Uploaded byaptureinc
- y000033Uploaded byaptureinc
- w000779Uploaded byaptureinc
- k000148Uploaded byaptureinc
- y000062Uploaded byaptureinc
- y000031Uploaded byaptureinc
- w000779Uploaded byaptureinc
- w000784Uploaded byaptureinc
- y000031Uploaded byaptureinc
- w000793Uploaded byaptureinc
- w000795Uploaded byaptureinc
- w000801Uploaded byaptureinc
- w000738Uploaded byaptureinc
- w000672Uploaded byaptureinc
- w000672Uploaded byaptureinc
- w000738Uploaded byaptureinc
- w000793Uploaded byaptureinc
- w000672Uploaded byaptureinc
- w000804Uploaded byaptureinc
- w000738Uploaded byaptureinc
- w000795Uploaded byaptureinc
- w000793Uploaded byaptureinc
- w000801Uploaded byaptureinc
- w000795Uploaded byaptureinc