C langugae only The first phase of compilation is called sca
C++ langugae only
The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser.
Your job is to write (in C++) a simple scanner for source code that obeys the language described below. You may assume that the input is syntactically correct.
Your program simply needs to read each token and then print out what type it is, based on the following language rules:
keyword -> if | then | else | begin | end | function | return identifier -> character | character identifier
integer -> digit | digit integer
real -> integer.integer
special -> + | - | = | < | >
digit -> 0|1|2|3|4|5|6|7|8|9 character -> a|b|c ... |z|A|B|C ... |Z
Solution
#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
//scan function definition
void scan(string code[], int len)
{
//Loop variable
int c, d;
//To store a word
string wo;
//Flag status for a word
int f = 0;
//Loops till end of the code entered by the user
for(c = 0; c < len; c++)
{
//Extract a word
wo = code[c];
//Checks for the keyword
if(wo == \"if\" | wo == \"then\" | wo == \"else\" | wo == \"begin\" | wo == \"end\" | wo == \"function\" | wo == \"return\" )
{
cout<<wo<<\": is a keyword\"<<endl;
}
else
{
//Loops till end of the word
for(d = 0; d < wo.length(); d++)
{
//Checks for the parenthesis
if(wo[d] == \'(\' || wo[d] == \')\')
{
f = 0;
break;
}
//Checks for the fractional number if true set the flag to 3
else if(isdigit(wo[d]) && wo[d+1] == \'.\')
{
f = 1;
break;
}
//Checks for the digit if true set the flag to 2
else if(isdigit(wo[d]))
{
f = 2;
break;
}
//Checks for the identifiers if true set the flag to 1
else if(isalnum(wo[d]))
{
f = 3;
break;
}
//Checks for the special character if true set the flag to 4
else if(wo[d] == \'+\' || wo[d] == \'+\' || wo[d] == \'-\' || wo[d] == \'=\' || wo[d] == \'<\' || wo[d] == \'>\')
{
f = 4;
break;
}
}//End of inner loop
if(f == 1)
cout<<wo<<\": is a Real number\"<<endl;
else if(f == 2)
cout<<wo<<\": is a Integer digit\"<<endl;
else if(f == 3)
cout<<wo<<\": is a Identifier\"<<endl;
else if(f == 4)
cout<<wo<<\": is a Special character\"<<endl;
}//End of else
}//End of outer loop
}//End of function
//Main function definition
int main()
{
//Declares a string array
string data[100];
//Declares a string
string st;
//To keep the length
int len = 0;
cout<<\"Enter you code (To stop enter \\\"finish\\\"): \";
do
{
cin>>st;
//If the entered string is finish then stop
if(st == \"finish\")
break;
//Stores data if it is not finish
data[len] = st;
///Increases the length
len++;
}while(1);
//Calls the scan method
scan(data, len);
}
Output:
Enter you code (To stop enter \"finish\"): function sum
a = 2
b = 2.5
if ( a > b )
then a = b
else b = a
return a + b
end
finish
function: is a keyword
sum: is a Identifier
a: is a Identifier
=: is a Special character
2: is a Integer digit
b: is a Identifier
=: is a Special character
2.5: is a Real number
if: is a keyword
a: is a Identifier
>: is a Special character
b: is a Identifier
then: is a keyword
a: is a Identifier
=: is a Special character
b: is a Identifier
else: is a keyword
b: is a Identifier
=: is a Special character
a: is a Identifier
return: is a keyword
a: is a Identifier
+: is a Special character
b: is a Identifier
end: is a keyword


