-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.h
173 lines (171 loc) · 3.05 KB
/
scanner.h
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
typedef struct keywords{
char *key;
int type;
}keywords;
const keywords keys[]={
{"start",START},
{"end",END},
{"print",PRINT}
};
Token string(char *str){
//printf("HI string\n");
Token token;
token.type=STRING;
//printf("Token type: %d\n",token.type );
int size=0,start=pos+1;
while(str[++pos]!='"'){
//printf("%c\n",str[pos] );
size++;
}
if(size>0)
token.id=str+(start);
else
token.id=NULL;
token.value=size;
//printf("pos after string %d\n",size);
return token;
}
int integer(char *str){
int i=0;
char *num;
num=(char*)malloc(sizeof(char));
num[i]=str[pos++];
while(str[pos]!='\0' && isdigit(str[pos])){
num=(char*)realloc(num,(++i)*sizeof(char));
num[i]=str[pos++];
}
//num[++i]='\0';
//printf("HI %s\n", num);
if(str[pos]=='.'){
num=(char*)realloc(num,(++i)*sizeof(char));
num[i]=str[pos++];
while(str[pos]!='\0' && isdigit(str[pos])){
num=(char*)realloc(num,(++i)*sizeof(char));
num[i]=str[pos++];
}
printf("%s\n", num);
}
pos--;
num[++i]='\0';
//printf("%s\n",num);
return atoi(num);
}
Token get_id(char *str){
//printf("Hi\n");
int i=0;
char *id;
Token token;
id=(char*)malloc(sizeof(char));
id[i]=str[pos++];
while(str[pos]!='\0' && isalnum(str[pos])){
id=(char*)realloc(id,(++i)*sizeof(char));
id[i]=str[pos++];
//printf("%d\n", i);
}
pos--;
id[++i]='\0';
for(i=0;i<3;i++){
//printf("Hi\n");
if(!strcmp(id,keys[i].key)){
token.id=id;
token.type=keys[i].type;
token.value=0;
//printf("Hi token is %s\n",id);
return token;
}
}
//printf("Hi token is %s\n",id);
token.id=id;
token.type=ID;
token.value=0;
return token;
//printf("%s\n",num);
}
Token scanner(char *str,char ch){
Token token;
//printf("ch=%c\n",ch);
while(ch== ' '||ch=='\n'){
ch=str[++pos];
//printf("%c\n",ch);
}
if(ch==';'){
//token=(ASSGN,0,NULL);
token.type=SEMI;
token.value=0;
token.id=NULL;
return token;
}
if(ch=='='){
//token=(ASSGN,0,NULL);
token.type=ASSIGN;
token.value=0;
token.id="=";
return token;
}
if(ch=='\0'){
token.type=EOF;
token.value=EOF;
token.id=NULL;
return token;
//printf("%c\n",ch);
}
if(ch=='('){
token.type=LPAREN;
token.value='(';
return token;
}
if(ch==')'){
token.type=RPAREN;
token.value=')';
token.id=NULL;
return token;
}
if(isalpha(ch)){
//token.type=ID;
//Token token=get_id(str);
//printf("%s",token.id);
return get_id(str);//token;
}
if(isdigit(ch)){
token.type=INT;
token.value=integer(str);
token.id=NULL;
return token;
}
if(ch=='+'){
token.type=SUM;
token.value='+';
token.id=NULL;
return token;
}
if(ch=='-'){
token.type=SUB;
token.value='-';
token.id=NULL;
return token;
}
if(ch=='*'){
token.type=MULT;
token.value='*';
token.id=NULL;
return token;
}
if(ch=='/'){
token.type=DIV;
token.value='/';
token.id=NULL;
return token;
}
if(ch==','){
token.type=COMMA;
token.value=0;
token.id=NULL;
return token;
}
if(ch=='"'){
return string(str);
}
printf(RED"[Interpretation error]"RESET"Scan error!! '%c' cannot be recognised as token!\n",ch);
exit(0);
//synerror();
}