0% found this document useful (0 votes)
8 views2 pages

Compilers Lab - Lecture 2

Uploaded by

shahadali.123987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Compilers Lab - Lecture 2

Uploaded by

shahadali.123987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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 < code.length(); ++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 < code.length(); ++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;
}

You might also like