Вы находитесь на странице: 1из 61

. . .

.., ..

.
.
( II )

, 1996.

519.682.1+681.142.2
,
, ,
.
.

,
.
..
,
, ..
.
:
. ..
. ..
.., .. .
. ( II )

. ..

2404090000-05
37(03)-96

ISBN 5-87597-021-9
. ..,
1996
- . ..,
1996
2



.
,
.
, , (
). - ,
, - .
.
, , ,
; , .


: - .
, ""
.
: V
.
: , ,
. .
V
:
(1) - V;
(2) - V a - , a -
V;
- V ,
(1) (2).
: - ,
( ) .
, = ab = cd, = abcd.
= = .
: ( ) ,
.
R.
, = abcdef, R = fedcba.
: R.

: n- n)
n
0 = ; n = n-1 = n-1.
: - .
, = abcdefg, 7.
| |. 0.
: V -
.
: V* ,
V, .
, V={0,1}, V* = {, 0, 1, 00, 11, 01, 10, 000, 001, 011, ...}.
: V+ ,
V, .
, V* = V+ {.
, V V*.
[3].
.
.
: A B A B
{(a,b) | a A, b B}.
: G - (VT, VN, P, S),

VT - (),
VN - (),
VT,
P - (VT VN)+ (VT VN)*; (,
) P ,
S - () , S VN.

1 2 ... n

1 | 2 |...| n.
i , i= 1, 2, ... ,n ,
.

:
G1 = ({0,1}, {A,S}, P, S),
P
S 0A1
0A 00A1
A
: (VT VN)*
(VT VN)+ G = (VT, VN, P, S) ( ), = 12,
= 12, 1, 2, (VT VN)*, (VT VN)+
P.
, 00A11 0A1 G1.
: (VT VN)*
(VT VN)+ G = (VT, VN, P, S) ( ),
0, 1, ... , n (n0), , = 0 1 ... n= .
: 0, 1, ... , n n.
, S 000A111 G1 (. ), ..
S 0A1 00A11 000A111. 3.
: , G = (VT, VN, P, S),
L(G)={ VT* | S }.
, L(G) - VT,
S P.
, L(G1) = {0n1n | n>0}.
: (VT VN)*, S ,
G = (VT, VN, P, S).
, , ,
.
: G1 G2 , L(G1) =
L(G2).
, G1 = ({0,1}, {A,S}, P1, S) G2 = ({0,1}, {S}, P2, S),
P1: S 0A1
P2: S 0S1 | 01
0A 00A1
A
, ..
L(G1) = L(G2) = {0n1n | n>0}.
: G1 G2 ,
L(G1) { = L(G2) {.
, , ,
, , .
, G1 = ({0,1}, {A,S}, P1, S) G2 = ({0,1}, {S}, P2, S),
P1: S 0A1
P2: S 0S1 |
5

0A 00A1
A
, .. L(G1)={0n1n | n>0}, L(G2)={0n1n | n0}, .. L(G2)
L(G1) , L(G1) .


( )
0:
G = (VT, VN, P, S) 0,
( ,
).
1:
G = (VT, VN, P, S) ,
P , (VT VN)+, (VT VN)+ | |
| |.
G = (VT, VN, P, S) - (),
P , = 1A2; = 12; A VN; (VT
VN)+; 1,2 (VT VN)*.
1
-.
,
, , ,
,
, -.
2:
G = (VT, VN, P, S) - (),
A , A VN, (VT VN)+.
G = (VT, VN, P, S) (), A , A VN, (VT
VN)*.
2 -
-.
, -
-.

3:
G = (VT, VN, P, S) ,
A tB A t, A VN, B VN, t VT.
G = (VT, VN, P, S) ,
A Bt A t, A VN, B VN, t VT.
3 (, -)
.
,
, , ,
,
, .

:
(1) -;
(2) -;
(3) - -;
(4) - ;
(5) - 0.
(6) 0.
: -, A ,
- .
: L(G) k,
k.

:
(1) -, -,
(, L = {anbn | n>0}).
(2) - -, -,
- (, L = {anbncn | n>0}).
(3) - 0.
: -, , -.
: , k,
, k (k>k), .
, k,
k.
, - G1 = ({0,1}, {A,S}, P1, S)
- G2 = ({0,1}, {S}, P2, S),
P1: S 0A1
P2: S 0S1 | 01
0A 00A1
A
L = L(G1) = L(G2) = { 0n1n | n>0}. L , .. -, .
,.. ,
[3].
7

.
: ,
, ,
S - , - .
1) 0: L = {a2 b n
G(L):
S aaCFD
F AFB | AB
AB bBA
Ab bA
AD D
Cb bC
CB C
bCD

| n 1}

2) 2: L={ 0 1 0 1}
G(L):
S ASB | AB
AB BA
A0
B1
3) 2: L = {(ac)n (cb)n | n > 0}
G(L):
S aQb | accb
Q cSc
4) 3: L = {| {a,b}+, }
G(L):
S A| B
A a | Ba
B b | Bb | Ab


, , ,
.
( , , )
.

- ( ) .

, -
.
, .

: (VT)* S VN - G = (VT,
VN, P, S), (),
.
: (VT)* S VN - G = (VT,
VN, P, S), (),

.
,
,
, .
, a+b+a
G = ({a,b}, {S,T}, {S T | T+S; T a|b}, S)
:
(1) ST+ST+T+ST+T+Ta+T+Ta+b+Ta+b+a
(2) ST+Sa+Sa+T+Sa+b+Sa+b+Ta+b+a
(3) ST+ST+T+ST+T+TT+T+aT+b+aa+b+a
(2) - , (3) - , (1)
, ,
.
- ,
,
.
: ( ) G = {VT, VN, P, S), :
(1) (VN VT ),
S; - (VT
);
(2) A VN,
- a1, a2, ... , an, ai (VT VN), A
a1a2...an - ;
(3) A VN,
, A -
.

a+b+a G:
S
T

S
+
S
a

+
T

b
a

: - G ,
L(G),
.
,
( ) .
: .
: , , ,
.
:
G = ({if, then, else, a, b}, {S}, P, S),
P = {S if b then S else S | if b then S | a}.
if b then if b then a else a
.
, L(G) .
- , , ..

