Recursive Descent Parser

Rasmus Anthin <e8rasmus@etek.chalmers.se>
9 Jan 2000 22:53:06 -0500

          From comp.compilers

Related articles
Recursive Descent Parser e8rasmus@etek.chalmers.se (Rasmus Anthin) (2000-01-09)
Re: Recursive Descent Parser world!cfc@uunet.uu.net (Chris F Clark) (2000-01-12)
Re: Recursive Descent Parser biocyn@erols.com (2000-01-25)
Re: Recursive Descent Parser chstapfer@bluewin.ch (Christian Stapfer) (2000-02-10)
Re: Recursive Descent Parser rhyde@shoe-size.com (Randall Hyde) (2000-02-12)
Re: Recursive Descent Parser mklarson@gte.net (Michael Larson) (2000-02-22)
Re: Recursive Descent Parser paulyg@clara.net (2000-02-22)
[4 later articles]
| List of all articles for this month |

From: Rasmus Anthin <e8rasmus@etek.chalmers.se>
Newsgroups: comp.compilers
Followup-To: poster
Date: 9 Jan 2000 22:53:06 -0500
Organization: Chalmers University of Technology
Keywords: parse, question

Now I have my parser working... almost.
I have some problems with the Node pointers. There is surely something
simple I've forgot to do. But I would appreciate some help with this
anyway.
The parser looks somewhat like this:


//---RDP---


void expr(Nod *pek){
    term(pek);
    if(t[toknum].token[0]=='+'){
        toknum++; //global
        pek->right=new Nod;
        pek->data="+";
        expr(pek->right);
    }
}


void term(Nod *pek){
    fact(pek);
    if(t[toknum].token[0]=='*'){
        status(2,6);
        toknum++;
        pek->right=new Nod;
        pek->data="*";
        term(pek->right);
    }
}




void fact(Nod *pek){
    if(t[toknum].token[0]=='('){
        toknum++;
        pek->right=new Nod;
        expr(pek->right);
    }
    else{
        num(pek);
    }
}


void num(Nod *pek){
    if(digit(t[toknum].token[0]) || digit(t[toknum].token[1])){
        if(t[toknum].row==t[toknum+1].row){
            pek->left=new Nod;
            pek->left->data=t[toknum].token;
        }
        else
            pek->data=t[toknum].token;
        toknum++;
    }
}


//----------------------------


Well this works fine with expressions like 1+2+3 and 1+2*3.
But 1*2+3 Doesn't work out very well. The tree for 1*2+3 should (I think)
look something like this:


              +
          / \
      * 3
  / \
1 2


What is wrong with this parser? Can somebody see it?
I would really appreciate some help on this.


Thanx in advance.
Sincerely,
\ B. Rasmus Anthin \ _ ______ |
  \ ================ \ / \___-=O`/|O`/__|
    \ \_______\ / | / )
    / e8rasmus@etek.chalmers.se / `/-==____/__|/__=-|
  / www.etek.chalmers.se/~e8rasmus / * \| |


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.