You are on page 1of 41

/* Write a program of Lexical Analyzer in C.

*/

/* LEX.C */

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<ctype.h>

#include<string.h>

#define EOL '\n'

#define int1 1

#define float1 2

#define char1 3

#define double1 4

#define printf1 5

#define scanf1 6

#define void1 7

#define if1 8

#define else1 9

#define for1 10

#define while1 11

#define struct1 12

#define typedef1 13

#define return1 14
1|Page
#define switch1 15

#define case1 16

#define break1 17

#define id 18

struct symtab

char tokenname[20];

int tokenvalue;

};

struct symtab s[50];

int nosymbentries=0;

int lineno=1;

FILE *fp,*fp1;

char linestr[100];

void init_symb_tab( )

strcpy(s[1].tokenname,"int");

s[1].tokenvalue=int1;

strcpy(s[2].tokenname,"float");

s[2].tokenvalue=float1;

strcpy(s[3].tokenname,"char");

s[3].tokenvalue=char1;

strcpy(s[4].tokenname,"double");
2|Page
s[4].tokenvalue=double1;

strcpy(s[5].tokenname,"printf");

s[5].tokenvalue=printf1;

strcpy(s[6].tokenname,"scanf");

s[6].tokenvalue=scanf1;

strcpy(s[7].tokenname,"void");

s[7].tokenvalue=void1;

strcpy(s[8].tokenname,"if");

s[8].tokenvalue=if1;

strcpy(s[9].tokenname,"else");

s[9].tokenvalue=else1;

strcpy(s[10].tokenname,"for");

s[10].tokenvalue=for1;

strcpy(s[11].tokenname,"while");

s[11].tokenvalue=while1;

strcpy(s[12].tokenname,"struct");

s[12].tokenvalue=struct1;

strcpy(s[13].tokenname,"typedef");

s[13].tokenvalue=int1;

strcpy(s[14].tokenname,"return");

s[14].tokenvalue=return1;

strcpy(s[15].tokenname,"switch");

s[15].tokenvalue=switch1;
3|Page
strcpy(s[16].tokenname,"case");

s[16].tokenvalue=case1;

strcpy(s[17].tokenname,"break");

s[17].tokenvalue=break1;

nosymbentries=17;

void verify(char token[50])

int i;

for( i=1;i<=nosymbentries;i++)

if (strcmp(s[i].tokenname,token)==0)

fprintf(fp1,"\t%s: token value : %d\n",token,s[i].tokenvalue);

return ;

strcpy(s[i].tokenname,token);

s[i].tokenvalue=id;

fprintf(fp1,"\t%s :identifier, token value : %d\n",token,s[i].tokenvalue);

nosymbentries=i;

main()

