CSci 150: Foundations of computer science I
Home Syllabus Assignments Tests

Exam 2 Review A: Questions

X2rA.1.
Consider the following context-free grammar.

S λ | a S b S | b S a S
Give a derivation of the string aabbab using this grammar.
X2rA.2.
Give the derivation or syntax tree for yyx+yx+z using the below context-free grammar.
Sz | A + S
Ax | y A
X2rA.3.
Consider the following context-free grammar.

S T + S | T
T F T | F
F x | y | ( S )

Draw a syntax tree for the sentence x ( x + y ).

X2rA.4.
Consider the following context-free grammar.
S V V | V
V( S )x | y | z
Each of the following sentences is either described by this grammar or not. If it is, give either a syntax tree with S at its root or a derivation from S. If it is not, simply say so.

a. x ( y )
b. x y z
c. x ( y z )

X2rA.5.

Write a context-free grammar describing each of the following languages.

a. the set of strings of a's and b's beginning and ending with the letter a
b. the set of strings containing either only a's or only b's
X2rA.6.

Write a context-free grammar describing each of the following languages.

a. the set of strings containing only a's and b's consisting entirely of repetitions of the same letter, except the final letter, which must be different. Examples include aaaab, bbbbbba, and ab.
b. the set of strings containing a's, b's, and c's where no a's appear after the first b
X2rA.7.

Write a context-free grammar describing the set of strings containing only a's and b's with at least one a and at least one b.

X2rA.8.

Write a context-free grammar that describes the strings of a's and b's containing exactly three a's. Examples in the language include aaa, abaab, and babbbabba, but not baaaab.

X2rA.9.

Write a context-free grammar describing the language containing all strings of a's and b's for which the number of a's is even. Examples include aa, abbababa, and bbbb (since 0 is an even number).

Exam 2 Review A: Solutions

X2rA.1.
SaSbS
aaSbSbS
aabSbS
aabbS
aabbaSbS
aabbabS
aabbab
[Note that it is important in a derivation to replace exactly one symbol in each step. Do not combine steps.]
X2rA.2.
S A + S
A + A + S
y A + A + S
y y A + A + S
y y x + A + S
y y x + A + z
y y x + y A + z
y y x + y z + z
X2rA.3.
X2rA.4.
a.x ( y ) It is described by the grammar.
b.x y z It is not described by the grammar
c.x ( y z ) It is described by the grammar.
X2rA.5.
a.
Sa T a
Ta T | b T | λ
b.
SA | B
Aa A | λ
Bb B | λ
X2rA.6.
a. S → a X | b Y
X → a X | b
Y → b Y | a
b. SX Y
Xa X | c X | λ
Yb Y | c Y | λ
X2rA.7.
Sa T b T | b T a T
Ta T | b T | λ
X2rA.8.

X  Y a Y a Y a Y
Y  b Y | λ

X2rA.9.

S → T a T a S | λ
T → b T | λ