Showing posts with label COMPILER C PROGRAM. Show all posts
Showing posts with label COMPILER C PROGRAM. Show all posts

Sunday, 23 March 2014

FIRST AND FOLLOW PROGRAM

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-TO FIND FIRST AND FOLLOW*/

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>

void follow(char c);
void first_1(char c);
void FIRST(char );

int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
int count,n1=0;
char  first[10];

int main()
{
 int i,z,choice;
 char c,ch;
 printf("Enter the no.of productions:");
 scanf("%d",&n);
 printf("Enter the productions(epsilon=$):\n");
 for(i=0;i<n;i++)
  scanf("%s%c",a[i],&ch);
count=n;

do
{
n1=0;
printf("Element :");
scanf("%c",&c);
FIRST(c);
printf("\n FIRST(%c)= { ",c);
for(i=0;i<n1;i++)
printf("%c ",first[i]);
printf("}\n");

printf("press 1 to continue : ");
scanf("%d%c",&choice,&ch);
}
while(choice==1);

 do
 {
  m=0;
  printf("Enter the element whose FOLLOW is to be found:");

  scanf("%c",&c);
  follow(c);
    printf("FOLLOW(%c) = { ",c);
  for(i=0;i<m;i++)
   printf("%c ",f[i]);
  printf(" }\n");
    printf("Do you want to continue(0/1)?");
  scanf("%d%c",&z,&ch);
 }
 while(z==1);
}

void follow(char c)
{

 if(a[0][0]==c)
 f[m++]='$';
 for(i=0;i<n;i++)
 {
  for(j=2;j<strlen(a[i]);j++)
  {
   if(a[i][j]==c)
   {
    if(a[i][j+1]!='\0')
     first_1(a[i][j+1]);

    if(a[i][j+1]=='\0'&&c!=a[i][0])
     follow(a[i][0]);

   }
  }
 }
}
void first_1(char c)
{
      int k;
                 if(!(isupper(c)))
                 f[m++]=c;

                 for(k=0;k<n;k++)
                 {
                 if(a[k][0]==c)
                 {
                 if(a[k][2]=='$')
                  follow(a[i][0]);
                 else if(islower(a[k][2]))
                 f[m++]=a[k][2];

                 else
                  first_1(a[k][2]);
                 }
                 }

}



void FIRST(char c)
{
int j;
if(!(isupper(c)))first[n1++]=c;
for(j=0;j<count;j++)
{
if(a[j][0]==c)
{
if(a[j][2]=='$') first[n1++]='$';
else if(islower(a[j][2]))first[n1++]=a[j][2];
else FIRST(a[j][2]);
}
}
}

/*OUTPUT

Enter the no.of productions:8
Enter the productions(epsilon=$):
E=TD
D=+TD
D=$
T=FS
S=*FS
S=$
F=(E)
F=a
Element :E
FIRST(E)= { ( a }
press 1 to continue : 1

Element :D
FIRST(D)= { + $ }
press 1 to continue : 1

Element :T
FIRST(T)= { ( a }
press 1 to continue : 1

Element :S
FIRST(S)= { * $ }
press 1 to continue : 1

Element :F
FIRST(F)= { ( a }
press 1 to continue : 0

Enter the element whose FOLLOW is to be found:E
FOLLOW(E) = { $ )  }
Do you want to continue(0/1)?1

Enter the element whose FOLLOW is to be found:D
FOLLOW(D) = { $ )  }
Do you want to continue(0/1)?1

Enter the element whose FOLLOW is to be found:T
FOLLOW(T) = { + $ )  }
Do you want to continue(0/1)?1

Enter the element whose FOLLOW is to be found:S
FOLLOW(S) = { + $ )  }
Do you want to continue(0/1)?1

Enter the element whose FOLLOW is to be found:F
FOLLOW(F) = { * + $ )  }
Do you want to continue(0/1)?0 */

Friday, 21 March 2014

LEXICAL TOOL PROGRAM IN C

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-LEXICAL ANALYSIS PROGRAM IN C*/

/*4 FILES ARE NEEDED FOR THIS PROGRAM
1.LEX.C
2.INTER.C-automatically generated on running
3.OPER.C-contains opeartors
4.KEY.C-contains keyword
and one file to perform lexical analysis
NOTE-PUT ALL THREE C FILES IN A SAME FOLDER AND ON RUNNING THE PROGRAM ENTER ANY FILE NAME,THAT SHOULD BE PRESENT IN SAME FOLDER*/

*/


1 LEX.C

#include"stdio.h"
#include"conio.h"
#include<string.h>
#include<ctype.h>