. ,
.
else then. , else
then, G,
:
S if b then S | if b then S else S | a
S if b then S else S | a
, - (..
),
.
,
:

10

(1)
(2)
(3)
(4)

A AA |
A AA |
A A | A |
A A | AA |

-:
L = {ai bj ck | i = j j = k}.
, i=j
, , j=k. ,
, i=j=k
, , .
, - L , [3, . 235-236]. ,
L, :
S AB | DC
A aA |
B bBc |
C cC |
D aDb |
, .
.
;
, ,
,
.
,
S; ,
- ;
, .
,
.


-
,
.
: x (VT VN)
G = (VT, VN, P, S),
.
:
: - G = (VT, VN, P, S)
: - G = (VT, VN, P, S),
, L(G) = L(G).
:
1. V0 = {S}; i = 1.
2. Vi = {x | x (VT VN), P A x A Vi-1} Vi-1.
11

3. Vi Vi-1, i = i+1 2, VN =
Vi VN; VT = Vi VT; P P,
Vi; G = (VT, VN, P, S).
: A VN G = (VT,
VN, P, S), { VT* | A } .
:
: - G = (VT, VN, P, S).
: - G = (VT, VN, P, S),
, L(G) = L(G).
:
N0, N1, ...
1. N0 = , i = 1.
2. Ni = {A | (A ) P (Ni-1 VT)*} Ni-1.
3. Ni Ni-1, i = i+1 2, VN = N i; P
P, VN VT;
G = (VT, VN, P, S).
: - G ,
.
:
(1) .
(2) .
,
.
: (1) (2),
.

-.

.
1. . .
a) S T | T+S | T-S
b) S aSBC | abC
T F | FT
CB BC
Fa|b
bB bb
a-ba+b .
bC bc
cC cc
aaabbbccc .
2. :
S A+B | B+A
Aa
Bb
12

3. :
a) S APA
P+|Aa|b

b) S aQb |
Q cSc

c) S 1B
B B0 | 1

d) S A | SA | SB
Aa
Bb

4. , :
a) L = {an bm | n, m 1}
b) L = {ccc | , , - a b}
c) L = {a1 a2 ... an an ... a2 a1 | ai = 0 1, n 1}
d) L = {an bm | n m ; n, m 0}
e) L = { 0 1 0 1}
f) L = { | {a,b}+}
g) L = { | {0,1}+ 0 1,
, , , }.
h) L = {(a2m bm)n | m 1, n 0}
n
i) L = { a 3 1 | n 1}
j) L = { a n | n 1}
3
k) L = { a n 1 | n 1}
2

5. :
a) S a | Ba
B Bb | b

b) S Ab
A Aa | ba

c) S 0A1 | 01
0A 00A1
A 01

d) S AB
AB BA
Aa
Bb

6. :
) S AB
A a | Aa
B b | Bb

S AS | SB | AB
Aa
Bb

b) S aSL | aL
L Kc
cK Kc
Kb

S aSBc | abc
cB Bc
bB bb

13

7. -, :
) S aAb
aA aaAb
A

b) S AB | ABS
AB BA
BA AB
Aa
Bb

8. , :
) S A | AS
A a | bb

b) S A.A
A B | BA
B0|1

9. , :
S aSBC | abC
CB BC
bB bb
bC bc
cC cc
L = {an bn cn | n 1}. ,
1) an bn cn (n 1)
2) .
10. :
a) S S0 | S1 | D0 | D1
D H.
H 0 | 1 | H0 | H1

b) S if B then S | B = E
B|B+E
Ba|b

:
a) 10.1001
b) if a then b = a+b+b
11. .
. -.

S P
P 1P1 | 0P0 | T
T 021 | 120R
R1 0R
R0 1
R 1
12. , {a,
b}, a .
14

13. - L,
aabbbcccc.
L = {a2n bm c2k | m=n+k, m>1}.
14. ,
{ a, ( , ), }.
: ,
a) ,
b) = (1) = 1 2, 1 2 .
15. -, L,
aacbbbcaa .
L = {an cbm can | n, m>0}.
16. -, L,
110000111 .
L = {1n 0m 1p | n+p>m; n, p, m>0}.
17. G. ; ,
; .
G: S 0A1
0A 00A1
A
18. L = {13n+2 0n | n0}. , ,
L. ,
1111111100.
19. ,
A Bt, A tB, A t,
.
20. - G1
G2, L1 L2, -
a) L1L2
b) L1 L2
c) L1*
: L = L1 L2 - L1 L2 ..
L = { | L1, L2}; L = L1* - L1, .. {} L1
L1L1L1L1L1

15

21. - L={i 2 i+1R | i N, i=(i)2 -


i, R - . -
L* (. 20).

22. ,
E+E | EE | (E) | i
. ?
23. , -
a) A AA |
b) A AA |
c) A A | A |
,, (VTVN)*, A VN, .
,
?
24. , G .
G: S abC | aB
B bc
bC bc
25. - G={VT, VN, P, S}.

X={A VN | A }.
26. - G , ,
L(G).
27. , .
a) S aABS | bCACd
A bAB | cSA | cCC
B bAB | cSB
C cS | c

b) S aAB | E
A dDA |
B bE | f
C cAB | dSD | a
D eA
fA | g

28. , ,

.
, . , ,
, .
29. , , ,
.

16

30. , - .
A VN - , A
1 A 2 . - ,
.
31. , (. 30)
.
32. , , , ,
. , A A,
|A|>1; .

17


.

, .
( , )
:





.

, ,
.
.
,
(, ),
,
, .
(-).
.
, ,
, [1, 2, 3, 4, 5, 8].


P program D1; B
D1 var D {;D}
D I {,I}: [ int | bool ]
B begin S {;S} end
S I := E | if E then S else S | while E do S | B | read (I) | write (E)
E E1 [ = | < | > | != ] E1
E1 T {[ + | - | or ] T}
T F {[ | / | and ] F}
F I | N | L | not F | (E)
L true | false
I C | IC | IR
N R | NR
C a | b | ... | z | A | B | ... |Z
R 0 | 1 | 2 | ... | 9
:

