CSE 305 Introduction to Programming Languages 1 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 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. 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! 567 nine using one s complement number (8pts) 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) 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). 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); } 6
CSE 305 Introduction to Programming Languages 7 program declaration-list declaration-list declaration-list declaration declaration declaration var-declaration fun-declaration var-declaration type-specifier ID ; type-specifier ID [ NUM ] ; type-specifier int void fun-declaration type-specifier ID ( params ) compound-stmt params param-list void param-list param-list, param param param type-specifier ID type-specifier ID [ ] compound-stmt { local-declarations statement-list } local-declarations local-declarations var-declarations empty statement-list statement-list statement empty statement expression-stmt compound-stmt selection-stmt iteration-stmt return-stmt expression-stmt expression ; ; selection-stmt if ( expression ) statement if ( expression ) statement else statement iteration-stmt while ( expression ) statement return-stmt return ; return expression ; expression var = expression simple-expression var ID ID [ expression ] simple-expression additive-expression relop additive-expression additive-expression relop <= < > >= ==!= additive-expression additive-expression addop term term addop + - term term mulop factor factor mulop * / factor ( expression ) var call NUM call ID ( args ) args arg-list empty arg-list arg-list, expression expression Keywords: else if int return void while Special symbols: + - * / < <= > >= ==!= = ;, ( ) [ ] { } /* */ ID = letter letter * NUM = digit digit * letter = a.. z A.. Z digit = 0.. 9 Comments: /*... */ 1) Please parse following statement using a parse-tree. sum=add(num) Notice: semi-colon is not required(8pts). 7
CSE 305 Introduction to Programming Languages 8 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! AxA A (Formula 1) A! 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). 8
CSE 305 Introduction to Programming Languages 9 Question 4: Lambda-Calculus/Application of Lambda-Calculus (total 20pts) Notice, in Scheme function body, set! means to redefine variable. 1) Please reduce following lambda expression as much as possible(8pts). (((λx.λy.λz.((x y) z) λf.λa.(f a)) λi.i) λj.j) 2) What is the result of following Scheme expressions? (6pts) (a) (define k (let ((a 3)) (lambda (x) (+ x a)))) (k 5) (b) (define m (lambda (x) (let ((a 3)) (+ x a)))) (m 5) (c ) (define p (let ((a 3)) (lambda (x) (if (equal? x new) (set! a (+ a 1)) (+ x a))))) (p 5) 9
CSE 305 Introduction to Programming Languages 10 3) Please evaluate following list operations using Lisp(Notice here x means: the value is not evaluated by the value assigned to x, but by the symbol x itself) (6pts). (a) (append (list a b) (c d)) (b) (list (list a b) (append (c) (d))) (c) (cdar ((1 2) (3 4))) 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). 11