WA4-solutions

WA4-solutions - CS 143 Compilers Handout 12 Solutions to...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 143 Compilers Handout 12 Solutions to Written Assignment 4 1. Suppose that we want to add the following conditional expression to Cool. cond <p1> => <e1>; <p2> => <e2>; . .. ; <pn> => <en>; dnoc There must be at least one predicate and expression pair (that is, n 1). The evaluation of a cond expression begins with the evaluation of the predicate <p1> , which must have static type Bool . If <p1> evaluates to true , then <e1> is evaluated, and the evaluation of the cond expression is complete. If <p1> evaluates to false , then <p2> is evaluated, and this process is repeated until one of the predicates evaluates to true . The value of the cond expression is the value of the expression <ei> corresponding to the first predicate <pi> that evaluates to true . If all the predicates evaluate to false , then the value of the cond expression is void . Write operational semantics rules for this conditional expression in Cool. The first rule applies to cond expressions with a single predicate and expression pair, and handles the case that all of the predicates in a conditional expression evaluate to false . so, S 1 , E ` p : Bool ( false ) , S 2 so, S 1 , E ` cond p e ; dnoc : void, S 2 [Cond-Single-False] The other rules handle the case that the first predicate evaluates to true , and the case that the first predicate in a cond expression with multiple predicate and expression pairs evaluates to false . so, S 1 , E ` p 1 : Bool ( true ) , S 2 so, S 2 , E ` e 1 : v 1 , S 3 so, S 1 , E ` cond p 1 e 1 ; . . . ; p n e n ; dnoc : v 1 , S 3 [Cond-True] so, S 1 , E ` p 1 : Bool ( false ) , S 2 so, S 2 , E ` cond p 2 e 2 ; . . . ; p n e n ; dnoc : v, S 3 so, S 1 , E ` cond p 1 e 1 ; p 2 e 2 ; . . . ; p n e n ; dnoc : v, S 3 [Cond-Multiple-False] 2. Write a code generation function cgen(cond <p1> => <e1>; . .. ; <pn> => <en>; dnoc)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/12/2010 for the course CS 143 at Stanford.

Page1 / 6

WA4-solutions - CS 143 Compilers Handout 12 Solutions to...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online