Solution 1
Regular expressions and context-free grammars
1. Describe the languages denoted by the following regular expressions:
a. 0(0|1)*0
The language contains binary numbers that starts with a 0 and
ends with another 0.
b. (0|1)*0(0|1)(0|1)
The language contains binary numbers that ends with 000,011,010,
or 001.
c. 0*10*10*10*
The language contains binary numbers that have exactly 3 digits 1.
2. Let = {a,b}. Write regular expressions for the languages over that contain:
a. All strings beginning with ab.
ab(a|b)*
b. All strings that contain exactly two as.
b*ab*ab*
c. All strings in which every a is followed by a b.
(b|ab)+
3. Floating-point decimals consist in an integer part, a decimal part and an exponent
part. The integer part is a sequence of one or more digits. The decimal part is a
decimal point followed by zero, one or more digits. The exponent part is the
character e or E followed by an optional + or - sign, followed by one or more
digits. The decimal part or the exponent part can be omitted, but not both. Write a
regular expression to specify floating-point decimals.
digit 0|1|2|3|4|5|6|7|8|9
integer_part digit+
decimal_part . digit*
exponent_part (E|e)(+|-)?digit+
floating-point integer_part decimal_part (exponent_part)?
| integer_part exponent_part
4. Consider the following grammar
S(L)|a
LL,S|S
a. What are the terminals, nonterminals, and start symbol?
Terminals: ( ) a ,
Nonterminals: S L
Start symbol: S
b. Find parse trees for the following sentences:
i. (a, a)
ii. (a, (a,a))
iii. (a, ((a,a),(a,a))
c. Construct a leftmost derivation for each of the sentences in (b)
S (L) (L,S) (S,S) (a,S) (a,a)
S (L) (L,S) (S,S) (a,S) (a,(L)) (a,(L,S)) (a,(S,S))
(a,(a,S)) (a,(a,a))
S (L) (L,S) (S,S) (a,S) (a,(L)) (a,(L,S)) (a,(S,S))
(a,(a,S)) (a,(a,(L))) (a,(a,(L,S))) (a,(a,(S,S)))
(a,(a,(a,S))) (a,(a,(a,a)))
d. Construct a rightmost derivation for each of the sentences in (b)
S (L) (L,S) (L,a) (S,a) (a,a)
S (L) (L,S) (L,(L)) (L,(L,S)) (L,(L,a)) (L,(S,a))
(L,(a,a)) (S,(a,a)) (a,(a,a))
S (L) (L,S) (L,(L)) (L,(L,S)) (L,(L,(L,S))) (L,(L,(L,a)))
(L,(L,(S,a))) (L,(L,(a,a))) (L,(S,(a,a))) (L,(a,(a,a)))
(S,(a,(a,a))) (a,(a,(a,a)))
5. Construct a grammar for regular expressions.
R R | T | T
TTF|F
F a | | ( R) | F*