18

a) {} , ..
, , , , ..
b) [ | ] ,
, .
c) P - ; - .
:
1. , , .
2. .
3.
.
4. .
5.
; .
, , ,
{<
, } >}.
True, false, read write - ( ,
).
,
.


,
.
, .
: , ,
.
, G = (VT, VN, P, S) ,
A Bt A t, A VN, B VN, t VT.
: ,
- .
,
, (
):
(1) a1a2...an A,
A a1 ( ,
"" a1 A)
(2) ( , )
:
A a i
B,
B Aai (i = 2, 3,.., n);

19

"-":
, ""
.
:
(1) ;
""; S. ,
a1a2...an L(G).
(2) ;
""; , S.
, a1a2...an L(G).
(3) , ..
A
ai
B, B
Aai. , a1a2...an L(G).
(4) ,
, ..
, ,
. .
.
, .
, ,
(
).
,
, - .
- ,
"-", .
, G = ({a, b, }, {S, A, B, C}, P, S),
:
P: S C
Ab | Ba
A a | Ca
B b | Cb

C
A
B
S

a
A
C
-

b
B
C
-

S
-

"-" , "-"
.

() - ,
:
(1) , (
- ), ,
, (, H).
. H - .
20

(2) :
a) W t
H W ( H W) t;
) W Vt V W ( V
W) t;
G (. ):

b
b

:
(1) H;
(2) ( , )
:
,
. , ,
.
(
,
):
(1) ; ,
;
S. ,
L(G).
(2) ; ""
; , S.
, L(G).
(3) ,
. ,
L(G).
(4) , ,
,
, .
.
.
,
, , ,

21

. -
,
- .
(,
) (
, ).
: () - (K, VT, F, H, S),
K - ;
VT - ;
F - K VT K, ;
F ;
H K - ;
S K - (
).
F(A, t) = B , A t
B.
: a1a2...an, F(H,a1) = A1;
F(A1,a2) = A2; . . . ; F(An-2,an-1) = An-1;
F(An-1,an) = S, ai VT, Aj K, j = 1, 2 ,
... ,n-1; i = 1, 2, ... ,n; H - , S -
.
:
.


:
a) ,
, ,
;
b) ,
, , .
c) (ER); ,
.

.

, G = ({a,b, }, {S,A,B,C}, P, S),


P: S C
Ab | Ba
A a | Ca
B b | Cb
:
#include <stdio.h>
int scan_G(){

22

enum state {H, A, B, C, S, ER}; /


state CS; / CS -
FILE fp;
/ ,

int c;
CS=H;
fp = fopen ("data","r");
c = fgetc (fp);
do {switch (CS) {
case H: if (c == 'a') {c = fgetc(fp); CS = A;}
else if (c == 'b') {c = fgetc(fp); CS = B;}
else CS = ER;
break;
case A: if (c == 'b') {c = fgetc(fp); CS = C;}
else CS = ER;
break;
case B: if (c == 'a') {c = fgetc(fp); CS = C;}
else CS = ER;
break;
case C: if (c =='a') {c = fgetc(fp); CS = A;}
else if (c == 'b') {c = fgetc(fp); CS = B;}
else if (c == '') CS = S;
else CS = ER;
break;
}
} while (CS != S && CS != ER);
if (CS == ER) return -1; else return 0;
}


,
, ,
(. 4 ).
, ,
, .
, G = ({a,b, }, {S,A,B}, P, S),
P: S A
A a | Bb
B b | Bb
(.. A B
- Bb).

.
: () -
(K, VT, F, H, S),
K - ;
VT - ;
23

F - K VT K;
H K - ;
S K - .
F(A,t) = {B1,B2,...,Bn} , A t
Bi, i = 1, 2, ... ,n.
,
() ;
, , ; ,
, .
,
, , , ,
" ".

, ,
, ,
.
,
, .

: M = (K, VT, F, H, S) - .
: M = (K, VT, F, H, S) - ,
, .
:
1. .
[A1A2...An], Ai K.
2. F F ([A1A2...An], t) = [B1B2...Bm],
1 j m F(Ai, t) = Bj - 1 i n.
3. H = {H1, H2, ..., Hk}, H = [H1, H2, ..., Hk].
4. S = {S1, S2, ..., Sp}, S - K,
[...Si...], .Si S - 1 i p.
: K ,
, .
.
M = ({H, A, B, S}, {0, 1}, F, {H}, {S}),
F(H, 1) = B
F(B, 0) = A
F(A, 1) = B
F(A, 1) = S ,
:
K = {[H], [A], [B], [S], [HA], [HB], [HS], [AB], [AS], [BS], [HAB], [HAS], [ABS],
[HBS], [HABS]}
F([A], 1) = [BS]
F([B], 0) = [A]
F([HB], 1) = [B]
F([HS], 1) = [B]

F([H], 1) = [B]
F([HA], 1) = [BS]
F([HB], 0) = [A]
F([AB], 1) = [BS]
24

F([AB], 0) = [A]
F([BS], 0) = [A]
F([HAB], 1) = [BS]
F([ABS], 1) = [BS]
F([HBS], 1) = [B]
F([HABS], 1) = [BS]

F([AS], 1) = [BS]
F([HAB], 0) = [A]
F([HAS], 1) = [BS]
F([ABS], 0) = [A]
F([HBS], 0) = [A]
F([HABS], 0) = [A]

S = {[S], [HS], [AS], [BS], [HAS], [ABS], [HBS], [HABS]}


[H], [B], [A] [BS],
.
, M = ({[H], [B], [A], [BS]}, {0, 1}, F, H, {[BS]}),
F([A], 1) = [BS]
F([H], 1) = [B]
F([B], 0) = [A]
F([BS], 0) = [A]


() - .
, ,
, .
:
a) , ,

;
b) ,
;
c) ,
.
, ,
.
: , ,
.

(_,
____).
: ,
.
, - ,
, , .
,
.
, (I):
I a|b|...|z|Ia|Ib|...|Iz|I0|I1|...|I9
(N):
N 0|1|...|9|N0|N1|...|N9
..
, ,
, ,
.
: ,
25

