This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.Lexical_Analyzer_using_c_MINIMIZED.c
174 lines (153 loc) · 4.09 KB
/
2.Lexical_Analyzer_using_c_MINIMIZED.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// default keywords, symbols and operators of 'c'.
const char keywords [32][10] = { "int", "char", "short", "float", "auto", "double", "struct", "break", "if", "else",
"long", "switch", "case", "enum", "register", "typedef","extern", "return", "union",
"const", "unsigned", "continue", "for", "signed", "void", "default", "goto", "sizeof",
"volatile", "do", "static", "while" };
int main()
{
// Declaring variables
FILE *ptr;
char ch, buffer[30] = "\0";
int lines_count = 0;
// Function prototyping
int is_keyword ( const char buffer [] );
int is_identifier ( const char buffer [] );
int is_operator ( const char ch );
int is_special_symbol ( const char ch );
// Assigning file address to file pointer
ptr = fopen ("code.c", "w");
// displaying prompt before writing into the file
system("cls");
fflush(stdin);
puts("Enter the code : \n");
// writing data into file
while( (ch = getchar()) != EOF )
putc(ch,ptr);
// reassigning file pointer as read mode
fclose(ptr);
ptr = fopen("code.c", "r");
// Prompt before displaying the tokens.
puts("\n--------------------------------------------");
puts("\t\tTokens");
puts("\n--------------------------------------------\n");
// token generator logic
for ( int i = 0; (ch = getc(ptr)) != EOF; i = 0, buffer[0] = '\0' )
{
// check only for identifiers and keywords
if(isalpha(ch) || ch == '_')
{
buffer[i++] = ch;
while( (ch = getc(ptr)) != EOF )
{
if( isalnum(ch) || ch == '_' )
buffer[i++] = ch;
else
break;
}
buffer[i] = '\0';
if( is_keyword ( buffer ) )
printf("%-10s is an keyword.\n",buffer);
else if( is_identifier( buffer ) )
printf("%-10s is an identifier.\n",buffer);
else
printf("%-10s is an unknown token.\n",buffer);
ungetc(ch,ptr);
}
// check only for numericals ( like intergers, floating point numbers )
else if ( isdigit(ch) )
{
buffer[i++] = ch;
while( (ch = getc(ptr) ) != EOF )
{
if( isdigit(ch) )
buffer[i++] = ch;
else
break;
}
printf("%-10d is an numerical.\n", atoi(buffer));
ungetc(ch,ptr);
}
// check for comments
else if ( ch == '/' )
{
// storing in buffer and reading the next element in the file
buffer[i++] = ch;
ch = getc(ptr);
if( ch == '/' )
{
while( (ch = getc(ptr)) != EOF || (ch = getc(ptr)) != '\n' )
continue;
if( ch == '\n' )
lines_count++;
}
else if ( ch == '*' )
{
while( (ch = getc(ptr)) != EOF || ( (ch = getc(ptr)) != '/' && buffer[strlen(buffer)-1] != '*' ) )
{
if( ch == '\n' )
lines_count++;
}
}
else
{
printf("%-10c is an operator.\n",buffer[0]);
ungetc(ch,ptr);
}
}
// check only for special symbols and operator
else if( ch != ' ' && ch != '\n' && ch != '\t' )
{
if( is_operator(ch) )
printf("%-10c is an operator.\n", ch);
else if ( is_special_symbol(ch) )
printf("%-10c is a special symbol.\n", ch );
}
// check for the delimitors
else
{
// counting the number of lines
if( ch == '\n' )
lines_count++;
continue;
}
}
printf("\nNumber of lines : %d", lines_count);
fclose(ptr);
return 0;
}
int is_keyword ( const char buffer [] )
{
for( int i = 0; i < 32; ++i )
if( strcmp( buffer, keywords[i] ) == 0 )
return 1;
return 0;
}
int is_identifier ( const char buffer [] )
{
if( buffer[0] == '_' || isalpha(buffer[0]) )
{
if( strlen(buffer) == 1 )
return 1;
int i,flag = 1;
for( i = 1; i < strlen(buffer); ++i )
flag = ( isalnum(buffer[i]) ) ? 0 : 1;
return (flag) ? 0 : 1;
}
return 0;
}
int is_operator ( const char ch )
{
if ( ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '!' || ch == '&' || ch == '<' || ch == '>' || ch == '=' || ch == '|' )
return 1;
return 0;
}
int is_special_symbol ( const char ch )
{
if ( ch == ',' || ch == '.' || ch == '(' || ch == ')' || ch == ';' || ch == '$' || ch == ':' || ch == '#' || ch == '[' || ch == ']' || ch == '{' || ch == '}' || ch == '~' || ch == '-' || ch == '?' )
return 1;
return 0;
}