Compsci 06/101 Spring 2011, Lab 12 Handin

By entering your name/net-id below you indicate you are present for Lab 12 to answer these questions and that you were part of the process that resulted in answers being turned in.
Name: ______________    Net id: _____________ || Name: ______________    Net id: _____________

Name: ______________    Net id: _____________ || Name: ______________    Net id: _____________

Snarf code for lab 12.
  1. Run the program rsgModel.py Why does it print two different excuses? Be brief.
    
    
    
    
    
    
  2. The line of code #print m.group() in the function initialize is commented out. Remove the # so that the line is executed when the program is run. Run the program and provide a high-level explanation of what the six-lines printed that begin with a curly-brace are.
    
    
    
    
    
    
  3. The regular expression "\s+" matches any sequence of adjacent white-space characters include space, tab, newline. What's the purpose of the code below in initialize. How did you figure this out?
       flat = re.sub(r"\s+", " ", text)
    
    
    
    
    
    
    
    
  4. In last week's lab we went over regular expressions like [^}]+ which represents one or more occurrences of any character other than a right curly brace. Explain the purpose of this regular expression in variable gram in the function initialize.
    
    
    
    
    
    
    
  5. In the function parse_rule the regular expression pattern = r"{\s*(<[^>]+>)(.*)}" is somewhat complex. This matches "{" followed by \s* which is 0 or more white-space characters, followed by <[^>]+>.

    1. Explain what this regular expression is used for, it is mat.group(1) in the program/code because it's the first regular expression surrounded by parentheses.
      
      
      
      
      
      
    2. The next part of pattern is the regular expression .* which matches anything (none or more occurrences of . which is any character). This is mat.group(2), what is its purpose in the program?
      
      
      
      
      
      
    3. Explain the purpose of the regular expression in variable partPattern at a high-level in how it's used to parse the grammar definition and append things to the list created in the dictionary. You may want to add print statements to the program to help you understand things.
      
      
      
      
      
      
      
      
  6. The function generate_from uses a nested, recursive function dowork to do the work. Explain how the variable words is like a global variable in the context of function dowork, but not global because it's used only in generate_from.
    
    
    
    
    
    
    
    
    
  7. Explain at a high-level why dowork is recursive -- that is why expanding a rule for a non-terminal is a recursive process.
    
    
    
    
    
    
    
  8. Run the program URLreader. Explain conceptually how the call to rsgModel.initialize works in initializing the model with a grammar read from a URL.