-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler_assignment.cpp
More file actions
42 lines (41 loc) · 1.87 KB
/
Copy pathCompiler_assignment.cpp
File metadata and controls
42 lines (41 loc) · 1.87 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
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
ifstream file("myFIle.txt");
int i = 1;
while(getline(file,s)){
stringstream ss(s);
string word;
cout << "Line " << i <<":" << endl;
while(ss >> word){
if(word == ")" || word == "(" || word == "{" || word == "}" || word == "[" || word == "]" || word == ";" || word == "#"){
cout << word << " -> Puntuations, " << endl;
}
else if(word == "int" || word == "main" || word == "cout" || word == "return" || word == "using" || word == "namespace" || word == "include" || word == "iostream" || word == "cin" || word == "float" || word == "double" || word == "char" || word == "string" || word == "void"){
cout << word << " -> Keyword, " << endl;
}
else if((word[0] >= 'a' && word[0] <= 'z') || (word[0] >= 'A' && word[0] <= 'Z') || word[0] == '_'){
bool valid = true;
for(int i = 1; i<word.length(); i++){
if(!(word[i] >= 'a' && word[i] <= 'z') || (word[i] >= 'A' && word[i] <= 'Z') || (word[i] >= '0' && word[i] <= '9')){
valid = false;
break;
}
}
if(valid){
cout << word << " -> Identifier, " << endl;
}
}
else if(word == ">" || word == "<" || word == "=" || word == "+" || word == "-" || word == "*" || word == "/" || word == "%" || word == "<<" || word == ">>" || word == "<=" || word == ">=" || word == "&&" || word == "||" || word == "=="){
cout << word << " -> Operator, " << endl;
}
else{
cout << word << " -> Constant, " << endl;
}
}
cout << endl;
i++;
}
return 0;
}