Вы находитесь на странице: 1из 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+a,ter #-
*'. 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 - + - ,
+ (% 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
b
m
c
&n+m
: n, m % !
S aScc - a.cc
. b.c - bc
,art! e an% )-
case 1: na = nb in L
case 11: ' na ( nb in L
case 111: na 2 nb in L
*20
.a/ Suppose that 3 = {*, +, S, 45 is a context free grammar such that each production is either of the form
. (% w0 , or of the form . (% 0w , or of the form . (% w, where ., 0 * and w +6. 1s L735 necessarily
regular8 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 sufficient8 4ro9e
your answers by examples, or counterexamples.
)n!wer-
7a5 L735 is not necessarily regular. counterexample: S (% a+ - , +(% Sb generates the language
{a
n
b
n
: n ! which is not regular.
7b5 :ot necessary: /ounterexample: the nonregular grammar S (% aSb - aS - Sb - generates the regular
language a6b6 . 1t is sufficient, we ha9e already pro9en that there exists a regular grammar for e9ery regular
language, example: the regular grammar: S(% aS - 0 - , 0 (% b0 - b also generates a6b6. See Lin; theorem '.',
'.<
*30 /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 expression
symbols, to distinguish from the empty string which could potentially be used your productions.
4roductions:
S (% a - b - -
S (% SS - S+S
S (% S6
S (% 7S5
*10 @or the /3@ below use set notation to define the language generated by the grammar:
S abSdc - .
. cd.ba -
)n!wer-
L = { 7ab!
i
7cd
5$
7ba5
$
7dc5
i
- i, $ !
*#0 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 2i), el!e, t+en, a, b, c, %3 to be terminal symbols
. possible grammar is:
S if / then S
S if / then S else S
S c
S d
/ a
/ b
? if, then else are terminal symbols
+he two different parse trees for the required statement are:
*40 /onsider the grammar
S.0.0, . a. - , 0 b0 -
Show that this grammar is ambiguous using the =test string> aaa
*70 3i9e a complete definition of 9ery simple and limited =/(li"e> programming language in 0ac"us(:aur form
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, assignment
Statements consist of combinations of "ey words 7terminal symbols5 and expressions.
.ssignment statements consist of 9ariables and arithmetic expressions
0oolean and arithmetic expressions are allowed
0oolean expressions are formed by comparing two arithmetic expressions
.rithmetic expressions are formed by 9ariables, constants, or an operation between two other
arithmetic 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 informal
specification 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 language
represents only a subset of the =executable> part of / language.
*roblem *7 !olution-
NO56- !olution below +a! !ome )law!0 7ou !+oul% recur!i8el& buil% u, 8ariable!, 98ar!: to be !trin"! be"innin" wit+ an non;
numeric an% +a8in" an& !trin" a al,+a;numeric! t+erea)ter0 <n a%%ition, &ou ma& al!o want to buil% u, a con!tant a! a !trin" o)
con!tant inte"er !&mbol!0 <=ll tr& to )i> u, t+i! !olution later0

Вам также может понравиться