Docsity

Midterm Exam for Programming Language Principles (COP-5555) - Prof. Manuel E. Bermudez, Exams of Computer Science

Partial preview of the text

Download Midterm Exam for Programming Language Principles (COP-5555) - Prof. Manuel E. Bermudez and more Exams Computer Science in PDF only on Docsity!

COP-

PROGRAMMING LANGUAGE PRINCIPLES

Instructor: Manuel Bermudez

Summer 2001

MIDTERM EXAM

In-Class, Closed Notes, 75 minutes

Problem 1 (30p.)

Problem 2 (30p.)

Problem 3 (25p.)

Problem 4 (15p.)

SCORE (100p.)

NAME

NOTE: Turn in your work on this exam only

PROBLEM 1. (30 points)

Below is the portion of the RPAL grammar that handles definitions. Write the

skeleton (i.e. pseudo-code) for a recursive descent parser for this grammar segment.

Assume a procedure exists for symbol E. Include in your parser the necessary

"BuildTree" statements to build the abstract syntax tree, in bottom-up fashion.

Definitions

D -> Da ’within’ D => ’within’

-> Da ;

Da -> Dr ( ’and’ Dr )+ => ’and’

-> Dr ;

Dr -> ’rec’ Db => ’rec’

-> Db ;

Db -> Vl ’=’ E => ’=’

-> ’’ Vb+ ’=’ E => ’function_form’ -> ’(’ D ’)’ ;

Variables

Vb -> ’’

-> ’(’ Vl ’)’ -> ’(’ ’)’ => ’()’;

Vl -> ’’ list ’,’ => ’,’?;

PROBLEM 2. (30 points)

Consider the following RPAL program.

let f(x,y)=x+y within g t = f t in g(3,2)

a) Show the Abstract Syntax tree for this program.

b) Show the (partially) standardized tree. Do not standardize any binary operators,

’tau’, or the ’,’. Standardize only ’let’, ’fcn_form’, and ’within’.

c) Flatten the standardized tree into control structures.

d) Evaluate the program using the CSE machine.

WORKSPACE

PROBLEM 4. (15 points)

Reduce the following AEs to normal form. SHOW YOUR WORK.

(a) (fn x. fn y. x y x) (fn x. fn y. x y x)

(b) (fn x. x x) (fn y. y x)

(c) (fn x. fn y. x y) (fn z. z+y) 3

Appendix: THE RPAL SPECIFICATIONS

RPAL Phrase Structure:

Rpal -> E; E -> ’let’ D ’in’ E => ’let’ -> ’fn’ Vb+ ’.’ E => ’lambda’ -> Ew; Ew -> T ’where’ Dr => ’where’ -> T;

Tuple Expressions

T -> Ta ( ’,’ Ta )+ => ’tau’ -> Ta ; Ta -> Ta ’aug’ Tc => ’aug’ -> Tc ; Tc -> B ’->’ Tc ’|’ Tc => ’->’ -> B ;

Boolean Expressions

B -> B ’or’ Bt => ’or’ -> Bt ; Bt -> Bt ’&’ Bs => ’&’ -> Bs ; Bs -> ’not’ Bp => ’not’ -> Bp ; Bp -> A Rl A => ’rl’ -> A ; Rl -> ’gr’ | ’>’ => ’gr’ -> ’ge’ | ’>=’ => ’ge’ -> ’ls’ | ’<’ => ’ls’ -> ’le’ | ’<=’ => ’le’ -> ’eq’ => ’eq’ -> ’ne’ => ’ne’;

Arithmetic Expressions

A -> A ’+’ At => ’+’ -> A ’-’ At => ’-’ -> ’+’ At -> ’-’ At => ’neg’ -> At ; At -> At ’’ Af => ’’ -> At ’/’ Af => ’/’ -> Af ; Af -> Ap ’’ Af => ’’ -> Ap ; Ap -> Ap ’@’ ’’ R => ’@’ -> R ;

Rators And Rands

R -> R Rn => ’gamma’ -> Rn ; Rn -> ’’ -> ’’ -> ’’ -> ’true’ => ’true’ -> ’false’ => ’false’ -> ’nil’ => ’nil’ -> ’(’ E ’)’ -> ’dummy’ => ’dummy’ ;

Definitions

D -> Da ’within’ D => ’within’ -> Da ; Da -> Dr ( ’and’ Dr )+ => ’and’ -> Dr ; Dr -> ’rec’ Db => ’rec’ -> Db ; Db -> Vl ’=’ E => ’=’ -> ’’ Vb+ ’=’ E => ’function_form’ -> ’(’ D ’)’ ;

Variables

Vb -> ’’ -> ’(’ Vl ’)’ -> ’(’ ’)’ => ’()’; Vl -> ’’ list ’,’ => ’,’?;

CSE Machine (11 Rules)

CONTROL STACK ENV

Initial Configuration e 0 δ 0 e 0 e 0 = PE

CSE Rule 1 (stacking a name) .... Name .... where Ob=Lookup(Name, ec )

.... Ob .... where ec = current environment

CSE Rule 2 (stacking a λ ) .... λ

x k .... .... c

x

k ....^ where^ ec = current environment

CSE Rule 3 (Applying a rator) .... γ Rator Rand ....

.... Result .... where Result=Apply[Rator,Rand]

CSE Rule 4 (Applying a λ -closure) .... γ

c

x

k Rand ....^ en = [Rand/x] ec

.... en δ k en ....

CSE Rule 5 (exit from environment) .... en value en ....

.... value ....

CSE Rule 6 (binop) .... binop Rand Rand .... .... Result .... where Result=Apply[binop,Rand,Rand]

CSE Rule 7 (unop) .... unop Rand .... .... Result .... where Result=Apply[unop,Rand]

CSE Rule 8 (Conditional) .... δ then δ else β true ....

.... δ then ....

.... δ then δ else β false ....

.... δ else ....

CSE Rule 9 (tuple formation) .... τ n V 1 ... Vn ....

.... ( V 1 ,..., Vn ) ....

CSE Rule 10 (tuple selection) .... γ ( V 1 ,..., Vn ) I ....

.... VI ....

CSE Rule 11 (n-ary function) .... γ

c

V 1 ,..., Vn

k Rand ....^ em^ =[Rand 1/ V^1 ]...[Rand n/ Vn ] ec

.... em δ k em ....