Showing posts with label spcc. Show all posts
Showing posts with label spcc. Show all posts

Saturday, February 12, 2022

Parser first and Follow LEX C program for Syntax analysis

 Below is the source code to:-

Develop a program to implement

a. Predictive parser

b. Operator precedence parser


#include<stdio.h> 

#include<conio.h> 

#include<string.h> 

char prol[7][10]={"S","A","A","B","B","C","C"}; 

char pror[7][10]={"A","Bb","Cd","aB","@","Cc","@"}; 

char prod[7][10]={"S->A","A->Bb","A->Cd","B->aB","B->@","C->Cc","C->@"}; char first[7][10]={"abcd","ab","cd","a@","@","c@","@"}; char 

follow[7][10]={"$","$","$","a$","b$","c$","d$"}; 

char table[5][6][10]; 

int numr(char c) 

switch(c){ 

case 'S': return 0; 

case 'A': return 1; 

case 'B': return 2; 

case 'C': return 3; 

case 'a': return 0; 

case 'b': return 1; 

case 'c': return 2; 

case 'd': return 3; 

case '$': return 4; 

return(2); 

void main() 

int i,j,k; 

clrscr(); 

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

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

strcpy(table[i][j]," "); 

printf("\nThe following is the predictive parsing table for the following grammar:\n"); for(i=0;i<7;i++) 

printf("%s\n",prod[i]); 

printf("\nPredictive parsing table is\n"); 

fflush(stdin); 

for(i=0;i<7;i++){ 

k=strlen(first[i]); 

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

if(first[i][j]!='@') 

strcpy(table[numr(prol[i][0])+1][numr(first[i][j])+1],prod[i]); } 

for(i=0;i<7;i++){ 

if(strlen(pror[i])==1) 

if(pror[i][0]=='@') 

k=strlen(follow[i]); 

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

strcpy(table[numr(prol[i][0])+1][numr(follow[i][j])+1],prod[i]); } 

strcpy(table[0][0]," "); 

strcpy(table[0][1],"a"); 

strcpy(table[0][2],"b"); 

strcpy(table[0][3],"c"); 

strcpy(table[0][4],"d"); 

strcpy(table[0][5],"$"); 

strcpy(table[1][0],"S"); 

strcpy(table[2][0],"A"); 

strcpy(table[3][0],"B"); 

strcpy(table[4][0],"C"); 

printf("\n--------------------------------------------------------\n"); for(i=0;i<5;i++) 

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

printf("%-10s",table[i][j]); 

if(j==5) 

printf("\n--------------------------------------------------------\n"); } 

getch(); 

}

Thursday, February 3, 2022

First and Follow code using flex in C language System Programming and Compilers

The concept of First and Follow is widely used in the System Programming and Compiler Design. This concept is quite important for all the students learning the subject. 

Code below....

For other Subjects program code do visit below links:-


Below is the code for first and Follow program using C.


First Program Source Code

#include<stdio.h>

#include<ctype.h>

void FIRST(char[],char );

void addToResultSet(char[],char);

int numOfProductions;

char productionSet[10][10];

void main()

{             int i;

    char choice;

    char c;

    char result[20];

    printf("How many number of productions ? :");

    scanf(" %d",&numOfProductions);

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

    {              printf("Enter productions Number %d : ",i+1);

        scanf(" %s",productionSet[i]);              }

    do

    {         printf("\n Find the FIRST of  :");

        scanf(" %c",&c);

 FIRST(result,c);

        printf("\n FIRST(%c)= { ",c);

        for(i=0;result[i]!='\0';i++)

 printf(" %c ",result[i]);      

        printf("}\n");

         printf("press 'y' to continue : ");

        scanf(" %c",&choice);       }

    while(choice=='y'||choice =='Y');   }

void FIRST(char* Result,char c)

{   int i,j,k;

    char subResult[20];

    int foundEpsilon;

    subResult[0]='\0';

    Result[0]='\0';

    if(!(isupper(c)))

    { addToResultSet(Result,c);

               return ; }

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

    { if(productionSet[i][0]==c)

        {  if(productionSet[i][2]=='#') addToResultSet(Result,'#');

      else

            {               j=2;

                while(productionSet[i][j]!='\0')

                {       foundEpsilon=0;

                FIRST(subResult,productionSet[i][j]);

                for(k=0;subResult[k]!='\0';k++)

                    addToResultSet(Result,subResult[k]);

                 for(k=0;subResult[k]!='\0';k++)

                     if(subResult[k]=='#')

                     {       foundEpsilon=1;

                         break;              }

                 if(!foundEpsilon)

                     break;

                 j++;     }              }             }              }

    return ;           }

void addToResultSet(char Result[],char val)

{             int k;

    for(k=0 ;Result[k]!='\0';k++)

        if(Result[k]==val)

            return;

    Result[k]=val;

    Result[k+1]='\0';       }


 

 


Follow Program Source Code:

#include<stdio.h>
#include<ctype.h>
#include<string.h>
int n,m=0,p,i=0,j=0;
char a[10][10],followResult[10];
void follow(char c);
void first(char c);
void addToResult(char);
int main()
{             int i;
 int choice;
 char c,ch;
 clrscr();
 printf("Enter the no.of productions: ");
scanf("%d", &n);
 printf(" Enter %d productions\nProduction with multiple terms should be give as separate productions \n", n);
 for(i=0;i<n;i++)
  scanf("%s%c",a[i],&ch);
    do
 {            m=0;
  printf("Find FOLLOW of -->");
  scanf(" %c",&c);
  follow(c);
  printf("FOLLOW(%c) = { ",c);
  for(i=0;i<m;i++)
   printf("%c ",followResult[i]);
  printf(" }\n");
  printf("Do you want to continue(Press 1 to continue....)?");
 scanf("%d",&choice);
 }     while(choice==1);              }
void follow(char c)
{ if(a[0][0]==c)addToResult('$');
 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(a[i][j+1]);
    if(a[i][j+1]=='\0'&&c!=a[i][0])
     follow(a[i][0]);              }             }              }             }
void first(char c)
{             int k;
                 if(!(isupper(c)))

                     addToResult(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]))

                     addToResult(a[k][2]);
                 else first(a[k][2]);      }              }             }
void  addToResult(char c)
{             int i;
    for( i=0;i<=m;i++)
        if(followResult[i]==c)
            return;
   followResult[m++]=c;                            }