Homework 2
10 points
Due September 29, 2004
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
Chapter 3 Describing Syntax and Semantics
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. Write a context free grammar (BNF) which generates strings of the form: an odd number of
pairs of ab followed by an a (e.g. abababa or aba).
3. Consider the grammar below where , , and are some unspecified binary operators.
E TE | ET |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?
4. For each of the languages described below, specify the language using a context free grammar
(if possible).
{anbn | n 0} which generates strings such as ab, aabb, aaabbb…
{wwr | w is a string of symbols from {a,b} and wr is the reverse of w, e.g.
abbabbabba}
{ancmbn| n,m 0}
{a2cnb2| n 0}
{ancnbn| n0}
5. In English, what language is generated by the following grammar? Be as precise as possible.
indicates the empty string.
T aTb|bTa|
6. Consider the grammar ( represents the empty string)
sentence nounPhrase verbPhrase
nounPhrase article noun
article the | those
noun girls | dogs |songbirds
verbPhrase verb nounPhrase |verb
verb see |pet
Suppose white space was completely ignored (as in FORTRAN). Thus, the sentences could be
written as thegirlsseethosedogs. Can this grammar still be parsed? If we include such verbs as
seethe and breathe, can the grammar be parsed? If an article could be , could the grammar be
parsed?
7. Here is a simple grammar for conditionals in a language. I've turned some nonterminals into
terminals for simplicity. Demonstrate the grammar is ambiguous.
1
Statement ::= Conditional
| OTHER
Conditional ::= IF TEST THEN Statement
| IF TEST THEN Statement ELSE Statement
8. In C and Java, expressions have the form
exp
sub_exp
type_name
:
|
;
:
|
|
|
|
;
:
|
;
exp '-' sub_exp
sub_exp
'(' type_name ')' sub_exp
'-' 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.
9. In English, describe what language is generated by the following grammar? Be as precise as
possible.
A+AA|-AA|a
10. In English, describe what language is generated by the following grammar? Be as precise as
possible. indicates the empty string. B BB|(B) |
11. 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?
12. Consider G= ({E,T,F},={+,*,(,),a}, P,E) where P is
E E + T |T
TT * F |F
F (E) |NUM
For this grammar, draw parse trees and abstract syntax trees (in which only the terminal symbols are
shown) for the arithmetic expressions below.
((2+4))
4*5+6*7+8
3 *(4 + 5)+(6+7)
13. Using the BNF grammar for expressions (as in the previous problem), add % for mod and ^ for
exponentiation. Recall that mod is left-associative (like division) and that power is right
associative.
14. Write a regular expression for each of the following
2
a. The set of strings that begin with a letter followed by zero or more letters/digits.
b. The set of strings that are C++ comments beginning with // and extending to the end of a line. (A
newline is specified by \n.)
c. The set of strings that consist of only a's and b's, and where the number of b's is a multiple of 3. (Note,
there may be no b’s.)
d. The set of strings that are valid double constants in C++. These can have a + or - sign at the front (or no
sign), followed by an integer (such as 42) or a decimal number (such as 20. or 10.39 or 0.39 or even .039).
After this there is an optional exponent which consists of the letter e or E, an optional + or - sign and an
integer. For example, a complete double number with an exponent could be 42.4E+12
15. Syntax diagrams show the rules in a railroad diagram. To read a diagram, trace it from left to right, in
the direction shown by the arrows. Commands and other keywords appear in UPPERCASE
inside rectangles. Parameters appear in lowercase inside ovals. Punctuation, operators,
delimiters, and terminators appear inside circles.
If the syntax diagram has more than one path, you can choose any path to travel.
<parallel_clause> :-
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) Write a syntax diagram (syntax graph) that describes Pascal variable declaration sections.
You may assume that <identifier> and <type_name> have already been defined elsewhere.
16. 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.
3
Give the equivalent grammar of the syntax diagram below using BNF.
4
© Copyright 2026 Paperzz