-
Notifications
You must be signed in to change notification settings - Fork 5
/
trial
89 lines (87 loc) · 1.65 KB
/
trial
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
%{
#include "y.tab.h"
extern char * yylval;
int x=0;
%}
%%
"int" {x++;return INT;}
"float" {x++;return FLOAT;}
"char" {x++; return CHAR;}
"double" {x++;return DOUBLE;}
[a-z]+ {yylval=yytext;if(x>0) {return ID;} return 0;}
"," return C;
[n] return NL;
";" {x--;return SE;}
%%
%{
#include<stdio.h>
#include<string.h>
int err_no=0,fl=0,i=0,j=0,type[100];
char symbol[100][100],temp[100];
%}
%token ID NL SE C INT FLOAT CHAR DOUBLE
%%
START:S1 NL {return;}
;
S1: S NL S1
|S NL
;
S: INT L1 SE
|FLOAT L2 SE
|CHAR L3 SE
|DOUBLE L4 SE
|INT L1 SE S
|FLOAT L2 SE S
|CHAR L3 SE S
|DOUBLE L4 SE S
L1: L1 C ID{strcpy(temp,(char *)$3); insert(0);}
|ID{strcpy(temp,(char *)$1); insert(0);}
;
L2: L2 C ID{strcpy(temp,(char *)$3); insert(1);}
|ID{strcpy(temp,(char *)$1); insert(1);}
;
L3: L3 C ID{strcpy(temp,(char *)$3); insert(2);}
|ID{strcpy(temp,(char *)$1); insert(2);}
;
L4: L4 C ID{strcpy(temp,(char *)$3); insert(3);}
|ID{strcpy(temp,(char *)$1); insert(3);}
;
E: SE
;
%%
void yyerror(const char *str) {printf("error");}
int yywrap(){return 1;}
main()
{
yyparse();
if(err_no==0)
{
for(j=0;j<i;j++)
{
if(type[j]==0) printf("INT-");
if(type[j]==1) printf("FLOAT-");
if(type[j]==2) printf("CHAR-");
if(type[j]==3) printf("DOUBLE-");
printf("%sn",symbol[j]);
}
}
}
void insert(int type1)
{
fl=0;
for(j=0;j<i;j++)
{
if(strcmp(temp,symbol[j])==0)
{
if(type[i]==type1) printf("Redeclaration of variable");
else {printf("Multiple Declaration of Variable");err_no=1;}
fl=1;
}
if(fl==0)
{
type[i]=type1;
strcpy(symbol[i],temp);
i++;
}
}
}