CSC 326, Midterm October 25, 2004 Name: Student ID: Signature: You have 75 minutes to write the 8 questions on this examination. A total of 120 marks is available. Justify all of your answers You may use one sheet of handwritten notes. Keep your answers short. If you run out of space for a question, you have written too much. The number in square brackets to the right of the question number indicates the number of marks allocated for that question. Use these to help you determine how much time you should spend on each question. Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120 Use the back of the page for rough work. Good luck UNIVERSITY REGULATIONS: No candidate shall be permitted to enter the examination room after the expiration of one half hour, or to leave during the first half hour of the examination. CAUTION: Candidates guilty of any of the following, or similar, dishonest practices shall be immediately dismissed form the examination and shall be liable to disciplinary action. 1. Making use of any books, papers or memoranda, electronic equipment, or other memory aid devices, other than those authorized by the examiners. 2. Speaking or communicating with other candidates. 3. Purposely exposing written papers to the view of other candidates. The plea of accident or forgetfulness shall not be received. 1
Language Design Issues Question 1 [12 points] [4/12] Give at least one pro and one con for implementing a language using an interpreter, and give at least one pro and one con for implementing a language using a compiler. [2/12] What does orthogonal mean with respect to a programming language? [2/12] What does data abstraction entail? (Question 1 continued on next page) 2
Language Design Issues [4/12] List, and briefly explain, the process/steps taken by a compiler to turn source code into object code and/or an executable. (End of question 1) 3
Java Question 2 [6 points] [3/6] Is Java compiled, interpreted, or a hybrid? Briefly explain. [3/6] Give an example of something in Java or C/C++ that must be checked for a program to be valid, but cannot be described by EBNF (i.e., give an example of static semantics for Java or C/C++.) (End of question 2) 4
Context-free Languages and EBNF Question 3 [14 points] Consider the following context-free grammar: E E + E id ɛ [5/14] Show a rightmost derivation of id + id + id and draw the parse tree. [5/14] Show a leftmost derivation of id + id + id and draw the parse tree. [4/14] Can you write a recursive descent parser for the grammar? if yes, do so, if no, explain why not. (End of question 3) 5
Weakest Precondition Question 4 [8 points] [4/8] Compute the weakest precondition for a = b 2 {a < 12} 3 [4/8] What does a weakest precondition tell us? (End of question 4) 6
Denotational Semantics Question 5 [5 points] Consider the following semantic function: M bin [ 0 ] = 0 M bin [ 1 ] = 1 M bin [ xxx0 ] = 2 M bin [ xxx ] M bin [ xxx1 ] = 2 M bin [ xxx ] + 1 Use M bin [ ] to calculate the decimal value of 10110. Show each step. (End of question 5) 7
Formal Languages and Lexical Analyzers Question 6 [16 points] [3/16] What formalism is used to describe the the possible textual values of the tokens returned by a lexical analyzer? [4/16] Describe (in words) the language (A) of the following regular expression: aa b(a b) [4/16] Determine if the following string are in L(A): aaa? abba? 8
Formal Languages and Lexical Analyzers [5/16] Is a n b n, n 0, a regular language? Explain why/why not. (End of question 6) 9
Question 7 [34 points] Parsing [4/34] What is the major difference between a top-down and a bottom-up parser? [4/34] Using EBNF, construct a context-free grammar that cannot be parsed using an LL parser. Consider the following grammar: [3/34] Is this grammar LL(1)? A ab aa bb B a b (Question 7 continued on next page) 10
Now consider this: S aa B ca ɛ A a B B b [6/34] Compute FIRST(S), FIRST(A), and FIRST(B). [4/34] State, using FIRST sets a necessary general condition that must be satisfied for all production X i α i,1 α i,2... α i,ni [4/34] Briefly explain the 2 major actions of a shift/reduce parser. (Question 7 continued on next page) 11
Parsing [3/34] Is the set of languages parsable by LL parsers smaller, larger, or the same as the set of languages parsable by LR parsers? [6/34] Explain the difference between synthesized, inherited, and intrinsic attributes. (End of question 7) 12
Types, Binding and Type Checking Question 8 [25 points] [4/25] Briefly explain why we do not want all variables and parameters to be static by default, i.e., why do we not want all variables and parameters to have fixed addresses assigned by the compiler? [4/25] List at least 4 different binding times and give an example (one line of code) for each. [3/25] How can dynamic type bindings help reduce the amount of lines of code in a program? (Question 8 continued on next page) 13
Types, Binding and Type Checking [3/25] Java does not have dynamic type bindings, but does have a mechanism that works almost like it. What is it? (Hint: This Object Oriented). [3/25] What is a heap dynamic variable? Give an example. [5/25] What does it mean if a language is strongly typed? (Question 8 continued on next page) 14
Types, Binding and Type Checking [3/25] Why is structured type compatibility harder to implement than name compatibility. (End of question 8) (End of the exam) 15