Docsity

Ambiguous Grammars for Various Programming Languages: APL, Propositional Calculus, and C++, Assignments of Programming Languages

Toggle sidebar
bg1
CMSC 330 HOMEWORK EXERCISES #2
1. Give unambiguous grammars for the following languages:
(a)
f
a
n
b
n
j
n
0 and
n
is even
g
(b)
f
a
i
b
j
c
2
i
+1
d
k
j
i; j; k
0
g
(c)
f
a
1
a
2
:::a
n
a
n
:::a
2
a
1
j
a
i
2f
a; b
g
;
1
i
n
g
(d)
f
a
m
b
n
a
m
+
n
j
m
0 and
n
1
g
(e)
f
a
n
b
m
a
n
,
m
j
n
m
0
g
(f) All possible sequences of balanced parentheses
(g)
f
w
j
w
2f
a; b
g
and
w
has an odd number of
a
's and an odd number of
b
's
g
.
(h)
f
w
j
w
2f
a; b
g
and
w
has an equal number of
a
's and
b
's
g
(i)
f
a
m
b
n
c
p
d
q
j
m
+
n
=
p
+
q
g
(j)
f
a
m
b
n
j
m
6
=
n
and
m; n
0
g
(k)
f
w
j
w
2f
a; b
g
and
w
contains no substrings of the form
ab
g
(l)
f
w
j
w
2f
a; b
g
and every pair of
a
's is followed by at least one
b
g
2. Write a grammar for the language
f
a
n
b
n
a
m
b
m
j
m; n
1
g[f
a
n
b
m
a
m
b
n
j
m; n
1
g
If your grammar is ambiguous, prove that it is.
3. Consider a grammar with the following productions:
<
stmtlist
>
::=
<
stmtlist
>
;
<
stmt
>
j
<
stmt
>
<
stmt
>
::=
<
if stmt
>
j
skip
<
if stmt
>
::= if b then
<
stmtlist
><
else part
>
<
else part
>
::= else
<
stmtlist
>
j2
Do these productions cure the \dangling" else problem by eliminating the ambiguity
of which
<
if stmt
>
the nal
<
else part
>
is associated with? Does the following
grammar cure the problem?
<
Stmt
>
::=
<
UStmt
>
j
<
CStmt
>
<
UStmt
>
::= skip
j
begin
<
StmtList
>
end
<
CStmt
>
::= if b then
<
UStmt
>
else
<
Stmt
>
j
if b then
<
UStmt
>
<
StmtList
>
::=
<
StmtList
>
;
<
Stmt
>
j
<
Stmt
>
1
pf2

Partial preview of the text

Download Ambiguous Grammars for Various Programming Languages: APL, Propositional Calculus, and C++ and more Assignments Programming Languages in PDF only on Docsity!

CMSC 330 HOMEWORK EXERCISES

  1. Give unambiguous grammars for the following languages:

(a) fan^ bn^ j n  0 and n is even g (b) fai^ bj^ c^2 i+1^ dk^ j i; j; k  0 g (c) fa 1 a 2 :::an an :::a 2 a 1 j ai 2 fa; bg; 1  i  ng (d) fam^ bn^ am+n^ j m  0 and n  1 g (e) fan^ bm^ anm^ j n  m  0 g (f ) All p ossible sequences of balanced parentheses (g) fw j w 2 fa; bg and w has an o dd numb er of a's and an o dd numb er of b's g. (h) fw j w 2 fa; bg and w has an equal numb er of a's and b's g (i) fam^ bn^ cp^ dq^ j m + n = p + q g (j) fam^ bn^ j m 6 = n and m; n  0 g (k) fw j w 2 fa; bg and w contains no substrings of the form abg (l) fw j w 2 fa; bg and every pair of a's is followed by at least one b g

  1. Write a grammar for the language

fan^ bn^ am^ bm^ j m; n  1 g [ fan^ bm^ am^ bn^ j m; n  1 g

If your grammar is ambiguous, prove that it is.

  1. Consider a grammar with the following pro ductions:

::= ; j ::= j skip ::= if b then ::= else j 2 Do these pro ductions cure the \dangling" else problem by eliminating the ambiguity of which the nal is asso ciated with? Do es the following grammar cure the problem? ::= j ::= skip j b egin end ::= if b then else j if b then ::= ; j

  1. Grammars can b e used to express concepts like asso ciativity and precedence. For each of the following languages, give unambiguous grammars capturing the appropriate asso ciativity and precedence.

(a) Op erators in APL are right asso ciative and have equal precedence, unless altered by parentheses. The op erators +, -, *, / can app ear as b oth monadic (unary) and dyadic (binary) op erators. Assignment ( ) is also treated as a binary op erator that returns the value assigned as its result. Write an unambiguous context free grammar for APL expressions containing the op erands a, b, and c. (b) Prop ositional calculus formulas having op erators unary  and binary , op erands p and q, and parentheses. The op erators should b oth b e right asso ciative and have their usual precedence. (c) C++ expressions with identi ers a and b and op erators - >, *, and ++. - > is a left asso ciative binary op erator, * is a right asso ciative unary op erator, and ++ is a right asso ciative p ost x op erator. The unary op erators have precedence lower than the p ost x expressions but higher than all binary op erators. For example, the expression *a++ increments a b efore dereferencing it.

  1. Context-free grammars cannot express all the restrictions normally placed on pro- gramming languages, e.g., that all pro cedure statements have the same numb er of actual parameters as the pro cedure's de nition had formals. Consider a LISP-like language which p ermits a single pro cedure de nition and pro cedure statement (each with an arbitrary numb er of parameters) de ned by the following grammar: ::= (defun p ( ) ) (p ) ::= j 2 ::= j 2 ::= ... ::= ... ::= ...

Develop another grammar for this language that enforces the restriction that a call must have the same numb er of arguments as the declaration (you can treat , and as non-terminals for this purp ose). Is it p ossible to generalize this to a language in which arbitrarily many calls to a pro cedure can o ccur?