, (_,
____). , ,
,
- -. :

t1,t2,...,tn
D1;D2;...;Dm

ti - A
ti - i = 1, 2 ,... n, B;
D1, D2, ... ,Dm.

-
- -;
- (
).
:

,
.
: .
: - ;
:
- 1,
- 2,
( ) - 3,
- 4.
- (_, __).
__ - .
, :
1) :
buf - ;
c - ;
d - ;
TW - -;
TD - -;
TID - ;
TNUM - -, .
TW TD , ..
; TID TNUM ;
, ; tabl -
, ptabl - tabl.

26

2) :
void clear (void); - buf;
void add (void); - buf;
int look (ptabl ); - buf; :
0,
;
int putl (ptabl ); - buf,
; : ;
int putnum (); - TNUM d, ; :
TNUM -;
void makelex (int k, int i); -
; k - , i - ;
void gc (void) - ,
.
:

27

gc();

H
j=putl(TID);makelex(4,j)

add();gc()

clear(); add(); gc()

j=look(TW)

ID

makelex(1,j)

d=d*10+(c-0); gc()

d=c-0; gc()

NUM

makelex(3,putnum())

gc()
{

COM

gc()

ER

ASS

gc()

makelex(2,N:)
gc(); makelex(2,N:=)

FIN

makelex (2, N)

DLM

clear(); add();

j=look(TD)

gc(); makelex(2,j)

ER

: Nx ( )
x .
:
#include <stdio.h>
#include <ctype.h>
#define BUFSIZE 80

28

typedef struct tabl ptabl;


extern ptabl TW, TID, TD, TNUM;
char buf[BUFSIZE]; / /
int c;
/ /
int d;
/
/
void clear(void); / buf /
void add(void); /
buf/
int look(ptabl); / buf;
: 0 /
int putl(ptabl); / buf,
; :
/
int putnum();
/ TNUM d,

;
:

TNUM /
int j;
/ ,
, look /
void makelex(int,int); /
/
void scan (void)
{enum state {H,ID,NUM,COM,ASS,DLM,ER,FIN};
state TC; / /
FILE fp;
TC = H;
fp = fopen("prog","r"); / "prog"
/
c = fgetc(fp);
do {switch (TC) {
case H:
if (c == ' ') c = fgetc(fp);
else if (isalpha(c))
{clear(); add(); c = fgetc(fp); TC = ID;}
else if (isdigit (c))
{d = c - '0'; c = fgetc(fp); TC = NUM;}
else if (c=='{') {c=fgetc(fp); TC = COM;}
else if (c == ':')
{c = fgetc(fp); TC = ASS;}
else if (c == '')
{makelex(2, N); TC = FIN;}
else TC = DLM;
break;
case ID:
if (isalpha(c) || isdigit(c)) {add(); c=fgetc(fp);}
else {if (j = look (TW)) makelex (1,j);
else {j = putl (TID); makelex (4,j);};
TC = H;};
29

break;
case NUM:
if (isdigit(c)) {d=d10+(c - '0'); c=fgetc (fp);}
else {makelex (3, putnum()); TC = H;}
break;
/
...........
/
} / switch /
} / /
while (TC != FIN && TC != ER);
if (TC == ER) printf("ERROR !!!");
else printf("O.K.!!!");
}

.
33. :
S S0 | S1 | P0 | P1
P N.
N 0 | 1 | N0 | N1 .

: 11.010 , 0.1 , 01. , 100 . ?
34. .
0,1
H

0,1

0,1

+,ER

a) 1011 , 10+011 0-101+1 .


b) ,
.
c) ?
35. c gc(),
. :

30


gc()

printf(%d,b)

0,1
b=c-0; gc()

0,1
b=2*b+c-0; gc()

gc()
a) ,
1+101//p11+++1000/5?
b) .

36. ,
L = {(abb)k| k 1},
, .
37. , , ,
<>, <= >=
.
38. :
S A
A Ab | Bb | b
B Aa
, ; ;
.
39. ,
( )
.
40. G1 G2.
G1: S 0C | 1B |
B 0B | 1C |
C 0C | 1C

G2: S 0D | 1B
B 0C | 1C
C 0D | 1D |
D 0D | 1D

L1 = L(G1);
L2 = L(G2).
:
a) L1L2
b) L1L2
,
, .
41. ,
, .
a) S 0S | 0B

b) S aA | aB | bA

31

B 1B | 1C
C 1C |

A bS
B aS | bB |

c) S aB
B aC | aD | dB
C aB
D

d) S 0B
B 1C | 1S
C

42.
a) ;
b) ;
c) -, ;
d) .
S 0S | S0 | D
D DD | 1A |
A 0B |
B 0A | 0
43. :
a) S C
b) S C
B B1 | 0 | D0
C B1
C B1 | C1
B 0 | D0
D D0 | 0
D B1
c) S A0
A A0 | S1 | 0
44. G L=L1L2, L1 L2 =.
G1, L1L2 (. 20).
.
S 0A | 1S
A 0A | 1B
B 0B | 1B |
45. G1 G2, L1 L2.

a) L1 L2
b) L1 L2
c) L1 L2 (. 20)
G1: S 0B | 1S
B 0C | 1B |
C 0B | 1S

G2: S B
A B1 | 0
B A1 | C1 | B0 | 1
C A0 | B1

b) .

32

46 G1 G2
L1 L1 (. 20), L1 = L(G1); G2 - .
G1: S 0S | 0B
B 1B | 1C
C 1C |
47. , :
a) L = { | {0,1}* , 1 0};
b) L = {11 | {0,1}+ , 1 0};
, .
48. () .
"", "" "" (, ..--. .- ...-).
, .
.


,
, , .
, : ,
, , .
, , .., ,
.
, .
- (), A , A VN, (VT VN)*.
, ,
; ,
-
.
,
- , ,
. (
) ,
.
([3]), n cn 3 ( -) cn2 ( ). ,
,
.
,
, -.
.


: G =({a,b,c, }, {S,A,B}, P, S),
P: S AB
33

