



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 7
This page cannot be seen from the preview
Don't miss anything!




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
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^ an m^ j n m 0 g rewrite as an m^ am^ bm^ an m
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 ! 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 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).
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
::= ; 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.
(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 >)
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.