You are on page 1of 7

search...

- Compiler Design

Searching for design program design


1. Looking for Design Program
2.
Top
answers
for
Design
Program
design lexical analyzer?
design lexical analyzer

w w w .InternetCorkBoard.com

lexical analyzer?
Searching for Design Program
Top
answers
for Design
Program
design
lexical
analyzer?
design lexical analyzer

w w w .Answ ered-Questions.com

3. Design Program design


Get
local analyzer
answers fornear
Design
lexical
you

Program design lexical analyzer


w w w .Answ ered-Questions.com

Chitika | Opt out?

C Program to Design Lexical Analyzer


Written by siddhartha. Posted in Compiler Design

Searching for design program design lexical analyzer?


1. Looking for Design Program design lexical analyzer?

w w w .InternetCorkBoard.com

Top answers for Design Program design lexical analyzer

2. Searching for Design Program design lexical


Top
answers for Design Program design lexical analyzer
analyzer?

w w w .Answ ered-Questions.com

3. Design Program design lexical analyzer near you

w w w .Answ ered-Questions.com

Get local answers for Design Program design lexical analyzer

Chitika | Opt out?

Source Code

1 /*********************************************************************************
****/
?
2 /* CProgramtoDesignLexicalAnalyzer*/
3 /* <spanid="IL_AD9"class="IL_AD">Download</span>more<spanid="IL_AD8"class="IL_AD">
4 /*************************************************************************************/
5 #include<string.h>
6 #include<ctype.h>
7 #include<stdio.h>
8 void keyword(char str[10])
9 {if(<spanid="IL_AD1" class="IL_AD">strcmp</span>("for",str)==0||strcmp("while",str)==0
10
strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0||
11
strcmp("double",str)==0||strcmp("static",str)==0||strcmp("switch",str)==0||
12
strcmp("case",str)==0)
1
3
printf("\n%sisakeyword",str);
Top-Countries
Top-Countries
1
4
else
Traffic
Traffic
1
5
p
r
i
n
t
f
(
"
\
n
%
s
i
s
a
n
i
d
e
n
t
i
f
i
e
r
"
,
s
t
r
)
;
Establish your
Establish your
6 }
web1
presence.
web presence.
17 main()
www.promotionsiteweb.net
www.promotionsiteweb.net
18 {
19
FILE *f1,*f2,*f3;
20
char c,str[10],st1[10];

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;
printf("\nEnterthecprogram");/*gets(st1);*/
f1=fopen("<spanid="IL_AD6"class="IL_AD">input</span>","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);
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("\nTheno'sin<spanid="IL_AD4"class="IL_AD">theprogram</span>are");
for(j=0;j<i;j++)
printf("%d",num[j]);
printf("\n");
f2=fopen("identifier","r");
k=0;
printf("The<spanid="IL_AD5"class="IL_AD">keywords</span>andidentifiersare:");
while((c=getc(f2))!=EOF)
{
if(c!='')
str[k++]=c;
else
{
str[k]='\0';
keyword(str);
k=0;
}
}

82
83
84
85
86
87
88
89
90

fclose(f2);
f3=fopen("specialchar","r");
printf("\nSpecialcharactersare");
while((c=getc(f3))!=EOF)
printf("%c",c);
printf("\n");
fclose(f3);
printf("Totalno.oflinesare:%d",lineno);

Out Put

Your website traffic

Increase your website traffic

Notebook Computer

Computer Sale

Website Traffic

Security System

Alarm System

Web page design

22 comments

Leave a message...

Discussion

Com m unity

C h o l a ve n d h a n

2 months ago

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define lengthof(array) sizeof(array)/sizeof(array[0])
FILE *fp;

Share

char ipc,token[25];
int noc=0,nol=0,not=0,nok=0,flag=0;
char *keywords[]={"do",
"case",
"break",
"extern",
"if",
"else",
"for",
"while",
"case",
"int",
"cont",
"none",
"float",
"void",
"double",
"char",
"sqrt",
"static"};
char *headers[]={"stdio.h","conio.h","ctype.h","string.h"};
char operators[7]={'+','-','*','/','<','>','='};
int isop(char);
void isIdentifiers(char[]);
void analyze();
int compare(char[],char**,int);
int main()
{
fp=fopen("input.txt","r");
if(fp!=NULL)
{
analyze();

}
fclose(fp);
return 0;
}
void analyze()
{
int ci=0;
while(!feof(fp))
{
ipc=getc(fp);
noc++;
if(ipc==' ' || ipc=='\t' || ipc=='~')
{
flag=1;
token[ci]='\0';
}
else if(ipc=='\n')
{
nol++;
}
else /* --- Seperating Alphabets and symbols*/
{
if(isalpha(ipc))
{
token[ci++]=ipc; /* Appending Characters as tokens*/
}
else if(ipc=='.'||ipc=='_')
{
token[ci++]=ipc;
}
else if(ispunct(ipc))
{

{
// --- seperating operators alone -if(isop(ipc))
{
printf("\n %c\tOperator",ipc);
flag=1;
token[ci]='\0';
}
else
{
printf("\n %c\tPunctuation",ipc);
flag=1;
token[ci]='\0';
}
}
}
// -- Check words for keywords by eliminating all others
if(flag==1 && token!='\0')
{
if(ipc!=' ')
not++;
isIdentifiers(token);
ci=0;
flag=0;
}
}
printf("\n----------------------\nNumber of characters = %d\n",noc);
printf("Number of lines = %d\n",nol);
printf("Number of tokens=%d\n",not);
printf("Number of keywords=%d\n",nok);
}
// ------ To check whether its an operator or not --------

int isop(char oc)


{
int i,res=0;
for(i=0;i<7;i++)
{
if(oc==operators[i])
res=1;
}
if(res) return 1;
else return 0;
}
// --------- To check whether its an keyword or not
void isIdentifiers(char t[])
{
int flag;
if(compare(t,keywords,lengthof(keywords)))
{
printf("\n %s\tKeyword",t);
nok++;
flag=1;
}
else flag=0;
if(flag==0)
if(compare(t,headers,lengthof(headers)))
printf("\n%s\tHeader",t);
}
int compare(char first[],char **second,int index)
{
int i,res=0;
for(i=0;i<index;i++) 0;="" 1;="" break;="" else="" if(res="=1)" if(strcmp(first,second[i])="=0)" res="1;"
return="" {="" }=""></index;i++)></string.h></ctype.h></stdio.h>
6

Reply

Share

You might also like