0% found this document useful (0 votes)
72 views

CD Lab Programs

This document describes a program to design a lexical analyzer. The program takes a C program as input, separates out the identifiers, keywords, and numeric literals, and writes them to different output files. It uses functions like fopen, fclose, getchar, putc, fprintf to open files, read input, write to output files, and compare strings to identify keywords. The main purpose is to tokenize a C program into its basic lexical units for further processing.

Uploaded by

shyamalaravi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

CD Lab Programs

This document describes a program to design a lexical analyzer. The program takes a C program as input, separates out the identifiers, keywords, and numeric literals, and writes them to different output files. It uses functions like fopen, fclose, getchar, putc, fprintf to open files, read input, write to output files, and compare strings to identify keywords. The main purpose is to tokenize a C program into its basic lexical units for further processing.

Uploaded by

shyamalaravi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

CD LAB PROGRAMS

1)A Program to Design Lexical Analyzer.

#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("static",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);
}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");
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);

You might also like