Re: simple syntax analysis

"Andreas Gieriet" <andreas.gieriet@externsoft.ch>
31 May 2002 23:08:47 -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: "Andreas Gieriet" <andreas.gieriet@externsoft.ch>
Newsgroups: comp.compilers
Date: 31 May 2002 23:08:47 -0400
Organization: eXternSoft GmbH
References: 02-05-140
Keywords: parse
Posted-Date: 31 May 2002 23:08:46 EDT

Julia,


enclosed please find a pseudo code implementation with the
following assumptions:
    - the syntax is something like (EBNF):
              expr : factor { op factor } .
              factor : IDENT | '(' expr ')' .
              op : 'AND' | 'OR' .


    - class tree:
                                SyntaxElement
                                        <- Expression
                                                  <- Factor
                                                  <- Identifier
                                        <- Operator


    - the Scanner provides the respective tokens and access methods


    - the create() methods construct a new object from the given arguments


    - the distinction between AND and OR is done when the tree is
evaluated,
        i.e., when some meaningful action is performed on the operands






Here goes the code:


------8<----------8<----------8<----------8<----------8<----------8<--------


Expression* Expression::parse() {
        Expression* expr = Factor::parse();
        while (Scanner::isToken(OP) {
                expr = Expression::create(expr, Operator::parse(),
Factor::parse());
        }
        return expr;
}


Expression* Factor::parse() {
        if (Scanner::isToken(LParen) {
                Scanner::next();
                Expression* expr = Expression::parse();
                Scanner::assertToken(RParen);
                Scanner::next();
                return expr;
        }
        return Identifier::parse();
}


Expression* Identifier::parse() {
      Scanner::assertToken(Ident);
      Expression* expr = Identifier::create(Scanner::string());
      Scanner::next();
      return expr;
}


------8<----------8<----------8<----------8<----------8<----------8<--------


Cheers,


Andi


--
Andreas Gieriet mailto:andreas.gieriet@externsoft.ch
eXternSoft GmbH http://www.externsoft.com/
Zurlindenstrasse 49 phone:++41 1 454 3077
CH-8003 Zurich/SWITZERLAND fax: ++41 1 454 3078




Julia Donawald schrieb:
>
> 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


Post a followup to this message

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