{
4|Page
int i=0,j=0,k;

char symb,str[50],var[50],line[1000];

init_symb_tab();

clrscr();

fp=fopen("D:\\Lexical\\test.c","r");

clrscr();

if(!fp)

printf("File not found\n");

getch();

exit(1);

fp1=fopen("D:\\Lexical\\output.txt","w");

if(!fp1)

printf("file not found\n");

exit(1);

fprintf(fp1,"LINENO TOKENS\n");

fprintf(fp1,"-----------------------------------------------\n");

fprintf(fp1,"%d:\n",lineno);

while((symb=getc(fp))!=EOF)
5|Page
{ /* ignoring spaces*/

while(symb==' ' || symb=='\t' && symb!=EOF || symb==EOL )

if (symb==EOL)

lineno=lineno+1;

fprintf(fp1,"%d:\n",lineno);

symb=getc(fp);

i=0;

/* CHECKING FOR INTEGER*/

if(isdigit(symb)||symb=='-'||symb=='.')

str[i++]=symb;

symb=getc(fp);

while(isdigit(symb)||symb=='.')

str[i++]=symb;

symb=getc(fp);

if(isalpha(symb))

{
6|Page
fprintf(fp1,"\tinvalid\n");

i=0;

str[i]='\0';

fprintf(fp1,"\t%s is a number\n",str);

i=0;

/*checking for keywords and identifiers*/

if(isalpha(symb) || symb=='_')

j=0;

var[j++]=symb;

symb=getc(fp);

while(isalpha(symb) || isdigit(symb) ||symb=='_')

var[j++]=symb;

symb=getc(fp);

var[j]='\0';

verify(var);

j=0;

7|Page
/*checking for operators*/

if(symb=='<' || symb== '>' || symb=='=')

if(symb=='<')

symb=getc(fp);

if(symb=='=')

fprintf(fp1,"\t<=: less than or equal to operator\n");

else

ungetc(symb,fp);

fprintf(fp1,"\t<: less than operator\n");

else if(symb=='>')

symb=getc(fp);

if(symb=='=')

fprintf(fp1,"\t>= : greater than or equal to operator\n");

}
8|Page
else

ungetc(symb,fp);

fprintf(fp1,"\t>: greater than operator\n");

else if(symb=='=')

symb=getc(fp);

if(symb=='=')

fprintf(fp1,"\t==: equal to operator\n");

else

ungetc(symb,fp);

fprintf(fp1,"\t=: assignment operator\n");

if(symb=='+' || symb=='-' ||symb=='*' || symb=='?' || symb=='|')

if(symb=='+')
9|Page
{

symb=getc(fp);

if(symb=='+')

fprintf(fp1,"\t++: increment operator\n");

else

ungetc(symb,fp);

fprintf(fp1,"\t+: plus operator\n");

else if(symb=='-')

symb=getc(fp);

if(symb=='-')

fprintf(fp1,"\t--: decrement operator\n");

else

ungetc(symb,fp);

fprintf(fp1,"\t-: minus operator\n");


10 | P a g e
}

else if(symb=='*')

fprintf(fp1,"\t*: multiplication operator\n");

else if(symb=='|')

symb=getc(fp);

if(symb=='|')

fprintf(fp1,"\t||: or operator\n");

else if(symb=='?')

fprintf(fp1,"\t?: conditional operator\n");

if(symb=='{' || symb=='}' || symb=='(' || symb ==')' || symb=='[' ||symb==']')

{
11 | P a g e
if(symb=='{')

fprintf(fp1,"\t{: open brace\n");

else if(symb=='}')

fprintf(fp1,"\t}: close brace\n");

else if(symb=='(')

fprintf(fp1,"\t(: open parenthesis\n");

else if(symb==')')

fprintf(fp1,"\t): close parenthesis\n");

else if(symb=='[')

fprintf(fp1,"\t[: open bracket\n");

else

fprintf(fp1,"\t]: close bracket\n");


12 | P a g e
}

if(symb==';' || symb==',')

if(symb==';')

fprintf(fp1,"\t;: semi colon\n");

else if(symb==',')

fprintf(fp1,"\t,: comma\n");

if(symb=='#')

symb=getc(fp);

if(symb=='d')

symb=getc(fp);

while(symb!=' ')

symb=getc(fp);

}
13 | P a g e
symb=getc(fp);

while(symb!=' ')

str[i++]=symb;

symb=getc(fp);

str[i]='\0';

fprintf(fp1,"\t#define statement: %s is a constant\n",str);

symb=getc(fp);

while(symb!=EOL)

symb=getc(fp);

lineno=lineno+1;

fprintf(fp1,"%d: \n",lineno);

else if(symb=='i')

str[i++]=symb;

symb=getc(fp);

while(symb!=EOL)

str[i++]=symb;
14 | P a g e
symb=getc(fp);

str[i]='\0';

fprintf(fp1,"\t#%s is a header file\n",str);

lineno=lineno+1;

fprintf(fp1,"%d:\n",lineno);

if(symb=='/')

symb=getc(fp);

if(symb=='*')

symb=getc(fp);

while(symb!='/')

symb=getc(fp);

if(symb==EOL)

lineno=lineno+1;

fprintf(fp1,"%d:\n",lineno);

}
15 | P a g e
}