A a | cA
B bA
, caba L(G).
:
S AB cAB caB cabA caba
, L(G).
"
":
S

S
A

b
S

B
A

B
A

(-) "
": ,
; - ,
. ,
,
, , .
,
.
:
,
. ,
, .

34

: G =
({a,b,c,}, {S,A,B}, P, S),
P: S AB
A a | cA
B bA
:
#include <stdio.h>
int c;
FILE fp; / ,
/
void A();
void B();
void ERROR(); / /
void S() {A(); B();
if (c != '') ERROR();
}
void A() {if (c=='a') c = fgetc(fp);
else if (c == 'c') {c = fgetc(fp); A();}
else ERROR();
}
void B() {if (c == 'b') {c = fgetc(fp); A();}
else ERROR();
}
void ERROR() {printf("ERROR !!!"); exit(1);}
main() {fp = fopen("data","r");
c = fgetc(fp);
S();
printf("SUCCESS !!!"); exit(0);
}


,
:
a) A , (VT VN)*
;
b) A a11 | a22 | ... | ann, ai VT i = 1,2,...,n; ai aj i
j; i (VT VN)*, . . ,
,
.
, ,
.
, :
, -,
? , ,
, .. .

35

, .
:
(1)
, ,
- - (,
, ..).
:
L a | a,L ( L a {,a})

, .. .
, a,a,a,a,a L
a , a,a , a,a,a,a,a. ,
, L. ,
(
), .
L :
void L()
{ if (c != 'a') ERROR();
while ((c = fgetc(fp)) == ',')
if ((c = fgetc(fp)) != 'a') ERROR();
}
, , , L,
( - ), L
, L. ,
B - L ,

S LB
L a {, a}
B ,b
, -,
,,,b , .
,
, ,
.
(2)
, , ..
, .
a) ,
, ..
A A1 | ... | An | 1 | ... | m,
+
i (VT VN) , j (VT VN)*, i = 1,2,...,n; j =1,2,...,m,
- .
:
A 1A | ... | mA
A 1A | ... | nA |
, , .. A j {i}, i = 1,2,...,n; j = 1,2,...,m.
36

b) ,
, ..
A a1 | a2 | ... | an | 1 | ... |m,
a VT; i,j (VT VN)*, - .
,
:
A aA | 1 | ... | m
A 1 | 2 | ... | n
, .
c) , ,
, , ..
A B11 | ... | Bnn | a11 | ... | amm
B1 11 | ... | 1k
...
Bn n1 | ... | np,
Bi VN; aj VT; i, j, ij (VT VN)*,
Bi , A
:
A 111 | ... | 1k1 | ... | n1n | ... | npn | a11 | ... | amm
d) , ..

A a11 | ... | ann | ,
( ,
).
, G = ({a,b}, {S,A}, P, S),
P: S bAa
A aA |
-, , :
void S(void)
{if (c == b) {c = fgetc(fp); A();
if (c != a) ERROR();}
else ERROR();
}
void A(void)
{if (c == a) {c = fgetc(fp); A();}
}
baaa A() ;
, - ,
S. , baaa , ,
.
, , ,
A, , , .
G = ({a,b,}, {S,A}, P, S),
37

P: S bA
A aA |
.
, - .
: FIRST(A) - ,

G = (VT, VN, P, S), .. FIRST(A) = { a VT | A a, A VN,


(VT VN)*}.
: FOLLOW(A) -
, ,
G = (VT, VN, P, S), .. FOLLOW(A) = { a VT | S A, a, A VN,
(VT VN)*}.
, FIRST(A) FOLLOW(A) ,
.

A 1A | ... | nA | 1 | ... |m|


B A
FIRST(A) FOLLOW(A) (- ),
:
B A
A 1A | ... | nA | 1 | ... |m|
A 1A | ... | nA | 1 | ... |m|
, .. B -
{i} j {i}
A
,

,
, , .
. -,
, ,
, , -
,
n cn.
LL(k)-, LR(k)-,
(., , [2], [3]).

-
,
:
;
, ;
"" ,
.
38


1) :
struct lex {int class; int
value;};

,
- getlex struct lex getlex (void);
struct lex curr_lex ,
.
2) :
int id (void); - 1, curr_lex.class = 4, .. curr_lex
, 0 - ;
int num (void); - 1, curr_lex.class = 3, .. curr_lex
-, 0 - ;
int eq (char s); - 1, curr_lex s, 0 ;
void error(void) - ;
.
,
:
P program D' ; B
void P (void){
if (eq ("program")) curr_lex = getlex();
else ERROR();
D1();
if (eq (";")) curr_lex = getlex(); else ERROR();
B();
if (!eq ("")) ERROR();
}
D' var D {; D}
void D1 (void){
if (eq ("var")) curr_lex = getlex();
else ERROR();
D();
while (eq (";"))
{curr_lex = getlex (); D();}
}
D I {,I}: [ int | bool ]
void D (void){
if (!id()) ERROR();
else {curr_lex = getlex();
while (eq (","))
{curr_lex = getlex();
if (!id()) ERROR();
else curr_lex = getlex ();
}
39

if (!eq (":")) ERROR();


else {curr_lex = getlex();
if (eq ("int") || eq ("bool"))
curr_lex = getlex();
else ERROR();}
}
}
E1 T {[ + | - | or ] T}
void E1 (void){
T();
while (eq ("+") || eq ("-") || eq ("or"))
{curr_lex = getlex(); T();}
}
...........

.
"" :
... curr_lex = getlex(); P(); ...


- ,
, ,
.
:
a) ,
;
b)
;
c)
, ;
; ;
..
.
,
(,
), .
:
, ,
. ,
,
.
,
-
"" ( ). ,
, ,
-.
40