void main()
{
FILE *fi,*fo,*fop,*fk;
int flag=0,i=1;
char c,t,a[15],ch[15],file[20];
clrscr();
printf("Enter the file name : ");
scanf("%s",file);
fi=fopen(file,"r");
fo=fopen("inter.c","w");
fop=fopen("oper.c","r");
fk=fopen("key.c","r");
c=getc(fi);
while(!feof(fi))
{
if(isalpha(c)||isdigit(c)||(c=='['||c==']'||c=='.'==1))
fputc(c,fo);
else
{
if(c=='\n')
fprintf(fo,"\t$\t");
else
fprintf(fo,"\t%c\t",c);
}
c=getc(fi);
}
fclose(fi);
fclose(fo);
fi=fopen("inter.c","r");
printf("\t\tLEXICAL ANALYSIS \n");
fscanf(fi,"%s",a);
printf("\nline : %d\n",i++);
while(!feof(fi))
{
if((strcmp(a,"$")==0))
{
printf("\nline : %d\n",i++);
fscanf(fi,"%s",a);
}
fscanf(fop,"%s",ch);
while(!feof(fop))
{
if(strcmp(ch,a)==0)
{
fscanf(fop,"%s",ch);
printf("\t\t%s\t:\t%s\n",a,ch);
flag=1;
}
fscanf(fop,"%s",ch);
}
rewind(fop);
fscanf(fk,"%s",ch);
while(!feof(fk))
{
if(strcmp(ch,a)==0)
{
fscanf(fk,"%s",ch);
printf("\t\t%s\t:\tkeyword\n",a);
flag=1;
}
fscanf(fk,"%s",ch);
}
rewind(fk);
if(flag==0)
{
if(isdigit(a[0]))
printf("\t\t%s\t:\tconstant\n",a);
else
printf("\t\t%s\t:\tidentifier\n",a);

}
flag=0;
fscanf(fi,"%s",a);
}
getch();
}

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

2.key.c

int
void
main
char
if
for
while
else
printf
scanf
FILE
include
stdio.h
conio.h
iostream.h

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

3.oper.c

( openpara
) closepara
{ openbrace
} closebrace
<     lesser
> greater
" doublequote
' singlequote
: colon
; semicolon
# preprocessor
= equal
== assign
% percentage
^ bitwise
& reference
* star
+ add
- sub
\ backslash
/ slash


------------------------------------------------------------------------------------------------------------
/*sample file is cd4.c

#include<stdio.h>
#include<conio.h>

int main()
{
int a;
printf("hello");
if(a==1)
{
printf("ok");
}
else
{
printf("notok");
}
}
*/

/*output-

Enter the file name : cd4.c
                LEXICAL ANALYSIS

line : 1
                #       :       preprocessor
                include :       keyword
                <       :       lesser
                stdio.h :       keyword
                >       :       greater

line : 2
                #       :       preprocessor
                include :       keyword
                <       :       lesser
                conio.h :       keyword
                >       :       greater

line : 3
                $       :       identifier
                int     :       keyword
                main    :       keyword
                (       :       openpara
                )       :       closepara

line : 4
                {       :       openbrace

line : 5
                int     :       keyword
                a       :       identifier
                ;       :       semicolon

line : 6
                printf  :       keyword
                (       :       openpara
                "       :       doublequote
                hello   :       identifier
                "       :       doublequote
                )       :       closepara
                ;       :       semicolon

line : 7
                if      :       keyword
                (       :       openpara
                a       :       identifier
                =       :       equal
                =       :       equal
                1       :       constant
                )       :       closepara

line : 8
                {       :       openbrace

line : 9
                printf  :       keyword
                (       :       openpara
                "       :       doublequote
                ok      :       identifier
                "       :       doublequote
                )       :       closepara
                ;       :       semicolon

line : 10
                }       :       closebrace

line : 11
                else    :       keyword

line : 12
                {       :       openbrace

line : 13
                printf  :       keyword
                (       :       openpara
                "       :       doublequote
                notok   :       identifier
                "       :       doublequote
                )       :       closepara
                ;       :       semicolon

line : 14
                }       :       closebrace

line : 15
                }       :       closebrace

line : 16
                $       :       identifier
*/

INFIX TO POSTFIX PROGRAM IN C

/*CSEMATTER.BLOGSPOT.IN
PROGRAM-INFIX TO POSTFIX PROGRAM IN C*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
#define size 10

char stack[size];
int tos=0,element;
void push(int element);
char pop();
void show();
int isempty();
int isfull();
char infix[30],output[30];
int prec(char);

int main()
{
        int i=0,j=0,k=0,length;
        char temp;
        printf("enter the infix expression\n");
        scanf("%s",infix);
        length=strlen(infix);
  for(i=0;i<length;i++)
  {
  if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' && infix[i]!='/' && infix[i]!='^' && infix[i]!=')' && infix[i]!='(' )
    {
   output[j++]=infix[i];
    }
   else
    {
    if(tos==0)
    {
    push(infix[i]);
    }
    else
    {
    if(infix[i]!=')' && infix[i]!='(')
    {
    if( prec(infix[i]) <= prec(stack[tos-1])  )
    {
    output[j++]=pop();
    push(infix[i]);
    }
    else
    {
    push(infix[i]);
    } }
    else
    {
    if(infix[i]=='(')
    {push(infix[i]);}
    if(infix[i]==')')
    {
    temp=pop();
    while(temp!='(')
    {output[j++]=temp;
    temp=pop();}
    }}}}}
    while(tos!=0)
    {
    output[j++]=pop();
    }
 printf("the postfix expression is: %s\n",output);}

void push(int element)
{
    stack[tos]=element;
    tos++;
}
char pop()
{
    tos--;
    return(stack[tos]);
}

int prec(char symbol)
{
if(symbol== '(')
return 0;
if(symbol== ')')
return 0;
if(symbol=='+' || symbol=='-')
return 1;
if(symbol=='*' || symbol=='/')
return 2;
if(symbol=='^')
return 3;
return 0;
}