-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
184 lines (161 loc) · 5.9 KB
/
Copy pathmain.cpp
File metadata and controls
184 lines (161 loc) · 5.9 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
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
175
176
177
178
179
180
181
182
183
184
#include <bits/stdc++.h>
#include <fstream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;
vector<string>op;
vector<string>id;
vector<string>key;
vector<string>num;
bool isIdentifierStart(char ch) {
return isalpha(ch) || ch == '_'; // Allow underscore as identifier start
}
bool isIdentifierNowhere(char ch) {
return isalnum(ch) || ch == '_'; // Allow underscore in identifiers
}
bool isKeyword(string str) {
string keywords[] = {"int", "float", "double", "if", "else", "while", "for", "return", "void", "main", "char", "bool", "true", "false", "using", "namespace", "std"};
return find(begin(keywords), end(keywords), str) != end(keywords);
}
bool isPunctuation(char ch) {
string punc = ".,;:(){}[]";
return punc.find(ch) != string::npos;
}
bool isOperator(const string &code, int start, int length) {
const string operators[] = {"+", "-", "*", "/", "%", "=", "==", "+=", "-=", "*=", "/=", "%=", "<", "<<", "<=", ">", ">>", ">=", "!", "!=", "&", "&&", "|", "||", "^", "~", "<<=", ">>="};
string potentialOperator = code.substr(start, length);
return find(begin(operators), end(operators), potentialOperator) != end(operators);
}
void lexical_analysis(const string &code) {
int length = code.length();
for (int i = 0; i < length; i++) {
// Skip single line comments
if (i < length - 1 && code[i] == '/' && code[i + 1] == '/') {
int x = i;
int y = x;
while (i < length && code[i] != '\n') {
i++;
}
//number of line
int cnt1=0;
for(int j = 0; j<=i; j++){
if(code[j] =='\n'){
cnt1++;
}
}
cout<<"single line comment at line:"<<cnt1<<endl;
while (x < code.length() && code[x] != '\n') {
x++;
}
cout<<code.substr(y,x-y)<<endl;
cout<<endl;
cout<<"length of commment:"<<x-y<<endl;
}
// Skip multiline comments
else if (i < length - 1 && code[i] == '/' && code[i + 1] == '*') {
i += 2;
int x = i;
while (i < length - 1 && !(code[i] == '*' && code[i + 1] == '/')) {
i++;
}
i += 1; // Skip the closing */
//number of line
int y = i;
int cnt2=0;
for(int j = 0; j<=i+1; j++){
if(code[j] =='\n'){
cnt2++;
}
}
cout<<"multi line comment at line:"<<cnt2<<endl;
cout<<code.substr(x,(y-x+1))<<endl;
cout<<"length of commment:"<<y-x+1<<endl;
}
// Identify string literal
else if (code[i] == '"') {
int start = ++i;
while (i < length && code[i] != '"') {
i++;
}
cout << "String literal found: " << code.substr(start, i - start) << endl;
}
// Identify identifiers & keywords
else if (isIdentifierStart(code[i])) {
int start = i;
while (i < length && isIdentifierNowhere(code[i])) {
i++;
}
string k = code.substr(start, i - start);
if (!isKeyword(k)) {
// cout << "Identifier found: " << k << endl;
id.push_back(k);
} else {
// cout << "Keyword found: " << k << endl;
key.push_back(k);
}
i--; // Adjust i because it will be incremented in the loop
}
// Identify numbers
else if (isdigit(code[i])) {
int start = i;
while (i < length && isdigit(code[i])) {
i++;
}
//cout << "Number found: " << code.substr(start, i - start) << endl;
num.push_back(code.substr(start, i - start));
i--; // Adjust i because it will be incremented in the loop
}
// Identify operators
else if (isOperator(code, i, 3)) { //i = 2,3,4,, i =5
//cout << "Operator found: " << code.substr(i, 3) << endl;
op.push_back(code.substr(i, 3));
i += 2; // Move index forward by 2 to skip next characters (since the length is 3)
}
else if (isOperator(code, i, 2)) {
//cout << "Operator found: " << code.substr(i, 2)<< endl;
i += 1; // Move index forward by 1 to skip next character (since the length is 2)
op.push_back(code.substr(i, 2));
}
else if (isOperator(code, i, 1)) {
//cout << "Operator found: " << code[i] << endl;
// No need to increment i here since length is 1
op.push_back(code.substr(i, 1));
}
// Identify punctuation
else if (isPunctuation(code[i])) {
cout << "Punctuation found: " << code[i] << endl;
}
}
}
int main() {
ifstream file("Y:\\1.compiler design\\example.cpp");
if (!file.is_open()) {
cout << "Error opening file" << endl;
return 1; // Exit if file cannot be opened
}
// Read the file contents into a string
string code((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
// Close the file
file.close();
// Perform lexical analysis
lexical_analysis(code);
// Print results
cout << "\n--- Identifiers ---" << endl;
for (const string &identifier : id) {
cout << identifier << endl;
}
cout << "\n--- Keywords ---" << endl;
for (const string &keyword : key) {
cout << keyword << endl;
}
cout << "\n--- Operators ---" << endl;
for (const string &operatorToken : op) {
cout << operatorToken << endl;
}
cout << "\n--- Numbers ---" << endl;
for (const string &number : num) {
cout << number << endl;
}
return 0;
}