CS 5363 Programming Languages & Compilers

Final Exam, Fall 2002

  1. Automata Theory:

    1. Use the subset algorithm to convert the following NFA into a DFA accepting the same language. (For full credit you must show the use of the subset algorithm.)

    2. By any means convert the following regular expressions to an NFA that accepts the language described by the regular expression. (The NFA can have epsilon moves if you like.)

        ( d ( a * b | c ) d ) *

  2. Formal Grammars:

    1. Consider the following grammar for Lisp S-expressions, (which you don't need to be familiar with. Here "S" is an S-expression, "T" is the tail end of an S-expression, and "A" is an atom, which here is just a lower-case letter.

        
              S  --->  A    |   '('   T
              T  --->  ')'  |   S     T
              A  --->  'a'  |   'b'   |   'c'
            

      Give the parse tree and a leftmost derivation for the sentence:

        ( ( a ) ( b c ) a)

    2. Consider the ambiguity given by the following fragment of a grammar for C:
             <stmt> ---> if ( <log-expr> ) <stmt>
             <stmt> ---> if ( <log-expr> ) <stmt> else <stmt>
            
      1. What simple rule is used in programming languages to resolve this ambiguity?
      2. How can one handle of this ambiguity with an actual parser without changing the grammar? (For example, how did you handle the ambiguity in your parser for the course, where the syntax was different but the construct was still an if-then-else with optional else?)

     

     

  3. Parsing:

    1. Write a recursive descent parser for the grammar in part II.1. above. (Preferably write code in C, C++, or Java. Assume a function getToken() is given that will return the next non-blank char (since tokens are the same as chars here). Use no other input or output. Do not write any frills -- just keep it as simple as possible.)

  4. Semantic Actions:

    1. In your program to translate assignment statements into MIPS code, various functions of the recursive descent parser (including those for expressions and terms and such) returned an integer. What did the value of this integer represent in your specific program?

    2. In our tiny language for part of the course, the while statement had the syntax:

        W ---> '{' E '?' { S } '}'

      1. Assume that E and S are translated into any kind of MIPS statements and give the rough MIPS code needed to translate this while statement. (Just the rough ideas. It makes no difference if you don't have the MIPS syntax exactly correct.)
      2. How did you keep these while statements from interfering with one another when the MIPS code is executed? (What forms achieved this at run time, and how did you arrange to output the proper forms?)

    3. Consider functions and parameters in the final programming assignment for the course. Describe the organization of the run-time stack in the particular code that you turned in for the assignment. (If you didn't do the assignment, describe how you might have done it.) This question is just asking what values are stored in each of your activation records and where they are stored. You should not give any code or description of how you handled the stack and the activation records.

  5. Run-time storage management:

    1. Explain briefly what the problem of dangling references is.

    2. Consider the implementation of malloc and free to give heap allocation in C that was discussed in class.
      1. Give details about how the collection of all free storage is represented as a data structure.
      2. When there is a call to free (at run time), how does the system know how much storage to free?

    3. Consider the mark-sweep algorithm. What is this algorithm for? What is being marked and what is being swept?

     

     

  6. LR parsing:

    1. Use the following grammar and its associated LR action and goto tables to carry out a shift-reduce parse of the given sentence.

        LR Grammar
        NumberGrammar Rule
        1E ---> E + T
        2E ---> T
        3T ---> T * F
        4T ---> F
        5F ---> ( E )
        6F ---> id
             
        LR Action and Goto Tables
        Stateaction goto
         id  +  *  (  )  $   E  T  F 
        0  s5   s4   1 2 3
        1   s6    acc   
        2   r2 s7  r2 r2   
        3   r4 r4  r4 r4   
        4  s5   s4   8 2 3
        5   r6 r6  r6 r6   
        6  s5   s4    9 3
        7  s5   s4     10
        8   s6   s11    
        9   r1 s7  r1 r1   
        10  r3 r3  r3 r3   
        11  r5 r5  r5 r5   

        Sentence:   id * ( id + id ) $

    2. In the action and goto tables above, what do blank entries mean?

    3. Give the definition of an LR(0) item.

    4. Give the definition of the closure operation for LR(0) sets of items.

    5. Illustrate this definition by finding the closure of the set of items consisting just of

        [E' ---> .E]

      using the grammar above augmented with the extra grammar rule E' ---> E