#include <stdio.h>
#include <string.h>
int i,j,k,sign,flag,number,run;
Char ch;
Char word[10];
Char program[500];
int Scan (char program[])
{
Char *keyword[8]={' III ', ' begin ', ' if ', ' then ', ' while ', ' Do ', ' end '};
number=0;
flag=0;
j=0;
Ch=program[i++];
while (ch== ' \ n ' | | ch== ' | | ch== ' \ t ' | | ch== ' \ R ')
{
Ch=program[i++];
}
if (ch>= ' a ' &&ch<= ' Z ')
{
while (ch>= ' a ' &&ch<= ' Z ')
{
Word[j++]=ch;
Ch=program[i++];
}
i--;
word[j++]= ' + ';
for (k=0;k<8;k++)
{
if (strcmp (Word,keyword[k]) ==0)
{
flag=1;
sign=k+1;
}
Else
{
flag=0;
sign=300;
}//is a keyword, not an identifier//
Break
}
}
if (ch> ' 0 ' &&ch< ' 9 ')//is not a number//
{
number=0;
while (ch> ' 0 ' &&ch< ' 9 ')
{
number=number*10+ (ch-' 0 ');
Ch=program[i++];
}
sign=200;
i--;
}
Else
Switch (CH)
{
Case ' = ': {
if (ch== ' = ')
{
Word[j++]=ch;
word[j]= ' + ';
sign=25;//=//
}
Break
}
Case ' < ': {
if (ch== ' < ')
{
Word[j++]=ch;
word[j]= ' + ';
Ch=program[i++];
}
if (ch== ' = ')
{
Word[j++]=ch;
word[j]= ' + ';
sign=21;
}
Else
{
i--;
sign=20;
}
Break
}
Case ' > ': {
if (ch== ' > ')
{
Word[j++]=ch;
word[j]= ' + ';
Ch=program[i++];
}
if (ch== ' = ')
{
Word[j++]=ch;
word[j]= ' + ';
sign=24;
}
Else
{
i--;
sign=23;
}
Break
}
Case ' (': {
if (ch== ' (')
{
Word[j++]=ch;
word[j]= ' + ';
sign=27;
}
Break
}
Case ') ': {
if (ch== ') ')
{
Word[j++]=ch;
word[j]= ' + ';
sign=28;
}
Break
}
Case ': ': {
if (ch== ': ')
{
Word[j++]=ch;
word[j]= ' + ';
Ch=program[i++];
}
if (ch== ' = ')
{
Word[j++]=ch;
word[j]= ' + ';
sign=18;
}
Else
{
i--;
sign=17;
}
Break
}
Case ' # ': {
if (ch== ' # ')
{
Word[j++]=ch;
word[j]= ' + ';
sign=0;
}
Break
}
Case '; ': {
if (ch== '; ')
{
Word[j++]=ch;
word[j]= ' + ';
sign=26;
}
Break
}
Case ' + ': {
if (ch== ' + ')
{
Word[j++]=ch;
word[j]= ' + ';
sign=13;
}
Break
}
Case '-': {
if (ch== '-')
{
Word[j++]=ch;
word[j]= ' + ';
sign=14;
}
Break
}
Case ' * ': {
if (ch== ' * ')
{
Word[j++]=ch;
word[j]= ' + ';
sign=15;
}
Break
}
Case '/': {
if (ch== '/')
{
Word[j++]=ch;
word[j]= ' + ';
sign=16;
}
Break
}
Case ' @ ': {
if (ch== ' @ ')
{
Word[j++]=ch;
word[j]= ' + ';
sign=0;
}
}
return sign;
}
int main ()
{
int i=0,run=1,sign=0,sum=1,ent;
while (run)
{
Int J;
for (j=0;j<500;j++)
Program[j]= ";
for (j=0;j<10;j++)
Word[j]= ";
printf ("Please enter a section of source code");
do{
CH =getchar ();
Program[i++]=ch;
}while (ch!= ' @ ');
i=0;
do{
Sign=scan (program);
if (sign==13)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==14)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==15)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==16)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==17)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==18)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==20)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==21)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==22)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==24)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==25)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==26)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==27)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign==28)
{
printf ("Operator%8s%5d", word,sign);
sum++;
Ent=1;
}
if (sign>=1&&sign<=8)
{
printf ("Keyword%8s%5d", word,sign);
sum++;
Ent=1;
}
} while (sign!=0);
printf ("Do you want to continue?" Y or N ");
GetChar ();
Ch=getchar ();
if (ch== ' n ' | | ch== ' N ')
run=0;
}
return 0;
Lexical analysis is implemented in C + +