0% found this document useful (1 vote)
7K views

Experiment No.1:: Write A LEX Program To Scan Reserved Word & Identifiers of C Language

The document describes an experiment to write a LEX program to scan reserved words and identifiers of the C language. The algorithm involves reading a C program as input, storing it in files, checking for identifiers, digits, operators, special characters, storing them separately, and printing the output. The program implements this algorithm by opening input and output files, scanning the input for different token types, writing identifiers and special characters to separate files, and printing the output including identifiers, numbers, and special characters.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
7K views

Experiment No.1:: Write A LEX Program To Scan Reserved Word & Identifiers of C Language

The document describes an experiment to write a LEX program to scan reserved words and identifiers of the C language. The algorithm involves reading a C program as input, storing it in files, checking for identifiers, digits, operators, special characters, storing them separately, and printing the output. The program implements this algorithm by opening input and output files, scanning the input for different token types, writing identifiers and special characters to separate files, and printing the output including identifiers, numbers, and special characters.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT NO.

1:
AIM: Write a LEX Program to scan reserved word & Identifiers of C Language

PREREQUISITE: Should have the knowledge of Language Processing and C language to


implement the real world problems.

ALGORITHM:
Step1: Read the mini language program statement as a input
Step2: Storing the input into Files.
Step3: Checking for Identifiers, Digits, Operators, Special Characters etc.
Step4: Storing Identifiers, Digits, Operators, Special Characters separately.
Step5: Printing Identifiers, Digits, Operators, Special Characters etc.

PROGRAM:
#include<string.h>
#include<ctype.h>
#include<stdio.h>
void keyword(char str[10])
{
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||strcmp("int",str
)==0||strcmp("float",str)==0||strcmp("char",str)==0||strcmp("double",str)==0||strcmp("stat
ic",str)==0||strcmp("switch",str)==0||strcmp("case",str)==0)
printf("\n%s is a keyword",str);

else

printf("\n%s is an identifier",str);
}
Void main()
{

FILE *f1,*f2,*f3;
char c,str[10],st1[10];
int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;
printf("\nEnter the c program");/*gets(st1);*/
f1=fopen("input","w"); while((c=getchar())!
=EOF)
putc(c,f1); fclose(f1);
f1=fopen("input","r");
f2=fopen("identifier","w");
f3=fopen("specialchar","w");

1
while((c=getc(f1))!=EOF)
{
if(isdigit(c))
{
tokenvalue=c-'0';
c=getc(f1);
while(isdigit(c))
{
tokenvalue*=10+c-'0';
c=getc(f1);
}
num[i++]=tokenvalue;
ungetc(c,f1);
}
else
if(isalpha(c))
{
putc(c,f2); c=getc(f1); while(isdigit(c)||
isalpha(c)||c=='_'||c=='$')
{
putc(c,f2);
c=getc(f1);
}
putc(' ',f2);
ungetc(c,f1);
}
else
if(c==' '||c=='\t')
printf(" ");
else
if(c=='\n')
lineno++;
else
putc(c,f3);
}
fclose(f2);
fclose(f3);
fclose(f1);
printf("\nThe no's in the program are");
for(j=0;j<i;j++)
printf("%d",num[j]);
printf("\n");
f2=fopen("identifier","r");
k=0;
printf("The keywords and identifiersare:");
while((c=getc(f2))!=EOF)
{
if(c!=' ')
str[k++]=c;
else
{
str[k]='\0';
keyword(str);
k=0;
}
}
fclose(f2);
f3=fopen("specialchar","r");
printf("\nSpecial characters are");
while((c=getc(f3))!=EOF)
printf("%c",c);
printf("\n");
fclose(f3);
printf("Total no. of lines are:%d",lineno);
}

INPUT/OUTPUT:
Enter the C program
a+b*c
Ctrl-D
The no’s in the program are:
The keywords and identifiers are:
a is an identifier and terminal
b is an identifier and terminal
c is an identifier and terminal
Special characters are:
+*
Total no. of lines are: 1

Viva Questions:
1. Define Lexical Analyzer?
2. What are the functions of Lexical Analyzer?
3. Define Token and Identifier?
4. Define the functionality of ungetc()?
5. Define FILE and give FILE operations?

You might also like