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

plan for today
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS453 : Handling Shift-Reduce Errors in JavaCUP

CS453 Shift-reduce Parsing 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

Summary of Precedence and Associativity in JavaCUP

CS453 Shift-reduce Parsing 2

If Statement code generation

By default, the result of the condition evaluation has been pushed on the RTS. When the visitor encounters ifStmt, inIF and outIF do not suffice.

  • WHY? We need more complex control:

if / | \ B S1 S2 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 labels. First, code needs to be generated for the condition, followed by branching 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

Branches and jumps

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. Notice: breq is replaced with with a brne followed by a jmp to handle this cp r24, r25 #WANT breq MJ_L6 brne MJ_L7 jmp MJ_L6 MJ_L7: ... inbounded stretch of code … MJ_L6:

CS453 Lecture Building ASTs and Visitor Design Pattern 4

slide-2
SLIDE 2

Not: there is no not in AVR, but there is xor

truth table for not and xor x y !x x xor y 0 0 1 0 0 1 1 1 1 0 0 1 1 1 0 0 We can implement NOT x with x XOR 1 :

  • utNotExp

pop r24! ldi r22, 1! eor r24,r22! push r24!

CS453 Lecture Building ASTs and Visitor Design Pattern 5

While statement

while / \ B S What is the wiring logic? SLbl:! eval B on stack! if false jump to endLbL! gen Code for S! jump to Slbl! endLbL:!

CS453 Lecture Building ASTs and Visitor Design Pattern 6

Short circuited (wired) AND, equals

Similar to the If Statement and While Statement, code generation will need to be implemented in the visitAndExp() && / \ B1 B2 can be implemented as: if (B1) return B2 else return false equalExp, the equality operator == 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 pair), making sure the int value correctly preserves the sign.

CS453 Lecture Building ASTs and Visitor Design Pattern 7

Handling Shift Reduce Errors

Examples of what they look like

– Download the AmbiguousExamples.tgz from the schedule – We will do some examples in class. Let’s look in the javacup.dump file.

Interpreting the javacup.dump file

– Recall LR parse tables and using them to parse an input. – 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 items is provided.

Getting rid of shift reduce errors using precedence and associativity

CS453 Shift-reduce Parsing 8

slide-3
SLIDE 3

Shift-reduce parsing in an LR parser

LR(k) parser

– Left-to-right parse – Right-most derivation – K-token look ahead

LR parsing algorithm using an LR parse table

– Performs a shift-reduce parse – Look at state at top of stack and input symbol to find action in table – shift(n): advance input, push state n on stack – 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 – accept: stop and success – error: stop and fail

CS453 Shift-reduce Parsing 9 CS453 Shift-reduce Parsing 10

Example JavaCUP LR Parse Table, Single Parentheses Nest

[0] S -> ( S ) [1] S -> S $ [2] S -> ID

Action Goto State ( ) $ ID S s3 s1 2 1 r2 r2 2 accept 3 s3 s1 4 4 s5 5 r0 r0

In javacup.dump look at action table and reduce table.

Example shift-reduce parse

CS453 Shift-reduce Parsing 11

[0] S -> ( S ) [1] S -> S $ [2] S -> ID

Action Goto State ( ) $ ID S s3 s1 2 1 r2 r2 2 acce pt 3 s3 s1 4 4 s5 5 r0 r0

Interpreting the javacup.dump file

Shows the whole state transition diagram, or finite state machine for push

down automata

Items

– A production from a grammar with a dot indicating position in parse. – item is (A  alpha . X beta, z) – A is nonterminal – alpha and beta are possibly empty strings of terminals and/or nonterminals where alpha is on top of the stack – X is a terminal or nonterminal – z is a terminal/token

CS453 Shift-reduce Parsing 12

slide-4
SLIDE 4

Interpreting the javacup.dump file cont …

Transitions in the lalr_state transition table in javacup.dump

– (A  alpha . X beta, z) – When X is a terminal, then will shift X and transition to another state. – 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

Back to the Expression Grammar

Show the shift-reduce errors for this and what those errors look like in

the state transitions.

How can we use precedence to fix it?

CS453 Shift-reduce Parsing 14

Back to the Dangling Else Grammar

Try various approaches for fixing this.

CS453 Shift-reduce Parsing 15

Summary on Precedence and Associativity in JavaCUP

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

  • r reduce.

– left associativity results in a reduce – right associativity results in a shift

CS453 Shift-reduce Parsing 16