Compiler lab experiment 1 program
Compiler lab experiment 1 program
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
Token Counts
Keywords:3
Identifiers:11
Operators:6
Special Symbols:14
Integer Literals:2
String Literals:0
Character Literals:0