plan for today
play

Plan for Today Finish control-flow code gen from Tuesday Handling - PowerPoint PPT Presentation

Plan for Today Finish control-flow code gen from Tuesday Handling shift-reduce errors Examples of what they look like Interpreting the javacup.dump output Getting rid of shift-reduce errors CS453 : Handling Shift-Reduce Errors in


  1. Plan for Today Finish control-flow code gen from Tuesday Handling shift-reduce errors – Examples of what they look like – Interpreting the javacup.dump output – Getting rid of shift-reduce errors CS453 : Handling Shift-Reduce Errors in JavaCUP Summary of Precedence and Associativity in JavaCUP CS453 Shift-reduce Parsing 1 CS453 Shift-reduce Parsing 2 If Statement code generation Branches and jumps By default, the result of the condition evaluation has been pushed on the RTS. An AVR detail : conditional branches can only go so far in the code, and code generated, e.g for then or else block is not bounded and thus can exceed that limit. Therefore we have to use jmp sometimes. When the visitor encounters ifStmt, inIF and outIF do not suffice. Notice: breq is replaced with with a brne followed by a jmp to handle this - WHY? We need more complex control: if cp r24, r25 / | \ #WANT breq MJ_L6 B S1 S2 brne MJ_L7 jmp MJ_L6 We need to handle the if statement with a visit statement so we can control the order that code is generated for its children, using branches, jumps and MJ_L7: labels. ... inbounded stretch of code … First, code needs to be generated for the condition, followed by branching MJ_L6: instructions, the then block, control to jump over else block, then the else block, and then the end label. CS453 Lecture Building ASTs and Visitor Design Pattern 3 CS453 Lecture Building ASTs and Visitor Design Pattern 4

  2. Not: there is no not in AVR, but there is xor While statement truth table for not and xor x y !x x xor y while 0 0 1 0 / \ 0 1 1 1 B S 1 0 0 1 1 1 0 0 What is the wiring logic? We can implement NOT x with x XOR 1 : SLbl: ! outNotExp eval B on stack ! pop r24 ! if false jump to endLbL ! ldi r22, 1 ! gen Code for S ! eor r24,r22 ! jump to Slbl ! push r24 ! endLbL: ! CS453 Lecture Building ASTs and Visitor Design Pattern 5 CS453 Lecture Building ASTs and Visitor Design Pattern 6 Short circuited (wired) AND, equals Handling Shift Reduce Errors Similar to the If Statement and While Statement, code generation will Examples of what they look like need to be implemented in the visitAndExp() – Download the AmbiguousExamples.tgz from the schedule && – We will do some examples in class. Let’s look in the javacup.dump file. / \ B1 B2 Interpreting the javacup.dump file – Recall LR parse tables and using them to parse an input. can be implemented as: if (B1) return B2 else return false – The javacup.dump file contains the LR parse table at the bottom – In the middle of the javacup.dump file, a state transition diagram with equalExp, the equality operator == items is provided. Just like in plus and minus, we need to take the mixed type semantics of Java into account, by promoting a byte (1 register) to an int (register Getting rid of shift reduce errors using precedence and associativity pair), making sure the int value correctly preserves the sign. CS453 Lecture Building ASTs and Visitor Design Pattern 7 CS453 Shift-reduce Parsing 8

  3. Shift-reduce parsing in an LR parser Example JavaCUP LR Parse Table, Single Parentheses Nest [0] S -> ( S ) [1] S � -> S $ [2] S -> ID LR(k) parser Action Goto – Left-to-right parse State ( ) $ ID S – Right-most derivation – K-token look ahead 0 s3 s1 2 1 r2 r2 LR parsing algorithm using an LR parse table 2 accept – Performs a shift-reduce parse – Look at state at top of stack and input symbol to find action in table 3 s3 s1 4 – shift(n): advance input, push state n on stack 4 s5 – reduce(k): pop rhs of grammar rule k, look up state on top of stack and lhs for goto n, push lhs(k) and n onto stack 5 r0 r0 – accept: stop and success In javacup.dump look at action table and reduce table. – error: stop and fail CS453 Shift-reduce Parsing 9 CS453 Shift-reduce Parsing 10 Example shift-reduce parse Interpreting the javacup.dump file [0] S -> ( S ) Shows the whole state transition diagram, or finite state machine for push [1] S � -> S $ [2] S -> ID down automata Action Goto Items State ( ) $ ID S – A production from a grammar with a dot indicating position in parse. 0 s3 s1 2 – item is (A  alpha . X beta, z) 1 r2 r2 – A is nonterminal 2 acce – alpha and beta are possibly empty strings of terminals and/or pt nonterminals where alpha is on top of the stack 3 s3 s1 4 – X is a terminal or nonterminal – z is a terminal/token 4 s5 5 r0 r0 CS453 Shift-reduce Parsing 11 CS453 Shift-reduce Parsing 12

  4. Interpreting the javacup.dump file cont … Back to the Expression Grammar Transitions in the lalr_state transition table in javacup.dump Show the shift-reduce errors for this and what those errors look like in the state transitions. – (A  alpha . X beta, z) – When X is a terminal, then will shift X and transition to another state. How can we use precedence to fix it? – When X is a non-terminal, then will goto another state once that non- terminal has been parsed via a reduction. – An item with the parsing dot at the end of the production – ( A  gamma ., w) – Causes a reduction to the production A  gamma when the next token in the input is w. See how this works with the single nest parentheses example CS453 Shift-reduce Parsing 13 CS453 Shift-reduce Parsing 14 Back to the Dangling Else Grammar Summary on Precedence and Associativity in JavaCUP Try various approaches for fixing this. Tokens and productions are given precedence. – Tokens are given precedence level in precedence declarations. – Productions are given precedence level of last operator on right-hand side, ie. top of the Stack When in shift-reduce conflict, – shift if look ahead token has higher precedence than reduce production – reduce if reduce production has higher precedence than the look ahead token Within the same precedence level associativity determines whether to shift or reduce. – left associativity results in a reduce – right associativity results in a shift CS453 Shift-reduce Parsing 15 CS453 Shift-reduce Parsing 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend