C++ Programming Blog

 
 
 
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

int main()
     {
       char tmp[15];
       char str[15], csf[15];
       int ssm=0, i, j, v, k, pos=0, a;
       clrscr();

       char pt[6][5][4]={\"\" , \"i\" , \"+\" , \"*\" , \"/\",
             \"E\", \"TX\", \"n\" , \"n\" , \"n\",
             \"X\", \"n\" , \"+TX\", \"n\" , \"\" ,
             \"T\", \"VY\", \"n\" , \"n\" , \"n\",
             \"Y\", \"n\" , \"\"  , \"*VY\", \"\" ,
             \"V\", \"i\" , \"n\" , \"n\" , \"n\"};
     cout<<\"\\n Enter An Expression: \"<<endl;
     cin>>str;
     strcpy(csf,\"E\");
     while(1)
    {
      pos=0;
      while(csf[pos]!=\'E\' &&         //find leftmost NT from csf
        csf[pos]!=\'X\' &&
        csf[pos]!=\'T\' &&
        csf[pos]!=\'Y\' &&
        csf[pos]!=\'V\' && csf[pos]!=\'\\0\')
        pos++;
      ssm=pos;
    if (csf[pos]==\'\\0\')             // if csf contains all terminals then exit
       break;

    for(i=1; i<6; i++)              // find left most NT into parser table into first column
      {
        if(csf[pos]==pt[i][0][0])
           break;
      }
       for(j=1; j<5; j++)            // find the ssm character from source string into parser table into first row
      {
         if(str[ssm]==pt[0][j][0])
        break;
      }

       if(strcmp(pt[i][j],\"n\")==0)     // if entry is not there in parser table, error and exit.
       {
          cout<<\"\\n Production does not exist: \"<<endl;
          cout<<pt[i][j];
          exit(0);
       }

       for(k=0; k<pos; k++)              // replace NT into csf with its parser table entry
       tmp[k]=csf[k];
       tmp[k]=\'\\0\';
       strcat(tmp, pt[i][j]);
       int l=strlen(tmp);

       for(a=pos+1; csf[a]!=\'\\0\'; a++, l++)
       tmp[l]=csf[a];
       tmp[l]=\'\\0\';
       strcpy(csf, tmp);
       cout<<\"\\n\"<<str[ssm]<<\"\\t\"<<csf;
       getch();
 }
       strcat(csf, \"/\");

       cout<<\"\\t\\tcsf ::\"<<csf;

     if(strcmp(csf, str)==0)
         {
         cout<<\"\\n Valid Expression: \"<<endl;
         }
     else
         {
          cout<<\"\\n Invalid Expression:\"<<endl;
         }
      getch();
      return 0;
}
/*

 Enter An Expression:
i+i*i/

i       TX
i       VYX
i       iYX
+       iX
+       i+TX
i       i+VYX
i       i+iYX
*       i+i*VYX
i       i+i*iYX
/       i+i*iX
/       i+i*i           csf ::i+i*i/
 Valid Expression:

*/

    Related Post:
  1. Program of vector class that perform different operations on vector

  2. Program to create vector

  3. Program that uses this DFA and validates whether an entered string is valid integer or not.

  4. Program to illustrate the implementation of arrays as a Linear Queue

  5. Program to illustrate the binary operator(+) overloading by creating a new object

  6. Program to illustrate the difference between the use of strcomp, strcmpi and stricmp

  7. Program to draw a line using Bresenhams Line Algorithm (BLA) for lines with slopes positive and less than 1

  8. Program to show the 3D Reflection Transformation along xy-plane

  9. Program of inline function

  10. Program to show the implementation of Linked List as a Binary Search Tree

  11. Program to estimate the value of First Derivative of the function at the given points from the given data using Central Difference Formula of order 4

  12. Write a Program to create Numeric Triangle

  13. Program to calculate distance summation, subtraction, multiplication and comparison using overloading operators also make it friend functions

  14. Program of singly link list

  15. Program that provides an example of inheritance using private data

  16. Program for Operator Overloading and Friend Function concept

  17. Program of Midpoint Circle Drawing

  18. Program to illustrate the binary operator(+) overloading without creating a new object

  19. Program to estimate the value of First Derivative of the function at the given points from the given data using Central Difference Formula

  20. Program to illustrate the binary operator(-) overloading without creating an object of that class

 
 
Didn't find what you were looking for? Find more on Program of LL parser1