You are on page 1of 3

Ex 1: convert Lexeme into Token

Ex 2: Implementation of a Symbol Table

Ex. No: 3

DEVELOP A LEXICAL ANALYZER

AIM:
To Create a Lexical Analyzer using C programming and to create lexemes that are grouped
into various categories.
ALGORITHM:
Step 1: Start the program.
Step 2: Create some character arrays to store and manipulate the characters.
Step 3: Create a file pointer and get the name of the input file to read the input from.
Step 4: Open the input file using the read category.
Step 5: Copy the content of the file into a string and then copy it to a character array for processing.
Step 6: Use a while loop and browse the content of the file till the end.
Step 7: Using a if condition, Separate some symbols like ;<>{}()#,& and print them as Special
characters.
Step 8: Using a if condition, print the letters/words following the int,char or float as the Variables.
Step 9: Using a if condition, print the words printf,scanf,main,void etc as the keywords.
Step 10: Terminate the program.

PROGRAM:
#include<stdio.h>
#include<string.h>
int main()
{
char tot[100][20];
char a[100],temp[100];
int i=0,j;
FILE *p;
p=fopen("input1.txt","r");
fscanf(p,"%s",a);
strcpy(tot[i],a);
strcpy(temp,"Null");
printf("\nLexeme\tToken\n\n");
i=1;
while(strcmp(a,"END")!=0)
{
if((strcmp(a,";")==0)||(strcmp(a,"<")==0)||(strcmp(a,"{")==0)||(strcmp(a,"(")==0)||(strcmp(a,")")==0)||
(strcmp(a,"}")==0)||(strcmp(a,"#")==0)||(strcmp(a,">")==0)||(strcmp(a,",")==0)||(strcmp(a,"&")==0))
{
printf ("\n%s\t", a);
printf("Special Character\t");
}
else if((strcmp(temp,"int")==0)||(strcmp(temp,"float")==0)||(strcmp(temp,"char")==0))
{
printf("\n%s\t",a);
printf("variable\t");

}
else if((strcmp(a,"scanf")==0)||(strcmp(a,"printf")==0)||(strcmp(a,"main")==0)||
(strcmp(a,"void")==0))
{
printf("\n%s\t",a);
printf("keywords\t");
}
strcpy(temp,a);
strcpy(tot[i],a);
i++;
fscanf(p,"%s",a);
}
printf("\t%d",i);
printf("\n%s",tot[i-1]);
return 0;
}
input1.txt
# include < stdio.h >
void main ( )
{
int i = 10 ;
}
END
OUTPUT:
[root@sys70 2]# cc ex2.c
[root@sys70 2]# ./a.out
LexemeToken
#
<
>
void
main
(
)
{
i
;
}

Special Character
Special Character
Special Character
keywords
keywords
Special Character
Special Character
Special Character
variable
Special Character
Special Character

RESULT:
Thus the C program to implement the Lexical Analyzer is done and the required Lexemes are
obtained and the output is verified.
VIVA QUESTIONS:

1. What is a Lexical Analyser?


2. What are Lexemes?
3. What are Lexical Tokens? Are they same as Lexemes? Justify your answer.
4. A program that performs Lexical Analysis is called as?
What is a Scanner? It is also referred to as the first stage of the

You might also like