Compsci 101 Fall 2012, 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 class this week (Wednesday) we discussed 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 two list methods to add elements to the list words: both append and extend. Explain when each is used (look at the code) and why extend is used instead of append -- use concepts, don't explain the Python code so much as why the Python code is written.
    
    
    
    
    
    
    
    
    
  7. Run the program URLreader. Explain conceptually how the call to rsgModel.initialize works in initializing the model with a grammar read from a URL.
    
    
    
    
    
    
  8. Play with grammars.