Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » ParsingRSS Feeds

To parse a string using Recursive-Descent parser

Posted By: Yasmin Silva     Category: C++ Programming     Views: 4765

To parse a string using Recursive-Descent parser.

Code for To parse a string using Recursive-Descent parser in C++ Programming

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


typedef struct treenode *tree;
struct treenode
{
    char info;
    tree left;
    tree right;
};

static i = 0;
char nextsym(char input[]),next;
char input[10];

void main()
{
 tree root;
 int len;
 tree proc_e();
 void inorder(tree root);

// clrscr();
 cout<<"enter an expression :";
 cin>>input;
 len = strlen(input);
 next = nextsym(input);
 root = proc_e();
 if (len != i-1)
    {
    cout<<"error";
    getch();
    exit(0);
    }
 else
    {
    cout<<"its a valid expression\n";
    inorder(root);
    }
}

tree treebuild(char x,tree a,tree b)
{
 tree t;
     t = new treenode;
     t->info = x;
     t->left = a;
     t->right = b;

    return(t);
}

tree proc_e()
{
    tree a,b;
    tree proc_t();
    a = proc_t();
    while(next == '+'||next == '-' )
    {
        if(next == '+' )
        {
        next = nextsym(input);
        b = proc_t();
        a = treebuild('+',a,b);
        }
        else
        {
        next = nextsym(input);
        b = proc_t();
        a = treebuild('-',a,b);
        }

    }
    return(a);
}

tree proc_t()
{
    tree a,b;
    tree proc_v();
    a = proc_v();
    while(next == '*' || next == '/')
    {
        if(next == '*')
        {
        next = nextsym(input);
        b = proc_v();
        a = treebuild('*',a,b);
        }
        else
        {
        next = nextsym(input);
        b = proc_v();
        a = treebuild('/',a,b);
        }
    }
    return(a);
}

tree proc_v()
{
    tree a;

    if (isalpha(next))
        a = treebuild(next,NULL,NULL);
    else
        {
        cout<<"error";
        getch();
        exit(0);
        }
    next = nextsym(input);
    return(a);
}

char nextsym(char input[])
{
    i++;
    return input[i-1];
}

void inorder(tree t)
{
if (t != NULL)
    {
    inorder(t->left);
    cout<<t->info<<"   ";
    inorder(t->right);
    }
}

/*************************************
OUTPUT
*************************************


enter an expression :a+b*c+d
its a valid expression
a + b * c + d

enter an expression :a+b*+
error

**************************************/
  
Share: 


Didn't find what you were looking for? Find more on To parse a string using Recursive-Descent parser Or get search suggestion and latest updates.

Yasmin    Silva
Yasmin Silva author of To parse a string using Recursive-Descent parser is from Salvador, Brazil.
 
View All Articles

 

Other Interesting Articles in C++ Programming:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!