Question

I have implemented descent recursive parser in C++ that is based on EBNF grammar and its pseudo-code. Here is the code:

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
char s[100];
int pos=-1,len;

void asignstmt();
void variable();
void expression();
void term();
void primary();
void subscriptlist();
void identifier();
void letter();
void digit();
void error();

void main()
{
clrscr();
cout<<"Enter the String ";
cin>>s;
len=strlen(s);
s[len]='$';
asignstmt();
if (len==pos)
cout<<"string Accepted";
else
cout<<"Strig not Accepted";

getch();
}
void asignstmt()
{
  pos++;
  cout<<pos<<" "<<s[pos]<<endl;
   if(pos<len)
    {
      variable();

       if(s[pos]== '=')
    {
      pos++;cout<<pos<<" "<<s[pos]<<endl;
      expression();
    }
    else
        error();
     }
}

void variable()
{
  identifier();
  if(s[pos]=='[')
   {
     pos++;cout<<pos<<" "<<s[pos]<<endl;
     subscriptlist();
     if(s[pos]==']')
     pos++;
   }
}

void expression()
{
   term();
    while (s[pos]=='+'  ||   s[pos]=='-')
     {
      pos++; cout<<pos<<" "<<s[pos]<<endl;
      term();
     }
}

void term()
{
  primary();
    while (s[pos]=='*'  ||   s[pos]=='/')
     {
      pos++; cout<<pos<<" "<<s[pos]<<endl;
      primary();
     }
}


void primary()
{
if ((s[pos]>='A'|| s[pos]>='a') &&(s[pos]<='Z'|| s[pos]<='z'))
   variable();
else if ( s[pos]>='0' && s[pos]<='9')
    digit();
else if ( s[pos]=='(')
  { pos++;    cout<<pos<<" "<<s[pos]<<endl;
    expression();
    if(s[pos]==')')
    pos++;  cout<<pos<<" "<<s[pos]<<endl;
  }
  else
    error();
}

void subscriptlist()
{
expression();
     if(s[pos]==',')
       pos++;  cout<<pos<<" "<<s[pos]<<endl;
     expression();

}

void identifier()
{
int fl=pos;
letter();
if(pos==fl)
error();
while ( (s[pos]>='A'&& s[pos]<='Z') ||(s[pos]>='a'&& s[pos]<='z')||(s[pos]>='0'&& s[pos]<='9'))
{    letter();
     digit();
}
}
void letter()
{
 if((s[pos]>='A'&& s[pos]<='Z') ||(s[pos]>='a'&& s[pos]<='z'))
   pos++;
   cout<<pos<<" "<<s[pos]<<endl;
}

void digit()
{
if(s[pos]>='0' && s[pos]<='9')
pos++;
cout<<pos<<" "<<s[pos]<<endl;
}

void error()
{
cout<<"Error Due to grammar Mismatch"<<endl;
getch();
exit(0);
}

This program simply takes an input(input will be a valid assignment statement without spaces) from user. Checks it whether the assignment statement is correctly suntax-ed or not. Then, prints a message of acceptance or rejection of input string.

My purpose of this implementation is to yield a parser. I have this code, that is working / recognizing correct assignment statement. But I am unable to implement this as a parser in which: It will take a .cpp file as an argument, check it character by character and see if it has correct assignment statement or not.

For example, if the name of my parser is userParser.cpp and the user code file that contains assignment statement is sample.cpp, then the command Like: userParser sample.cpp should parser and check the file for correct syntax of assignment statement. Please guide me to implement the c++ implementation as a parser. Thank you.

Was it helpful?

Solution

First of all, this is not really C++. <iostream.h> has never been part of the C++ standard and has therefore been outdated at least 15 years. And aside from the cout part, there is no C++ left. The procedural approach, the use of a fixed char array instead of a dynamic reziable string, the headers you include and the lack of classes make the rest of your program pure C code.

To parse the input from a file instead of the console, just open a corresponding filestream, get the input from there and parse it. You might want to refactor your program first a bit, e.g. using a string instead of the error prone char[], maybe throw an exception instead of just exiting the application in case of errors, and then wrap your parser logic into a class.

I highlighted some words there, which, reading your code, I think you might not be familiar with. Look them up in the C++ textbook of your choice. It will help you a lot if you want to create more complex programs.

OTHER TIPS

Something like this

#include <fstream.h>
#include <iostream.h>

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        cerr << "Wrong number of arguments\n";
        return 1;
    }
    ifstream file(argv[1]);
    file >> s;
    ...
}

Seems easier than the code you've already written.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top