if(symb=='/')

symb=getc(fp);

while(symb!=EOL)

symb=getc(fp);

lineno=lineno+1;

fprintf(fp1,"%d:\n",lineno);

else

fprintf(fp1,"\t/: division operator\n");

} //if

if(symb=='&')

symb=getc(fp);

if(symb=='&')

fprintf(fp1,"\t&&:and operator\n");

}
16 | P a g e
else

fprintf(fp1,"\t&%c:address operator\n",symb);

if(symb=='"')

symb=getc(fp);

while(symb!='"')

str[i++]=symb;

symb=getc(fp);

str[i]='\0';

fprintf(fp1,"\t%s : is a string\n",str);

}//while

getch();

return;

17 | P a g e
/* test.C */

#include<stdio.h>

#include<stdlib.h>

#define abc 100

void main()

int a, b;

printf("Enter 2 no.s\n"); // printf statement

scanf("%d%d",&a,&b);

/* scanf

statement*/

if(a<20) a=a+1;

printf("Total : %d", a);

18 | P a g e
/* Output.C */

LINENO TOKENS

-----------------------------------------------

1:

#include<stdio.h> is a header file

2:

#include<stdlib.h> is a header file

3:

#define statement: abc is a constant

4:

void: token value : 7

main :identifier, token value : 18

(: open parenthesis

): close parenthesis

5:

{: open brace

6:

int: token value : 1

a :identifier, token value : 18

,: comma

b :identifier, token value : 18

;: semi colon

7:
19 | P a g e
printf: token value : 5

(: open parenthesis

Enter 2 no.s\n : is a string

): close parenthesis

;: semi colon

8:

scanf: token value : 6

(: open parenthesis

%d%d : is a string

,: comma

&a:address operator

,: comma

&b:address operator

): close parenthesis

;: semi colon

9:

10:

11:

if: token value : 8

(: open parenthesis

a: token value : 18

<: less than operator

20 is a number
20 | P a g e
): close parenthesis

12:

a: token value : 18

=: assignment operator

a: token value : 18

+: plus operator

1 is a number

;: semi colon

13:

printf: token value : 5

(: open parenthesis

Total : %d : is a string

,: comma

a: token value : 18

): close parenthesis

;: semi colon

14:

}: close brace

15:

21 | P a g e
/* Write a program of Paranthesis Checker in C */

#include<stdio.h>

#define MAX 20

#define true 1

#define false 0

int top = -1;

int stack[MAX];

push(char);

char pop();

main()

char exp[MAX],temp;

int i,valid=true;

printf("Enter an algebraic expression : ");

gets(exp);

for(i=0;i<strlen(exp);i++)

if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')

push( exp[i] );

if(exp[i]==')' || exp[i]=='}' || exp[i]==']')


22 | P a g e
if(top == -1) /* stack empty */

valid=false;

else

temp=pop();

if( exp[i]==')' && (temp=='{' || temp=='[') )

valid=false;

if( exp[i]=='}' && (temp=='(' || temp=='[') )

valid=false;

if( exp[i]==']' && (temp=='(' || temp=='{') )

valid=false;

}/*End of else */

}/*End of for*/

if(top>=0) /*stack not empty*/

valid=false;

if( valid==true )

printf("Valid expression\n");

else

printf("Invalid expression\n");

getch();

}/*End of main()*/

23 | P a g e
push(char item)

if(top == (MAX-1))

printf("Stack Overflow\n");

else

top=top+1;

stack[top] = item;

}/*End of push()*/

char pop()

if(top == -1)

printf("Stack Underflow\n");

else

return(stack[top--]);

}/*End of pop()*/

24 | P a g e
/*Write a program of infix to prefix and postfix conversion */

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define MAX 15

#define true 1

#define false 0

/*Structure Declaration*/

typedef struct

char data[MAX];

char top;

}STK;

/*Function Declarations*/

void input(char str[]);

void intopre(char str1[],char pre[]);

void intopost(char str1[],char post[]);

