Document 10805459

advertisement
1 CSE 305 Introduction to Programming Languages Midterm Exam Duration 150 minutes (2.5 hours) June 27, 2013
•
Please read all instructions (including these) carefully.
•
There are 5 questions on the exam, each worth between 10 and 30 points, you
have 2.5 hours to work on the exam.
•
You may not discuss the exam with anyone who has not taken the exam until
4:00 pm Monday.
•
This exam is closed book, and you may need to use a calculator and if you don’t
have one, you can ask for help.
•
Please write all of your answers in the space of blue book, and clearly mark your
solutions. You may use the backs of the exam pages as scratch paper. If you
have difficulties in finding scratch paper or you don’t have enough space in your
blue book, you can ask for help.
•
Solutions will be graded on correctness and clarity. Each problem has a relatively
simple and straightforward solution. You may get as few as 0 points for a
question if your solution is far more complicated than necessary. Partial
solutions will be graded for partial credit.
Name (
) Person Number (
)
Problem 1 2 3 4 5 Total Max points 24 20 24 20 12 100 Actual Points This exam uses online material, and we appreciate EECS dept. of MIT and EECS dept. of U.C.Berkeley. 1 CSE 305 Introduction to Programming Languages 2 Question 1: Calculation/Number/Storage Systems (total 24pts)
EMC corporation remains the largest providers of data storage platforms in the world,
competing with IBM, NetApp, Hewlett-Packard, and Hitachi Data Systems. The company
started to ship its flagship product, the Symmetrix in 1990 and, the newest Symmetrix
VMAX 40K was launched on May12, 2012, which implements MIRROED, RAID-5 3+1,
RAID-5 7+1, RAID-6 6+2, RAID-6 14+2 architectures.
This exam uses online material, and we appreciate EECS dept. of MIT and EECS dept. of U.C.Berkeley. 2 CSE 305 Introduction to Programming Languages 3 1)
Above figure shows a small cluster, Symmetrix VMAX 40K frames, Iomega internal
hard drive. Assume Symmetrix VMAX 40K is implemented using Iomega ( now
LenovoEMC) internal hard drives, which has following parameters: 255 heads, 45600
cylinders/head, and 63 sectors/track, 4096 bytes/sector, please calculate capacity of the
hard drive in terms of TB (8pts).
2) Now assume a Symmetrix VMAX 40K system is implemented using 2400 internal hard
drives, and each hard drive has 2TB capacity. Please use Egyptian Hieroglyphics to
calculate capacity of the system in terms of TB (8pts).
3) Do a calculation 456 nine
! 567nine using one’s complement number (8pts)
This exam uses online material, and we appreciate EECS dept. of MIT and EECS dept. of U.C.Berkeley. 3 CSE 305 Introduction to Programming Languages 4 Question 2: Lexical System/Regular Expression (total 20pts)
Please refer to following Perl Regular Expression table. Notice: Escaping characters like
‘$’, ‘#’, ‘(‘, ‘)’, need to add a backslash or \ in front of them.
1) Answer following questions by writing down required patterns in Perl (8pts):
(a) Assume we want to represent any number of dollars greater than or equal to 1 dollar,
dealing with fractions with a decimal point and two digits(2pts).
(b) We need a pattern to represent processor’s speed, matching one of the following four
words: megahertz, MHz, gigahertz or GHz (2pts)
(c ) Assume we are processing source code of a C program, by reading each line and
storing the line in $sentence variables, please write down a pattern, matching all “if”
keyword (Tip: assume all “if” keywords always start with one or more space and end with
either a space or a left parenthesis) (2pts)
(d) Assume we have following string:
#!/usr/bin/perl
$string = "The time is: 12:31:02 on 6/27/13";
Please write down two patterns matching time(12:31:02) and date(6/27/13) respectively
(2pts)
This exam uses online material, and we appreciate EECS dept. of MIT and EECS dept. of U.C.Berkeley. 4 CSE 305 Introduction to Programming Languages 5 2) Many OS requires user password conform to certain rules to reduce the odds that an
attacker can guess a password. Consider following rules:
•
•
•
A password consists of upper and lower case letters and digits.
A password has at least four characters.
At least one character of a password is digit.
We define:
Letter = [A-za-z]
Digit = [0-9]
Char = Digit | Letter
Please write down the password regular expression pattern, using our definition of Letter,
Digit and Char (6pts).
3) Think about rules of Context Free Grammars (left-hand side -> right-hand side). Give a
regular expression, representing all rules of Context-Free Grammars with non-terminals
A,B, …..Z, and terminals 0,1,2,……,epslon. A production may not have alternatives on
the right-hand side(i.e. no productions have “|” on the right hand side). Productions are
terminated by a space \s. Your solution may generate rules with useless or duplicate
productions. Don’t worry about which non-terminal is the start symbol.
NonTerm = [A-Z]
Term = [0-9]
All = NonTerm | Term | ‘epslon’
Please use our definition of NonTerm, Term, and All, to write down your regular
expression(6pts).
This exam uses online material, and we appreciate EECS dept. of MIT and EECS dept. of U.C.Berkeley. 5 CSE 305 Introduction to Programming Languages 6 Question 3: Context-Free-Grammars(CFG)/Push Down Automata(PDA) /
Grammars (total 24pts)
Please refer to the following C program and C grammar
#include<stdio.h>
int add(int);
//------------------------------------void main()
{
int i,num;
int sum;
scanf("%d\n",&num);
sum=add(num);
printf("d\n",sum);
}
//--------------------------------------int add(int m)
{
int sum;
if(m==1)
return(1);
else
sum=m+add(m-1);
return(sum);
}
This exam uses online material, and we appreciate EECS dept. of MIT and EECS dept. of U.C.Berkeley. 6 BNF Grammar for C-Minus
CSE 305 Introduction to Programming Languages 1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
7 1. program o declaration-list
BNF Grammar for C-Minus
2.
declaration-list o declaration-list declaration | declaration
3. declaration o var-declaration | fun-declaration
program
o declaration-list
4. var-declaration o type-specifier ID ; | type-specifier ID [ NUM ] ;
declaration-list o declaration-list declaration | declaration
o int | void
5.
type-specifier
declaration
o var-declaration
| fun-declaration
6.
fun-declaration
o type-specifier
( params )ID
compound-stmt
var-declaration
o type-specifier
ID ; ID
| type-specifier
[ NUM ] ;
o
param-list
|
void
7.
params
int | void
type-specifier
fun-declaration
( params
8.
param-list o
o type-specifier
param-list , ID
param
| param) compound-stmt
o param-list
| void ID | type-specifier ID [ ]
params
9.
param
o type-specifier
param-list
o param-list
param | param statement-list }
o {, local-declarations
10. compound-stmt
param
o
type-specifier
ID
| type-specifier ID
[ ]
11. local-declarations o local-declarations
var-declarations
| empty
o
{
local-declarations
statement-list
compound-stmt
12. statement-list o statement-list statement | empty}
local-declarations
local-declarations
var-declarations
| empty
13. statement oo
expression-stmt
| compound-stmt
| selection-stmt
| iteration-stmt | return-stmt
statement-list o statement-list statement | empty
14. expression-stmt o expression ; | ;
statement o expression-stmt | compound-stmt | selection-stmt | iteration-stmt | return-stmt
15.
selection-stmt o if ( expression ) statement | if ( expression ) statement else statement
expression-stmt o expression ; | ;
( expression
) statement
16. iteration-stmt
selection-stmt
o ifo(while
expression
) statement
| if ( expression ) statement else statement
17.
return-stmt
o
return
;
|
return
expression
;
iteration-stmt o while ( expression ) statement
18.
expression
o
var
=
expression
|
simple-expression
return-stmt o return ; | return expression ;
[ expression
]
19. var o
expression
o ID
var| =IDexpression
| simple-expression
ID | ID [ expression
]
var
20. osimple-expression
o additive-expression
relop additive-expression | additive-expression
simple-expression
21. relop o <=o| <additive-expression
| > | >= | == | relop
!= additive-expression | additive-expression
relop
o <= | < | > | >=
== | !=
22. additive-expression
o |additive-expression
addop term | term
additive-expression
o
additive-expression
addop term | term
23. addop o + | addop
o +o| term
24. term
mulop factor | factor
term o term mulop factor | factor
25. mulop o * | /
mulop o * | /
26. factor o ( expression ) | var | call | NUM
factor o ( expression ) | var | call | NUM
27. ocall
call
IDo( ID
args( )args )
o arg-list
| empty
28. o
args
arg-list
| empty
args
29.
arg-list
o
arg-list
, expression
| expression
arg-list o arg-list , expression
| expression
Keywords:
else else
if int
void void
while
Keywords:
if return
int return
while
SpecialSpecial
symbols:
+ - *
>= >==
; ,
( ,
) (
[ )
] [
{ ]
} {
/*} */
symbols:
+ /
- <
* <=
/ <> <=
>=!=
===!=
= ;
/* */
*
*
ID = ID
letter
letter
= letter
letter
*
*
NUM = digit digit
NUM = digit digit
letter = a | .. | z | A | .. | Z
a |
| 9
.. | z | A | .. | Z
digitletter
= 0 | =..
digit = 0 | .. | 9
Comments: /* ... */
Comments: /* ... */
1) Please parse following statement using a parse-tree.
sum=add(num)
Notice: semi-colon is not required(8pts).
This exam uses online material, and we appreciate EECS dept. of MIT and EECS dept. of U.C.Berkeley. 7 8 CSE 305 Introduction to Programming Languages 2) Please show how the tree is parsed using leftmost-derivation in Push-Down Automata
(Stack) (8pts).
3) Notice in above C grammars, there are two rules:
simple-expression ! additive-expression relop additive-expression
(1)
| additive-expression
additive-expression ! additive-expression addop term | term
(2)
If we define:
simple-expression := S
additive-expression := A
relop := x
addop := y
term := B
Then we have:
S !
A
AxA | A
(Formula 1)
! AyB | B
(Formula 2)
Please write down the language generating Formula1 and Formular2, which is also the
language S stands for, in terms of A, B, x, y (8pts).
This exam uses online material, and we appreciate EECS dept. of MIT and EECS dept. of U.C.Berkeley. 8 Chris Brown
CSE 305 Introduction to Programming
Languages
November
27, 2007 9 Please write your name on the bluebook. This is a closed-book Exam. There are 75 possible
points (one per minute). Stay cool and please write neatly.
Question 4: Lambda-Calculus/Application of Lambda-Calculus (total 20pts)
1
λ Expression Evaluation (25 mins)
Notice, in Scheme function body, set! means to redefine variable.
1) Please reduce following lambda expression as much as possible(8pts).
ed:
1. Evaluate the following λ expression (Hint: six new lines).
ed:
ued:
(((λx.λy.λz.((x y) z) λf.λa.(f a)) λi.i) λj.j)
mple programs
for you to run in Scheme. Predict the result before you try
Answer:
mple programs
for
to
in
the
you
erstand
what Scheme
actually
ask forPredict
help! Don’t
wastebefore
your time
by
ample
programs
for you
you
to run
rundoes,
in Scheme.
Scheme.
Predict
theresult
result
before
youtry
try
erstand
what
Scheme
actually
does,
ask
for
help!
Don’t
waste
your
time
by
(((λx.λy.λz.((x
y)
z)
λf.λa.(f
a))
λi.i)
λj.j)
=>
ng
attention
the results.
erstand
whattoScheme
actually does, ask for help! Don’t waste your time by
ng
attention
to
the
results.
((λy.λz.((λf.λa.(f
a) y) z) λi.i) λj.j) =>
ng attention to the
results.(x)
((lambda
))
))
))
))
))
))
(λz.((λf.λa.(f a) λi.i) z) λj.j) =>
((lambda
(x)3))
(let ((a
((lambda
(x)
((λf.λa.(f
a) λi.i) λj.j) =>
(let ((a 3))
(λa.(λi.i
a)
λj.j)
(let
3)) =>
(+ x((a
a)))
(+
x a)))
(λi.i
λj.j)
=>
5) (+ x a)))
5)is the
2) What
λj.j
5) => result of following Scheme expressions? (6pts)
(a)
(define kk
(define
(define
kthe3))
(let
((a
2. Show
that
function (a) below is equivalent to the function resulting from expression (b)
(let
((a
3))
(let
((a
3))
below
by
applying
arbitrary argument <arg>. Hint: nothing but => ... => and one
(lambda
(x)each
(+ the
x a))))
a))))
(lambda (x)
(+
x
(lambda
(x)
(+
x
a))))
=> needed. Preserve functions
as just their names (in fact, sa and ss are OK and quicker
to write.)
as long as possible, do minimal rewriting down to the λ level. (Hint: (a) is a
(k
5)
(k 5) (b) is (for me) 7 lines with two λs appearing.)
one-liner,
(a) identity
(define
m
(define
m
(b)
(self-apply
(lambda
(lambda (x)
(x) (self-apply select-second))
(lambda
(x)3))
(let
((a
(let
((a
3))
((a
3))
Answer:(let
(+
x
a))))
(+
x
a))))
x a))))
(a) (identity(+<arg>)
=> ... =>
<arg>(m 5)
(m
(m 5)
5)
1
(define p
p
(c ) (define
(define
p 3))
(let ((a
(let
((a
(let
((a 3))
3))
(lambda
(x)
(lambda
(x)
(lambda
(x) x ’new)
(if (equal?
(if
(equal?
(if (set!
(equal?
x ’new)
’new)
a x
(+
a 1))
(set!
(+ aa 1))
1))
(set!
aa (+
(+
x a)))))
(+
x
a)))))
(+ x a)))))
(p 5)
(p 5)
5)
(p
(p 5)
(p 5)
5)
(p
(p ’new)
This exam uses material, and we appreciate EECS dept. of MIT and EECS dept. of U.C.Berkeley. (ponline ’new)
(p
’new)
(p 5)
(p 5)
(b)
(p 5)
9 CSE 305
Introduction
Programming
Languages Problem
1 (Whattowill
Scheme print?)
Problem 1 (What will Scheme print?)
Sample midterm 2 #3
10 Whatwill
willScheme
Schemeprint
printininresponse
responsetotothe
thefollowing
followingexpressions?
expressions?IfIfan
anexpression
expressionproduces
produces
What
3) Please
evaluate
following
list
using
Lisp(Notice
here
‘x
means:
anerror
errormessage,
message,
youmay
mayoperations
justwrite
write“error”;
“error”;
youdon’t
don’thave
haveto
toprovide
providethe
thevalue
exacttext
textofof
an
you
just
you
the
exact
is not evaluated
by the
value
assigned
to and
x, butpointer
by the symbol
x itself)
(6pts)
.
for the
value
produced by
the message.
Also,
draw
a box
diagram
theProblem
message. 1Also,
draw
box andprint?)
pointer diagram for the value produced by
(What
willaScheme
each
expression.
each expression.
(a)
What will
Scheme
print
in response
to the following expressions? If an expression produces
(append
(list
’a’b)
’b)’(c
’(c
d))
(append
(list
’a
d))
an error message, you may just write “error”; you don’t have to provide the exact text of
the message. Also, draw a box and pointer diagram for the value produced by
each expression.
(cons
(list’a
’a’b)
’b)(cons
(cons ’c
’c’d))
’d))
(cons (list
(append (list ’a ’b) ’(c d))
(b)
(list(list
(list’a
’a’b)
’b)(append
(append ’(c)
’(c) ’(d)))
’(d)))
(list
(cons (list ’a ’b) (cons ’c ’d))
(cdar’((1
’((12)
2)(3
(34)))
4)))
(cdar
(list (list ’a ’b) (append ’(c) ’(d)))
(c)
Problem
(Tree
recursion)
Problem
22(Tree
recursion)
(cdar ’((1
2) (3
4)))
Thefollowing
followingdefinition
definitioncomes
comesfrom
frompage
page116
116ofofSICP
SICP: :
The
(define(accumulate
(accumulate op
opinitial
initial sequence)
sequence)
(define
(if
sequence)
(if(null?
(null?
sequence)
Problem
2 (Tree
recursion)
initial
initial
(op
(op(car
(carsequence)
sequence)
(accumulate
(cdr
sequence)))))
(accumulate
op initial
initial
(cdr
sequence)))))
The following
definition op
comes
from page
116
of SICP :
(define (accumulate op initial sequence)
(ifproblem
(null?you’re
sequence)
InInthis
going
this
problem
you’re
goingtotoextend
extendthe
theidea
ideaofofaccumulation
accumulationfrom
fromsequences
sequencestoto“deep
“deep
initial
lists”:
ofoflists
lists”:lists
lists
listsofoflists
liststotoarbitrary
arbitrarydepth.
depth.Write
Writedeep-accumulate,
deep-accumulate,aafunction
functionofofthree
three
(op
(car
sequence)
arguments:
arguments:aatwo-argument
two-argumentfunction,
function,an
aninitial
initialvalue,
value,and
andaalist
liststructure.
structure.ItItshould
shouldwork
work
(accumulate op initial (cdr sequence)))))
like
likethis:
this:
>>(deep-accumulate
(deep-accumulate ++00’(3
’(3 (4
(4 (5)
(5) ((6)
((6) 7)
7) 8)
8) (9
(9 10)))
10)))
In
this
problem
you’re
going
to
extend
the
idea
of
accumulation
from sequences to “deep
52
52
lists”: lists of lists of lists to arbitrary depth. Write deep-accumulate, a function of three
arguments: a two-argument function, an 170
initial
170 value, and a list structure. It should work
like this:
> (deep-accumulate + 0 ’(3 (4 (5) ((6) 7) 8) (9 10)))
52
170
This exam uses online material, and we appreciate EECS dept. of MIT and EECS dept. of U.C.Berkeley. 10 CSE 305 Introduction to Programming Languages 11 Question 5: Code Generation/Miscellaneous Short Answers (total 12pts)
Please refer C program in Question 3.
1) Recall “add” function in the C program, and please rewrite the function “add” in
Scheme (3pts)
2) Convert your Scheme “add” program into MIPS assembly language (3pts)
3) Briefly sketch stack trace of your MIPS program (3pts)
4) Please briefly show in C, Scheme, Fortran programming languages, how parameterlist (or argument-list) of function calls are implemented. Please explain similarities and
difference(3pts).
This exam uses online material, and we appreciate EECS dept. of MIT and EECS dept. of U.C.Berkeley. 11 
Download