Professional Documents
Culture Documents
Compiler Construction: Group Members
Compiler Construction: Group Members
Lab: #03
Group Members:
Toheed Ejaz Khan ▷ 01-134192-108
int main()
{
char character;
fstream file;
file.open("TEST.txt");
if (!file.is_open())
{
cout << "Error opening file \n";
}
else
{
while (file.get(character))
{
if (character != '\n' && character != '\t' && character != ' ')
{
if (character == '/')
{
file.get(character);
if (character == '/')
{
while (file.get(character))
{
if (character == '\n')
{
break;
}
}
}
}
if (character == ',')
{
cout << "COMMA\n";
}
if (character == ']')
{
cout << "RCSB\n";
}
else
{
//file.putback(character);
cout << "PLUS\n";
}
}
}
Output:
Analysis:
Here is an exception rising at the run time that the last minus sign is getting neglect. It happens because
at the end when code encounter an (-) sign it doesn’t recognize it so we are applying a new technique in
the next task that we are moving the cursor backward once or putting the minus character back into the
file so our code can recognize it.
Text file:
Task #02
Code:
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
int main()
{
char character;
fstream file;
file.open("TEST.txt");
if (!file.is_open())
{
cout << "Error opening file \n";
}
else
{
while (file.get(character))
{
if (character != '\n' && character != '\t' && character != ' ')
{
if (character == '/')
{
file.get(character);
if (character == '/')
{
while (file.get(character))
{
if (character == '\n')
{
break;
}
}
}
}
if (character == ',')
{
cout << "COMMA\n";
}
if (character == ']')
{
cout << "RCSB\n";
}
else
{
file.putback(character);
cout << "PLUS\n";
}
}
}
Analysis:
Now our code is recognizing the negative sign it the text file we use the C++ built in function
{putback(ch)}.
Task #03
Code:
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
int main()
{
char character;
fstream file;
file.open("TEST.txt");
if (!file.is_open())
{
cout << "Error opening file \n";
}
else
{
while (file.get(character))
{
if (character != '\n' && character != '\t' && character != ' ')
{
if (character == '/')
{
file.get(character);
if (character == '/')
{
while (file.get(character))
{
if (character == '\n')
{
break;
}
}
}
}
if (character == ',')
{
cout << "COMMA\n";
}
if (character == ']')
{
cout << "RCSB\n";
}
else
{
file.putback(character);
cout << "PLUS\n";
}
do {
file.get(character);
s += character;
} while (isdigit(character));
file.putback(character);
cout << "NUM\n";
}
}
}
THE END.