int isoperand(char sym);

int prcd(char sym);

void push(STK *s1,char elem);

int pop(STK *s1);


25 | P a g e
int empty(STK *s2);

int full(STK *s2);

void dis(char str[]);

void main()

STK s;

int cs,ans;

char str[MAX],pre[MAX],post[MAX];

clrscr();

do /*Using Do-while Loop*/

clrscr();

printf("

-----Program for Expressions-----");

printf("

Input The String:");

printf("

MENU:

");

printf("1.Infix to Prefix

");

printf("2.Infix to Postfix");
26 | P a g e
printf("

3.Exit");

cs=getche();

switch(cs) /*Using Switch Case*/

case 1:

intopre(str,pre);

break;

case 2:

intopost(str,post);

break;

case 3:

break;

default:

printf("

Enter a Valid Choise!"); /*Default Case*/

break;

printf("

Do you wish to Continue?(y/n)");

ans=getche();

}while(ans=='y'||ans=='Y'); /*Condition for Do-while loop*/

27 | P a g e
getch();

/**************************************************/

/*To Input String*/

/**************************************************/

void input(char str)

printf("Enter the Infix String:");

scanf("%s",str);

/**************************************************/

/*To Covert Infix To Prefix*/

/**************************************************/

void intopre(STK s1,char str1[],char pre[])

int len,flag;

len=strlen(str1);

int check=0,cnt=len-1,pos=0;

char elem;

while(cnt>=0) /*while condition*/

flag=0;

if(isoperand(str1[cnt])) /*Checking for Operand*/


28 | P a g e
{

printf("%c",str1[cnt]);

cnt--;

pos++;

else

check=prcd(str1[cnt]);

while(check==false)

pre[pos]=str1[cnt];

flag=1;

pos++;

cnt--;

if(flag==0)

elem=pop(&s1);

printf("%c",elem);

}
29 | P a g e
/**************************************************/

/*To Convert Infix To Postfix*/

/**************************************************/

void intopost(STK s1,char str1[],char post[])

int len;

len=strlen(str1);

int check=0,cnt=len-1,pos=0;

/**************************************************/

/*To Check For Operand*/

/**************************************************/

int isoperand(char sym)

if('A'<sym<'Z'||'a'<sym<'z')

return(true);

return(false);

/**************************************************/

/*To Check The Precedence*/

/**************************************************/

int prcd(char sym)

{
30 | P a g e
}

/**************************************************/

/*To Display String*/

/**************************************************/

void dis(char str[])

/******************************************/

/*Push Function Definition*/

/******************************************/

void push(STK *s1,char elem)

if(!full(s1))

s1->top++; /*Incrementing top*/

s1->data[s1->top]=elem; /*Storing element*/

else

printf("

Stack is Full!");

/******************************************/
31 | P a g e
/*Full Function Definition*/

/******************************************/

int full(STK *s2)

if(s2->top==MAX) /*Condition for Full*/

return(true);

return(false);

/******************************************/

/*Pop Function Definition*/

/******************************************/

int pop(STK *s1)

char elem;

if(!empty(s1))

elem=s1->data[s1->top]; /*Storing top stack element in elem*/

s1->top--; /*Decrementing top*/

return(elem);

return(false);

}
32 | P a g e
/******************************************/

/*Empty Function Definition*/

/******************************************/

int empty(STK *s2)

if(s2->top==-1) /*Condition For Empty*/

return(true);

return(false);

33 | P a g e
/*top down parser parses a given string for an

input string which may match rhe table which is

printed at the beggining of the program.

*/

#include<stdio.h>

#include<conio.h>

#include<string.h>

char str[10],out,in,output[10],input[10],temp;

char tl[10]={'x','+','*','(',')','$','@'};

char ntl[10]={'E','e','T','t','F'};

int err=0,flag=0,i,j,k,l,m;

char c[10][10][7]={{{"Te"},{"ERROR!"},{"ERROR!"},{"Te"},{"ERROR!"},
{"ERROR!"}},

{"ERROR!","+Te","ERROR!","ERROR!","@","@"},

{"Ft","ERROR!","ERROR!","Ft","ERROR!","ERROR!"},

{"ERROR!","@","*Ft","ERROR!","@","@"},

{"x","ERROR!","ERROR!","(E)","ERROR!","ERROR!"}};

struct stack{

char sic[10];

int top;

};

void push(struct stack *s,char p)

34 | P a g e
{

s->sic[++s->top]=p;

s->sic[s->top+1]='\0';

char pop(struct stack *s)

char a;

a=s->sic[s->top];

s->sic[s->top--]='\0';

return(a);

char sttop(struct stack *s)

return(s->sic[s->top]);

void pobo(struct stack *s)

//printf("%s\n",str);

m=0;

while(str[m]!='\0')
35 | P a g e
m++;

m--;

while(m!=-1)

if(str[m]!='@')

push(s,str[m]);

m--;

void search(int l)

for(k=0;k<7;k++)

if(in==tl[k])

break;

if(l==0)

strcpy(str,c[l][k]);

else if(l==1)

strcpy(str,c[l][k]);

else if(l==2)

strcpy(str,c[l][k]);

else if(l==3)

strcpy(str,c[l][k]);

else strcpy(str,c[l][k]);
36 | P a g e
}

void main()

struct stack s1;

struct stack *s;

s=&s1;

s->top=-1;

clrscr();

printf("\t\tPARSING TABLE\n~
\t=============================================\n\n\tx\t+\t*\t(\t)\t
$\n");

printf("
\t=============================================\n\n");

for(i=0;i<5;i++)

printf("%c\t",ntl[i]);

for(j=0;j<6;j++)

if(strcmp(c[i][j],"ERROR!")==0)

printf("ERROR!\t");

else

printf("%c->%s\t",ntl[i],c[i][j]);

printf("\n\n");

37 | P a g e
printf("
\t=============================================\n\n");

push(s,'$');

push(s,'E');

printf("\nENTER THE INPUT STRING: ");

scanf("%s",input);

printf("\n\nTHE BEHAVIOUR OF THE PARSER FOR GIVEN INPUT


STRING IS:\n\n");

printf("STACK\tINPUT\tOUTPUT\n");

i=0;

in=input[i];

printf("%s\t",s->sic);

for(k=i;k<strlen(input);k++)

printf("%c",input[k]);

if(strcmp(str,"")!=0)

printf("\t%c->%s",ntl[j],str);

printf("\n");

while((s->sic[s->top]!='$')&&err!=1&&strcmp(str,"ERROR!")!=0)

strcpy(str,"");

flag=0;

for(j=0;j<7;j++)

38 | P a g e
if(in==tl[j])

flag=1;

break;

if(flag==0)

in='x';

flag=0;

out=sttop(s);

for(j=0;j<7;j++)

if(out==tl[j])

flag=1;

break;

if(flag==1)

if(out==in)

temp=pop(s);

in=input[++i];

if(str=='@')

temp=pop(s);
39 | P a g e
// err=1;

else

strcpy(str,"ERROR!");

err=1;

else

flag=0;

for(j=0;j<5;j++)

if(out==ntl[j])

flag=1;

break;

if(flag==1)

search(j);

temp=pop(s);

pobo(s);

}
40 | P a g e
else

strcpy(str,"ERROR!");

err=1;

if(strcmp(str,"ERROR!")!=0)

printf("%s\t",s->sic);

for(k=i;k<strlen(input);k++)

printf("%c",input[k]);

if((strcmp(str,"")!=0)&&(strcmp(str,"ERROR!")!=0))

printf("\t%c->%s",ntl[j],str);

printf("\n");

if(strcmp(str,"ERROR!")==0)

printf("\n\nTHE STRING IS NOT ACCEPTED!!!!");

else printf("$\t$\tACCEPT\n\n\nTHE STRING IS ACCEPTED!!!");

getch();

41 | P a g e

You might also like