,
.
: , -
, -.
,
A a<D1>B<D1;D2> | bC<D3> ,
A,D,C VN; a,b VT; <Di> Di, i = 1,
2, 3. ,
,
:
void A() {
if (c=='a') {c = fgetc(fp); D1(); B(); D1(); D2();}
else if (c == 'b') {c = fgetc(fp); C(); D3();}
else ERROR();
}
: , L
= { (0,1)+ | 0 1}.
,
, .
0 1,
0 1:
S <k0 = 0; k1 = 0;> A
A 0<k0 = k0+1>A | 1<k1 = k1+1>A |
0<k0 = k0+1; check()> | 1<k1 = k1+1; check()>,
void check()
{if (k0 != k1) { printf("ERROR !!!"); exit(1);}
else { printf("SUCCESS !!!");exit(0);}
}
,
.

-
,
-, :
1. , ,
.
2.
.
3.
.
4. .
5.
( ).
.
,
, .

41



, , .
, , .
,
. ,
,
.
TID
-,
.
.
TID
struct record {
char name; / /
int declare; / ? 1-"", 0-"" /
char type; / /
...
};
TID -
#define MAXSIZE_TID 1000
struct record TID [MAXSIZE_TID];
i- - (4,i).
name; declare type
.
:
void decid (int i, char t) - i- TID
declare , (4,i) ,
type:
void decid (int i, char t)
{if (TID [i].declare) ERROR(); / /
else {TID [i].declare = 1; / ! /
strcpy (TID [i].type, t);} / t ! /
}

D I {,I}: [int | bool],
.. (int bool) .
(, TID)
(, ), ,
declare type .
:
void ipush (int i); / i - /
int ipop (void);
/ - /
, (-1) - "" ;
void dec (char t)
42

{int i;
while ((i = ipop()) != -1)
decid(i,t);
}
TID
t.

:
D <ipush (-1)> I <ipush (curr_lex.value)>
{,I <ipush (curr_lex.value)>}:
[ int <dec ("int")> | bool < dec ("bool")> ]


char gettype (char op, char t1, char t2),
t1 ( )
t2 ( ) op; ,
; - "no".
;
:
void spush (char s); / s - /
char spop (void); / - /
-_
true false, spush("int")
spush("bool").
- -, , ;
, .
checkid:
void checkid (void)
{int i;
i = curr_lex.value;
if (TID [i].declare) / ? /
spush (TID [i].type); / - /
else ERROR();
/ /
}
- "-" checkop:
void checkop (void)
{char op;
char t1;char t2;
char res;
t2 = spop(); / - /
op = spop(); / - /
t1 = spop(); / - /
43

res = gettype (op,t1,t2); / ? /


if (strcmp (res, "no")) spush (res); / ! /
else ERROR();
/ ! /
}
not
checknot:
void checknot (void)
{if (strcmp (spop (), "bool")) ERROR();
else spush ("bool");}
: ?
:
, -
(, /, and), (+,-,or),
.
E E1 | E1 [ = | < | > ] E1
E1 T {[ + | - | or ] T}
T F {[ | / | and ] F}
F I | N | [ true | false ] | not F | (E)
.
: , ,
+, , (, ), i:
G1: E E+E | EE | (E) | i
G4: E T | E+T
G2: E E+T | ET | T
T F | TF
T i | (E)
F i | (E)
G3: E T+E | TE | T
G5: E T | T+E
T i |(E)
T F | FT
F i | (E),
, .

:
E E1 | E1 [ = | < | > ] <spush(TD[curr_lex.value])>E1 <checkop()>
E1 T {[ + | - | or ] <spush(TD[curr_lex.value])>T <checkop()>}
T F {[ | / | and ] <spush(TD[curr_lex.value])>F<checkop()>}
F I <checkid()> | N <spush("int")> |[ true | false ] <spush ("bool") |
not F <checknot()> | (E)
: TD - ,
; ,
#define MAXSIZE_TD 50
char TD[MAXSIZE_TD];

.

44


S I := E | if E then S else E | while E do S |
B | read (I) | write (E)
1) I := E
: I
E .
E
( );
I , , (
checkid()),
:
void eqtype (void)
{if (strcmp (spop (), spop ())) ERROR();}
, :
I <checkid()> := E <eqtype()>
2)
if E then S else S | while E do S
:
.
E
( ); ,
:
void eqbool (void)
{if (strcmp (spop(), "bool")) ERROR();}
:
if E <eqbool()> then S else S | while E <eqbool()> do S

- ,
.
D ( ):
#include <string.h>
#define MAXSIZE_TID 1000
#define MAXSIZE_TD 50
char TD[MAXSIZE_TD];
struct record
{char name;
int declare;
char type;
/ ... /
};
struct record TID [MAXSIZE_TID];

45

/ ERROR(), getlex(), id(), eq(char ),


struct lex curr_lex -
- /
void ERROR(void);
struct lex {int class; int value;};
struct lex curr_lex;
struct lex getlex (void);
int id (void);
int eq (char s);
void ipush (int i);
int ipop (void);
void decid (int i, char t)
{if (TID [i].declare) ERROR();
else {TID [i].declare = 1; strcpy (TID [i].type, t);}
}
void dec (char t)
{int i;
while ((i = ipop()) != -1) decid (i,t);}
void D (void)
{ipush (-1);
if (!id()) ERROR();
else {ipush (curr_lex.value);
curr_lex = getlex ();
while (eq (","))
{curr_lex = getlex ();
if (!id ()) ERROR ();
else {ipush (curr_lex.value);
curr_lex = getlex();}
}
if (!eq (":")) ERROR();
else {curr_lex = getlex ();
if (eq ("int")) {curr_lex = getlex ();
dec ("int");}
else if (eq ("bool"))
{curr_lex = getlex();
dec ("bool");}
else ERROR();
}
}
}

.
49. , ,
:
a) S E
E () | (E {, E}) | A
Aa|b
b) S P := E | if E then S | if E then S else S
46

P I | I (e)
E T {+T}
T F {F}
F P | (E)
Ia|b
c) S type I = T {; I = T}
T int | record I: T {; I: T} end
Ia|b|c
d) S P = E | while E do S
P I | I (E {, E})
EE+T|T
TTF|F
F P | (E)
50.
, .

a) S E
E E+T | E-T | T
T TP | P
P (E) | I
Ia|b|c

b) S E
E E+T | E-T | T
T TF | T/F | F
F I | I^N | (E)
Ia|b|c|d
N2|3|4

