Professional Documents
Culture Documents
CC Assignment#3 Lexical AnalyzerC++ M.shayan 1755-2020 Muhammad Hassan
CC Assignment#3 Lexical AnalyzerC++ M.shayan 1755-2020 Muhammad Hassan
Task: 1. Choose a Compiler Phase: Select one of the following compiler phases: • Lexical
Analysis • Syntax Analysis • Semantic Analysis • Intermediate Code Generation • Code
Optimization • Code Generation
1- LEXICAL ANALYZER.
1- C++.
Component Development: Implement the chosen compiler phase using the selected platform.
Clearly document the design decisions and algorithms employed. Ensure proper error handling
and reporting.
Submission Guidelines: Submit the assignment in written form, including code snippets,
diagrams, and explanations as needed. Include a README file providing instructions on how to
build and run your compiler component. Clearly mention the compiler phase chosen and the
platform used.
LEXICAL ANALYZER:
Code C++:
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
if (isKeyword(sub) == true)
{
cout<< sub <<" IS A KEYWORD\n";
}
else if (isNumber(sub) == true)
{
cout<< sub <<" IS A NUMBER\n";
}
else if (validIdentifier(sub) == true
&& isPunctuator(str[right - 1]) == false)
{
cout<< sub <<" IS A VALID IDENTIFIER\n";
}
else if (validIdentifier(sub) == false
&& isPunctuator(str[right - 1]) == false)
{
cout<< sub <<" IS NOT A VALID IDENTIFIER\n";
}
left = right;
}
}
return;
}
int main()
{
char c[100] = "int m = n + 3p";
parse(c);
return 0;
}