Context sensitive examples:

advertisement
Homework 3
10 points
Note, these exercises may be done in groups of one, two, or three. Working with someone else is strongly
recommended. If more than one person is involved, list all the names on ONE set of answers. Groups may change
throughout the term. Working in groups is a BIG plus for you. Take advantage of it. If you work in groups, you must
work in the group for the ENTIRE assignment. It is considered cheating if you work with someone else for some of
the answers, but turn in an individual copy of the answers. It is an all or nothing situation. You can't work together
on some questions and alone on some. Sometimes I see an individual whose name is listed in two groups. This is
strictly forbidden and is considered cheating. You cannot work in two groups. Assignments are due at the beginning
of class and should be typed.
For this assignment, use the following notation for regular expressions
o
o
o
o
o
o
a|b (or)
ab (and - concatenation)
(a) (grouping)
a* (kleene closure - zero or more repetitions)
a+ (one or more repetitions)
a? (optional)
Some extensions to make writing easier (PERL)
 [abc] one of a or b or c
 [a-z] anything from a to z inclusive
 [a-zA-Z] anything from a to z or A to Z inclusive
 [^abc] anything but a, b, or c
 . Any character except newline
Other pattern matching capabilities are NOT part of regular expressions and should not be
used.
Chapter 4 Syntax
1. Write the following regular expressions

Write the regular expression which defines a language in which the total number of b's is
divisible by two, such as aabaabbaba
 Write the regular expression which defines a language in which three b's never occur
together.
Note that you must show that every string that fits the description can be generated and
nothing that does not fit the description is generated.
0
2. Consider the grammar below where , , and are some unspecified binary operators.
E TE | ET |T
T F T |F
F (E) |a
What kind of associativity does  have?
What kind of associativity does  have?
What kind of associativity does  have?
What operation has higher precedence or are they equal?
3. In English, what language is generated by the following grammar? Be as precise as possible.
 indicates the empty string.
T aTb|bTa|
4. Here is a simple grammar for conditionals in a language. I've turned some nonterminals into
terminals for simplicity. Demonstrate the grammar is ambiguous.
Notice that ::== is used in place of the → . The meaning is the same
Statement ::= Conditional
| OTHER
Conditional ::= IF TEST THEN Statement
| IF TEST THEN Statement ELSE Statement
5. In C and Java, expressions have the form
exp
→ exp '-' sub_exp
| sub_exp
sub_exp
→ '(' type_name ')' sub_exp
|
|
|
|
type_name
'-' sub_exp
id
literal
'(' exp ')'
→ id
| more_complex_type_descriptions
This allows expressions like: 1, a, a - 1, ( a ), ( a - 1 ), - a, ( int ) a . Demonstrate
that the grammar is ambiguous.
6. In English, describe what language is generated by the following grammar? Be as precise as
possible.  indicates the empty string. B BB|(B) | 
7. A nested comment is a comment within a comment. Many programming languages do not
allow nested comments. Given what you know about grammars, why is this restriction made?
1
8. Consider the grammar G represented by the four tuple = ({E,T,F},={+,*,(,),a}, P,E) where
P is the set of productions
E E + T |T
TT * F |F
F (E) |NUM
For this grammar, draw a parse tree and an abstract syntax tree (in which only the terminal
symbols are shown) for 3 *(4 + 5)+(6+7)
9. Using the BNF grammar for expressions (as in the previous problem), add operators % for
mod and ^ for exponentiation. Recall that mod is left-associative (like division) and that
power is right associative. Test out your grammar to verify that it works.
10. Syntax diagrams show the rules in a railroad diagram. A variable declaration section in Pascal has
the following form:
var
<identifier>, <identifier>,...,<identifier>:<type_name>;
<identifier>, <identifier>,...,<identifier>:<type_name>;
...
<identifier>, <identifier>,...,<identifier>:<type_name>;
For example:
var
x:
y,
a,
n:
integer;
z: real;
b, c, d, e, f, g: char;
integer;
a) Write a set of Extended Backus-Naur Form (EBNF) grammar rules that describe Pascal
variable declaration sections. You may assume that <identifier> and <type_name> have already
been defined elsewhere.
b) Draw a syntax diagram that describes Pascal variable declaration sections. You may
assume that <identifier> and <type_name> have already been defined elsewhere.
11. Consider the syntax diagram below in which ovals represent terminals and rectangles
represent non-terminals. Note, the items in rectangles will be referenced in your answer, but
not defined. Give the equivalent grammar of the syntax diagram below using BNF.
2
12. Given the regular expression (a|b|c*)bb use Thompson’s construction to produce a
NFA.
13. For the NFA produced in the previous problem, use the subset construction
(discussed in class) to create a DFA.
3
Download