Informatics 42 • Winter 2012 • David G. Kay • UC Irvine
Get your work checked and signed off by a classmate, then show it to your TA in lab by Wednesday, February 22 (since Monday is a holiday).
(a) Write regular expressions to match each of the following patterns. Note that these are natural language descriptions, so they will certainly be ambiguous; disambiguate them as you see fit and note what decisions you made. In some cases you may not be able to match the described set completely; it's fine to make some simplifying assumptions.
^") matches the start of a line and the dollar sign matches the end of the line.)
Feel free to write FSAs for these languages if you'd like the practice.
(b) A grammar is a set of rules that can generate all the strings in a formal language. In the right form, a grammar for a programming language can be used with other software to produce automatically part of a compiler for that language.
Below is a grammar (in Backus-Naur Form, or BNF notation) that describes arithmetic expressions:
<expression> ::= <real> | <variable> | ( <expression> ) | <expression> <operator> <expression> | ( <variable> == <expression> ) <real> ::= <positive-real> | - <positive-real> <positive-real> ::= <integer-part> | <integer-part> . <integer-part> <integer-part> ::= <digit> | <digit> <integer-part> <variable> ::= <letter> <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 <letter> ::= a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z <operator> ::= + | - | * | / | %(b.1) Some of the following expressions can be generated by this grammar; others can not. Indicate which are the valid expressions. (The easiest way to do this might be to photocopy the page, or print it from the on-line version, and circle the valid expressions.)
(b.2) Using the grammar, generate four more expressions that aren't on the above list. Each expression should involve applying at least ten rules. For each expression, show its derivation tree (with <expression> at the root and terminal symbols—i.e., without angle brackets—at the leaves).
(b.3) Give three arithmetic expressions that are syntactically valid in Python but are not generated by this grammar.
(b.4) Modify the grammar to allow multi-letter variable names. This requires changing only one of the existing rules.
(c) Read the problem description for Lab D, the Random Sentence Generator.
(d) For Labs E and F, we'll use an already existing program that simulates visitors to an amusement park. Read the problem description for the amusement park simulator, noting (a) that it's not essential that you memorize every detail and (b) that your task won't be to build this from scratch, but to enhance it in various ways (though for that, you'll need to become familiar with the existing code that we will supply.
Written by David G. Kay, Winter 2005, and modified Winter 2006; regular expression exercise written by David G. Kay, Winter 2001. BNF grammar exercise written by David G. Kay, Spring 1999. Reorganized by David G. Kay, Winter 2012.