COMP 144 Midterm Exam (Spring 2003)


This is a closed book, closed notes, closed classmates exam. You have 70 minutes to complete it. When you are done, please sign here as a pledge that the exam represents your own work. Hand in this exam along with any paper on which you have written your answers. Be sure your name is written legibly on each page.


NAME (print): ___________________________________________________________


PLEDGE (sign): __________________________________________________________



  1. Consider the Perl scripting language. List each of the 6 major binding times and for each discuss properties and aspects of programs in Perl that are bound at each time. Briefly explain each item you name.



  2. (a) Write a Context-free grammar (CFG) that generates a language consisting of strings of zero or more balanced nested sets of parens, square brackets, and curly braces, with the stipulation that every close square bracket "]" is immediately followed by a "{". Note this implies no string can end in "]", and no substrings such as "](" or "]]" can appear. Clearly indicate which symbol is the root non-terminal.

    These are examples of strings that are in the language:

        e (the empty string)
        ()
        []{} 
        [()()]{[]{()}}
        {{{}}}
        []{[]{()}}
        ([[]{}]{()})
        etc...
    
    Note that these strings cannot be in the language:
        [[]]
        [()()[]]{}
        [[()()]()]()
        etc...
    
    (b) Using your grammar, draw a parse tree for this string
        ( [ ] { } ) { }
    



  3. Compare and contrast scripting languages to more traditional application development languages. Be sure to explain what each is good for, what each is poor for, when you would want to use each one.



  4. Write a Perl program that will read in all lines of a file (standard input is fine) and check each line to see if it contains a Pascal-style comment. Print each line that does contain a match, and toss each line that does not.

       (* this is a Pascal comment *)
       (* they may not be nested *) (* but two or more per line is ok *)
       it is also ok (* if they are only part *) of the line
       (* assume a comment will open and *) close on one line
       therefore (* this line would not be a match ... it has no close
       this line also is no *) match since it has no open
    
    Note that this solution can be done in 3 or 4 lines... I am not asking for a significant program, but rather a demonstration of using regular expressions in pattern matching.



  5. Does a programming language have to have a run-time stack? Are there (or have there been) any languages that do not have a runtime stack?

    What common features of programming languages can I not have if I do not have a runtime stack?



  6. We used the words "static" and "dynamic" a lot so far in the class, applied to several different concepts. Define the following terms highlighting the essential difference(s) between each pair of terms:

       (a) Static scoping and dynamic scoping 
       (b) Static memory management and dynamic memory management
       (c) Static type checking and dynamic type checking
    


  7. Consider this program:

    	x: int; /* global declaration */
    
    	procedure one (int z) {
    	   x := z*12; 
    	}
    
    	procedure two (int k) {
    	   x: int;
    	   one(k);
    	}
    
    	main {
    	   x = 5 ;
    	   two(x) ;
    	   print x ;
    	}
    
    (a) What is the output when the program is executed with static scope rules?

    (b) What output is produced when the program is executed with dynamic scope rules?