Docsity

Exam 1 | Programming Language Concepts - Fall 2008 | COMP 524, Exams of Programming Languages

Partial preview of the text

Download Exam 1 | Programming Language Concepts - Fall 2008 | COMP 524 and more Exams Programming Languages in PDF only on Docsity!

COMP 524:

Programming Language Concepts

Exam 1

Thursday, February 21

st

Pledge: I have neither given nor received unauthorized aid on this exam. Signature: __________________________ Name: __________________________ Begin: 3:30pm End: 5:00pm (Total: 100 points + 10 points bonus)

1. (10 points) Consider a Python dictionary that holds a mathematical term of the form

c x

p

, where c is the constant (a number), x is the symbol (a string), and p is the

power (a number). The components are stored in a Python dictionary with key values 'c' , 'x' , and 'p' , respectively. Write a function named power_rule that accepts a term as the argument and returns the derivative of the term. Recall that the derivative

of c x

p

is p c x

p-

, unless p is 0, in which case the derivative is 0 x

p .

2. (10 points) Consider a list of integers in the form {1,02,3,0,7,21}. The list can be of arbitrary length (having anywhere from zero to infinite integers), and each integer is 1 or more digits. Write a regular expression that matches this form of list. Use the regular expression syntax we learned in class, not the Python syntax. (That is, only use

concatenation, alternation, parentheses for grouping, and the Kleene * and +.) You may

use as a predefined class meaning 0|1|2|3|4|5|6|7|8|.

4. (15 points) Consider the following grammar for balanced braces:

p := {}p | {p} | p{} | ε

Remove left recursion and common prefixes to create an LL(1) grammar expressing the

same language. Do not use Kleene operators (* or +) or grouping parentheses in your

grammar. (Hint: think carefully before applying the algorithm to eliminate left recursion.) 5a) (6 points) Consider a procedure, (map f items) , which applies a function f to every item in a list items. For example, (map square (list 1 2 3)) returns ( 4 9) , assuming (square x) does what you expect. Write out the recursive process (not the procedure) for (map square (list 1 2 3)).

5b) (3 points) What is the base case for recursion? 5c) (6 points) Write out the procedure in Scheme. 5d) (5 points bonus) Write out the iterative process for (map square (list 1 2 3)). 5e) (5 points bonus) Write out the iterative procedure. Call it map-iter. You may assume the existence of an (append-item list item) function to append an item to the end of a list.

6d) (2 points) What is different about the namespace stack at the same point in the program in (b)? You may use words or draw another diagram to explain. (Hint: when using dynamic scope, closures do not need to maintain a link to the defining environment.) 7a) (5 points) I have a Python program called sort , and I want to run it on an x machine. Draw a Tombstone diagram to show how this is done. 7b) (5 points) I have a Python compiler that translates from Python code to x86 code. The compiler is expressed in x86 code. Show how to compile the sort program to x using Tombstone diagrams.