-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab4.c
More file actions
126 lines (104 loc) · 3.08 KB
/
Lab4.c
File metadata and controls
126 lines (104 loc) · 3.08 KB
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 50
typedef struct {
char name[MAX];
int line;
} Symbol;
Symbol global[MAX], local[MAX];
int g_count = 0, l_count = 0, pos = 0, line = 1, scope = 0;
char input[1000];
char* keywords[] = {"int", "float", "char", "if", "else", "while", "for", "return", "void"};
int isKeyword(char* str) {
for (int i = 0; i < 9; i++)
if (strcmp(str, keywords[i]) == 0) return 1;
return 0;
}
void addSymbol(char* name) {
if (scope == 0) {
for (int i = 0; i < g_count; i++)
if (strcmp(global[i].name, name) == 0) return;
strcpy(global[g_count].name, name);
global[g_count++].line = line;
} else {
for (int i = 0; i < l_count; i++)
if (strcmp(local[i].name, name) == 0) return;
strcpy(local[l_count].name, name);
local[l_count++].line = line;
}
}
char* getNextToken() {
static char token[MAX];
// Skip whitespace
while (input[pos] && isspace(input[pos])) {
if (input[pos] == '\n') line++;
pos++;
}
if (!input[pos]) return NULL;
int i = 0;
// Identifier or Keyword
if (isalpha(input[pos]) || input[pos] == '_') {
while (isalnum(input[pos]) || input[pos] == '_')
token[i++] = input[pos++];
token[i] = '\0';
if (!isKeyword(token)) addSymbol(token);
return token;
}
// Number
if (isdigit(input[pos])) {
while (isdigit(input[pos]) || input[pos] == '.')
token[i++] = input[pos++];
token[i] = '\0';
return token;
}
// Operators
if (strchr("+-*/%=<>!&|", input[pos])) {
token[0] = input[pos++];
if (strchr("=&|", input[pos]) && input[pos] == token[0])
token[1] = input[pos++], token[2] = '\0';
else
token[1] = '\0';
return token;
}
// Delimiters
if (strchr("(){}[];,", input[pos])) {
if (input[pos] == '{') scope = 1;
if (input[pos] == '}') scope = 0;
token[0] = input[pos++];
token[1] = '\0';
return token;
}
token[0] = input[pos++];
token[1] = '\0';
return token;
}
void displayTable(Symbol* table, int count, char* name) {
printf("\n=== %s SYMBOL TABLE ===\n", name);
printf("%-20s %s\n", "Name", "Line");
printf("------------------------\n");
if (count == 0) printf("(Empty)\n");
for (int i = 0; i < count; i++)
printf("%-20s %d\n", table[i].name, table[i].line);
}
int main() {
char* code =
"int main() {\n"
" int x, y;\n"
" float result;\n"
" x = 10;\n"
" if (x > 5) {\n"
" int temp;\n"
" }\n"
"}\n"
"int globalVar;\n";
strcpy(input, code);
printf("Source Code:\n%s\n", code);
printf("\n=== TOKENS ===\n");
char* token;
while ((token = getNextToken()) != NULL)
printf("%s ", token);
displayTable(global, g_count, "GLOBAL");
displayTable(local, l_count, "LOCAL");
return 0;
}