LEXICAL ANALYSER:
Lexical analyser is the first phase of compiler.It just converts the sequence of characters to sequence of tokens.In my program,it reads the input sting or sequence of character and seperate that into tokens.
CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("lex.txt","r");
while((ch=fgetc(fp))!=EOF)
{
if(ch>47 && ch<58)
{
printf(" %c -> integer\n",ch);
}
switch(ch)
{
case '+':
printf(" %c -> addition operator\n",ch);
break;
case '-':
printf(" %c -> subtraction operator\n",ch);
break;
case '*':
printf(" %c -> Multiplication operator\n",ch);
break;
case '/':
printf(" %c -> division operator\n",ch);
break;
case '%':
printf(" %c -> modulo operator\n",ch);
break;
case '=':
printf(" %c -> equal operator\n",ch);
break;
case ';':
printf(" %c -> special symbol\n",ch);
break;
case '(':
printf(" %c -> Open Parameter\n",ch);
break;
case ')':
printf(" %c -> Close Parameter\n",ch);
break;
}
if((ch>64 && ch<91) || (ch>96 && ch<123))
{
printf(" %c -> identifier\n",ch);
}
}
fclose(fp);
getch();
}
Lexical analyser is the first phase of compiler.It just converts the sequence of characters to sequence of tokens.In my program,it reads the input sting or sequence of character and seperate that into tokens.
CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("lex.txt","r");
while((ch=fgetc(fp))!=EOF)
{
if(ch>47 && ch<58)
{
printf(" %c -> integer\n",ch);
}
switch(ch)
{
case '+':
printf(" %c -> addition operator\n",ch);
break;
case '-':
printf(" %c -> subtraction operator\n",ch);
break;
case '*':
printf(" %c -> Multiplication operator\n",ch);
break;
case '/':
printf(" %c -> division operator\n",ch);
break;
case '%':
printf(" %c -> modulo operator\n",ch);
break;
case '=':
printf(" %c -> equal operator\n",ch);
break;
case ';':
printf(" %c -> special symbol\n",ch);
break;
case '(':
printf(" %c -> Open Parameter\n",ch);
break;
case ')':
printf(" %c -> Close Parameter\n",ch);
break;
}
if((ch>64 && ch<91) || (ch>96 && ch<123))
{
printf(" %c -> identifier\n",ch);
}
}
fclose(fp);
getch();
}
TEXT FILE:
OUTPUT:
Leave me a comment if it has any bugs.

