0

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.

sana
  • 410
  • 2
  • 6
  • 24

2 Answers2

2

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.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
1

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.

john
  • 85,011
  • 4
  • 57
  • 81
  • So by this, all file characters will be shifted to 'S' array, right ? – sana Apr 26 '13 at 06:34
  • Also please guide me: I am using turbo C++, how will I compile the parser and sample files in command line ? – sana Apr 26 '13 at 06:36
  • `` and `` are no C++, at least they have never been standard C++. They were only used by some compiler vendors in the last century. – Arne Mertz Apr 26 '13 at 06:38
  • 1
    @sana the `operator>>` is overloaded for streams, so its not acuallty a shift. And no, not *all* file characters will be put into `s`. It depends on the type of `s` what will happen. If it's the `char[]` defined in your first program, the operator will put only the first word in the file into that array, and produce an overflow with undefined behavior (probably a crash), if the word is too long. But this is all C++ first month's basics. You should learn that from any C++ textbook. – Arne Mertz Apr 26 '13 at 06:43
  • @sana No that will not shift all file characters into the s array. It will only read characters until the first white space. Just the same as `cin >> s;`. – john Apr 26 '13 at 06:53
  • 1
    @sana Your compiler is incredibly old. There are compilers that are free, better and more modern. Your first job should be to get one of those. Next job should really to be to get a C++ book. All the basics will be explained in there. – john Apr 26 '13 at 06:55
  • So, what will you suggest for char[] in place ? – sana Apr 26 '13 at 06:56
  • Modern C++ uses std::string, trouble is you don't have a modern compiler, so it's very hard to help. – john Apr 26 '13 at 06:56
  • Then also please suggest me any one modern compiler. – sana Apr 26 '13 at 06:58
  • 1
    @sama Microsoft Visual Studio Express 2012, gcc, llvm. All are good. http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-products http://gcc.gnu.org/install/binaries.html http://llvm.org/releases/download.html – john Apr 26 '13 at 06:59
  • One last thing, please suggest any good free editor for C++ / C. – sana Apr 26 '13 at 07:00
  • 1
    @sana If you download Visual Studio Express it comes with a built in editor. Good is matter of opinion. Another option is Eclipse which you can use with gcc. – john Apr 26 '13 at 07:02
  • @sana judging from how much john helped you it is surprising that you selected another reply as accepted answer - a reply that is vague and a tad patronizing. – AndersK Apr 26 '13 at 08:56