Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a recursive descent parser that analyzes assignment_statement if_statement

ID: 3697498 • Letter: W

Question

Write a recursive descent parser that analyzes assignment_statement if_statement while_statement The parser validates that an input program (containing only assignments, if statement and while loop) is structured correctly according to the language grammar. The parser reads the tokenized representation of a program, generates a simplified symbol table entries on the fly for each symbol defined in the program, and validates it against the associated grammar. To simplify your coding efforts, You may only allow two values for the -- true and false; you may only allow to take integer value only; You may define a single other than compound inside or .

Requirements: 1. Grammar BNF definition of assignment_statement, if_else_statement, and while_statement EBNF definition of assignment_statement, if_else_statement, and while_statement

2. Programming Please pick your favorite programming language, OO or procedural, to code the parser program. Lexical analysis: 1) Lexical analysis of the input program 2) Generates a simplified symbol table.

Syntax analysis 1) Create recursive descent parser (a function for each non-terminal symbol) to validate the input program 2) Generate meaningful messages to inform users of syntactic errors if any (2 points) d. Test driver Please create a test program to run against your parser to see if the parser recognize a user input program.

Bonus: Can you allow nested statements in the grammar definition and parser validation?

Explanation / Answer

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

//program for lexical analysis of a string

void removeduplicate();

void final();

int Isiden(char ch);

int Isop(char ch);

int Isdel(char ch);

int Iskey(char * str);

void removeduplicate();

char op[8]={'+','-','*','/','=','<','>','%'};

char del[8]={'}','{',';','(',')','[',']',','};

char *key[]={"int","void","main","char","float"};

//char *operato[]={"+","-","/","*","<",">","=","%","<=",">=","++"};

int idi=0,idj=0,k,opi=0,opj=0,deli=0,uqdi=0,uqidi=0,uqoperi=0,kdi=0,liti=0,ci=0;

int uqdeli[20],uqopi[20],uqideni[20],l=0,j;

char uqdel[20],uqiden[20][20],uqop[20][20],keyword[20][20];

char iden[20][20],oper[20][20],delem[20],litral[20][20],lit[20],constant[20][20];

void lexanalysis(char *str)

{

   int i=0;

   while(str[i]!='')

    {

     if(Isiden(str[i]))     //for identifiers

       {

          while(Isiden(str[i]))

        {

            iden[idi][idj++]=str[i++];

        }

          iden[idi][idj]='';

          idi++;idj=0;

       }

      else

      if(str[i]=='"')         //for literals

         {

         lit[l++]=str[i];

         for(j=i+1;str[j]!='"';j++)

           {

            lit[l++]=str[j];

           }

         lit[l++]=str[j];lit[l]='';

         strcpy(litral[liti++],lit);

         i=j+1;

         }

      else

      if(Isop(str[i]))        // for operators

          {

         while(Isop(str[i]))

            {

             oper[opi][opj++]=str[i++];

            }

         oper[opi][opj]='';

         opi++;opj=0;

          }

       else

       if(Isdel(str[i]))     //for delemeters

          {

          while(Isdel(str[i]))

            {

              delem[deli++]=str[i++];

            }

           }

        else

           {

            i++;

        }

     }

   removeduplicate();

   final();

}

int Isiden(char ch)

{

   if(isalpha(ch)||ch=='_'||isdigit(ch)||ch=='.')

   return 1;

   else

   return 0;

}

int Isop(char ch)

{

int f=0,i;

for(i=0;i<8&&!f;i++)

   {

    if(ch==op[i])

     f=1;

   }

return f;

}

int Isdel(char ch)

{

int f=0,i;

for(i=0;i<8&&!f;i++)

   {

    if(ch==del[i])

     f=1;

   }

return f;

}

int Iskey(char * str)

{

int i,f=0;

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

   {

    if(!strcmp(key[i],str))

      f=1;

    }

return f;

}

void removeduplicate()

{

   int i,j;

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

    {

     uqdeli[i]=0;

     uqopi[i]=0;

     uqideni[i]=0;

    }

   for(i=1;i<deli+1;i++) //removing duplicate delemeters

     {

       if(uqdeli[i-1]==0)

     {

           uqdel[uqdi++]=delem[i-1];

           for(j=i;j<deli;j++)

          {

               if(delem[i-1]==delem[j])

                uqdeli[j]=1;

          }

      }

     }

    for(i=1;i<idi+1;i++) //removing duplicate identifiers

       {

      if(uqideni[i-1]==0)

         {

        strcpy(uqiden[uqidi++],iden[i-1]);

        for(j=i;j<idi;j++)

         {

            if(!strcmp(iden[i-1],iden[j]))

               uqideni[j]=1;

         }

          }

    }

     for(i=1;i<opi+1;i++) //removing duplicate operators

     {

        if(uqopi[i-1]==0)

           {

           strcpy(uqop[uqoperi++],oper[i-1]);

           for(j=i;j<opi;j++)

             {

               if(!strcmp(oper[i-1],oper[j]))

                 uqopi[j]=1;

             }

           }

     }

}

void final()

{

int i=0;

idi=0;

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

   {

     if(Iskey(uqiden[i]))        //identifying keywords

     strcpy(keyword[kdi++],uqiden[i]);

     else

      if(isdigit(uqiden[i][0]))    //identifying constants

     strcpy(constant[ci++],uqiden[i]);

     else

     strcpy(iden[idi++],uqiden[i]);

   }

// printing the outputs

printf(" Delemeter are : ");

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

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

printf(" Operators are : ");

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

{

printf(" ");

puts(uqop[i]);

}

printf(" Identifiers are : ");

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

{

printf(" ");

puts(iden[i]);

}

printf(" Keywords are : ");

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

{

printf(" ");

puts(keyword[i]);

}

printf(" Constants are : ");

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

{

printf(" ");

puts(constant[i]);

}

printf(" Literals are : ");

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

{

   printf(" ");

   puts(litral[i]);

}

}

void main()

{

char str[50];

//clrscr();

printf(" Enter the string : ");

scanf("%[^ ]c",str);

lexanalysis(str);

//getch();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote