simple syntax analysis

"Julia Donawald" <>
27 May 2002 01:21:14 -0400

          From comp.compilers

Related articles
simple syntax analysis (Julia Donawald) (2002-05-27)
Re: simple syntax analysis (Joachim Durchholz) (2002-05-27)
Re: simple syntax analysis (Andreas Gieriet) (2002-05-31)
Re: simple syntax analysis (Walter Misar) (2002-06-07)
| List of all articles for this month |

From: "Julia Donawald" <>
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

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)
                    Letter* pRight = expr();
                    pLeft = CreateOperatorOr(pLeft, Pright);
          else if(p[j] == AND)
                    Letter* pRight = expr();
                    pLeft = CreateOperatorAnd(pLeft, Pright);
          return pLeft;

Letter* factor()
          Letter* pResult;
          if(p[j] == "(" )
                    pResult = expr();
                    if(p[j] == ")" )
          else if(p[j] == Letter)
                    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:

                                                            / \
                                                        A AND
                                                                            / \
                                                                        B C
but I want:
                                                      / \
                                                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

Post a followup to this message

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