CSCE 531 Spring 2009 Final Exam Do all problems. Write your solutions on the paper provided. This test is open book, open notes, but no electronic devices. For your own sake, please read all problems before trying any of them the point value of a problem may not be commensurate with its difficulty. You need not show your work unless explicitly asked to do so, but if your answer is incorrect, you are more likely to get partial credit if you show your work. There are 100 points total in the exam. Any points earned beyond those limits counts as extra credit. 1. (10 points) Using the method described in the textbook or in class, convert the regular expression (ab c)* into an equivalent NFA (which can have -moves). Below is one possible answer. There are other correct answers. 3 a 5 6 b 8 2 1 4 c 7 2. (15 points total) A list is a finite sequence of things surrounded by parentheses. For example, (1 4 9 16) is a list of the first four perfect squares. (Items in the list are separated by whitespace if necessary.) Anything that is not a list is called an atom, e.g., numbers, symbols, strings, etc. The items of a list could be other lists, e.g., ((1 1) (1 2) (2 1) (2 2)). The empty list () with no items is also a list. 1
A generalized list is either an atom or a list whose items (if any) are themselves generalized lists. This is recursive definition, of course. So a generalized list could be a list of lists of lists.... (a) (5 points) Assuming three terminal symbols: (, ), and atom, give a grammar for a list of atoms suitable for bottom-up parsing. Express your grammar in abstract form, with start symbol A. A (L) L L atom L (b) (10 points) Assuming three terminal symbols: (, ), and atom, give a grammar for generalized lists suitable for bottom-up parsing. Express your grammar in abstract form, with start symbol B. B atom B (L) L LB L In both cases, you should assume that any whitespace is consumed by the lexical analyzer to separate items and is not itself a token. [Hint: Both answers are rather short.] 3. (20 points) Consider the following grammar with start symbol S for all strings of matching parentheses: S S S S(S) S Using the method described in class or the text, construct the set of states for a canonical LR(1) parser for this grammar, defining the transition function at the same time. Note that in class I denoted the transition function as trans. The textbook denotes the same function as goto. To ensure a unique correct answer, you must stick to the following rules of order, which mirror the order I used for my example in class: (a) Give each state as a list of LR(1) items, omitting the brackets. (b) Give the start state first, and denote it by s 0. Denote other states s 1, s 2,... in the order they are constructed. 2
(c) List the kernel items first in each state. List additional nonkernal items in the order that they enter the closure. (d) For each i 0, define all transitions out of s i before defining those out of s i+1. (e) When finding the transitions out of a state, or computing a closure, consider each item of the state in the order you listed it. (f) Do not list the empty set as a state. State Full form Short form s 0 (start state) S.S, $ S.S, $ S.S(S), $ S.S(S), $/( S., $ S., $/( S.S(S), ( S., ( s 1 = trans(s 0, S) S S., $ S S., $ S S.(S), $ S S.(S), $/( S S.(S), ( s 2 = trans(s 1, ( ) S S(.S), $ S S(.S), $/( S S(.S), ( S.S(S), )/( S.S(S), ) S., )/( S., ) S.S(S), ( S., ( s 3 = trans(s 2, S) S S(S.), $ S S(S.), $/( S S(S.), ( S.S(S), )/( S S.(S), ) S S.(S), ( s 4 = trans(s 3, ) ) S S(S)., $ S S(S)., $/( S S(S)., ( s 5 = trans(s 3, ( ) S S(.S), ) S S(.S), )/( S S(.S), ( S.S(S), )/( S.S(S), ) S., )/( S., ) S.S(S), ( S., ( s 6 = trans(s 5, S) S S(S.), ) S S(S.), )/( S S(S.), ( S S.(S), )/( S S.(S), ) S S.(S), ( s 7 = trans(s 6, ) ) S S(S)., ) S S(S)., )/( S S(S)., ( s 5 = trans(s 6, ( ) 3
4. (25 points total) Consider the following fragment of bison code for expressions with constants TRUE and FALSE, and boolean operators & (AND), (OR), and! (NOT): bool_expr: boolean_expr conjunction conjunction conjunction: conjunction & literal literal literal:! literal primary primary: TRUE FALSE ( bool_expr ) (a) (10 points) Give a parse tree with root bool expr for FALSE FALSE!(TRUE &!TRUE) bool_expr bool_expr conjunction bool_expr conjunction literal conjunction literal! literal literal primary primary primary FALSE ( bool_expr ) FALSE conjunction conjunction & literal literal! literal 4
primary primary TRUE TRUE (b) (15 points) Assuming each nonterminal has an integer attribute, with 0 representing FALSE and any nonzero value representing TRUE, add semantic actions to evaluate the truth value of a boolean expression. This evaluation takes place while the expression is parsed. Do not declare or use any variables besides $-variables (semantic stack items). bool_expr: boolean_expr conjunction { $$ = $1 $3 } conjunction conjunction: conjunction & literal { $$ = $1 && $3 } literal literal:! literal { $$ =!$2 } primary primary: TRUE { $$ = 1 } FALSE { $$ = 0 } ( bool_expr ) { $$ = $2 } 5. (10 points) Consider the following Pascal declaration: var a : array[3..7] of array[4..6] of Integer Assuming that a Pascal Integer is four bytes and that the base address of a is 1500, find the base address of the integer variable a[6][5]. The element type of the array a is itself an array of three integers, and so it has size 3 4 = 12. Variable Base Address a 1500 a[6] 1500 + (6 3) 12 = 1536 a[6][5] 1536 + (5 4) 4 = 1540 5
So the base address of a[6][5] is 1540. 6. (20 points total) Consider the following three-address code (line numbers added): 1 L1: i := a 2 L2: if i <= 10 then goto L4 3 i := i + 1 4 goto L2 5 L3: if j <= i then goto L5 6 L4: j := j + 1 7 goto L3 8 L5: a := a - 1 9 if a > 0 then goto L1 10 Assume that control enters at line 1 and that there are no other entry points. (a) (5 points) Describe the basic blocks B 1, B 2,... by giving an inclusive range of line numbers for each block. Block Line(s) B 1 1 B 2 2 B 3 3, 4 B 4 5 B 5 6, 7 B 6 8, 9 (b) (10 points) Draw the flow diagram as a directed graph, labeling the nodes B 1, B 2,.... Give dangling arrows both for the entry point and for the exit point of the code as a whole. (c) (5 points) Using the strict definition of a loop as defined in class and in the text, list sets of vertices that constitute loops. Label any inner loop(s) as such. Loops: {B 1, B 2, B 3, B 4, B 5, B 6 } {B 2, B 3 } (inner) {B 4, B 5 } (inner) 6