-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic1.l
138 lines (106 loc) · 3.09 KB
/
basic1.l
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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symbol_table.h" // You would need to define symbol_table.h
int isKeyword(char* lexeme) {
// List of keywords (you can extend this list as needed)
char* keywords[] = {
"if", "else", "while", "for", "int", "float", "return", "break"
// Add more keywords here
};
int numKeywords = sizeof(keywords) / sizeof(keywords[0]);
for (int i = 0; i < numKeywords; i++) {
if (strcmp(lexeme, keywords[i]) == 0) {
return 1; // It's a keyword
}
}
return 0; // It's not a keyword
}
// Function to handle identifier tokens
void handleIdentifier(char* lexeme) {
// Check if it's a keyword
if (isKeyword(lexeme)) {
printf("Keyword: %s\n", lexeme);
} else {
printf("Identifier: %s\n", lexeme);
}
}
// Function to handle numeric tokens
void handleNumber(char* lexeme) {
// Add code here to detect numeric data types (int, float, etc.)
printf("Number: %s\n", lexeme);
}
// Function to handle string literals
void handleString(char* lexeme) {
// Add code here to handle escape sequences and other string-related tasks
printf("String: %s\n", lexeme);
}
%}
%option noyywrap
DIGIT [0-9]
LETTER [a-zA-Z]
NUMBER {DIGIT}+(\.{DIGIT}+)?
IDENTIFIER ({LETTER}|{DIGIT})*
STRING \"([^\"\\\n]|(\\.))*\"
WS [ \t\n\r]
OPERATOR [=+-/*]
%%
{WS}+ /* Ignore whitespace */
"/*" { /* Begin a C-style comment */
int c;
while ((c = input()) != EOF) {
if (c == '*') {
if ((c = input()) == '/') {
break;
}
}
}
}
"//" { /* Begin a C++-style comment */
int c;
while ((c = input()) != EOF && c != '\n') {
// Ignore characters until the end of the line
}
}
"#include" { /* Handle #include directive */
int c;
while ((c = input()) != EOF && c != '\n') {
// Collect characters for the included file name
// Resolve the file path and process it if necessary
}
}
{NUMBER} {
handleNumber(yytext);
}
{IDENTIFIER} {
// Check for valid identifier naming convention
if (yytext[0] >= '0' && yytext[0] <= '9') {
printf("Lexical Error: Invalid identifier naming convention: %s\n", yytext);
} else {
handleIdentifier(yytext);
}
}
{STRING} {
handleString(yytext);
}
{OPERATOR} { printf("Operator: %s\n", yytext);}
. { /* Unrecognized character */
printf("Unrecognized: %s\n", yytext);
}
%%
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
return 1;
}
FILE* input_file = fopen(argv[1], "r");
if (input_file == NULL) {
perror("Error opening file");
return 1;
}
yyin = input_file;
yylex();
fclose(input_file);
return 0;
}