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

Compiler lab experiment 1 program

The document describes a lexical analyzer implemented in C that processes a given C source file to identify and categorize tokens such as keywords, identifiers, operators, and literals. It includes functions to check the type of characters and to process tokens, printing the results along with token counts. The output provides a detailed breakdown of the tokens found in the input source code.

Uploaded by

abhishek21032004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Compiler lab experiment 1 program

The document describes a lexical analyzer implemented in C that processes a given C source file to identify and categorize tokens such as keywords, identifiers, operators, and literals. It includes functions to check the type of characters and to process tokens, printing the results along with token counts. The output provides a detailed breakdown of the tokens found in the input source code.

Uploaded by

abhishek21032004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LEXICAL ANALYSER

vi input .c

#include<stdio.h>
int main()
{
int a=20,b=30,c;
c=a+b;
printf("addition of the above is %d\n",c);
}

Program

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdbool.h>
bool isoperator(char ch)
{
char operators[]="+-*/><=%&|";
return strchr(operators,ch)!=NULL;
}
bool isspecialsymbol(char ch)
{
char specialsymbols[]=";{}()?,.!#";
return strchr(specialsymbols,ch)!=NULL;
}
bool isidentifierstart(char ch)
{
return isalpha(ch)||ch=='_';
}
bool isidentifierchar(char ch)
{
return isalnum(ch)||ch=='_';
}
bool isliteraldelimiter(char ch)
{
return ch == '\''|| ch == '"';
}
bool isintegerchar(char ch)
{
return isdigit(ch);
}
bool iskeyword(const char *word)
{
char keywords[][20]={"int","void","if","else","do","while","for","printf"};
int numkeywords = sizeof(keywords)/sizeof(keywords[0]);
int i;
for(i=0;i<numkeywords;i++)
{
if(strcmp(word,keywords[i])==0)
{
return true;
}
}
return false;
}
void processtoken(int l,int t,const char*tokentype,const char*lexeme)
{
printf("%7d\t\t %7d\t\t %s\t %7s\n",l,t,tokentype,lexeme);
}
void processidentifier(int l ,int t,const char*identifier)
{
processtoken(l,t,"identifier",identifier);
}
void processliteral(int l,int t, const char*literaltype,const char *lexeme)
{
processtoken(l,t,literaltype,lexeme);
}
int main()
{
FILE *input;
int l = 1;
int t = 0;
char ch;
char identifier[20];
int identifieridx = 0;
char lexeme[20];
int lexemeidx = 0;
int keywordcount = 0;
int identifiercount = 0;
int integerliteralcount=0;
int operatorcount=0;
int specialsymbolcount=0;
int stringliteralcount=0;
int characterliteralcount=0;
input = fopen("input.c","r");
printf("Line No : \tToken No : \t\tToken\t\t\tLexeme\n");
while((ch = fgetc(input))!=EOF)
{
if(ch == '\n')
{
l++;
}
else if (ch == ' ' || ch == '\t')
{
}
else if(isoperator(ch))
{
char op[2] = {ch,'\0'};
processtoken(l,t,"operator",op);
t++;
operatorcount++;
}
else if(isspecialsymbol(ch))
{
char sym[2]={ch,'\0'};
processtoken(l,t,"special symbol",sym);
t++;
specialsymbolcount++;
}
else if(isidentifierstart(ch))
{
lexemeidx=0;
lexeme[lexemeidx++]=ch;
while((ch = fgetc(input))!= EOF && isidentifierchar(ch))
{
lexeme[lexemeidx++]=ch;
}
lexeme[lexemeidx]='\0';
if(iskeyword(lexeme))
{
processtoken(l,t,"keyword",lexeme);
keywordcount++;
}
else
{
processidentifier(l,t,lexeme);
identifiercount++;
}
t++;
ungetc(ch,input);
}
else if(isliteraldelimiter(ch))
{
char literaltype[20];
lexemeidx=0;
lexeme[lexemeidx++]=ch;
while((ch=fgetc(input))!= EOF && ch != '\'' && ch !='"')
{
lexeme[lexemeidx++]=ch;
}
lexeme[lexemeidx++]=ch;
lexeme[lexemeidx]='\0';
if(lexeme[0] == '\'')
{
strcpy(literaltype,"character literal");
stringliteralcount++;
}
processliteral(l,t,literaltype,lexeme);
t++;
}
else if(isintegerchar(ch))
{
lexemeidx=0;
lexeme[lexemeidx++]=ch;
while((ch= fgetc(input))!= EOF && isintegerchar(ch))
{
lexeme[lexemeidx++]=ch;
}
lexeme[lexemeidx]='\0';
processliteral(l,t,"integer literal",lexeme);
t++;
integerliteralcount++;
ungetc(ch,input);
}
}
printf("\nToken Counts\n");
printf("Keywords:%d\n",keywordcount);
printf("Identifiers:%d\n",identifiercount);
printf("Operators:%d\n",operatorcount);
printf("Special Symbols:%d\n",specialsymbolcount);
printf("Integer Literals:%d\n" ,integerliteralcount);
printf("String Literals:%d\n", stringliteralcount);
printf("Character Literals:%d\n", characterliteralcount);
fclose(input);
return 0;
}

Output

Line No : Token No : Token Lexeme


1 0 special symbol #
1 1 identifier include
1 2 operator <
1 3 identifier stdio
1 4 special symbol .
1 5 identifier h
1 6 operator >
2 7 keyword int
2 8 identifier main
2 9 special symbol (
2 10 special symbol )
3 11 special symbol {
4 12 keyword int
4 13 identifier a
4 14 operator =
4 15 integer literal 20
4 16 special symbol ,
4 17 identifier b
4 18 operator =
4 19 integer literal 30
4 20 special symbol ,
4 21 identifier c
4 22 special symbol ;
5 23 identifier c
5 24 operator =
5 25 identifier a
5 26 operator +
5 27 identifier b
5 28 special symbol ;
6 29 keyword printf
6 30 special symbol (
6 31 "addition of the above is %d\n"
6 32 special symbol ,
6 33 identifier c
6 34 special symbol )
6 35 special symbol ;
7 36 special symbol }

Token Counts
Keywords:3
Identifiers:11
Operators:6
Special Symbols:14
Integer Literals:2
String Literals:0
Character Literals:0

You might also like