Name:atif Ali Enrollment: (01-134191-008)
Name:atif Ali Enrollment: (01-134191-008)
Compiler Construction
LAB-6
PART-1 Test your Lexical analyzer by: Part-1 Results:
Part-1 Results:
g. Test keywords and identifiers and symbol
table.
If21 if while while123 xyzif foryes
// abcdefgh
//jjhhtyuuiii
()[]{},;:
([)]{,;]:
+ += ++ * == * * * ++ += ==<< >==
++++++******======➔>>>>>>>
Your task is to write a program which processes a file containing any number of
abovementioned keywords or operators. your program reads characters, groups them in an
internal buffer and if the characters in the buffer match with any of the above it prints a
message.
Strategy:
white space.
2- Read file character by character in a char variable.
#include<iostream>
#include<fstream>
#include<string>
#include<ctype.h>
#include<iomanip>
using namespace std;
enum TokenType
{
TRUE,FLASE,PLUS, PLUSEQUAL, MINUS, MINUSEQUAL, MUL, MULEQUAL, DIV,
DIVEQUAL,MOD, MODEQUAL,
UNDERSCORE,LSB, RSB, LRB, RRB, LCB, RCB, HASH, EQUAL, LESS, GREATER,
INCREMENT, DECREMENT, COLON, SEMICOLON, DOT, QUESTIONMARK, AND,
OR,OR, NOT, COMMA, NUM, ID, MAIN, IF, THEN, ELSE, BOOL, BREAK, CASE,
CHAR, CLASS, CONST, CONTINUE, DEFAULT, DELETE, DO, DOUBLE, ENUM, EXPLICIT, EXPOT1,
FALSE, FLOAT, FOR, GOTO, INLINE, INT, LONG, NAMESPACE, NEW,
OPERATOR, PRIVATE, PROTECTED, PUBLIC, RETURN, SIZEOF, STRUCT,
SWITCH, UNION, UNSIGNED, USING, VIT1UAL,
VOID, VOLATILE, WCHAR_T, WHILE
};
struct TokenRecord
{
TokenType TokenValue; string StringValue;
int numval;
};
c = f.get();
if (c == '/' || c == ' ' || c == '\t' || c == '\n')
{
if (c == '/')
{
c = f.get();
if (c == '/')
{
while (c != '\n')
{
c = f.get();
if (f.eof())
{
exit(0);
}
}
f.seekg(-1, ios::cur);
}
}
}
else if (c == ' ')
{
continue;
}
else if (c == '\t')
{
continue;
}
else if (c == '\n')
{
continue;
}
}
T1.TokenValue = DIVEQUAL; T1.StringValue = "/=";
cout << "<" << T1.StringValue << ">" << endl;
f.seekg(-1, ios::cur); T1.TokenValue = DIV; T1.StringValue = "/";
cout << "<" << T1.StringValue << ">" << endl;
else
{
if (c == ',' || c == ';' || c == '(' || c == ')' || c == '{' || c == '}' ||
c== '=' || c == '+' || c == '-' ||
c == '*' || c == '[' || c == ']' || c == '%' ||
c == '#' || c == '<' || c == '>' || c == ':' || c == '.' || c == '^' ||
c == '&' || c == '|' || c == '!' || c == '~' || c == '"' || c == '?' || c== '\'')
{
if (c == ',')
{
T1.TokenValue = COMMA; T1.StringValue = ",";
}
else if (c == ';')
{
T1.TokenValue = SEMICOLON; T1.StringValue = ";";
}
else if (c == '(')
{
T1.TokenValue = LRB; T1.StringValue = "(";
}
else if (c == ')')
{
T1.TokenValue = RRB; T1.StringValue = ")";
}
else if (c == '{')
{
T1.TokenValue = LCB; T1.StringValue = "{";
}
else if (c == '}')
{
T1.TokenValue = RCB; T1.StringValue = "}";
}
else if (c == '=')
{
c = f.get();
T1.TokenValue = EQUAL; T1.StringValue = "==";
f.seekg(-1, ios::cur); T1.TokenValue = ASSIGN; T1.StringValue = "=";
else if (c == '+')
{
c = f.get();
if (c == '+')
{
T1.TokenValue = INCREMENT; T1.StringValue = "++";
}
else
{
f.seekg(-1, ios::cur); T1.TokenValue = PLUS; T1.StringValue = "+";
}
}
else if (c == '-')
{
c = f.get();
if (c == '-')
{
T1.TokenValue = DECREMENT; T1.StringValue = "--";
}
else if (c == '=')
{
}
else
{
}
}
else if (c == '*')
{
c = f.get();
if (c == '=')
{
}
else
{
}
}
else if (c == '[')
{
T1.TokenValue = LSB; T1.StringValue = "[";
}
else if (c == ']')
{
}
else
{
}
}
else if (c == '#')
{
T1.TokenValue = HASH; T1.StringValue = "#";
}
else if (c == '<')
{
c = f.get();
if (c == '=')
{
T1.TokenValue = LE; T1.StringValue = "<=";
}
else if (c == '<')
{
}
else
{
}
}
else if (c == '>')
{
c = f.get();
if (c == '=')
{
}
else
{
}
}
else if (c == ':')
{
c = f.get();
if (c == ':')
{
}
else
{
}
}
T1.TokenValue = SCOPERESOLUTION;
T1.StringValue = "::";
else if (c == '.')
{
T1.TokenValue = DOT; T1.StringValue = ".";
}
else if (c == '^')
{
T1.TokenValue = POWER; T1.StringValue = "^";
}
else if (c == '&')
{
c = f.get();
if (c == '&')
{
T1.TokenValue = COMPARISONAND; T1.StringValue = "&&";
}
else
{
}
}
f.seekg(-1, ios::cur); T1.TokenValue = AND; T1.StringValue = "&";
else if (c == '|')
{
c = f.get();
if (c == '|')
{
}
else
{
}
}
else if (c == '!')
{
c = f.get();
if (c == '=')
{
}
else
{
}
}
else if (c == '~')
{
T1.TokenValue = TILDE; T1.StringValue = "~";
}
else if (c == '"')
{
T1.TokenValue = DICOMMA; T1.StringValue = '"';
}
else if (c == '?')
{
T1.TokenValue = QUESTIONMARK; T1.StringValue = '?';
}
else if (c == '\'')
{
T1.TokenValue = SICOMMA; T1.StringValue = '\'';
}
cout << "<" << T1.StringValue << ">" << endl;
}
else if (isdigit(c))
{
T1.TokenValue = NUM; T1.StringValue = c; while (isdigit(c))
{
c = f.get();
if (!isdigit(c))
{
break;
}
T1.StringValue += c;
}
endl;
f.seekg(-1, ios::cur);
cout << "<" << "NUM," << T1.StringValue << '>' <<
1
Compiler Construction
3- Check whether the character read is a space, tab, newline, if so skip ot and read next
character, --- we skip space characters because C language uses space characters for
formatting purpose only.
4- Consume C style comments starting with // consume all character until newline char
found.