Java Reference
In-Depth Information
Exercises
1. While ambiguity is avoided in programming languages, (some) humor
can be derived from ambiguity in natural languages. For each of the
following English sentences, explain why it is ambiguous. First try to
determine multiple grammar diagrams for the sentence. If only one
such diagram exists, explain why the meaning of the words makes the
sentence ambiguous.
(a) I saw an elephant in my pajamas.
(b) I cannot recommend this student too highly.
(c) I saw her duck.
(d) Students avoid boring professors.
(e) Milk drinkers turn to powder.
2. In some programming languages, the same symbol can have di
ff
erent
meanings in the same statement. For example, PL
/
I allows the statement
IF IF = THEN THEN = ELSE; ELSE ELSE = END; END
For the following bizarre English sentence, determine the role of each
“bu
ff
alo” by analyzing the grammar of the sentence:
Bu
ff
alo bu
ff
alo Bu
ff
alo bu
ff
alo bu
ff
alo bu
ff
alo Bu
ff
alo bu
ff
alo.
3. Transform the following grammar into a standard CFG using the algo-
rithm in Figure 4.4:
1 S
Number
2 Number
[ Sign ][Digs period ] Digs
3 Sign
plus
4
|
minus
5 Digs
digit
{
digit
}
4. Design a language and context-free grammar to represent the following
languages:
(a) The set of strings of base-8 numbers
(b) The set of strings of base-16 numbers
(c) The set of strings of base-1 numbers
(d) A language that o
ff
ers base-8, base-16, and base-1 numbers
 
 
Search WWH ::




Custom Search