c) F function I(I) S; I:=E end


S ; I:=E S |
E EI | E+I | I

d) S P := E | while E do S
P I | I (E {, E})
EE+T|T
TTF|F
F P | (E)

51. - ,
.
#include <stdio.h>
int c; FILE fp;
void A();
void ERROR();
void S (void)
{if (c == 'a')
{c = fgetc(fp); S();
if (c == 'b') c = fgetc(fp);
else ERROR();
else A();
}
void A (void)
{if (c == 'b') c = fgetc(fp);
47

else ERROR();
while (c == 'b')
c = fgetc(fp);
}
void main()
{fp = fopen("data", "r");
c = fgetc(fp);
S();
printf("O.K.!");
}
?
52. , (.
. 37-38), ,
.
53. ?
(a,(b,a),(a,(b)),b).
S <k = 0> E
A | (<k=k+1; if (k == 3) ERROR();> E {,E}) <k = k-1>
Aa|b
54. , {0, 1, 2, }:
S A
A 0A | 1A | 2A |
, ,
002.
55. , {a, b, c, }:
S A
A aA | bA | cA |
, ,
:
;
,
b.
56. , {0, 1}:
S 0S | 1S |
,
, 101.
57.

-
n k
L = {a b c | m+k = n m-k = n}.

58.

-
m p
L = {1 0 1 | n+p > m, m 0}.

48

59. :
S < A = 0; B = 0 > L {L} < if (A > 5) ERROR() >
L a < A = A+1 > | b < B = B+1; if (B > 2) ERROR() > |
c < if (B == 1) ERROR() >
?
60. :
S E
E () | (E {, E}) | A
Aa|b
,
:
1. ;
2.
.
61. L ,
,
S for I = E step E to E do S
,
:
1. I ;
2. .
.
62.
P program D; begin S {; S} end
D var D' {; D'}
D' I {, I}: record I: R {; I: R} end | I {, I} : R
R int | bool
S I := E | I.I := E
E T {+T}
T F {F}
F I | (E) | I.I | N | L ,
I - , N - , L - .
,
:
1. , ,
;
2.
.
: ) (
);
b) .

49



,
.
, .


:
a) ;
b)
;
c)
.
:
a)
b)
c)
d)
e) , .

.
:
, ,
A B, A, B VN.

( - ).
.
,
.
, ()
a(b+c)-(d-e)/f
:
abc+de-f/-.
: ,
, , , .

:
(1) , -
;
(2)
1 2, - ,
1 2 , E1 E2 , E1 E2 -
1 2 ;
(3) E, - , - ,
E , E - ;
(4) () .

50


(.. ) :
,
(1) - ,
;
(2) - , ""
(
); ,
, ;
(3) , , ,
- .
: , ,
, .
: ,
; , "-"
,
. "-"
:
. , , :
a) , .. , "-" "0-";
b) ;
, "-" "a". ,

.
.
n-
, .

I := E

I E :=
":=" - , I - ; I ,
":=" I, .
,
,
.
, ,
, 1 (,
).
, L, p,
goto L
p!
! - , p.

51


.
- " "
if (not B) then goto L
B L. !F,

B p F!
p - , ,
L.

if B then S1 else S2
:
if (not B) then goto L2; S1; goto L3; L2: S2; L3: ...
:
B p2 !F S1 p3 ! S2 ... ,
pi - , ,
Li, i = 2,3.
while B do S :
L0: if (not B) then goto L1; S; goto L0; L1: ... .
while :
B p1 !F S p0 ! ... ,
pi - , ,
Li, i = 0,1.
- .
R - , W - .
read (I) I R;
write (E) - E W.
,
,
,
( ; ,
, ).

,
.
.


,
.
.
, ,
.

(. ). ,
,
52

.
.
-
-, ,
.
, :
E T {+T}
T F {F}
F a | b | (E)

:
E T {+T <putchar('+')>}
T F {F <putchar('')>}
F a <putchar('a')> | b<putchar('b')> | (E)

(, , ,
).
,

L1 = {0n1m | n,m>0}

L2 = {ambn | n,m>0}:
L1
S 0S | 1A
A 1A |
0n1m
ambn :
S 0S <putchar('b')> | 1 <putchar('a')> A
A 1 <putchar('a')> A |
L1
L2.

-
- , .. (_,
__). :
1) , (!, !F, R, W)
, ;
2) 0, .. (0,p)
- , p- ;
3) , -- - (, ),
- 4, 5.
, P,
free - :
#define MAXLEN_P 10000
53

struct lex
{int class;
int value;}
struct lex P [ MAXLEN_P];
int free = 0;
P
put_lex:
void put_lex (struct lex l)
{P [ free++] = l;}
, - put_lex5,
, 4- 5- (
value):
void put_lex5 (struct lex l)
{l.class = 5; P [ free++] = l;}

struct lex make_op(char op),
op
(2,i), i -
.

,
, ""
; ,
, .
,
:
void checkop_p (void)
{char op; char t1; char t2; char res;
t2 = spop(); op = spop(); t1 = spop();
res = gettype (op,t1,t2);
if (strcmp (res, "no"))
{spush (res);
put_lex (make_op (op));} / ! -
op /
else ERROR();
}
,
, :
E E1 | E1 [=|>|<] <spush (TD[curr_lex.value])> E1
<checkop_p()>
E1 T {[+|-|or] <spush (TD[curr_lex.value])>
T <checkop_p()>}
54

T F {[|/|and] <spush (TD[curr_lex.value])>


F <checkop_p()>}
F I <checkid(); put_lex (curr_lex)> |
N <spush("int"); put_lex (curr_lex)> |
[true|false] <spush ("bool"); put_lex (curr_lex)> |
not F <checknot(); put_lex (make_op ("not"))> | (E)
, ,
:
S I <checkid(); put_lex5 (curr_lex)> :=
E <eqtype(); put_lex (make_op (":="))>

P . if E then S1
else S2 ,
" " :
if (!E) goto l2; S1; goto l3; l2: S2; l3:...
P,
, , , .

