Project Compiler
Project Compiler
Project Compiler
S1 > fbody.S1*
}
I10: goto(I5,TYPE)
{
fbody > TYPE*ID.LP.S4
}
I11: goto(I8,LP)
{
fdec > TYPE.ID.LP*S2
fbody > TYPE.ID.LP*S4
S2 > *TYPE.S3
S2 > *RP.SEMI
S4 > *TYPE.ID.S5
S4 > *RP.LC.tdec.stmt.RC
}
I12: goto(I10,ID)
{
fbody > TYPE.ID*LP.S4
}
I13: goto(I11,S2)
{
fdec > TYPE.ID.LP.S2*
}
I14: goto(I11,S4)
{
fbody > TYPE.ID.LP.S4*
}
I15: goto(I11,TYPE)
{
S2 > TYPE*S3
S4 > TYPE*ID.S5
S3 > *COMMA.S2
}
I16: goto(I11,RP)
{
S2 > RP*SEMI
S4 > RP*LC.tdec.stmt.RC
}
I17: goto(I12,LP)
{
fbody > TYPE.ID.LP*S4
S4 > *TYPE.ID.S5
S4 > *RP.LC.tdec.stmt.RC
}
I18: goto(I15,S3)
{
S2 > TYPE.S3*
}
I19: goto(I15,ID)
{
S4 > TYPE.ID*S5
S5 > *COMMA.S4
}
I20: goto(I15,COMMA)
{
S3 > COMMA*S2
S2 > *TYPE.S3
S2 > *RP.SEMI
}
Project For the Analysis and Design the Protocol of Compiler 7
I21: goto(I16,SEMI)
{
S2 > RP.SEMI*
}
I22: goto(I16,LC)
{
S4 > RP.LC*tdec.stmt.RC
tdec > *TYPE.ID.S6
tdec > *e
}
I23: goto(I17,TYPE)
{
S4 > TYPE*ID.S5
}
I24: goto(I17,RP)
{
S4 > RP*LC.tdec.stmt.RC
}
I25: goto(I19,S5)
{
S4 > TYPE.ID.S5*
}
I26: goto(I19,COMMA)
{
S5 > COMMA*S4
S4 > *TYPE.ID.S5
S4 > *RP.LC.tdec.stmt.RC
}
I27: goto(I20,S2)
{
S3 > COMMA.S2*
}
I28: goto(I20,TYPE)
{
S2 > TYPE*S3
S3 > *COMMA.S2
}
I29: goto(I20,RP)
{
S2 > RP*SEMI
}
I30: goto(I22,tdec)
{
S4 > RP.LC.tdec*stmt.RC
stmt > *expr.SEMI.stmt
stmt > *ifelse.stmt
stmt > *forloop.stmt
stmt > *doloop.SEMI.stmt
stmt > *RETURN.SEMI.stmt
stmt > *e
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
ifelse -> *IF.LP.expr.RP.LC.stmt.RC.S11
forloop > *FOR.LP.S12.SEMI.S12.SEMI.S12.RP.LC.stmt.RC
doloop > *DO.LC.stmt.RC.WHILE.LP.expr.RP
8 Fundamentals of Compiler Design
}
I44: goto(I28,fcall)
{
expr > fcall*S8
S8 > *BINOP.expr.S8
S8 > *RELOP.expr.S8
S8 > *e
}
I45: goto(I28,UNOP)
{
expr > UNOP*expr.S8
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
fcall > *ID.LP.S9
}
10 Fundamentals of Compiler Design
I46: goto(I28,LP)
{
expr > LP*expr.RP.S8
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
fcall > *ID.LP.S9
}
I47: goto(I28,FOR)
{
forloop > FOR*LP.S12.SEMI.S12.SEMI.S12.RP.LC.stmt.RC
}
I48: goto(I28,DO)
{
doloop > DO*LC.stmt.RC.WHILE.LP.expr.RP
}
I49: goto(I28,IF)
{
ifelse -> IF*LP.expr.RP.LC.stmt.RC.S11
}
I50: goto(I29,ID)
{
tdec > TYPE.ID*S6
S6 > *COMMA.ID.S6
S6 > *SEMI.tdec
}
I51: goto(I34,RC)
{
S4 > RP.LC.tdec.stmt.RC*
}
I52: goto(I35,SEMI)
{
stmt > expr.SEMI*stmt
stmt > *expr.SEMI.stmt
stmt > *ifelse.stmt
stmt > *forloop.stmt
stmt > *doloop.SEMI.stmt
stmt > *RETURN.SEMI.stmt
stmt > *e
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
ifelse -> *IF.LP.expr.RP.LC.stmt.RC.S11
forloop > *FOR.LP.S12.SEMI.S12.SEMI.S12.RP.LC.stmt.RC
doloop > *DO.LC.stmt.RC.WHILE.LP.expr.RP
fcall > *ID.LP.S9
}
I53: goto(I36,stmt)
{
stmt > forloop.stmt*
}
I54: goto(I37,SEMI)
{
Project For the Analysis and Design the Protocol of Compiler 11
stmt > doloop.SEMI*stmt
stmt > *expr.SEMI.stmt
stmt > *ifelse.stmt
stmt > *forloop.stmt
stmt > *doloop.SEMI.stmt
stmt > *RETURN.SEMI.stmt
stmt > *e
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
ifelse -> *IF.LP.expr.RP.LC.stmt.RC.S11
forloop > *FOR.LP.S12.SEMI.S12.SEMI.S12.RP.LC.stmt.RC
doloop > *DO.LC.stmt.RC.WHILE.LP.expr.RP
fcall > *ID.LP.S9
}
I55: goto(I38,stmt)
{
stmt > ifelse.stmt*
}
I56: goto(I39,SEMI)
{
stmt > RETURN.SEMI*stmt
stmt > *expr.SEMI.stmt
stmt > *ifelse.stmt
stmt > *forloop.stmt
stmt > *doloop.SEMI.stmt
stmt > *RETURN.SEMI.stmt
stmt > *e
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
ifelse -> *IF.LP.expr.RP.LC.stmt.RC.S11
forloop > *FOR.LP.S12.SEMI.S12.SEMI.S12.RP.LC.stmt.RC
doloop > *DO.LC.stmt.RC.WHILE.LP.expr.RP
fcall > *ID.LP.S9
}
I57: goto(I41,S7)
{
expr > ID.S7*
}
I58: goto(I41,LP)
{
fcall > ID.LP*S9
S9 > *ID.S10
S9 > *NUM.S10
S9 > *CONST.S10
S9 > *RP
}
I59: goto(I41,S8)
{
S7 > S8*
}
12 Fundamentals of Compiler Design
I60: goto(I41,ASSOP)
{
S7 > ASSOP*expr.S8
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
fcall > *ID.LP.S9
}
I61: goto(I41,INCOP)
{
S7 > INCOP*S8
S8 > *BINOP.expr.S8
S8 > *RELOP.expr.S8
S8 > *e
}
I62: goto(I41,BINOP)
{
S8 > BINOP*expr.S8
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
fcall > *ID.LP.S9
}
I63: goto(I41,RELOP)
{
S8 > RELOP*expr.S8
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
fcall > *ID.LP.S9
}
I64: goto(I41,e)
{
S8 > e*
}
I65: goto(I42,S8)
{
expr > NUM.S8*
}
I66: goto(I43,S8)
{
expr > CONST.S8*
}
I67: goto(I44,S8)
{
expr > fcall.S8*
}
I68: goto(I45,expr)
{
Project For the Analysis and Design the Protocol of Compiler 13
expr > UNOP.expr*S8
S8 > *BINOP.expr.S8
S8 > *RELOP.expr.S8
S8 > *e
}
I69: goto(I46,expr)
{
expr > LP.expr*RP.S8
}
I70: goto(I47,LP)
{
forloop > FOR.LP*S12.SEMI.S12.SEMI.S12.RP.LC.stmt.RC
S12 > *expr
S12 > *e
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
fcall > *ID.LP.S9
}
I71: goto(I48,LC)
{
doloop > DO.LC*stmt.RC.WHILE.LP.expr.RP
stmt > *expr.SEMI.stmt
stmt > *ifelse.stmt
stmt > *forloop.stmt
stmt > *doloop.SEMI.stmt
stmt > *RETURN.SEMI.stmt
stmt > *e
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
ifelse -> *IF.LP.expr.RP.LC.stmt.RC.S11
forloop > *FOR.LP.S12.SEMI.S12.SEMI.S12.RP.LC.stmt.RC
doloop > *DO.LC.stmt.RC.WHILE.LP.expr.RP
fcall > *ID.LP.S9
}
I72: goto(I49,LP)
{
ifelse -> IF.LP*expr.RP.LC.stmt.RC.S11
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
fcall > *ID.LP.S9
}
I73: goto(I50,S6)
{
tdec > TYPE.ID.S6*
}
I74: goto(I50,COMMA)
{
14 Fundamentals of Compiler Design
S6 > COMMA*ID.S6
}
I75: goto(I50,SEMI)
{
S6 > SEMI*tdec
tdec > *TYPE.ID.6
tdec -> *e
}
I76: goto(I52,stmt)
{
stmt > expr.SEMI.stmt*
}
I77: goto(I54,stmt)
{
Stmt > doloop.SEMI.stmt*
}
I78: goto(I56,stmt)
{
stmt > RETURN.SEMI.stmt*
}
I79: goto(I58,S9)
{
fcall > ID.LP.S9*
}
I80: goto(I58,ID)
{
S9 > ID*S10
S10 > *COMMA.S9
}
I81: goto(I58,NUM)
{
S9 > NUM*S10
S10 > *COMMA.S9
}
I82: goto(I58,CONST)
{
S9 > CONST*S10
S10 > *COMMA.S9
}
I83: goto(I58,RP)
{
S9 > RP*
}
I84: goto(I60,expr)
{
S7 > ASSOP.expr*S8
S8 > *BINOP.expr.S8
S8 > *RELOP.expr.S8
S8 > *e
}
I85: goto(I61,S8)
{
S7 > INCOP.S8*
}
I86: goto(I62,expr)
{
S8 > BINOP.expr*S8
S8 > *BINOP.expr.S8
Project For the Analysis and Design the Protocol of Compiler 15
S8 > *RELOP.expr.S8
S8 > *e
}
I87: goto(I63,expr)
{
S8 > RELOP.expr*S8
S8 > *BINOP.expr.S8
S8 > *RELOP.expr.S8
S8 > *e
}
I88: goto(I68,S8)
{
expr > UNOP.expr.S8*
}
I89: goto(I69,RP)
{
expr > LP.expr.RP*S8
S8 > *BINOP.expr.S8
S8 > *RELOP.expr.S8
S8 > *e
}
I90: goto(I70,S12)
{
forloop > FOR.LP.S12*SEMI.S12.SEMI.S12.RP.LC.stmt.RC
}
I91: goto(I70,expr)
{
S12 > expr*
}
I92: goto(I70,e)
{
S12 > e*
}
I93: goto(I71,stmt)
{
doloop > DO.LC.stmt*RC.WHILE.LP.expr.RP
}
I94: goto(I72,expr)
{
ifelse -> IF.LP.expr*RP.LC.stmt.RC.S11
}
I95: goto(I74,ID)
{
S6 > COMMA.ID*S6
S6 > *COMMA.ID.S6
S6 > *SEMI.tdec
}
I96: goto(I74,tdec)
{
S6 > SEMI.tdec*
}
I97: goto(I80,S10)
{
S9 > ID.S10*
}
I98: goto(I80,COMMA)
{
S10 > COMMA*S9
S9 > *ID.S10
16 Fundamentals of Compiler Design
S9 > *NUM.S10
S9 > *CONST.S10
S9 > *RP
}
I99: goto(I81,S10)
{
S9 > NUM.S10*
}
I100: goto(I82,S10)
{
S9 > CONST.S10*
}
I101: goto(I84,S8)
{
S7 > ASSOP.expr.S8*
}
I102: goto(I86,S8)
{
S8 > BINOP.expr.S8*
}
I103: goto(I87,S8)
{
S8 > RELOP.expr.S8*
}
I104: goto(I89,S8)
{
expr > LP.expr.RP.S8*
}
I105: goto(I90,SEMI)
{
forloop > FOR.LP.S12.SEMI*S12.SEMI.S12.RP.LC.stmt.RC
S12 > *expr
S12 > *e
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
fcall > *ID.LP.S9
}
I106: goto(I93,RC)
{
doloop > DO.LC.stmt.RC*WHILE.LP.expr.RP
}
I107: goto(I94,RP)
{
ifelse > IF.LP.expr.RP*LC.stmt.RC.S11
}
I108: goto(I95,S6)
{
S6 > COMMA.ID.S6*
}
I109: goto(I98,S9)
{
Project For the Analysis and Design the Protocol of Compiler 17
S10 > COMMA.S9*
}
I110: goto(I105,S12)
{
forloop > FOR.LP.S12.SEMI.S12*SEMI.S12.RP.LC.stmt.RC
}
I111: goto(I106,WHILE)
{
doloop > DO.LC.stmt.RC.WHILE*LP.expr.RP
}
I112: goto(I107,LC)
{
ifelse -> IF.LP.expr.RP.LC*stmt.RC.S11
stmt > *expr.SEMI.stmt
stmt > *ifelse.stmt
stmt > *forloop.stmt
stmt > *doloop.SEMI.stmt
stmt > *RETURN.SEMI.stmt
stmt > *e
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
ifelse > *IF.LP.expr.RP.LC.stmt.RC.S11
forloop > *FOR.LP.S12.SEMI.S12.SEMI.S12.RP.LC.stmt.RC
doloop > *DO.LC.stmt.RC.WHILE.LP.expr.RP
fcall > *ID.LP.S9
}
I113: goto(I108,SEMI)
{
forloop > FOR.LP.S12.SEMI.S12.SEMI*S12.RP.LC.stmt.RC
S12 > *expr
S12 > *e
}
I114: goto(I111,LP)
{
doloop > DO.LC.stmt.RC.WHILE.LP*expr.RP
expr > *ID.S7
expr > *NUM.S8
expr > *CONST.S8
expr > *fcall.S8
expr > *UNOP.expr.S8
expr > *LP.expr.RP.S8
ifelse -> *IF.LP.expr.RP.LC.stmt.RC.S11
forloop > *FOR.LP.S12.SEMI.S12.SEMI.S12.RP.LC.stmt2.RC
doloop > *DO.LC.stmt2.RC.WHILE.LP.expr.RP
fcall > *ID.LP.S9
ifelse2 > *IF.LP.expr.RP.LC.stmt.S13.SEMI.stmt.RC.S14
}
I115: goto(I112,stmt)
{
ifelse > IF.LP.expr.RP.LC.stmt*RC.S11
}
I116: goto(I113,S12)
{
18 Fundamentals of Compiler Design
{
struct sym_table *leftptr;
char lexeme[8]; /*stores the lexeme of ID,in case of NUM stores null */
char **type; /*points to 2-D array kwords for type of ID,for NUM
stores null */
int val;
struct sym_table *nextarg;
struct sym_table *rightptr;
Goto table:
20
Non-terminals
State S S0 S1 S2 S3 S4 S5 S6 S7 S8 S9 S10 S11 S12 fdec fbody tdec stmt expr fcall ifelse forloop doloop
0 1 3 2 5
1
2 7 3 2 5
3
4
5 9 5
6
7
8
9
10
11 13 14
12
13
14
15 18
16
17 14
18
19 25
20 27
21
22 30
23
Fundamentals of Compiler Design
24
25
26 33
27
28 18
29
30 34 35 44 38 36 37
31
32
33
34
35
36 53 35 44 38 36 37
37
38 34 35 44 38 36 37
39
40
41 57 59
42 65
43 66
Project For the Analysis and Design the Protocol of Compiler
44 67
45 68 44
46 69 44
47
48
49
50 73
51
52 76 35 44 38 36 37
21
53
...continued on the next page
54 77 35 44 38 36 37
55
22
56 78 35 44 38 36 37
57
58 79
59
60 84 44
61 85
62 86 44
63 87 44
64
65
66
67
68 88
69
70 90 91 44
71 93 35 44 38 36 37
72 94 44
73
74
75 96
76
77
78
79
80 97
81 99
Fundamentals of Compiler Design
82 100
...continued on the next page
83
84 ##
85
86 ##
87 ##
88
89 ##
90
91
92
93
94
95 ##
96
97
98 ##
99
100
101
Project For the Analysis and Design the Protocol of Compiler
102
103
104
105 110 91 44
106
107
108
109
110
23
111
114 117 44
115
116
117
118 121
119
120
121
122
123
124 126 35 44 38 36 37
125 127 35 44 38 36 37
126
127
128
129
Fundamentals of Compiler Design
Action Table :
Items ID NUM CONST BINOP INCOP UNOP ASSOP RELOP SEMI COMMA LP RP LC RC TYPE DO ELSE FOR IF RETURN WHILE e $
0 S4 S6
1 Accept
2 S4 S6
3 R3
4 S8
5 S10 S6
6 R5
7 R2
8 S11
9 R4
10 S12
11 S16 S15
12 S17
13 R6 R6
14 R10 R10
15 S19 S20
16 S21 S22
17 S24 S23
18 R7 R7
Project For the Analysis and Design the Protocol of Compiler
19 S26
20 S29 S28
21 R8 R8
22 S31 S32
23 S19
24 S22
25 R11 R11
26 S24 S23
27 R9 R9
28 S20
29 S21
25
30 S41 S42 S43 S45 S46 S48 S47 S49 S39 S40
31 S50
32 R15 R15 R15 R15 R15 R15 R15 R15 R15 R15
26
33 R13 R13
34 S51
35 S52
36 S41 S42 S43 S45 S46 S48 S47 S49 S39 S40
37 S54
38 S41 S42 S43 S45 S46 S48 S47 S49 S39 S40
39 S56
40 R23
41 S62 S61 S60 S63 S58 S64
42 S62 S63 S64
43 S62 S63 S64
44 S62 S63 S64
45 S41 S42 S43 S45 S46
46 S41 S42 S43 S45 S46
47 S70
48 S71
49 S72
50 S75 S74
51 R12 R12
52 S41 S42 S43 S45 S46 S48 S47 S49 S39 S40
53 R20
54 S41 S42 S43 S45 S46 S48 S47 S49 S39 S40
55 R19
56 S41 S42 S43 S45 S46 S48 S47 S49 S39 S40
57 R24 R24 R24 R24
58 S80 S81 S82 S83
59 R30 R30 R30 R30
60 S41 S42 S43 S45 S46
61 S62 S63 S64
62 S41 S42 S43 S45 S46
63 S41 S42 S43 S45 S46
64 R35 R35 R35 R35
65 R25 R25 R25 R25
66 R26 R26 R26 R26
67 R27 R27 R27 R27
Fundamentals of Compiler Design
93 S106
94 S107
95 S75 S74
96 R17 R17 R17 R17 R17 R17 R17 R17 R17 R17
97 R37 R37 R37 R37
98 S80 S81 S82 S83
99 R38 R38 R38 R38
100 R39 R39 R39 R39
101 R31 R31 R31 R31
102 R33 R33 R33 R33
103 R34 R34 R34 R34
104 R29 R29 R29 R29
27
#define S10 34
#define S11 35
#define S12 36
#define fdec 37
#define fbody 38
#define tdec 39
#define stmt 40
#define expr 41
#define fcall 42
#define ifelse 43
#define forloop 44
#define doloop 45
#define $ 46
#define accept 400
#define error 500
init.c
void init_kword()
{
kword[0]=int;
kword[1]=char;
kword[2]=break;
kword[3]=cotinue;
kword[4]=do;
kword[5]=else;
kword[6]=if;
kword[7]=for;
kword[8]=return;
kword[9]=while;
}
void init_schar()
{
schar[0]=+;
schar[1]=-;
schar[2]=*;
schar[3]=/;
schar[4]=%;
schar[5]=&&;
schar[6]=||;
schar[7]=++;
schar[8]=;
schar[9]=!;
schar[10]==;
schar[11]=<;
schar[12]=<=;
schar[13]=>;
schar[14]=>=;
schar[15]=!=;
schar[16]=;;
schar[17]=,;
schar[18]=(;
schar[19]=);
schar[20]={;
schar[21]=};
}
lex.c
/***************************************************
LEXICAL PHASE
*****************************************************/
BUFFER* getbuffernode();
Project For the Analysis and Design the Protocol of Compiler 31
void display(BUFFER*);
int id_chk(char);
FILE *fptr;
int lines;
char token[10];
int count;
int num;
fptr=fopen(source,r);
bhead=(BUFFER*)malloc(sizeof(BUFFER));
bhead->prev_tok=NULL;
bhead->token=-1;
bhead->kw=NULL;
bhead->sc=NULL;
bhead->func_ptr=NULL;
bhead->tok_ptr=NULL;
bhead->next_tok=NULL;
btail=bhead;
for(;(tok=gettoken())!=EOF;)
{
bptr=getbuffernode();
token[count]=\0';
switch(tok)
{
case ID:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=ID;
bptr->kw=NULL;
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case NUM:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=NUM;
bptr->kw=NULL;
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
32 Fundamentals of Compiler Design
case CONST:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=CONST;
bptr->kw=NULL;
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case BINOP:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=BINOP;
bptr->kw=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
switch(token[0])
{
case +:
bptr->sc=schar[0];
break;
case -:
bptr->sc=schar[1];
break;
case *:
bptr->sc=schar[2];
break;
case /:
bptr->sc=schar[3];
break;
case %:
bptr->sc=schar[4];
break;
case &:
bptr->sc=schar[5];
break;
case |:
bptr->sc=schar[6];
break;
}
break;
case INCOP:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=INCOP;
bptr->kw=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
switch(token[0])
{
case +:
Project For the Analysis and Design the Protocol of Compiler 33
bptr->sc=schar[7];
break;
case -:
bptr->sc=schar[8];
break;
}
break;
case UNOP:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=UNOP;
bptr->kw=NULL;
bptr->sc=schar[9];
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case ASSOP:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=ASSOP;
bptr->kw=NULL;
bptr->sc=schar[10];
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case RELOP:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=RELOP;
bptr->kw=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
switch(token[0])
{
case <:
if(token[1]===)
bptr->sc=schar[12];
else
bptr->sc=schar[11];
break;
case >:
if(token[1]===)
bptr->sc=schar[14];
else
bptr->sc=schar[13];
break;
case !:
bptr->sc=schar[15];
break;
}
break;
34 Fundamentals of Compiler Design
case SEMI:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=SEMI;
bptr->kw=NULL;
bptr->sc=schar[16];
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case COMMA:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=COMMA;
bptr->kw=NULL;
bptr->sc=schar[17];
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case LP:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=LP;
bptr->kw=NULL;
bptr->sc=schar[18];
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case RP:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=RP;
bptr->kw=NULL;
bptr->sc=schar[19];
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case LC:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=LC;
bptr->kw=NULL;
bptr->sc=schar[20];
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case RC:
btail->next_tok=bptr;
bptr->prev_tok=btail;
Project For the Analysis and Design the Protocol of Compiler 35
bptr->token=RC;
bptr->kw=NULL;
bptr->sc=schar[21];
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case TYPE:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=TYPE;
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
switch(token[0])
{
case i:
bptr->kw=kword[0];
break;
case c:
bptr->kw=kword[1];
break;
}
break;
case BREAK:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=BREAK;
bptr->kw=kword[2];
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case CONTINUE:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=CONTINUE;
bptr->kw=kword[3];
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case DO:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=DO;
bptr->kw=kword[4];
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
36 Fundamentals of Compiler Design
case ELSE:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=ELSE;
bptr->kw=kword[5];
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case FOR:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=FOR;
bptr->kw=kword[6];
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case IF:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=IF;
bptr->kw=kword[7];
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case RETURN:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=RETURN;
bptr->kw=kword[8];
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
case WHILE:
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=WHILE;
bptr->kw=kword[9];
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
break;
default:
return(NULL);
}
btail=btail->next_tok;
}
Project For the Analysis and Design the Protocol of Compiler 37
bptr=getbuffernode();
btail->next_tok=bptr;
bptr->prev_tok=btail;
bptr->token=$;
bptr->kw=NULL;
bptr->sc=NULL;
bptr->func_ptr=NULL;
bptr->tok_ptr=NULL;
bptr->next_tok=NULL;
btail=btail->next_tok;
display(bhead->next_tok);
fclose(fptr);
return(bhead->next_tok);
}
BUFFER* getbuffernode()
{
return((BUFFER*)malloc(sizeof(BUFFER)));
}
int gettoken()
{
int state;
char ch;
int i;
state=0;
count=0;
while(1)
{
if((ch=fgetc(fptr))==EOF)
return EOF;
switch(state)
{
case 0:
switch(ch)
{
case :
break;
case \n:
lines++;
break;
38 Fundamentals of Compiler Design
case b :
state=1;
token[count++]=ch;
break;
case c :
state=2;
token[count++]=ch;
break;
case d :
state=3;
token[count++]=ch;
break;
case e :
state=4;
token[count++]=ch;
break;
case f :
state=5;
token[count++]=ch;
break;
case i :
state=6;
token[count++]=ch;
break;
case r :
state=7;
token[count++]=ch;
break;
case w :
state=8;
token[count++]=ch;
break;
case 0 :
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
case 8 :
case 9 :
num=ch-0';
state=9;
break;
case \:
state=10;
break;
case < :
state=11;
token[count++]=ch;
break;
case > :
state=12;
token[count++]=ch;
break;
case = :
state=13;
Project For the Analysis and Design the Protocol of Compiler 39
token[count++]=ch;
break;
case ! :
state=14;
token[count++]=ch;
break;
case & :
state=15;
token[count++]=ch;
break;
case | :
state=16;
token[count++]=ch;
break;
case + :
state=17;
token[count++]=ch;
break;
case - :
state=18;
token[count++]=ch;
break;
case ( :
return(LP);
case ) :
return(RP);
case { :
return(LC);
case } :
return(RC);
case , :
return(COMMA);
case * :
return(BINOP);
case / :
return(BINOP);
case % :
return(BINOP);
case ; :
return(SEMI);
default :
if((ch>=65 && ch<=91) || (ch>=97 && ch<=123))
{
count=0;
state=19;
token[count++]=ch;
}
else
{
printf(\nLEXICAL ERROR : invalid
identifier %c\n\n,ch);
state=-1;
}
break;
}
break;
40 Fundamentals of Compiler Design
case 1:
switch(ch)
{
case r :
state=20;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID);
}
break;
}
break;
case 20:
switch(ch)
{
case e :
state=21;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID);
}
break;
}
break;
case 21:
switch(ch)
{
case a :
state=22;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
Project For the Analysis and Design the Protocol of Compiler 41
{
fseek(fptr,-1,SEEK_CUR);
return(ID);
}
break;
}
break;
case 22:
switch(ch)
{
case k :
state=23; //token- break
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID);
}
break;
}
break;
case 23:
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(BREAK);
}
break;
case 2:
switch(ch)
{
case h :
state=24;
token[count++]=ch;
break;
case o :
state=25;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
42 Fundamentals of Compiler Design
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID);
}
break;
}
break;
case 24:
switch(ch)
{
case a :
state=26;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-ch*/
}
break;
}
break;
case 26:
switch(ch)
{
case r :
state=27;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-cha*/
}
break;
}
break;
case 27:
if(id_chk(ch))
{
Project For the Analysis and Design the Protocol of Compiler 43
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(TYPE);
}
break;
case 25:
switch(ch)
{
case n :
state=28;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-co*/
}
break;
}
break;
case 28:
switch(ch)
{
case t :
state=29;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-con*/
}
break;
}
break;
case 29:
switch(ch)
{
case i :
44 Fundamentals of Compiler Design
state=30;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-cont*/
}
break;
}
break;
case 30:
switch(ch)
{
case n :
state=31;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-conti*/
}
break;
}
break;
case 31:
switch(ch)
{
case u :
state=32;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-contin*/
Project For the Analysis and Design the Protocol of Compiler 45
}
break;
}
break;
case 32:
switch(ch)
{
case e :
state=33;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-continu*/
}
break;
}
break;
case 33:
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(CONTINUE);
}
break;
case 3:
switch(ch)
{
case o :
state=34; //token- do
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-d*/
46 Fundamentals of Compiler Design
}
break;
}
break;
case 34:
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(DO);
}
break;
case 4:
switch(ch)
{
case l :
state=35;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-e*/
}
break;
}
break;
case 35:
switch(ch)
{
case s :
state=36;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-el*/
}
Project For the Analysis and Design the Protocol of Compiler 47
break;
}
break;
case 36:
switch(ch)
{
case e :
state=37;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-els*/
}
break;
}
break;
case 37:
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ELSE);
}
break;
case 5:
switch(ch)
{
case o :
state=38;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-f*/
}
break;
}
break;
48 Fundamentals of Compiler Design
case 38:
switch(ch)
{
case r :
state=39;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-fo*/
}
break;
}
break;
case 39:
switch(ch)
{
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(FOR);
}
break;
}
break;
case 6:
switch(ch)
{
case f :
state=40; //token- if
token[count++]=ch;
break;
case n :
state=41;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
Project For the Analysis and Design the Protocol of Compiler 49
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-i*/
}
break;
}
break;
case 40:
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(IF);
}
break;
case 41:
switch(ch)
{
case t :
state=42;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-in*/
}
break;
}
break;
case 42:
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(TYPE);
}
break;
case 7:
switch(ch)
50 Fundamentals of Compiler Design
{
case e :
state=43;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-r*/
}
break;
}
break;
case 43:
switch(ch)
{
case t :
state=44;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-re*/
}
break;
}
break;
case 44:
switch(ch)
{
case u :
state=45;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
Project For the Analysis and Design the Protocol of Compiler 51
return(ID); /*idtoken-ret*/
}
break;
}
break;
case 45:
switch(ch)
{
case r :
state=46;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-retu*/
}
break;
}
break;
case 46:
switch(ch)
{
case n :
state=47;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-retur*/
}
break;
}
break;
case 47:
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
52 Fundamentals of Compiler Design
fseek(fptr,-1,SEEK_CUR);
return(RETURN);
}
break;
case 8:
switch(ch)
{
case h :
state=48;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-w*/
}
break;
}
break;
case 48:
switch(ch)
{
case i :
state=49;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); 7/*idtoken-wh*/
}
break;
}
break;
case 49:
switch(ch)
{
case l :
state=50;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
Project For the Analysis and Design the Protocol of Compiler 53
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-whi*/
}
break;
}
break;
case 50:
switch(ch)
{
case e :
state=51;
token[count++]=ch;
break;
default :
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken-whil*/
}
break;
}
break;
case 51:
if(id_chk(ch))
{
state=19;
token[count++]=ch;
}
else
{
fseek(fptr,-1,SEEK_CUR);
return(WHILE);
}
break;
case 9:
switch(ch)
{
case 0 :
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
case 8 :
54 Fundamentals of Compiler Design
case 9 :
num=num*10+(ch-0');
break;
case :
case \n:
case + :
case - :
case * :
case / :
case % :
case ! :
case & :
case | :
case < :
case > :
case = :
case , :
case ) :
case ; :
fseek(fptr,-1,SEEK_CUR);
return(NUM); /*number constant*/
default :
state=-1;
printf(\nLEXICAL ERROR-NONDIGITS IN NUMBER);
break;
}
break;
case 10:
state=52;
token[count++]=ch;
break;
case 52:
switch(ch)
{
case \:
return(CONST); /*character constant*/
default :
state=-1;
printf(\nLEXICAL ERROR invalid char constant);
break;
}
break;
case 11:
switch(ch)
{
case = :
token[count++]=ch;
return(RELOP);
default :
fseek(fptr,-1,SEEK_CUR);
return(RELOP);
}
Project For the Analysis and Design the Protocol of Compiler 55
case 12:
switch(ch)
{
case = :
token[count++]=ch;
return(RELOP);
default :
fseek(fptr,-1,SEEK_CUR);
return(RELOP);
}
case 13:
switch(ch)
{
case = :
token[count++]=ch;
return(RELOP);
default :
fseek(fptr,-1,SEEK_CUR);
return(ASSOP);
}
case 14:
switch(ch)
{
case = :
token[count++]=ch;
return(RELOP);
default :
fseek(fptr,-1,SEEK_CUR);
return(UNOP);
}
case 15:
switch(ch)
{
case & :
token[count++]=ch;
return(BINOP);
default :
state=-1;
printf(LEXICAL ERROR invalid operator &);
break;
}
break;
case 16:
switch(ch)
{
case | :
token[count++]=ch;
return(BINOP);
default :
state=-1;
printf(LEXICAL ERROR invalid operator |);
break;
}
break;
56 Fundamentals of Compiler Design
case 17:
switch(ch)
{
case + :
token[count++]=ch;
return(INCOP);
default :
fseek(fptr,-1,SEEK_CUR);
return(BINOP);
}
case 18:
switch(ch)
{
case - :
token[count++]=ch;
return(INCOP);
default :
fseek(fptr,-1,SEEK_CUR);
return(BINOP);
}
case 19:
switch(ch)
{
case :
case \n:
case + :
case - :
case * :
case / :
case % :
case ! :
case & :
case | :
case < :
case > :
case = :
case , :
case ( :
case ) :
case ; :
fseek(fptr,-1,SEEK_CUR);
return(ID); /*idtoken*/
default:
if(id_chk(ch))
{
if(count<10)
token[count++]=ch;
else
{
state=-1;
return(ID); /*idtoken*/
}
}
else
{
Project For the Analysis and Design the Protocol of Compiler 57
state=-1;
printf(\nLEXICAL ERROR invalid identifier);
}
break;
}
break;
case -1:
switch(ch)
{
case :
case \n:
case + :
case - :
case * :
case / :
case % :
case ! :
case & :
case | :
case < :
case > :
case = :
case , :
case ( :
case ) :
case { :
case } :
case ; :
fseek(fptr,-1,SEEK_CUR);
return 0;
default :
if(id_chk(ch))
{
fseek(fptr,-1,SEEK_CUR);
return 0;
}
}
break;
}
}
}
int id_chk(char c)
{
if((c>=48 && c<=57)||(c>=65 && c<=90)||(c>=97 && c<=122))
return 1;
else
return 0;
}
parser.c
STACK* createstack();
int stacktop(STACK *shead);
void push(STACK *shead,int n);
void reduce(STACK *shead,int s);
void pop(STACK *shead);
void stackstatus(STACK *s);
58 Fundamentals of Compiler Design
printf(\nSYNTACTICAL PHASE);
shead = createstack();
push(shead,0);
while(1)
{
s=action(stacktop(shead),bp->token);
if(s==accept)
{
printf(\n.........SOURCE CODE PARSED..........);
return;
}
else if(s<0)
{
reduce(shead,s) ;
}
else if(s==error)
{
t=action(stacktop(shead),e);
if(t>=0 && t!=error)
{
push(shead,e);
push(shead,t);
}
else
{
printf(\n.....Code CANT be Parsed : SYNTAX ERROR....\n);
return;
}
}
stackstatus(shead->next);
}
}
STACK* createstack()
{
STACK *s;
s=(STACK*)malloc(sizeof(STACK));
s->info=-1;
s->next=NULL;
return(s);
}
nt=S1;
printf(S1>fbody.S1);
break;
case -5:
len=1;
nt=S1;
printf(S1>e);
break;
case -6:
len=4;
nt=fdec;
printf(fdec>TYPE.ID.LP.S2);
break;
case -7:
len=2;
nt=S2;
printf(S2>TYPE.S3);
break;
case -8:
len=2;
nt=S2;
printf(S2>RP.SEMI);
break;
case -9:
len=2;
nt=S3;
printf(S3>COMMA.S2);
break;
case -10:
len=4;
nt=fbody;
printf(fbody>TYPE.ID.LP.S4);
break;
case -11:
len=3;
nt=S4;
printf(S4>TYPE.ID.S5);
break;
case -12:
len=5;
nt=S4;
printf(S4>RP.LC.tdec.stmt.RC);
break;
case -13:
len=2;
nt=S5;
printf(S5>COMMA.S4);
break;
case -14:
len=3;
nt=tdec;
printf(tdec>TYPE.ID.S6);
break;
case -15:
len=1;
nt=tdec;
printf(tdec>e);
break;
Project For the Analysis and Design the Protocol of Compiler 61
case -16:
len=3;
nt=S6;
printf(S6>COMMA.ID.S6);
break;
case -17:
len=2;
nt=S6;
printf(S6>SEMI.tdec);
break;
case -18:
len=3;
nt=stmt;
printf(stmt>expr.SEMI.stmt);
break;
case -19:
len=2;
nt=stmt;
printf(stmt>ifelse.stmt);
break;
case -20:
len=2;
nt=stmt;
printf(stmt>forloop.stmt);
break;
case -21:
len=3;
nt=stmt;
printf(stmt>doloop.SEMI.stmt);
break;
case -22:
len=3;
nt=stmt;
printf(stmt>RETURN.SEMI.stmt);
break;
case -23:
len=1;
nt=stmt;
printf(stmt>e);
break;
case -24:
len=2;
nt=expr;
printf(expr>ID.S7);
break;
case -25:
len=2;
nt=expr;
printf(expr>NUM.S8);
break;
case -26:
len=2;
nt=expr;
printf(expr>CONST.S8);
break;
case -27:
len=2;
nt=expr;
62 Fundamentals of Compiler Design
printf(expr>fcall.S8);
break;
case -28:
len=3;
nt=expr;
printf(expr>UNOP.expr.S8);
break;
case -29:
len=4;
nt=expr;
printf(expr>LP.expr.RP.S8);
break;
case -30:
len=1;
nt=S7;
printf(S7>S8);
break;
case -31:
len=3;
nt=S7;
printf(S7>ASSOP.expr.S8);
break;
case -32:
len=2;
nt=S7;
printf(S7>INCOP.S8);
break;
case -33:
len=3;
nt=S8;
printf(S8>BINOP.expr.S8);
break;
case -34:
len=3;
nt=S8;
printf(S8>RELOP.expr.S8);
break;
case -35:
len=1;
nt=S8;
printf(S8>e);
break;
case -36:
len=3;
nt=fcall;
printf(fcall>ID.LP.S9);
break;
case -37:
len=2;
nt=S9;
printf(S9>ID.S10);
break;
case -38:
len=2;
nt=S9;
printf(S9>NUM.S10);
break;
case -39:
len=2;
Project For the Analysis and Design the Protocol of Compiler 63
nt=S9;
printf(S9>CONST.S10);
break;
case -40:
len=1;
nt=S9;
printf(S9>RP);
break;
case -41:
len=2;
nt=S10;
printf(S10>COMMA.S9);
break;
case -42:
len=8;
nt=ifelse;
printf(ifelse>IF.LP.expr.RP.LC.stmt.RC.S11);
break;
case -43:
len=4;
nt=S11;
printf(S11>ELSE.LC.stmt.RC);
break;
case -44:
len=1;
nt=S11;
printf(S11>e);
break;
case -45:
len=11;
nt=forloop;
printf(forloop>FOR.LP.S12.SEMI.S12.SEMI.S12.RP.LC.stmt.RC);
break;
case -46:
len=1;
nt=S12;
printf(S12>expr);
break;
case -47:
len=1;
nt=S12;
printf(S12>e);
break;
case -48:
len=8;
nt=doloop; printf(doloop>DO.LC.stmt.RC.WHILE.LP.expr.RP);
break;
}
for(i=1;i<=2*len;i++)
pop(shead);
x=stacktop(shead);
push(shead,nt);
push(shead,gotos(x,nt));
return;
}
int action(int s,int a)
{
64 Fundamentals of Compiler Design
switch(s)
{
case 0:
switch(a)
{
case TYPE:
return(4);
case e:
return(6);
default:
return(error);
}
case 1:
switch(a)
{
case $:
return(accept);
default:
return(error);
}
case 2:
switch(a)
{
case TYPE:
return(4);
case e:
return(6);
default:
return(error);
}
case 3:
switch(a)
{
case $:
return(-3);
default:
return(error);
}
case 4:
switch(a)
{
case ID:
return(8);
default:
return(error);
}
case 5:
switch(a)
{
case TYPE:
return(10);
case e:
return(6);
default:
return(error);
}
case 6:
switch(a)
Project For the Analysis and Design the Protocol of Compiler 65
{
case $:
return(-5);
default:
return(error);
}
case 7:
switch(a)
{
case $:
return(-2);
default:
return(error);
}
case 8:
switch(a)
{
case LP:
return(11);
default:
return(error);
}
case 9:
switch(a)
{
case $:
return(-4);
default:
return(error);
}
case 10:
switch(a)
{
case ID:
return(12);
default:
return(error);
}
case 11:
switch(a)
{
case RP:
return(16);
case TYPE:
return(15);
default:
return(error);
}
case 12:
switch(a)
{
case LP:
return(17);
default:
return(error);
}
case 13:
switch(a)
66 Fundamentals of Compiler Design
{
case TYPE:
case $:
return(-6);
default:
return(error);
}
case 14:
switch(a)
{
case TYPE:
case $:
return(-10);
default:
return(error);
}
case 15:
switch(a)
{
case ID:
return(19);
case COMMA:
return(20);
default:
return(error);
}
case 16:
switch(a)
{
case SEMI:
return(21);
case LC:
return(22);
default:
return(error);
}
case 17:
switch(a)
{
case RP:
return(24);
case TYPE:
return(23);
default:
return(error);
}
case 18:
switch(a)
{
case TYPE:
case $:
return(-7);
default:
return(error);
}
case 19:
switch(a)
Project For the Analysis and Design the Protocol of Compiler 67
{
case COMMA:
return(26);
default:
return(error);
}
case 20:
switch(a)
{
case RP:
return(29);
case TYPE:
return(28);
default:
return(error);
}
case 21:
switch(a)
{
case TYPE:
case $:
return(-8);
default:
return(error);
}
case 22:
switch(a)
{
case TYPE:
return(31);
case e:
return(32);
default:
return(error);
}
case 23:
switch(a)
{
case ID:
return(19);
default:
return(error);
}
case 24:
switch(a)
{
case LC:
return(22);
default:
return(error);
}
case 25:
switch(a)
{
case TYPE:
case $:
return(-11);
68 Fundamentals of Compiler Design
default:
return(error);
}
case 26:
switch(a)
{
case RP:
return(24);
case TYPE:
return(23);
default:
return(error);
}
case 27:
switch(a)
{
case TYPE:
case $:
return(-9);
default:
return(error);
}
case 28:
switch(a)
{
case COMMA:
return(20);
default:
return(error);
}
case 29:
switch(a)
{
case SEMI:
return(21);
default:
return(error);
}
case 30:
case 36:
case 38:
case 52:
case 54:
case 56:
case 71:
case 112:
case 124:
case 125:
switch(a)
{
case ID:
return(41);
case NUM:
return(42);
case CONST:
return(43);
case UNOP:
return(45);
Project For the Analysis and Design the Protocol of Compiler 69
case LP:
return(46);
case DO:
return(48);
case FOR:
return(47);
case IF:
return(49);
case RETURN:
return(39);
case e:
return(40);
default:
return(error);
}
case 31:
switch(a)
{
case ID:
return(50);
default:
return(error);
}
case 32:
switch(a)
{
case ID:
case NUM:
case CONST:
case UNOP:
case LP:
case RC:
case DO:
case FOR:
case IF:
case RETURN:
return(-15);
default:
return(error);
}
case 33:
switch(a)
{
case TYPE:
case $:
return(-13);
default:
return(error);
}
case 34:
switch(a)
{
case RC:
return(51);
default:
return(error);
}
case 35:
switch(a)
{
case SEMI:
return(52);
default:
return(error);
70 Fundamentals of Compiler Design
}
case 37:
switch(a)
{
case SEMI:
return(54);
default:
return(error);
}
case 39:
switch(a)
{
case SEMI:
return(56);
default:
return(error);
}
case 40:
switch(a)
{
case RC:
return(-23);
default:
return(error);
}
case 41:
switch(a)
{
case BINOP:
return(62);
case INCOP:
return(61);
case ASSOP:
return(60);
case RELOP:
return(63);
case LP:
return(58);
case e:
return(64);
default:
return(error);
}
case 42:
case 43:
case 44:
switch(a)
{
case BINOP:
return(62);
case RELOP:
return(63);
case e:
return(64);
default:
return(error);
}
case 45:
case 46:
case 60:
case 62:
case 63:
case 72:
case 114:
switch(a)
Project For the Analysis and Design the Protocol of Compiler 71
{
case ID:
return(41);
case NUM:
return(42);
case CONST:
return(43);
case UNOP:
return(45);
case LP:
return(46);
default:
return(error);
}
case 47:
switch(a)
{
case LP:
return(70);
default:
return(error);
}
case 48:
switch(a)
{
case LC:
return(71);
default:
return(error);
}
case 49:
switch(a)
{
case LP:
return(72);
default:
return(error);
}
case 50:
case 95:
switch(a)
{
case SEMI:
return(75);
case COMMA:
return(74);
default:
return(error);
}
case 51:
switch(a)
{
case TYPE:
case $:
return(-12);
default:
return(error);
}
case 53:
switch(a)
{
case RC:
return(-20);
default:
return(error);
72 Fundamentals of Compiler Design
}
case 55:
switch(a)
{
case RC:
return(-19);
default:
return(error);
}
case 57:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-24);
default:
return(error);
}
case 58:
case 98:
switch(a)
{
case ID:
return(80);
case NUM:
return(81);
case CONST:
return(82);
case RP:
return(83);
default:
return(error);
}
case 59:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-30);
default:
return(error);
}
case 61:
case 68:
switch(a)
{
case BINOP:
return(62);
case RELOP:
return(63);
case e:
return(64);
default:
return(error);
}
case 64:
switch(a)
{
case BINOP:
case RELOP:
Project For the Analysis and Design the Protocol of Compiler 73
case SEMI:
case RP:
return(-35);
default:
return(error);
}
case 65:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-25);
default:
return(error);
}
case 66:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-26);
default:
return(error);
}
case 67:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-27);
default:
return(error);
}
case 69:
switch(a)
{
case RP:
return(89);
default:
return(error);
}
case 70:
case 105:
case 113:
switch(a)
{
case ID:
return(41);
case NUM:
return(42);
case CONST:
return(43);
case UNOP:
return(45);
case LP:
return(46);
74 Fundamentals of Compiler Design
case e:
return(92);
default:
return(error);
}
case 73:
switch(a)
{
case ID:
case NUM:
case CONST:
case UNOP:
case LP:
case RC:
case DO:
case FOR:
case IF:
case RETURN:
return(-14);
default:
return(error);
}
case 74:
switch(a)
{
case ID:
return(95);
default:
return(error);
}
case 75:
switch(a)
{
case TYPE:
return(31);
case e:
return(32);
default:
return(error);
}
case 76:
switch(a)
{
case RC:
return(-18);
default:
return(error);
}
case 77:
switch(a)
{
case RC:
return(-21);
default:
return(error);
}
case 78:
switch(a)
{
case RC:
return(-22);
default:
return(error);
}
case 79:
Project For the Analysis and Design the Protocol of Compiler 75
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-36);
default:
return(error);
}
case 80:
case 81:
case 82:
switch(a)
{
case COMMA:
return(98);
default:
return(error);
}
case 83:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-40);
default:
return(error);
}
case 84:
case 86:
case 87:
case 89:
switch(a)
{
case BINOP:
return(62);
case RELOP:
return(63);
case e:
return(64);
default:
return(error);
}
case 85:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-32);
default:
return(error);
}
case 88:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-28);
76 Fundamentals of Compiler Design
default:
return(error);
}
case 90:
switch(a)
{
case SEMI:
return(105);
default:
return(error);
}
case 91:
switch(a)
{
case SEMI:
case RP:
return(-46);
default:
return(error);
}
case 92:
switch(a)
{
case SEMI:
case RP:
return(-47);
default:
return(error);
}
case 93:
switch(a)
{
case RC:
return(106);
default:
return(error);
}
case 94:
switch(a)
{
case RP:
return(107);
default:
return(error);
}
case 96:
switch(a)
{
case ID:
case NUM:
case CONST:
case UNOP:
case LP:
case RC:
case DO:
case FOR:
case IF:
case RETURN:
return(-17);
default:
return(error);
}
case 97:
switch(a)
{
Project For the Analysis and Design the Protocol of Compiler 77
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-37);
default:
return(error);
}
case 99:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-38);
default:
return(error);
}
case 100:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-39);
default:
return(error);
}
case 101:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-31);
default:
return(error);
}
case 102:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-33);
default:
return(error);
}
case 103:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-34);
default:
return(error);
}
case 104:
switch(a)
78 Fundamentals of Compiler Design
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-29);
default:
return(error);
}
case 106:
switch(a)
{
case WHILE:
return(111);
default:
return(error);
}
case 107:
switch(a)
{
case LC:
return(112);
default:
return(error);
}
case 108:
switch(a)
{
case ID:
case NUM:
case CONST:
case UNOP:
case LP:
case RC:
case DO:
case FOR:
case IF:
case RETURN:
return(-16);
default:
return(error);
}
case 109:
switch(a)
{
case BINOP:
case RELOP:
case SEMI:
case RP:
return(-41);
default:
return(error);
}
case 110:
switch(a)
{
case SEMI:
return(113);
default:
return(error);
}
case 111:
switch(a)
{
case LP:
Project For the Analysis and Design the Protocol of Compiler 79
return(114);
default:
return(error);
}
case 115:
switch(a)
{
case RC:
return(118);
default:
return(error);
}
case 116:
switch(a)
{
case RP:
return(119);
default:
return(error);
}
case 117:
switch(a)
{
case RP:
return(120);
default:
return(error);
}
case 118:
switch(a)
{
case ELSE:
return(122);
case e:
return(123);
default:
return(error);
}
case 119:
switch(a)
{
case LC:
return(124);
default:
return(error);
}
case 120:
switch(a)
{
case SEMI:
return(-48);
default:
return(error);
}
case 121:
switch(a)
{
case ID:
case NUM:
case CONST:
case UNOP:
case LP:
case RC:
case DO:
case FOR:
80 Fundamentals of Compiler Design
case IF:
case RETURN:
return(-42);
default:
return(error);
}
case 122:
switch(a)
{
case LC:
return(125);
default:
return(error);
}
case 123:
switch(a)
{
case ID:
case NUM:
case CONST:
case UNOP:
case LP:
case RC:
case DO:
case FOR:
case IF:
case RETURN:
return(-44);
default:
return(error);
}
case 126:
switch(a)
{
case RC:
return(128);
default:
return(error);
}
case 127:
switch(a)
{
case RC:
return(129);
default:
return(error);
}
case 128:
switch(a)
{
case ID:
case NUM:
case CONST:
case UNOP:
case LP:
case RC:
case DO:
case FOR:
case IF:
case RETURN:
return(-45);
default:
return(error);
}
case 129:
Project For the Analysis and Design the Protocol of Compiler 81
switch(a)
{
case ID:
case NUM:
case CONST:
case UNOP:
case LP:
case RC:
case DO:
case FOR:
case IF:
case RETURN:
return(-43);
default:
return(error);
}
}
}
case 5:
switch(a)
{
case S1:
return(9);
case fbody:
return(5);
default:
return(error);
}
case 11:
82 Fundamentals of Compiler Design
switch(a)
{
case S2:
return(13);
case S4:
return(14);
case fbody:
return(5);
default:
return(error);
}
case 15:
switch(a)
{
case S3:
return(18);
default:
return(error);
}
case 17:
switch(a)
{
case S4:
return(14);
default:
return(error);
}
case 19:
switch(a)
{
case S5:
return(25);
default:
return(error);
}
case 20:
switch(a)
{
case S2:
return(27);
default:
return(error);
}
case 22:
switch(a)
{
case tdec:
return(30);
default:
return(error);
}
case 26:
switch(a)
{
case S4:
return(33);
default:
return(error);
}
case 28:
switch(a)
{
Project For the Analysis and Design the Protocol of Compiler 83
case S3:
return(18);
default:
return(error);
}
case 30:
switch(a)
{
case stmt:
return(34);
case expr:
return(35);
case fcall:
return(44);
case ifelse:
return(38);
case forloop:
return(36);
case doloop:
return(37);
default:
return(error);
}
case 36:
switch(a)
{
case stmt:
return(53);
case expr:
return(35);
case fcall:
return(44);
case ifelse:
return(38);
case forloop:
return(36);
case doloop:
return(37);
default:
return(error);
}
case 38:
switch(a)
{
case stmt:
return(55);
case expr:
return(35);
case fcall:
return(44);
case ifelse:
return(38);
case forloop:
return(36);
case doloop:
return(37);
default:
return(error);
}
case 41:
switch(a)
{
case S7:
return(57);
case S8:
84 Fundamentals of Compiler Design
return(59);
default:
return(error);
}
case 42:
switch(a)
{
case S8:
return(65);
default:
return(error);
}
case 43:
switch(a)
{
case S8:
return(66);
default:
return(error);
}
case 44:
switch(a)
{
case S8:
return(67);
default:
return(error);
}
case 45:
switch(a)
{
case expr:
return(68);
case fcall:
return(44);
default:
return(error);
}
case 46:
switch(a)
{
case expr:
return(69);
case fcall:
return(44);
default:
return(error);
}
case 50:
switch(a)
{
case S6:
return(73);
default:
return(error);
}
case 52:
switch(a)
{
case stmt:
return(76);
case expr:
return(35);
case fcall:
return(44);
Project For the Analysis and Design the Protocol of Compiler 85
case ifelse:
return(38);
case forloop:
return(36);
case doloop:
return(37);
default:
return(error);
}
case 54:
switch(a)
{
case stmt:
return(77);
case expr:
return(35);
case fcall:
return(44);
case ifelse:
return(38);
case forloop:
return(36);
case doloop:
return(37);
default:
return(error);
}
case 56:
switch(a)
{
case stmt:
return(78);
case expr:
return(35);
case fcall:
return(44);
case ifelse:
return(38);
case forloop:
return(36);
case doloop:
return(37);
default:
return(error);
}
case 58:
switch(a)
{
case S9:
return(79);
default:
return(error);
}
case 60:
switch(a)
{
case expr:
return(84);
case fcall:
return(44);
default:
return(error);
}
case 61:
switch(a)
86 Fundamentals of Compiler Design
{
case S8:
return(85);
default:
return(error);
}
case 62:
switch(a)
{
case expr:
return(86);
case fcall:
return(44);
default:
return(error);
}
case 63:
switch(a)
{
case expr:
return(87);
case fcall:
return(44);
default:
return(error);
}
case 68:
switch(a)
{
case S8:
return(88);
default:
return(error);
}
case 70:
switch(a)
{
case S12:
return(90);
case expr:
return(91);
case fcall:
return(44);
default:
return(error);
}
case 71:
switch(a)
{
case stmt:
return(93);
case expr:
return(35);
case fcall:
return(44);
case ifelse:
return(38);
case forloop:
return(36);
case doloop:
return(37);
default:
return(error);
}
case 72:
Project For the Analysis and Design the Protocol of Compiler 87
switch(a)
{
case expr:
return(94);
case fcall:
return(44);
default:
return(error);
}
case 75:
switch(a)
{
case tdec:
return(96);
default:
return(error);
}
case 80:
switch(a)
{
case S10:
return(97);
default:
return(error);
}
case 81:
switch(a)
{
case S10:
return(99);
default:
return(error);
}
case 82:
switch(a)
{
case S10:
return(100);
default:
return(error);
}
case 84:
switch(a)
{
case S8:
return(101);
default:
return(error);
}
case 86:
switch(a)
{
case S8:
return(102);
default:
return(error);
}
case 87:
switch(a)
{
case S8:
return(103);
default:
return(error);
}
88 Fundamentals of Compiler Design
case 89:
switch(a)
{
case S10:
return(104);
default:
return(error);
}
case 95:
switch(a)
{
case S6:
return(108);
default:
return(error);
}
case 98:
switch(a)
{
case S9:
return(109);
default:
return(error);
}
case 105:
switch(a)
{
case S12:
return(110);
case expr:
return(91);
case fcall:
return(44);
default:
return(error);
}
case 112:
switch(a)
{
case stmt:
return(115);
case expr:
return(35);
case fcall:
return(44);
case ifelse:
return(38);
case forloop:
return(36);
case doloop:
return(37);
default:
return(error);
}
case 113:
switch(a)
{
case S12:
return(116);
case expr:
return(91);
case fcall:
return(44);
default:
return(error);
Project For the Analysis and Design the Protocol of Compiler 89
}
case 114:
switch(a)
{
case expr:
return(117);
case fcall:
return(44);
default:
return(error);
}
case 118:
switch(a)
{
case S11:
return(121);
default:
return(error);
}
case 124:
switch(a)
{
case stmt:
return(126);
case expr:
return(35);
case fcall:
return(44);
case ifelse:
return(38);
case forloop:
return(36);
case doloop:
return(37);
default:
return(error);
}
case 125:
switch(a)
{
case stmt:
return(127);
case expr:
return(35);
case fcall:
return(44);
case ifelse:
return(38);
case forloop:
return(36);
case doloop:
return(37);
default:
return(error);
}
}
}
hinac.c
#include<stdio.h>
#include<string.h>
#include<globalds.h>
#include<lex.h>
#include <parser.h>
90 Fundamentals of Compiler Design
#includeinit.c
#includelex.c
#includeparser.c
b=lex(argv[1]);
if(b!=NULL)
parser(b);
return(1);
}
int extension(char* source)
{
int len;
len = strlen(source);
GGG