Exercises of Chapter 2: A Simple One-Pass Compiler
2.1 Consider the context-free grammar
SSS+|SS*|a
a) Show how the string aa+a* can be generated by this grammar.
S => S S * => S S + S * => a S + S * => a a + S * => a a + a *
b) Construct a parse tree for this string.
S
S
S
S
S
a
a
+
*
a
c) What language is generated by this grammar? Justify your answer.
The language of this grammar is:
Arithmetic expression in postfix notation.
Arithmetic expression of a’s with two binary operators + , *.
L = { a, aa+, aa*, aa+a+, aa*a*, … }
2.2 What language is generated by the following grammars? In each case justify your
answer.
a) S 0 S 1 | 0 1
The language generated by this grammar is:
Expression in binary numbers.
Number of (0) equal to number of (1).
Expression contains 1’s in the right hand side & 0’s in the left hand side.
Expression begin with (0) & end with (1).
Expression contain at least one (0).
Expression contain at least one (1).
L = { 01, 0011, 000111, 00001111, …}
= { 0^n 1^n, n > 0 }
b) S + S S | - S S | a
The language generated by this grammar is:
Expression in prefix notation.
Expression contains the two binary operators + , -.
Expression contains at least one a.
Expression contains number of (a) more than number of binary operator by 1.
L = { a, +aa, -aa, ++aaa, --aaa, +-aaa, -+aaa, …}
= { (+U-)^n a^(n+1), n >=0 }
c) S S (S) S | ε
The language generated by this grammar is:
Empty string.
1
L = { ε , ( ) , ( ( ) ) , ( )( ) , ( )( )( ) , … }
d) S aSbS | bSaS | ε
The language generated by this grammar is:
Number of (a) equal to number of (b).
Expression contains at least one (a) & one (b).
L = { ε , ab, ba, abab, baba, ababab, …}
e) S a | S+S | SS | S* | (S)
The language generated by this grammar is:
Expression contains at least one a.
Expression contains the two binary operators + , *.
Expression begin with a or ( .
L = { a, a+a, aa, a*, (a), ((a)), a+a*, aa+a, … }
2.3 Which of the grammars in Exercise 2.2 are ambiguous?
a) unambiguous.
b) unambiguous.
c) Suppose the sentence ( )( ):
S
S
S
ε
S
( S )
S
( S ) S ε
ε
S ( S
ε
ε
)
ε
ε
S
S
( S )
S
ε
ε
ε
We have two parse trees => the grammar is ambiguous.
d) Suppose the sentence abab:
S
a
b
S
S
ε
S
b
a
S
S ε
a
S
b
S
ε
a
S
ε
ε
b
S
ε
We have two parse trees => the grammar is ambiguous.
e) Suppose the sentence aa* :
S
S
2
S
a
S
S
*
a
S
S
a
*
S
a
We have two parse trees => the grammar is ambiguous.
2.4 Construct unambiguous context-free grammars for each of the following languages.
In each case show that your grammar is correct.
a) Arithmetic expression in postfix notation.
S SS+ | SS- | SS/ | SS* | a
L = { a, aa+, aa - , aa /, aa*, aaa+* , … }
b) Left-associative lists of identifiers separated by commas.
L L , id | id
id a | b | … | z | A | B | … | Z
L = { a a,b a,b,c A,b,Z … }
c) Right-associative lists of identifiers separated by commas.
R id , R | id
id a | b | … | z | A | B | … | Z
L = { a a,b a,b,c A,b,D … }
d) Arithmetic expressions of integers & identifiers with the four binary operands.
Expr Expr + Term | Expr –Term | Term
Term Term * Factor | Term / Factor | Factor
Factor id | int | (Expr)
id Letters | Letters digits | ε
int digits
Letters Letter | Letter Letters | ε
Letter a | b | … | z | A | B | … |Z
digits digit | digit digits | ε
digit 0 | 1 | … | 9
e) Add unary plus & minus to the arithmetic operators of (d).
Expr Expr + Term | Expr –Term | Term
Term Term * Factor | Term / Factor | Factor
Factor id | int | (Expr) | + Expr | - Expr
id Letters | Letters digits | ε
int digits
Letters Letter | Letter Letters | ε
Letter a | b | … | z | A | B | … |Z
digits digit | digit digits | ε
digit 0 | 1 | … | 9
2.5
a) Show that all binary strings generated by the following grammar have values
3
divisible by 3. Hint. Use induction on the number of nodes in a parse tree.
num 11 | 1001 | num 0 | num num
The strings generated by this grammar are:
L = { 11, 1001, 110, 10010, 1111, 10011001,111001, … }
All these numbers divisible by 3
num
num
num
11
1001
num
num
num 0
num 0
num
0
11
1001
num
num
11
num
num
11
num
0
num
num
num
num
1001
1001
11 1001
0
num
b) Does the grammar generate all binary strings with values divisible by 3?
The grammar doesn’t generate all binary strings with values divisible by 3
because
0 doesn’t appear here.
2.7 Construct a syntax-directed translation scheme that translates arithmetic
expressions
from infix notation into prefix notation in which an operator appears before its
operands; e.g., -xy is the prefix notation for x-y. Give annotated parse trees for
the
inputs 9-5+2 & 9-5*2.
expr { print(‘ + ’) } expr + term
| { print(‘ - ‘) } expr – term | term
term { print(‘ * ’) } expr * term
| { print(‘ / ’) } expr / term | factor
factor { print(‘ 0 ‘) } 0
factor { print(‘ 1 ‘) } 1
…
…
…
factor { print(‘ 9 ‘) } 9
Annotated parse tree for 9-5+2 +-952
expr
{ print (‘+’) }
expr
+
term
4
{ print (‘-’) }
expr
-
{ print (‘2’)}
term
term
factor
factor
{ print(‘5’) }
{ print(‘9’) }
2
5
9
Annotated parse tree 9-5*2 -9*52
expr
{ print (‘-’) }
expr
term
-
term
{ print (‘*’)} term *
factor
{ print(‘9’) }
factor
factor { print(‘2’) } 2
9
{ print(‘5’) }
5
2.8 Construct a syntax-directed translation scheme that translates arithmetic
expressions
from postfix notation into infix notation. Give annotated parse trees for the inputs
95-2* & 952*-.
expr expr { print(‘ + ’) } term +
| expr { print(‘ – ‘) } term | term
term term { print(‘ * ’) } factor *
| term { print(‘ / ‘) } factor /
| factor
factor 0 { print(‘ 0 ‘) }
factor 1 { print(‘ 1 ‘) }
…
…
…
factor 9 { print(‘ 9 ‘) }
Annotated parse tree for 95-2* 9-5*2:
expr
expr
9
{ print(‘ - ‘) }
term
-
term
term
{ print(‘ * ‘) }
factor
factor
2
{ print(‘ 9 ‘) }
5
factor
*
{ print(‘ 2 ‘) }
{ print(‘ 5 ‘) }
5
Annotated parse tree for 952*- 9-5*2:
expr
{ print(‘-’) }
expr
term
factor
factor
5
-
{ print(‘*’) }
term
{ print(‘9’) }
9
term
factor
2
*
{ print(‘2’) }
{ print(‘5’) }
2.11 Construct recursive-descent parsers for the grammars in Exercise 2.2 (a), (b), &
(c).
a) S 0 S 1 | 0 1
For the sentence 0011:
S
0
S
S
1
0
S
S
1
0
0 S 1
(a)
S
0
(b)
1
1
(c)
b) S + S S | - S S | a
For the sentence +aa:
S
S
S S
S S
S
+
(a)
+
S
(c)
S
S
S
a
+
S
S S
a +
(d)
S
S
(b)
S
SS
S
+
S S
(e)
S S
a
- S
(f)
S
S
S
a
a
6
S
(g)
c) S S (S) S | ε
For the sentence: ( )
S
S (
S
)
S
S
S
S ( S ) S
S ( S ) S
ε
S ( S ) S
(a)
(b)
(c)
S
S
(
S
S )
ε S ( S )
S
S
S
(
S
ε
S
)
S
ε
(d)
S
ε
(e)
( S
)
ε
S
S ( S ) S
(f)
S
S
(
S
ε
ε
)
S
ε
(g)
2.12 Construct a syntax-directed translator that verifies that the parentheses in an
input
string are properly balanced.
S ( { print( ‘ ( ‘ ) } S ) { print ( ‘ )’ ) }
| Letter S | ε
Letter A { print (‘A’) }
| B { print (‘B’) }
… …
| Z { print (‘Z’) }
| a { print (‘a’) }
| b { print (‘b’) }
… …
| z { print (‘z’) }
7
2.16 Consider the following grammar fragment for if-then & if-then-else
statements:
stmt if expr then stmt
| if expr then stmt else stmt | other
where other stands for the other statements in the language.
a) show that this grammar is ambiguous.
For the sentence: if E1 then if E2 then other else other
stmt
if
E1
then
stmt
if
E2
else
stmt
then stmt
other
other
stmt
if
E1
then
stmt
if
E2
then stmt else
other
stmt
other
we have 2 parse trees for the above sentence so, the grammar is ambiguous.
b) Construct an equivalent unambiguous grammar that associates each else with the closet
previous unmatched then. stmt matched_stmt | unmatched_stmt matched_stmt if expr
then matched_stmt else matched_stmt
| other
unmatched_stmt if expr then stmt
| if expr then matched_stmt else unmatched_stmt
8
Exercises of Chapter 3: Lexical Analysis
3.1 What is the input alphabet of each of the following languages:
b) C
S Letters | digits | special_char | Letters digits | ε
Letters Letter | Letter Letters | ε
Letter a | b | … | z | A | B | … | Z
digits digit | digit digits | ε
digit 0 | 1 | … | 9
special_char ! | @ | $ | ^ | ... | ε
3.2
What are the conventions regarding the use of blanks in each of the
languages of
Exercise 3.1?
ws delim+
delim blank | tab | newline
3.3 Identify the lexemes that make up the tokens in the following programs. Give
reasonable attribute values for the tokens:
a) Pascal
function max ( i , j : integer ) : integer ;
{ return maximum of integers i and j }
begin
if i > j then max : = i
else max : = j
end ;
Lexemes
Tokens
A
function
max
function_keyword
id
(
i
,
j
:
integer
)
:
integer
;
{
}
begin
If
Opened_pranth
id
comma
id
colon
integer_keyword
Closed_pranth
colon
semicolon
Comment_starting
Comment_closing
begin_keyword
if_keyword
Pointer of max in the entry
table
Pointer of i in the entry tab
Pointer of j in the entry tab
integer_keyword
neglected
neglected
-
9
i
>
j
then
max
id
Relation_op
id
then_keyword
id
:=
i
else
max
Assignment_op
id
else_keyword
id
:=
j
end
;
Assignment_op
id
end_keyword
semicolon
Pointer of i in the entry tab
GT
Pointer of j in the entry tab
Pointer of max in the entry
table
Pointer of i in the entry tab
Pointer of max in the entry
table
Pointer of j in the entry tab
-
C
int max ( i , j ) int i , j ;
/* return maximum of integers i and j */
{
return i > j ? i : j ;
}
Lexemes
Tokens
int integer_keyword
max
id
(
Opened_pranth
i
id
,
comma
j
id
)
Closed_pranth
int
integer_keyword
i
id
,
comma
j
id
;
semicolon
/*
Comment_starting
*/
Comment_closing
{
Opened_curly
return return_keyword
i
id
>
Relation_op
Pointer of
Pointer of
Pointer of
Pointer of
Pointer of
Pointer of
Attributes
max in the entry table
i in the entry table
j in the entry table
i in the entry table
j in the entry table
neglected
neglected
i in the entry table
GT
10
j
?
i
:
j
;
id
id
colon
id
semicolon
Pointer of j in the entry table
Condition_op
Pointer of i in the entry table
Pointer of j in the entry table
-
b) Fortran 77
FUNCTION MAX ( I , J )
C
RETURN MAXIMUM OF INTEGERS I AND J
IF ( I .GT. J ) THEN
MAX = I
ELSE
MAX = J
END IF
RETURN
Lexemes
FUNCTION
MAX
(
I
,
J
)
C
IF
(
I
.GT.
J
)
THEN
MAX
=
I
ELSE
MAX
Tokens
FUNCTION_keyword
id
Opened_pranth
id
comma
id
Closed_pranth
Comment_starting
IF_keyword
Opened_pranth
id
Relation_op
id
Closed_pranth
THEN_keyword
id
Assignment_op
id
ELSE_keyword
id
Pointer of
Pointer of
Pointer of
Pointer of
Pointer of
Pointer of
Pointer of
Pointer of
Attributes
MAX in the entry table
I in the entry table
J in the entry table
neglected
I in the entry table
GT
J in the entry table
MAX in the entry table
I in the entry table
MAX in the entry table
11
=
J
END
IF
RETURN
Assignment_op
id
END_keyword
IF_keyword
RETURN_keyword
Pointer of J in the entry table
-
3.5 In a string of length n, how many of the following are there?
a) prefixes:
Suppose we take the word: rate n = 4
Prefixes of rate = ε, rate, r, ra, rat = n+1
b) suffixes:
Suffixes of rate = rate, ate, te, e, ε = n+1
substrings:
n
Substrings of rate = r, a, t, e, ra, at, te, rat, ate, rate, ε = ( Σ i ) + 1
i =1
c) proper prefixes:
Proper prefixes of rate = r, ra, rat = n-1
d) subsequences
Subsequences of rate = r, a, t, e, ra, rt, re, at, ae, te, rat, rae, ate, rte, rate, ε
= 2^n
3.6 Describe the languages denoted by the following regular expressions:
a) 0(0 | 1)*0
= 0({0, 1})*0
= 0{ ε, 0, 1, 00, 11, 01, 10, …}0
= {00, 000, 010, 0000, 0110, 0010, 0100, …}
All strings of 0’s & 1’s that begin & end with zero followed by one or more of
0’s or
1’s or concatenate of them.
b) ((ε | 0)1*)*
= (({ε , 0})1*)*
= ({ε , 0}{ ε , 1, 11, 111, …})*
= ({ε , 1, 11, 111, …, 0, 01, 011, 0111, …})*
= { ε , 1, 11, 111, …, 0, 01, 011, 0111, …, 10, 110, 1110, …, 00, 0101, …}
All strings of 0’s & 1’s that begin with zero or one followed by one or more of
0’s
or 1’s, or the string may be empty.
c) (0 | 1)*0(0 | 1)(0 | 1)
= ({0, 1})*0({0, 1})({0, 1})
= {ε , 0, 1, 00, 11, 01, 10, 0101, 1010, 001, …}0{00, 01, 10, 11}
= {ε , 0, 1, 00, 11, 01, 10, 0101, 1010, 001, …}{000, 001, 010, 011}
= {000, 001, 010, …, 0000, 0001, …, 1000, 1001, 1010, …, 00000, 00001, 00010,
…}
All strings of 0’s & 1’s whose third digit from the tail is 0.
d) 0*10*10*10*
12
= { ε , 0, 00, 000, …}1{ ε , 0, 00, 000, …}1{ ε , 0, 00, 000, …}1{ ε , 0, 00,
000, …}
= {1, 01, 001, 0001, …}{1, 01, 001, 0001, …}{1, 01, 001, 0001, …}{ ε , 0,
00, 000, …}
= {11, 101, 1001, …, 011, 0101, 01001, …, 0011, 00101, 001001, …}{1, 10,
100, …, 01, 010, 0100, …, 001, 0010, 00100, …}
= {111, 1110, 11100, …, 1101, 11010, 110100, …, 11001, 110010, 1100100,
…, 1011, 10110, 101100, …, 10101, 10101, 101010, 1010100, …, 101001,
1010010, 10100100, …, 0111, 01110, 011100, …, 01101, 011010, 0110100,
…, 011001, 0110010, 01100100, …}
All strings of 0’s & 1’s that containing only three digits of 1.
e)
(00 | 11)*((01 | 10)(00 | 11)* (01 | 10)(00 | 11)*)* = ({00, 11})*(({01,
10})({00, 11})*({01, 10})({00, 11})*)*
= { ε , 00, 11, 0011, 1100, 000011, 001100, …}({01, 10}{ ε , 00, 11, 0011,
1100, 000011, 001100, …}{01, 10}{ ε , 00, 11, 0011, 1100, 000011, 001100,
…})*
= { ε , 00, 11, 0011, 1100, 000011, 001100, …}({01, 10, 0100, 1000, 0111,
1011, …}{01, 10, 0100, 1000, 0111, 1011, …})*
= { ε , 00, 11, 0011, 1100, 000011, 001100, …}({0101, 0110, 010100,
011000, …, 1001, 1010, 100100, 101000, …})*
= { ε , 00, 11, 0011, 1100, 000011, 001100, …}{ ε , 0101, 0110, 010100,
011000, …, 1001, 1010, 100100, 101000, …, 01010101, 01100110, …,
10011001, 10101010, …}
All strings of 0’s & 1’s with an even length or the string may be empty.
3.7 Write regular definitions for the following languages:
a) All strings of letters that contain the five vowels in order.
Static b | c | d | f | … | z
(Static)* a (Static)* e (Static)* i (Static)* o (Static)* u (Static)*
b) All strings of letters in which the letters are in ascending lexicographic order.
[a?-z? | A?-Z?]
c) Comments consisting of a string surrounded by /* and */ without an
intervening */
unless it appears inside the quotes “ and ”.
/* (id)* */
id letter | digit | “ */ ”
letter [a-z | A-Z]
digit [0-9]
d) All strings of digits with no repeated digit.
0? 1? 2? 3? 4? 5? 6? 7? 8? 9?
e) All strings with at most one repeated digit.
(0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9)? (0? 1? 2? 3? 4? 5? 6? 7? 8? 9?)
f) All strings of 0’s & 1’s with an even number of 0’s & an odd number of
1’s.
13
00 (00 | 11)* 1 | (010 | 100) (00 | 11)*
h) All strings of 0’s & 1’s that don’t contain the substring 011.
0? (1 (0)+)*
i) All strings of 0’s & 1’s that don’t contain the subsequence 011.
1* 0* 10*
3.8 Specify the lexical form of numeric constants in the languages of Exercise
3.1.
num digit+ (. digit+)? (E(+ | -)? digit+ )
digit 0 | 1 | … | 9
3.9 Specify the lexical form of identifiers & keywords in the languages of
Exercise 3.1.
( _ | letter) id*
id letter | digit
letter a | b | … | z | A | B | … | Z
digit 0 | 1| … | 9
14
Exercises of Chapter 4: Syntax Analysis
4.1 Consider the grammar
S (L) | a
L L,S|S
a) What are the terminals, nonterminals, & start symbol?
Teminals: a , ( , ) , ‘,’ .
NonTerminals: S , L .
Start Symbol: S .
b) Find parse trees for the following sentences:
i)
(a , a)
S
(
L
L
)
,
S
S
a
a
ii)
(a , (a , a))
S
(
L
L
)
,
S
S
( L )
a
L ,
S
S
a
a
iii)
(a , ((a , a), (a , a)))
S
(
L
L
,
)
S
15
S
(
L
)
a
L
,
S
S
(
L
)
(
L )
L ,
S
L
, S
S
a
S
a
a
a
c) Construct a lestmost derivation for each of the sentences in (b).
i)
(a , a)
S => (L) => (L , S) => (S , S) => (a , S) => (a , a)
ii)
(a , (a , a))
S => (L) => (L , S) => (S , S) => (a , S) => (a , (L)) => (a , (L , S))
S =>
(a , (S , S)) => (a , (a , S)) => (a , (a , a))
iii)
(a , ((a , a) , (a , a)))
(L) => (L , S) => (S , S) => (a , S) => (a , (L)) => (a , (L , S))
(a , (S , S)) => (a , ((L) , S)) => (a , ((L , S) , S))
(a , ((S , S) , S)) => (a , ((a , S) , S)) => (a , ((a , a), S))
(a , ((a , a), (L))) => (a , ((a , a), (L , S))) => (a , ((a , a), (S , S)))
(a , ((a , a), (a , S))) => (a , ((a , a), (a , a)))
d) Construct a rightmost derivation for each of the sentences in (b).
i)
(a , a)
S => (L) => (L , S) => (L , a) => (S , a) => (a , a)
ii)
(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))
iii)
(a , ((a , a) , (a , a)))
S => (L) => (L , S) => (L , (L)) => (L , (L , S)) => (L , (L , (L)))
(L , (L , (L , S))) => (L , (L , (L , a))) => (L , (L , (S , a)))
(L , (L , (a , a))) => (L , (S , (a , a))) => (L , ((L) , (a , a)))
(L , ((L , S) , (a , a))) => (L , ((L , a) , (a , a)))
(L , ((S , a) , (a , a))) => (L , ((a , a) , (a , a)))
(S , ((a , a) , (a , a))) => (a , ((a , a) , (a , a)))
e) What language does this grammar generate?
16
Number of a’s between equal nested paranthesis.
4.2 Consider the grammar
S aSbS | bSaS | ε
a) Show that this grammar is ambiguous by constructing two different
leftmost derivations for the sentence abab.
S => aSbS => abSaSbS => abaSbS => ababS => abab
S => aSbS => abS => abaSbS => ababS => abab
b) Construct the corresponding rightmost derivations for abab.
S => aSbS => aSb => abSaSb => abSab => abab
S => aSbS => aSbaSbS => aSbaSb => aSbab => abab
c) Construct the corresponding parse trees for abab.
Parse trees for the leftmost:
S
a
S
b
S
b
S
S
a
S ε
a
ε
S
b
S
ε
a
S
ε
b
S
ε
ε
Parse trees for the rightmost:
S
a
S
S
b
S
a
S
ε
b
S
a
ε
b
a
ε
S
S
S
ε
b
S
ε
ε
d) What language does this grammar generate?
All strings that contain an equal numbers of a’s & b’s.
4.3 Consider the grammar
bexpr bexpr or bterm | bterm
bterm bterm and bfactor | bfactor
bfactor not bfactor | (bexpr) | true | false
a) Construct a parse tree for the sentence not (true or false)?
bexpr
bterm
bfactor
not
bfactor
17
(
bexpr
bexpr
or
)
bterm
bterm
bfactor
bfactor
false
true
b) Show that this grammar generates all Boolean expressions.
Boolean expressions are: true , false.
Relations between them are: and , or , not.
For the sentence not (true or false):
bexpr => bterm => bfactor => not bfactor => not (bexpr) => not (bexpr or
bterm)
not (bterm or bterm) => not (bfactor or bterm) => not (true or bterm)
not (true or bfactor) => not (true or false)
For the sentence not (true and false):
bexpr => bterm => bfactor => not bfactor => not (bexpr)
not (bexpr and bterm) => not (bterm and bterm)
not (bfactor and bterm) => not (true and bterm)
not (true and bfactor) => not (true and false)
For the sentence not true or not false:
bexpr => bexpr or bterm => bterm or bterm => bfactor or bterm
not bfactor or bterm => not true or bterm => not true or bfactor
not true or not bfactor => not true or not false
So, this sentences generate all relations between Boolean expressions.
c) Is this grammar ambiguous? Why?
No, this grammar isn’t ambiguous because any sentence performed on this
grammar
generate only one derivation & one parse tree.
4.4 Consider the grammar
R R ‘ | ’ R | RR | R* | ® | a | b
Note that the first vertical bar is the “or” symbol, not a separator between
alternatives.
a) Show that this grammar generates all regular expressions over the symbols a
and b.
18
For the sentence (ab | ba)* :
R => R* => ®* => (RR)* => (aR | R)* => (ab | RR)* => (ab | bR)*
=> (ab | ba)*
b) Show that this grammar is ambiguous.
For the sentence a | b* :
R
R
‘ |’
a
R
R
R
R
*
R
b
*
‘|‘
a
R
b
So, this grammar is ambiguous.
c) Construct an equivalent unambiguous grammar that gives the operators *, concatenation, & | the
precedences & associativities defined in Section 3.3.
R Multiply_R | Concat_R | OR_R | Letter
Letter a | b | ®
Multiply_R Letter*
Concat_R Letter.Letter | Multiply_R.Letter | Letter. Multiply_R
| Multiply_R. Multiply_R
OR_R Letter ‘ |‘ Letter | Letter ‘ |’ Multiply_R | Multiply_R ‘ |’ Letter
| Multiply_R ‘ |’ Multiply_R | Letter ‘ |’ Concat_R | Concat_R ‘ |’ Letter
| Multiply_R ‘ |’ Concat_R | Concat_R ‘ |’ Multiply_R
| Concat_R ‘ |’ Concat_R
d) Construct a parse tree in both grammars for the sentence a | b*c.
R
R
OR_R
R
‘ |’
a
R
b
R
Letter
R
R
a
*
c
‘ |’
Concat_R
Multiply_R
Letter
.
Letter
*
c
b
4.6 Try to design a grammar for each of the following languages. Which languages
are
regular?
a) The set of all strings of 0’s & 1’s such that every 0 is immediately followed by at
least one 1.
19
digit 0 1 digit | 1 digit 0 1 digit | ε
This grammar is regular.
The regular expression is: (0 1+)+
b)
Strings of 0’s & 1’s with an equal number of 0’s & 1’s. digit 0 digit 1 digit | 1 digit 0
digit | ε
This grammar is regular.
The regular expression is: (0 (01|10)* 1)+ | (1 (01|10)* 0)+
c)
Strings of 0’s & 1’s with an unequal number of 0’s & 1’s. digit 0 0 1 digit | 1 1 0 digit |
ε or digit 0 1 digit | A
A0|1
This grammar is regular.
The regular expression is: (001)+ | (110)+ or (01)+ (0 | 1)
d)
Strings of 0’s & 1’s in which 011 doesn’t appear as a substring. digit 0 digit | 1 0 digit
|ε
This grammar is regular.
The regular expression is: 0? (1 (0)+)*
e) Strings of 0’s & 1’s of the form xy where x ≠ y.
digit SL | LS
S0
L1
This grammar is regular.
The regular expression is: (01) | (10)
f) Strings of 0’s & 1’s of the form xx.
digit SS | LL
S0
L1
This grammar is regular.
The regular expression is: (00) | (11)
4.7 Construct a grammar for the expressions of each of the following languages:
a) C
Expr Expr op Expr | (Expr) | -(Expr) | id
op + | - | * | /
id Letter (Letter | Digit)*
Letter A | B | … | Z | a | b | … | z
Digit 0 | 1 | … | 9
4.8 Construct unambiguous grammars for the statements in each of the languages of
Exercise 4.7.
Expr Expr + Term | Expr - Term | Term
Term Term * Factor | Term / Factor | Factor
Factor id | (Expr) | -(Expr)
id Letter (Letter | Digit)*
Letter A | B | … | Z | a | b | … | z
Digit 0 | 1 | … | 9
4.9 We can use regular-expression-like operators in the right sides of grammar
productions…
20
a) Modify the above stmt-production so that a semicolon-terminated list of
stmt’s
appears on the right side.
stmt begin L end
L stmt | stmt ; L
b) Give a set of context-free productions generating the same set of strings as
A B*a (C | D).
A Zac | ZaD
Z bB | ε
B bB | ε
c) Show how to replace any production A r, where r is a regular expression,
by a
finite collection of context-free productions.
A rL
LA|ε
4.11 a) Eliminate the left-recursion from the grammar in Exercise 4.1.
S (L) | a
L SL‘
L‘ , SL‘ | ε
b) Construct a predictive parser for the grammar in (a).
Nonterminal
S
L
L‘
Input symbol(terminals)
,
(
)
a
$
S (L)
Sa
L SL‘
L SL‘
L‘ S L‘
L‘ ε
L‘ ε
Parse Table *
First (S) = {( , a}
First (L) = First (S) = {( , a}
First (L‘) = {‘,’ , $}
Follow (L‘) = { ) }
-
Predictive parser for (a , a):
Stack
$S
$ )L(
$ )L
$ )L‘S
$ )L‘a
$ )L‘
Input
(a , a) $
(a , a) $
a , a) $
a , a) $
a , a) $
, a) $
Output
Stack
$ )L‘S,
S (L) $ )L‘S
$ )L‘a
L SL‘ $ )L‘
Sa
$)
$
Input
Output
, a) $ L‘ ,S L‘
a) $
a) $ S a
)$
) $ L‘ ε
$
21
-
Predictive parser for (a , (a , a)):
Stack
$S
$ )L(
$ )L
$ )L‘S
$ )L‘a
$ )L‘
$ )L‘S,
$ )L‘S
$ )L‘)L(
$ )L‘)L
$ )L‘)L‘S
-
Stack
Input
(a , (a , a)) $
(a , (a , a)) $
a , (a , a)) $
a , (a , a)) $
a , (a , a)) $
, (a , a)) $
, (a , a)) $
(a , a)) $
(a , a)) $
a , a)) $
a , a)) $
Output
Stack
$ )L‘)L‘a
S (L)
$ )L‘)L‘
$ )L‘)L‘S,
L SL‘
$ )L‘)L‘S
Sa
$ )L‘)L‘a
$ )L‘)L‘
L‘ ,S L‘ $ )L‘)
$ )L‘
S (L)
$)
$
L SL‘
Input
a , a)) $
, a)) $
, a)) $
a)) $
a)) $
)) $
)) $
)$
)$
$
Output
Sa
L‘ ,S L‘
Sa
L‘ ε
L‘ ε
Predictive parser for (a , ((a , a) , (a , a))):
Input
$S
(a , ((a , a) , (a , a))) $
$ )L(
(a , ((a , a) , (a , a))) $
$ )L
a , ((a , a) , (a , a))) $
$ )L‘S
a , ((a , a) , (a , a))) $
$ )L‘a
a , ((a , a) , (a , a))) $
$ )L‘
, ((a , a) , (a , a))) $
$ )L‘L‘S,
, ((a , a) , (a , a))) $
$ )L‘L‘S
((a , a) , (a , a))) $
$ )L‘L‘)L(
((a , a) , (a , a))) $
$ )L‘L‘)L
(a , a) , (a , a))) $
$ )L‘L‘) L‘S
(a , a) , (a , a))) $
$ )L‘L‘) L‘)L(
(a , a) , (a , a))) $
$ )L‘L‘) L‘)L
a , a) , (a , a))) $
$ )L‘L‘) L‘) L‘S
a , a) , (a , a))) $
$ )L‘L‘) L‘) L‘a
a , a) , (a , a))) $
$ )L‘L‘) L‘) L‘
, a) , (a , a))) $
$ )L‘L‘) L‘) L‘S,
, a) , (a , a))) $
$ )L‘L‘) L‘) L‘S
a) , (a , a))) $
$ )L‘L‘) L‘) L‘a
a) , (a , a))) $
$ )L‘L‘) L‘) L‘
) , (a , a))) $
Output
Stack
$ )L‘L‘) L‘)
S (L)
$ )L‘L‘) L‘
$ )L‘L‘) L‘S,
L SL‘
$ )L‘L‘) L‘S
Sa
$ )L‘L‘) L‘)L(
$ )L‘L‘) L‘)L
L‘ ,S L‘ $ )L‘L‘) L‘) L‘S
$ )L‘L‘) L‘) L‘a
S (L)
$ )L‘L‘) L‘) L‘
$ )L‘L‘) L‘) L‘S,
L SL‘
$ )L‘L‘) L‘) L‘S
S (L)
$ )L‘L‘) L‘) L‘a
$ )L‘L‘) L‘) L‘
L SL‘
$ )L‘L‘) L‘)
Sa
$ )L‘L‘) L‘
$ )L‘L‘)
L‘ ,S L‘ $ )L‘L‘
$ )L‘
Sa
$)
$
Input
Output
) , (a , a))) $
, (a , a))) $
, (a , a))) $
(a , a))) $
(a , a))) $
a , a))) $
a , a))) $
a , a))) $
, a))) $
, a))) $
a))) $
a))) $
))) $
))) $
)) $
)) $
)$
)$
)$
$
L‘ ε
22
L‘ ,S L‘
S (L)
L SL‘
Sa
L‘ ,S L‘
Sa
L‘ ε
L‘ ε
L‘ ε
L‘ ε
Exercises of Chapter 5: Syntax-Directed Translation
5.1 For the input expression (4*7+1)*2, construct an annotated parse tree according
to the
syntax-directed definition of Fig. 5.2.
L.val = 58
E.val = 58
n
T.val = 58
T.val = 29
*
F.val = 29
(
E.val = 29
E.val = 28
+
T.val = 28
T.val = 4
*
F.val = 4
F.val = 7
F.val = 2
digit.lexval = 2
)
T.val = 1
F.val = 1
digit.lexval = 1
digit.lexval = 7
digit.lexval = 4
5.2 Construct the parse tree & syntax tree for the expression ((a)+(b)) according to
a) the syntax-directed definition of Fig. 5.9
- The parse tree is:
23
E
T
(
E
E
+
T
(
)
T
(
E
E
)
)
T
T
id
id
b
a
-
The syntax tree is:
E.nptr
T.nptr
(
E.nptr
E.nptr
+
T.nptr
(
E.nptr
)
T.nptr
(
)
E.nptr
)
T.nptr
T.nptr
id
+
id
id
id
To entry for a
To entry for b
b) The translation scheme of Fig. 5.28.
- The parser tree is:
E
T
R
24
(
E
T
(
ε
)
R
E
)
+
T
T
R
(
E
id
ε
T
R
id
ε
a
R
ε
)
b
-
The syntax tree is:
E.nptr
(
T.nptr
R R.s
E.nptr )
ε R.i
T.nptr
(
R.i
E.nptr )
+
T.nptr
E.nptr
R R.s
T.nptr
R R.s
(
)
id
ε R.i
T.nptr
R R.s
id
ε R.i
ε R.i
+
id
To entry of a
id
To entry of b
5.3 Construct the DAG & identify the value numbers for the subexpressions of the
following expression, assuming + associates from the left:
a + a + (a + a + a + (a + a + a + a))
a + a + (a + a + a + (a + a + a + a))
(a + a + a + (a + a + a + a))
25
(a + a + a + a)
(a + a + a)
(a + a)
a
5.6 The following grammar generates expressions formed by applying an arithmetic
operator + to integer & real constants. When two integers are added, the
resulting type is integer. Otherwise, it is real.
E E+T |T
T num . num | num
a) Give a syntax-directed definition to determine the type of each subexpression.
Production
EE+T
Semantic Rules
E.type = { if (E1.type = int && T.type = int )
return int ;
else return real ; }
ET
E.type = T.type
T num . num T.type = real
T num
T.type = int
b) Extend the syntax-directed definition of (a) to translate expressions into postfix notation as well
as determining types. Use the unary operator inttoreal to convert an integer value into an equivalent
real value, so that both operands of + in the postfix form have the same type.
Production
EE+T
Semantic Rules
E.type ={ if (E1.type = int && T.type = int )
return int ;
else if (E1.type = real && T.type = real )
return real ;
else if (E1.type = int && T.type = real )
E.type = inttoreal ;
else if (E1.type = real && T.type = int )
T.type = inttoreal ; }
, print ( + )
ET
E.type = T.type
T num . num T.type = real , print (T.val)
T num
T.type = int , print (num.val)
26
5.9 Rewrite the underlying grammar in the syntax-directed definition of Example 5.3 so that
type information can be propagated using synthesized attributes alone.
The new grammar of the syntax-directed definition is becomes as follows:
Production
D TL
TL
L L1, id
L id
id int
id real
Semantic Rules
D.type = T.type + L.type
T.type = L.type
L.type = L1.type
L.type = id.type
id.type = int
id.type = real
And the parse tree of this new grammar is as follows:
D.type
D.type
T.type
L.type
T.type
L.type
L.type
L.type
id.type
L.type
L.type
id.type
id.type
id.type
int
id.type
id.type
real
int
int
real
real
For integer identifiers *
* For real identifiers *
27
Exercises of Chapter 6: Type Checking
6.1 Write type expressions for the following types:
a) An array of pointers to reals, where the array index ranges from 1 to 100.
array ((1 … 100) pointer (real))
b)
A two-dimensional array of integers (i.e., an array of arrays) whose rows are indexed from
0 to 9 & whose columns are indexed from -10 to 10. array ((0 … 9), array (-10 … 10, (int)))
c)
Functions whose domains are functions from integers to pointers to integers & whose
ranges are records consisting of an integer & a character.
(int pointer (int)) int x char
6.2
Suppose we have the following C declarations: typedef
int a, b;
} CELL, *PCELL;
CELL foo[100];
PCELL bar (x, y) int x; CELL y {……}
Write type expressions for the types of foo & bar.
struct {
- For foo:
array (1 … 100, int x int)
- For bar:
int x (int x int) pointer(int x int)
6.3 The following grammar defines lists of lists of literals. The interpretation of
symbols
is the same as that for the grammar of Fig. 6.3 with the addition of the type list,
which
indicates a list of elements of the type T that follows:
PD;E
D D ; D | id : T
T list of T | char | integer
E (L) | literal | num | id
LE,L|E
Write translation rules similar to those in Section 6.2 to determine the types of
expressions (E) & lists (L).
Productions
E (L)
E num
L literal
L id
L E, L
Semantic Rules
{ E.type = L.type }
{ E.type = num.type}
{ E.type = char}
{E.type = id.type},
addtype(id.entry, E.type)
L.type = { if E.type = L.type = list of T
28
LE
then list of T ;
else “error” ; }
{L.type = E.type}
6.4 Add to the grammar of Exercise 6.3 the production
E nil
meaning that an expression can be the null (empty) list. Revise the rules in your
answer to Exercise 6.2 to take account of the fact that nil can stand for an empty list
of elements of any type.
Productions
E (L)
E num
L literal
L id
L E, L
LE
E nil
Semantic Rules
{ E.type = L.type }
{ E.type = num.type}
{ E.type = char}
{E.type = id.type},
addtype(id.entry, E.type)
L.type = { if E.type = L.type = list of T
then list of T ;
else “error” ; }
{L.type = E.type}
{E.type = L.type},
{L.type = integer or real or char …}
29
© Copyright 2026 Paperzz