0% found this document useful (0 votes)
63 views15 pages

Name:atif Ali Enrollment: (01-134191-008)

The document describes a program that performs lexical analysis on a file containing C/C++ code. It tests the lexical analyzer by: 1) Calling it with an invalid file name 2) Submitting an empty file 3) Checking handling of single-line comments 4) Testing single character tokens 5) Testing operators with and without spaces 6) Mixing single character tokens and operators 7) Testing keywords, identifiers, and symbol table The program strategy is to: 1) Open the input file 2) Read it character by character 3) Group characters into tokens based on the lexical rules 4) Print token names if they match predefined types

Uploaded by

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

Name:atif Ali Enrollment: (01-134191-008)

The document describes a program that performs lexical analysis on a file containing C/C++ code. It tests the lexical analyzer by: 1) Calling it with an invalid file name 2) Submitting an empty file 3) Checking handling of single-line comments 4) Testing single character tokens 5) Testing operators with and without spaces 6) Mixing single character tokens and operators 7) Testing keywords, identifiers, and symbol table The program strategy is to: 1) Open the input file 2) Read it character by character 3) Group characters into tokens based on the lexical rules 4) Print token names if they match predefined types

Uploaded by

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

Name:atif ali Enrollment:(01-134191-008)

Compiler Construction

LAB-6
PART-1 Test your Lexical analyzer by: Part-1 Results:

a. Call with wrong file name.


b. Submit an empty file and see how your lex
behaves.
c. Test whether your lexical analyzer handles
single c style one-line comments.
// abcdefgh
//jjhhtyuuiii

d. Test single character lexical units. With


and without space in between
()[]{},;:
([)]{,;]:

e. Test operators with and without spaces in


between
+ += ++ * == * * * ++ += ==<< >==
++++++******======➔>>>>>>>
f. Mix single character and operators (+-) ==

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:

1- Open input text file in read mode Task 1. Skip

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;
};

TokenRecord T1; char c;


TokenRecord Table[100]; int Size = 0;
int Search(string);
void Entry(TokenType, string); TokenRecord LexicalAnalyzer();
int main()
{
LexicalAnalyzer(); return 0;
}
int Search(string a)
{
for (int i = 0; i <= Size; i++)
{
if (a == Table[i].StringValue)
{
return i;
}
}
T1.TokenValue = ID;
T1.StringValue = a;
Entry(T1.TokenValue, T1.StringValue); return Size - 1;
}

void Entry(TokenType token, string value)


{
Table[Size].TokenValue = token; Table[Size].StringValue = value; Size++;
}
TokenRecord LexicalAnalyzer()
f.open("Hamza.txt", ios::in);
if (!f)
{
cout<<"File has been opened sucessfully"<<endl;
}
else
{
cout << "File couldn't be opened." << endl;
}
while (!f.eof())
{

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

T1.TokenValue = PLUSEQUAL; T1.StringValue = "+=";

{
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
{

}
}

T1.TokenValue = MINUSEQUAL; T1.StringValue = "-=";

f.seekg(-1, ios::cur); T1.TokenValue = MINUS; T1.StringValue = "-";

else if (c == '*')
{
c = f.get();
if (c == '=')
{

}
else
{

}
}

T1.TokenValue = MULEQUAL; T1.StringValue = "*=";

f.seekg(-1, ios::cur); T1.TokenValue = MUL; T1.StringValue = "*";

else if (c == '[')
{
T1.TokenValue = LSB; T1.StringValue = "[";
}
else if (c == ']')
{

T1.TokenValue = RSB; T1.StringValue = "]";


}
else if (c == '%')
{
c = f.get();
if (c == '=')
{

}
else
{
}
}

T1.TokenValue = MODEQUAL; T1.StringValue = "%=";

f.seekg(-1, ios::cur); T1.TokenValue = MOD; T1.StringValue = "%";

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
{

}
}

T1.TokenValue = ShiftL; T1.StringValue = "<<";

