Compsci 06/101 Spring 2012, Lab 11 Handin
By entering your name/net-id below you indicate you are present for Lab
11 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 11.
- Run the program
rsgModel.py Why does it print two
different excuses? Be brief.
- 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.
- 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)
- In class this week (Tuesday) 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.
- 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 <[^>]+>.
-
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.
- 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?
- 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.
- 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.
- Run the program
URLreader. Explain
conceptually how the call to
rsgModel.initialize works in initializing the model with a
grammar read from a URL.
- Play with grammars.