simple syntax analysis

"Julia Donawald" <julia.donawald@gmx.de>
27 May 2002 01:21:14 -0400

          From comp.compilers

Related articles
simple syntax analysis julia.donawald@gmx.de (Julia Donawald) (2002-05-27)
Re: simple syntax analysis joachim_d@gmx.de (Joachim Durchholz) (2002-05-27)
Re: simple syntax analysis andreas.gieriet@externsoft.ch (Andreas Gieriet) (2002-05-31)
Re: simple syntax analysis misar@rbg.informatik.tu-darmstadt.de (Walter Misar) (2002-06-07)
| List of all articles for this month |

From: "Julia Donawald" <julia.donawald@gmx.de>
Newsgroups: comp.compilers
Date: 27 May 2002 01:21:14 -0400
Organization: T-Online
Keywords: syntax, question, parse
Posted-Date: 27 May 2002 01:21:14 EDT

Hi,
I have written the following pseudo-code to built up a parse tree of the
following sequence: [A] [OR] [B] [AND] [C]. I read in some books about that
theme and create the following grammar:
expr := factor | factor OR expr | factor AND expr
factor:= letter | (expr)


>From this grammar I wrote with the help of the books the following recursive
"C++ similar"-pseudo-code ( p contains the string to parse ):


Letter* expr()
{
        Letter* pLeft = factor();


          if(p[j] == OR)
          {
                    j++;
                    Letter* pRight = expr();
                    pLeft = CreateOperatorOr(pLeft, Pright);
          }
          else if(p[j] == AND)
          {
                    j++;
                    Letter* pRight = expr();
                    pLeft = CreateOperatorAnd(pLeft, Pright);
          }
          return pLeft;
}


Letter* factor()
{
          Letter* pResult;
          if(p[j] == "(" )
        {
                    j++;
                    pResult = expr();
                    if(p[j] == ")" )
                              j++;
                    else
                              Error();
          }
          else if(p[j] == Letter)
          {
                    j++;
                    pResult = CreateLetter();
          }
          return pResult;
}


The code I wrote works fine and create a parse tree, the only problem is,
that it didnt create the tree I want:


                                                                OR
                                                            / \
                                                        A AND
                                                                            / \
                                                                        B C
but I want:
                                                        AND
                                                      / \
                                                OR C
                                                / \
                                            A B
So I want that first the both left letters are considered and after the
result of these both in connection with the next is calculated and so on...
so maybe I want something like: more left, first considered.
Maybe my grammar is wrong ( I dont need any priority of the operators ), or
the code I wrote from it. But really dont know what I have done wrong.


Thanks for any advices
Julia



Post a followup to this message

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