struct lex make_labl (int k),
- (0,k).
,
, :
S if E <eqbool(); pl2 = free++; put_lex (make_op ("!F"))>
then S <pl3 = free++; put_lex (make_op ("!")); P[pl2] = make_labl (free) >
else S < P[pl3] = make_lable (free) >
: pl2 pl3 S,
.

.



, , ,
.
: ;
, ; ,
,
( ) ..
, P; N
-. -
struct lex {int class; int value;},
class:
0 - - ( )
1 - true false ( -
)
55

- ( - )
-
- - (
)
5 - - (
).
,
, address TID TNUM,
.
-
, .
2
3
4

void interpreter(void) {
int ip;
int i, j, arg;
for (i = 0; i<=N; i++)
{curr_lex = P[i];
switch (curr_lex.class) {
case 0: ipush (curr_lex.value); break;
/ - /
case 1: if (eq ("true")) ipush (1);
else ipush (0); break;
/ - /
case 2: if (eq ("+")) {ipush (ipop() + ipop()); break};
/ , - /
if (eq ("-"))
{arg = ipop(); ipush (ipop() - arg); break;}
/
/
if (eq ("not")) {ipush (!ipop()); break;};
if (eq ("!")) {j = ipop(); i = j-1; break;};
/ j-
/
if (eq ("!F")) {j = ipop(); arg = ipop();
if (!arg) {i = j-1}; break;};
/ arg ,
j - ,
/
if (eq (":=")) {arg = ipop(); ip = (int)ipop();
ip = arg; break;};
if (eq ("R")) {ip = (int) ipop();
scanf("%d", ip); break;};
/ "R" - /
if (eq ("W")) {arg = ipop();
printf ("%d", arg); break;};
/ "W" - /
case 3: ip = TNUM [curr_lex.value].address;
ipush(ip); break;
56

/ - /
case 4: ip = TID [curr_lex.value].address;
ipush(ip); break;
/ - /
case 5: ip = TID [curr_lex.value}.address;
ipush((int)ip); break;
/ - /
} / switch /
} / for /
}

.
63. :
) a+b-c
b) ab+c/a
c) a/(b+c)a
d) (a+b)/(c+ab)
e) a and b or c
f) not a or b and a
g) x+y=x/y
h ) (xx+yy < 1) and (x > 0)
64.
:
) abc
b) abc/
c) ab+c
d) ab+bc-/a+
e) a not b and not
f) abca and or and
g ) 2x+2x<
65. , :
) xyxy/+ x = 8, y = 2 ;
b) a2+b/b4 a = 4, b = 3 ;
c) ab not and a or not a = b = true ;
d) xy0 > y2x- < and x = y = 1 .
66. , ,
:
) if (x != y) x = x+1 ; x = 3 ;
b) if (x > y) x = y ; else y = x ; x = 5, y = 7;
c) while (b > a) {b = b-a;} ; a = 3, b = 7;
d) do {x = y; y = 2;} while (y > 9); y = 2;
e) S = 0; for (i = 1; i <= k; i = i + 1) {S = S + ii;} k = 3 ;
f) switch (k) {
case 1: a = not a; break;
case 2: b = a or not b ;
case 3: a = b ;
}
k = 2, a = b = false .
67. , , ,
x = 9, y = 15 (, 1).

57

z, x, y, , :=, x, y, <>, 30, !F, x, y, <, 23, !F, y, y, x, -, :=, 6, !, x, x, y, -, :=, 6, !, z,


z, x, /, :=
, goto.
68. :
a) for I := E1 to E2 do S
b) case E of
c1: S1;
c2: S2;
....
cn: Sn
end;
c) repeat S1; S2; ... ;Sn until B;
69. :
a) case k of
1: begin a:=not(a or b and c); b:=a and c or b end;
2: begin a:=a and (b or not c); b:= not a end;
3: begin a:=b or c or not a; b:==b and c or a end
end
b) S:=0; for i:=1 to N do
begin d:=i2; a:=a+d((i-1)N+5)
S:=-ad+S
end
c) c:=ab; while a<>b do
if a<b then b:=b-a else a:=a-b;
c:=c/a
70. , ,
+, -, , / ( ),
, .
.
71. - (
). , .
, ,
.
72. -, ,
T {+T}
T F {F}
F (E) | i
,
,

(,
a-b+-c a(-b)+(-c).

. C.

58

73. , :

TE
T FT
F PF
P (E) | i

E +TE |
T FT |
F ^PF |

.
.
74. , ,
+, -, , /, ( ) .

( ).
.
75. , ,
( )
.
,
+b ==> + (a, b)
a+bc ==> + (a, (b, c))
76. L1,
L1 L2.
L1 = {1m 0n | n,m>0}
L2 = {1m-n | m>n;
0 | m<n;
| m=n}
( ).
77. L1,
L1 L2.
L1 = {1n 0m 1m 0n | m,n > 0}
L2 = {1m 0n+m | m,n > 0}
78. L1,
L1 L2.
L1 = {bi | bi =(i)2, .. bi - i N}
L2 = {(bi+1)R | bi+1=(i+1)2, R - }
79. , (
).
.

59


1. .. . ., , 1975.
2. ., ., ..
. - ., , 1979.
3. ., .. , . - .
1,2. - ., , 1979.
4. .. . - ., , 1977.
5. ... . - ., , 1975.
6. .. - . - ., , 1970.
7. .. . - ., , 1975.
8. ... . - ., , 1975.

60


.... 3
. ..................................................................................................................... 3
.......................................................................... 3
........................................... 6
. ................................................................................. 8
............................................................................................................ 8
................................................................................... 11
. ....................................................................................................................... 12
........................................................... 18
. ................................................................................................................... 18
.................................................................................. 18
................................................................................................ 19
........................................................................ 23
................................................................................ 25
-................................................................. 26
...................................................................................................................... 30
......................................................... 33
.................................................................................. 33
.................................................. 35
-.......................................................... 38
..................................................................................... 40
- ........................................................... 41
...................................................................................................................... 46
............................................ 50
................................................... 50
.................................................................. 52
-....................................... 53
.............................................. 55
...................................................................................................................... 57
.......................................................................................................... 60
........................................................................................................ 61

61

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