-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetTokens.c
152 lines (128 loc) · 4.79 KB
/
getTokens.c
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
#include "header.h"
int myAtoi(char *);
char* infixToPostfix(char infx[]);
int isoperator(char ch)
{
if( (ch == '+')||(ch == '-')||(ch == '*')||(ch == '/')||
(ch == '=')||(ch == '!')||(ch == '>')||(ch == '<'))
return 1;
else
return 0;
}
void addSpaces(char *tok, char expr[20], int lineno)
{
int tkn_ind=0, exprsn_ind=0,l =strlen(tok);
while( l-- )
{
int n =0;
if(isoperator(tok[tkn_ind]))
{
expr[exprsn_ind++] = tok[tkn_ind++];
if(tok[tkn_ind] != '=')
expr[exprsn_ind++] =' ';
}
else if((tok[tkn_ind]>='a')&&(tok[tkn_ind]<='z'))
{
expr[exprsn_ind++] = tok[tkn_ind++];
expr[exprsn_ind++] =' ';
}
else if(isdigit(tok[tkn_ind]))
{
while(isdigit(tok[tkn_ind]))
expr[exprsn_ind++] =tok[tkn_ind++];
expr[exprsn_ind++] =' ';
}
else if(tok[tkn_ind] == ' ' || tok[tkn_ind] == '\n' || tok[tkn_ind] == '\0')
{
//...do nothing
}
else
{
printf("Syntax Error: Invalid identifier at Line no. %d\n", lineno );
printf("%s \n", tok );
exit(0);
}
}
expr[exprsn_ind] = '\0';
}
void getTokens(FILE *input,char Token[SIZE][5][LINESIZE],int *lastline)
{
char line[LINESIZE], expr[LINESIZE], del[4]=" \n\0";
char *tok, *postfix;
int j, i =*lastline;
while( fgets(line, LINESIZE, input))
{
j = 0;
tok = strtok(line, del);
//line no
strcpy(Token[i][j++],tok);
if( myAtoi(Token[i][0]) <= myAtoi(Token[i-1][0]) && i>0)
{ //printf("%s",line );
printf("Syntax Error: Invalid line no. Line no. :%d\n", i+1);
exit(0);
}
// Token[i][j++] = tok;
tok = strtok(NULL, del);
//keywords
strcpy(Token[i][j++],tok);
tok = strtok(NULL, del);
if( !strcmp(Token[i][j-1], "let"))
{
addSpaces(tok,expr, i+1);
postfix=infixToPostfix(expr);
strcpy(Token[i][j++],postfix);
}
else if(!strcmp(Token[i][j-1], "input")
|| !strcmp(Token[i][j-1], "print") || !strcmp(Token[i][j-1], "goto"))
{
addSpaces(tok,expr, i+1);
strcpy(Token[i][j++],expr);
if( strlen(expr) > 2 && strcmp(Token[i][j-2], "goto") )
{
printf("Syntax Error: Line Termination - %s at Line no. :%s\n", expr, Token[i][0]);
exit(0);
}
}
else if(!strcmp(Token[i][j-1], "if"))
{
addSpaces(tok,expr, i+1);
tok = strtok(NULL, del);
if(strcmp(tok,"goto"))
{
printf("Error: goto+branchaddress not present at line no. %d ", i+1);
exit(0);
}
else
{
strcpy(Token[i][3],"goto");
tok = strtok(NULL, del);
strcpy(Token[i][4],tok);
}
postfix=infixToPostfix(expr);
strcpy(Token[i][2],postfix);
//goto
}
else if(!strcmp(Token[i][j-1], "end\n") || !strcmp(Token[i][j-1], "end"))
{
//..do nothing
}
else if(!strcmp(Token[i][j-1], "rem\n") || !strcmp(Token[i][j-1], "rem"))
{ //IGNORE REST OF COMMENT
i++;
continue;
}
else
{
printf("Syntax Error: Wrong keyword - %s at Line no. :%d\n",Token[i][j-1], i+1);
exit(0);
}
char *empty = strtok(NULL,del);
if( empty != NULL )
{
printf("Syntax Error: Line Termination - %s at Line no. :%s\n", empty, Token[i][0]);
exit(0);
}
i++;
}
*lastline = i;
}