Grammars for Various Languages with Operator Precedence and Associativity, Assignments of Programming Languages

Unambiguous context-free grammars for various languages with different operator precedence and associativity rules. The grammars cover apl expressions, propositional calculus formulas, and c++ expressions. Each grammar is written in backus-naur form and clearly indicates the associativity and precedence of operators.

Typology: Assignments

Pre 2010

Uploaded on 02/13/2009

koofers-user-q9j
koofers-user-q9j 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 330 HOMEWORK EXERCISES #2
1. Give unambiguous grammars for the following languages:
In all the grammars shown, single capital letters are used for non-terminals. Unless
otherwise stated, S is the start symbol.
(a)
f
a
n
b
n
j
n
0and
n
is even
g
S
,!
aaSbb
j
(b)
f
a
i
b
j
c
2
i
+
1
d
k
j
i; j; k
0
g
S
,!
QD
Q
,!
Rc
R
,!
aRcc
j
P
P
,!
Pb
j
D
,!
Dd
j
(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
S
,!
aSa
j
bSb
j
aa
j
bb
(d)
f
a
m
b
n
a
m
+
n
j
m
0and
n
1
g
rewrite as
a
m
b
n
a
n
a
m
S
,!
aSa
j
R
R
,!
bRa
j
ba
(e)
f
a
n
b
m
a
n
,
m
j
n
m
0
g
rewrite as
a
n
,
m
a
m
b
m
a
n
,
m
S
,!
aSa
j
R
R
,!
aRb
j
(f) All possible sequences of balanced parentheses
S
,!
(
S
)
S
j
1
pf3
pf4
pf5

Partial preview of the text

Download Grammars for Various Languages with Operator Precedence and Associativity and more Assignments Programming Languages in PDF only on Docsity!

CMSC 330 HOMEWORK EXERCISES

  1. Give unambiguous grammars for the following languages:

In all the grammars shown, single capital letters are used for non-terminals. Unless otherwise stated, S is the start symbol.

(a) fan^ bn^ j n  0 and n is even g

S ! aaS bb j 

(b) fai^ bj^ c^2 i+^1 dk^ j i; j; k  0 g

S ! QD

Q ! Rc R ! aRcc j P P ! P b j  D ! D d j 

(c) fa 1 a 2 :::anan :::a 2 a 1 j ai 2 fa; bg; 1  i  ng

S ! aS a j bS b j aa j bb

(d) fam^ bn^ am+n^ j m  0 and n  1 g rewrite as am^ bn^ an^ am

S ! aS a j R

R ! bRa j ba

(e) fan^ bm^ anm^ j n  m  0 g rewrite as anm^ am^ bm^ anm

S ! aS a j R

R ! aRb j 

(f) All possible sequences of balanced parentheses

S ! (S )S j 

(g) fw j w 2 fa; bg and w has an odd number of a’s and an odd number of b’s g.

Use this DFA, and the algorithm given in class to generate the grammar from the DFA. A is the start symbol.

a

b

a a a

b

b

b

A

B

C

D

A ! aB j bC

B ! aA j bD j b C ! bA j aD j a D ! aC j bB

(h) fw j w 2 fa; bg and w has an equal number of a’s and b’s g

S ! aX j bY j 

X ! bS j aX X Y ! aS j bY Y

(i) fam^ bn^ cp^ dq^ j m + n = p + q g

S ! aS d j aT c j bU d j bV c j 

T ! aT c j bV c j  V ! bV c j  U ! bU d j bV c j 

(l) fw j w 2 fa; bg and every pair of a’s is followed by at least one b g Use this DFA, and the algorithm given in class to generate the grammar from the DFA. S’ is the start symbol.

S P Q R

a

b

a a

b

b

a,b

S 0 ! S j 

S ! aP j a j bS j b P ! aQ j bS j b Q ! aR j bS j b R ! aR j bR

Again, the productions from non-terminal R are dead productions (so are any pro- ductions that generate R).

  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.

S ! P j Q

P ! AA A ! aAb j ab Q ! aQb j aRb R ! bRa j ba

Show two leftmost derivations for the string abab , to show the grammar is ambiguous.

S =) P =) AA =) abA =) abab

S =) Q =) aRb =) abab

  1. Consider a grammar with the following productions:

::= ; j ::= j skip ::= if b then fi ::= else j 2 Do these productions cure the “dangling” else problem by eliminating the ambiguity of which the final is associated with? Does the following grammar cure the problem? ::= j ::= skip j begin end ::= if b then else j if b then ::= ; j Both these grammars cure the ambiguity. The first uses the fi ending an if statement to allow the else to be correctly associated with the proper if statement. The second grammar also cures the problem, by requiring the then part of the if statement to be surrounded by a begin / end. The best way to see this is to try derivations for each grammar.

  1. Grammars can be used to express concepts like associativity and precedence. For each of the following languages, give unambiguous grammars capturing the appropriate associa- tivity and precedence. For these grammars, we use Backus-Naur form, meaning that non-terminals are surrounded by <>.

(a) Operators in APL are right associative and have equal precedence, unless altered by parentheses. The operators +, -, *, / can appear as both monadic (unary) and dyadic (binary) operators. Assignment ( ) is also treated as a binary operator that returns the value assigned as its result. Write an unambiguous context free grammar for APL expressions containing the operands a, b, and c. The start symbol is < apl expr >.

< apl expr > ! < binar y expr >j< unar y expr >

< binar y expr > ! < unar y expr >< binar y op >< binar y expr > < unar y expr > ! < unar y op >< unar y expr >j< oper and > < binar y op > ! < unar y op >j < unar y op > ! + j j = j  < oper and > ! a j b j c j (< apl expr >)

  1. Context-free grammars cannot express all the restrictions normally placed on programming languages, e.g., that all procedure statements have the same number of actual parameters as the procedure’s definition had formals. Consider a LISP-like language which permits a single procedure definition and procedure statement (each with an arbitrary number of parameters) defined 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 number of arguments as the declaration (you can treat , and as non-terminals for this purpose).

< S > ! (def un p (< M >)

< M > ! < F or mal >< M >< Actual > j ) < B ody >) (p < F or mal > ! < I d > < Actual > ! < E xp > Is it possible to generalize this to a language in which arbitrarily many calls to a procedure can occur? The idea of the grammar is that it matches the first formal parameter (in the procedure definition) with the last actual parameter (in the procedure call), the second formal with the second to last actual, etc. So, it doesn’t work correctly for more than one procedure call. As a matter of fact, it is not possible to do this sort of match with a context free grammar (CFG), since a CFG cannot match arbitrarily many symbols. For example, the language L = fan^ bn^ cn^ j n  0 g is not a context free language. And this is the formal language that essentially describes matching exactly 2 procedure calls with one procedure definition.