Hw5f03 Sol Post

7
CS 373 Fall 2003 – NMG*** **************** Solution ************ October 9, 2003 Homework !!i"nment # $ue Fri%a& '0('703 a)ter cla!! roblem! relate% to c+ater #- '. Show that the following languages are context free by exhibiting context free grammars for each: .a/ L = {a m  b n c  p d q  : m+n=p+q, p, q  ! .b/ L = { a n  b n c "  : n, "   ! .c/ L = { i #  $ "  : $ % i+" , i. "  ! .%/ L = {a n  b m  c &n+m : n, m % ! .e/ L = { a n  b m  : n  m+' ! .)/  L = { a n  b m  : n  m(# ! )n!wer!- art a- L = {a m  b n c  p d q  : m+n=p+q ! * = {S, , , *!, S is start *ariable S (% aSd - - (% ac - * (% bd * * (% b*c - λ art b- S(% / - λ , (% ab - λ, /(%c/ - λ art c- L = L # L & L ' L# = { i # i  : i   ! L& = {# m  : m % ! L ' = {# "  "  : "   ! S (% 0/ (% # - λ 0 (% #0 - # / (% #/ - λ art %- L = {a n  b m  c &n+m : n, m % ! S  aScc - acc  bc - bc art! e an% )-

Transcript of Hw5f03 Sol Post

Page 1: Hw5f03 Sol Post

8/10/2019 Hw5f03 Sol Post

http://slidepdf.com/reader/full/hw5f03-sol-post 1/7

CS 373 – Fall 2003 – NMG*** **************** Solution ************October 9, 2003Homework !!i"nment #$ue Fri%a& '0('703 a)ter cla!!roblem! relate% to c+a ter #-

' . Show that the following languages are context free by exhibiting context free grammars for each:.a/ L = {a m bnc pdq : m+n=p+q, p, q ≥ !

.b/ L = { a n bnc" : n, " ≥ !

.c/ L = { i# $ " : $ % i+" , i. " ≥ !

.%/ L = {a n bm c&n+m: n, m % !

.e/ L = { a n bm : n ≤ m+' !

.)/ L = { a n bm : n ≠ m(# !

) n!wer!-art a-L = {a m bnc pdq : m+n=p+q !

* = {S, , , *!, S is start *ariableS (% aSd - - (% a c - * (% b d ** (% b*c - λ

art b- S(% / - λ , (% a b - λ , /(%c/ - λ

art c- L = L#L&L'

L# = { i#i : i ≥ !L& = {# m : m % !

L' = {#" "

: " ≥ !

S (% 0/ (% # - λ0 (% #0 - #/ (% #/ - λ

art %-L = {a n bm c&n+m: n, m % !S → aScc - a cc

→ b c - bc

art! e an% )-

Page 2: Hw5f03 Sol Post

8/10/2019 Hw5f03 Sol Post

http://slidepdf.com/reader/full/hw5f03-sol-post 2/7

case 1: na = n b in L

case 11: ' ≥ na ( n b ≥ in L

case 111: na 2 n b in L

Page 3: Hw5f03 Sol Post

8/10/2019 Hw5f03 Sol Post

http://slidepdf.com/reader/full/hw5f03-sol-post 3/7

2.a/ Suppose that 3 = {*, , S, 45 is a context free grammar such that each production is eithe r of the form (% w0 , or of the form (% 0w , or of the form (% w, where , 0 * and w 6. 1s L735 necessarilyregular8 4ro9e it or gi9e a counter example. .

.b/ 3i9en the regular language L. 1f 3 generates L, is it necessary for 3 to be regular8 1s it sufficient 8 4ro9eyour answers by examples, or counterexamples.

) n!wer-7a5 L735 is not necessarily regular. counterexample: S (% a - λ , (% Sb generates the language{an bn : n ≥ ! which is not regular.

7b5 ot necessary: /ounterexample: the nonregular grammar S (% aSb - aS - Sb - λ generates the regularlanguage a6b6 . 1t is sufficient, we ha9e already pro9en that there exists a regular grammar for e9ery regularlanguage, example: the regular grammar: S(% aS - 0 - λ , 0 (% b0 - b also generates a6b6. See Lin; theorem '.','.<

3 /onsider the alphabet ∑ = {a, b, Λ, 7, 5, +,6 , !,. /onstruct a context(free grammar that generates all

strings in ∑6 that are regular expressions o9er {a, b!. ote Λ> is uppercase lambda representing one of the primiti9e regular expression symbols, to distinguish from the empty string λ which could potentially be used your productions.

) n!wer-3rammar:* = {S!, S = start 9ariable.∑ = {a, b, Λ, 7, 5, +,6 ! ? ote Λ> is uppercase lambda representing one of the primiti9e regular expressionsymbols, to distinguish from the empty string λ which could potentially be used your productions.

4roductions:

S (% a - b - - Λ S (% SS - S+SS (% S6S (% 7S5

1 @or the /3@ below use set notation to define the language generated by the grammar:S → abSdc -

→ cd ba - λ) n!wer-L = { 7ab! i 7cd5$ 7ba5 $ 7dc5i - i, $≥ !

Page 4: Hw5f03 Sol Post

8/10/2019 Hw5f03 Sol Post

http://slidepdf.com/reader/full/hw5f03-sol-post 4/7

# 3i9e an ambiguous grammar to generate if ? then ?else> statements, in which the statement:if a then if b then c else d

would ha9e two different parse trees. /onsider the set i), el!e, t+en, a, b, c, % to be terminal symbols

possible grammar is:S → if / then SS → if / then S else SS → cS → d/ → a/ → b? if, then else are terminal symbolshe two different parse trees for the required statement are:

Page 5: Hw5f03 Sol Post

8/10/2019 Hw5f03 Sol Post

http://slidepdf.com/reader/full/hw5f03-sol-post 5/7

4 /onsider the grammarS→ 0 0, → a - λ , 0 → b0 - λShow that this grammar is ambiguous using the test string> aaa

7 3i9e a complete definition of 9ery simple and limited /(li"e> programming language in 0ac"us( aur form

Page 6: Hw5f03 Sol Post

8/10/2019 Hw5f03 Sol Post

http://slidepdf.com/reader/full/hw5f03-sol-post 6/7

70 @5. se the notation gi9en on page #A# of the text. Bou may assume the following language definition:• statement consists of one of the following:

if ( else, while, do(while, assignmentStatements consist of combinations of "ey words 7terminal symbols5 and expressions.ssignment statements consist of 9ariables and arithmetic expressions

• 0oolean and arithmetic expressions are allowed0oolean expressions are formed by comparing two arithmetic expressionsrithmetic expressions are formed by 9ariables, constants, or an operation between two otherarithmetic expression.

• statement list 0 @ 9ariable> may represent a list of / statements• ssignment operator is =• /ompare 7boolean5 operators are 2, %, 2=, %=, =, C= 7not equal5• rithmetic operators are: +, (, 6, D• /onstants are: , #, &, ', <, A, E, F, G, H• *ariables are: a, b, c, ... x, y, ;

/onsult references for the syntax of boolean and arithmetic expressions. 1f you feel the abo9e informalspecification is not sufficient, then clearly state your assumptions. Supply whate9er 0 @ 9ariables are needed.

he resulting 0 @ grammar should be context free. ote that declare statements are ignored here. he languagerepresents only a subset of the executable> part of / language.

Page 7: Hw5f03 Sol Post

8/10/2019 Hw5f03 Sol Post

http://slidepdf.com/reader/full/hw5f03-sol-post 7/7

roblem 7 !olution-NO56- !olution below +a! !ome )law! ou !+oul% recur!i8el& buil% u 8ariable!, 8ar!: to be !trin"! be"innin" wit+ an non;numeric an% +a8in" an& !trin" a al +a;numeric! t+erea)ter <n a%%ition, &ou ma& al!o want to buil% u a con!tant a! a !trin" o) con!tant inte"er !&mbol! <=ll tr& to )i> u t+i! !olution later