# Include <iostream>
# Include <fstream>
# Include <string>
Using namespace STD;
Char word [5] [10] = {"int", "char", "float", "for", "Return"}; // keyword
Char oprator [11] = {'+', '-', '*', '/', '<', '>', '= ','(', ')', '{', '}; // keyword
String line [100];
Int line_num = 0;
Bool isword (char CHR []) // keyword Determination
{
Bool flag = false;
For (INT I = 0; I <5; I ++)
{
If (strcmp (word [I], CHR) = 0)
{
Flag = true;
Break;
}
}
Return flag;
}
Bool isoprator (char ch) // operator Determination
{
Bool flag = false;
For (INT I = 0; I <11; I ++)
{
If (oprator [I] = CH)
{
Flag = true;
Break;
}
}
Return flag;
}
Int main ()
{
Ifstream input ("1.txt", IOS: In); // source file
If (! Input)
{
Cerr <"Open error! "<Endl;
Exit (1 );
} Else {
Cout <"Source code:" <Endl;
While (Getline (input, line [line_num]) // read a row
{
Cout <line [line_num] <Endl;
Line_num ++;
}
Cout <Endl <"lexical analysis result:" <Endl;
}
For (INT I = 0; I <line_num; I ++ ){
For (Int J = 0; j <line [I]. Length (); j ++ ){
While (line [I] [J] = ''){
J ++;
}
Char CH = line [I] [J];
If (CH> = 'A' & Ch <= 'Z') // if it is a character
{
Int K = 0;
Char A [10];
While (CH> = 'A' & Ch <= 'Z' | ch> = '0' & Ch <= '9 ')
{
If (k <10)
{
A [k] = CH;
K ++;
}
A [k] = '\ 0 ';
J ++;
Ch = line [I] [J];
}
If (isword (A) = true ){
Cout <"keyword" <A <Endl;
} Else {
Cout <"identifier" <A <Endl;
}
If (k <10 & isoprator (CH )){
Cout <"operator" <ch <Endl;
}
} Else {
If (CH> = '0' & Ch <= '9') // if it is a number
{
Int K = 0;
Char num [10];
While (CH> = '0' & Ch <= '9 ')
{
If (k <10)
{
Num [k] = CH;
K ++;
}
If (line [I] [J] = ''){
}
Num [k] = '\ 0 ';
J ++;
Ch = line [I] [J];
}
Cout <"digit string" <num <Endl;
If (k <10 & isoprator (CH )){
Cout <"operator" <ch <Endl;
}
} Else
{
If (isoprator (CH )){
Cout <"operator" <ch <Endl;
} Else {
// Cout <"not recognized" <ch <Endl;
}
}
}
}
}
Input. Close ();
Return 0;
}
Running result: