Java Reference
In-Depth Information
Exercises
1. The CFG shown in Figure 2.1 defines the syntax of ac programs. Explain
how this grammar enables you to answer the following questions.
(a) Can an ac program contain only declarations (and no statements)?
(b) Can a print statement precede all assignment statements?
2. Sometimes it is necessary to modify the syntax of a programming lan-
guage. This is done by changing the CFG that the language uses. What
changes would have to be made to ac's CFG (Figure 2.1) to implement
the following changes?
(a) All ac programs must contain at least one statement.
(b) All integer declarations must precede all float declarations.
(c) The first statement in any ac programmust be an assignment state-
ment.
3. Extend the ac scanner (Figure 2.5) in the following ways:
(a) A floatdcl can be represented as either f or float, allowing a more
Java-like syntax for declarations.
(b) An intdcl can be represented as either i or int.
(c) Anummay be entered in exponential (scientific) form. That is, an ac
num may be su
xed with an optionally signed exponent (1.0e10,
123e-22 or 0.31415926535e1).
4. Write the recursive-descent parsing procedures for all nonterminals in
Figure 2.1.
5. The recursive-descent code shown in Figure 2.7 contains redundant tests
for the presence of some terminal symbols. How would you decided
which ones are redundant?
6. Variables are considered uninitialized after they are declared in some
programming languages. In ac a variable must be given a value in an
assignment statement before it can be correctly used in an expression or
print statement.
Suggest how to extend ac's semantic analysis (Section 2.7) to detect
variables that are used before they are properly initialized.
 
 
Search WWH ::




Custom Search