Organization of Programming Languages Answers to On-line Quiz #1 January 27, 1997 Rewrite the following conditionals as sequential choices by using the else-if form instead. 1. (10 pts) if c >= 5 then if c > 5 then S1 else S2 if c<5 then S2 elseif c > 5 then S1 2. (10 pts) if cnt > min then if cnt < max then S1 else S2 end end if cnt> min and cnt< max then S1 elseif cnt > min then S2 3. (10 pts) For each of the following paradigms, give the names of 3 popular programming languages a) imperative: C, PASCAL, Modula b) functional: Lisp, Scheme, ML c) object-oriented: C++, Smalltalk, Java 4. (10 pts) What are the three major phases of compilation? Which of these phases is/are not part of interpretation? 1) Lexical analysis 2) Syntatic analysis 3) Code generation The last phase is not part of interpretation. 5. (25 pts) Given the following abstract-syntax-tree, write the corresponding notations a) prefix: mod * a b + c * d e b) postfix: a b * c d e * + mod c) infix: (a * b) mod (c + (d * e)) mod / \ * + / \ / \ a b c * / \ d e 6. (20 pts) Give a grammar that generates all binary numbers, that is, strings of 0s and 1s. Numbers should have no leading 0s except the number 0. Then give a derivation for 1011. S -> 1L | 0 L -> 0L | 1L | S -> 1L -> 10L -> 101L -> 1011L -> 1011 7. 10pts a. Give the grammar rules for conditional statements in a language like C or Pascal. Is your grammar ambiguous, yes or no? S -> if E then S S -> if E then S else S Yes, this is ambiguous. We can not always identify for each else the matching if. b. Give the rules where the conditionals end with the word endif. is this grammar ambiguous, yes or no? S -> if E then S endif S -> if E then S else S endif No, this is not ambiguous. We can always identify for each else the matching if. %%%%%%%%%%%End of Quiz%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%