compilers || 3rd stage Shahad Ali
Compilers Lab - Lecture 2
Identifiers
#include <iostream>
#include <string>
using namespace std;
int main() {
string code = "int a56 = 7;";
string keywords[3] = {"int" , "if", "else"};
string x;
char c;
for (int i = 0; i < [Link](); ++i) {
c = code[i];
if (isalpha(c)) {
x = x+c;
}
else if (isdigit(c) && x!="") {
x = x+c;
}
else {
for (int j=0; j<3; j++) {
if(keywords[j] == x)
x="";
}
if(x!="") {
cout<<x<<"\tid\n"; x="";
}
}
}
return 0;
}
compilers || 3rd stage Shahad Ali
Operators
#include <iostream>
#include <string>
using namespace std;
int main() {
string code = "int a = 5 + 3;";
char c;
for (int i = 0; i < [Link](); ++i) {
c = code[i];
if (!isalpha(c) && !isdigit(c)) {
if (c == '=' || c == '+' || c == '-' || c == '*' || c == '%'
|| c == '/') {
cout << c << "\toperator\n";
} else if (c != '\n' && c != ' ' ) {
cout << c << "\tspecial character\n";
}
}
}
return 0;
}