f.seekg(-1, ios::cur); T1.TokenValue = LESS; T1.StringValue = "<";

else if (c == '>')
{
c = f.get();
if (c == '=')
{

T1.TokenValue = GE; T1.StringValue = ">=";


}
else if (c == '>')
{

}
else
{
}
}

T1.TokenValue = ShiftR; T1.StringValue = ">>";

f.seekg(-1, ios::cur); T1.TokenValue = GREATER; T1.StringValue = ">";

else if (c == ':')
{
c = f.get();
if (c == ':')
{

}
else
{

}
}

T1.TokenValue = SCOPERESOLUTION;
T1.StringValue = "::";

f.seekg(-1, ios::cur); T1.TokenValue = COLON; 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
{

}
}

T1.TokenValue = COMPARISONOR; T1.StringValue = "||";

f.seekg(-1, ios::cur); T1.TokenValue = OR; T1.StringValue = "|";

else if (c == '!')
{
c = f.get();
if (c == '=')
{

}
else
{

}
}

T1.TokenValue = NE; T1.StringValue = "!=";

f.seekg(-1, ios::cur); T1.TokenValue = NOT; T1.StringValue = "!";

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 << '>' <<

else if (c == '_' || isalpha(c))


{
T1.StringValue = c;
while (isalpha(c) || c == '_' || isdigit(c))
{
c = f.get();
if (c != '_' && !isalpha(c) && !isdigit(c))
{
break;
}
T1.StringValue += c;
}
f.seekg(-1, ios::cur);
if (T1.StringValue == "main" || T1.StringValue == "if" || T1.StringValue == "then" || T1.StringValue
== "else" || T1.StringValue == "asm" || T1.StringValue == "auto" || T1.StringValue == "bool" ||
T1.StringValue == "break" || T1.StringValue == "case" || T1.StringValue == "catch" || T1.StringValue
== "char" || T1.StringValue == "class" || T1.StringValue
== "const" || T1.StringValue == "continue" || T1.StringValue == "default" || T1.StringValue ==
"delete" || T1.StringValue == "do" || T1.StringValue == "double" || T1.StringValue == "enum" ||
T1.StringValue == "explicit" || T1.StringValue == "expoT1" || T1.StringValue == "extern" ||

T1.StringValue == "false" || T1.StringValue == "float" || T1.StringValue == "for" || T1.StringValue ==


"friend" || T1.StringValue == "goto" || T1.StringValue == "inline" || T1.StringValue == "int" ||
T1.StringValue == "long" || T1.StringValue == "mutable" || T1.StringValue == "namespace" ||
T1.StringValue == "new" || T1.StringValue == "operator" || T1.StringValue == "private" ||
T1.StringValue == "protected" || T1.StringValue == "public" || T1.StringValue == "register" ||
T1.StringValue == "return" || T1.StringValue == "shoT1" || T1.StringValue == "signed" ||
T1.StringValue == "sizeof" || T1.StringValue == "static" || T1.StringValue == "struct" ||
T1.StringValue == "switch" || T1.StringValue == "template" || T1.StringValue == "this" ||
T1.StringValue == "throw" || T1.StringValue == "true" || T1.StringValue == "try" || T1.StringValue
== "typedef" || T1.StringValue == "typeid" || T1.StringValue == "typename" || T1.StringValue ==
"union" || T1.StringValue == "unsigned" || T1.StringValue == "using" || T1.StringValue == "viT1ual"
|| T1.StringValue == "void" || T1.StringValue == "volatile" || T1.StringValue == "wchar_t" ||
T1.StringValue == "while")

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.

Task 2. Detect single character patterns and return corresponding tokens.

Task 3. Detect character patterns for operators and return corresponding


tokens.

Task 4. Detect integer numbers and return corresponding tokens.

Task 5. Detect key words return corresponding tokens.

Task 6. Detect identifiers return corresponding tokens.


Task -7 Make a symbol table for identifiers with the following functions: entry

*Search(string) . entry * make_entry(string) .

Get your code checked by lab engineer.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy