yacc grammar / expressions parsed wrong ?

chresan@fastmail.fm
Mon, 18 Feb 2008 22:16:53 -0800 (PST)

          From comp.compilers

Related articles
yacc grammar / expressions parsed wrong ? chresan@fastmail.fm (2008-02-18)
Re: yacc grammar / expressions parsed wrong ? christianschneider@fastmail.fm (chresan) (2008-02-21)
| List of all articles for this month |
From: chresan@fastmail.fm
Newsgroups: comp.compilers
Date: Mon, 18 Feb 2008 22:16:53 -0800 (PST)
Organization: Compilers Central
Keywords: yacc, question, comment
Posted-Date: 19 Feb 2008 12:53:29 EST

Hi,


I have a problem in getting my yacc grammer (byacc/j) / parser to
handle arithmetic expressions correctly.
- For the string "((b*c)+a)" the parser produces the equivalent
expression (b*c)+a.
- But for the string "a+(b*c)" the produced expression is (b*c)+(b*c).


The parser code which creates the add-expression is
{ yyval.obj = Expression.add ( (Expression) val_peek(2).obj,
(Expression) val_peek(0).obj ); }


In the second case ("a+(b*c)") both val_peek(2).obj, val_peek(0).obj
refer to the same Expression instance.


Here are the relevant parts of my grammar:


(...)


%left CMP_EQ CMP_NEQ CMP_LT CMP_LEQ CMP_GT CMP_GEQ
%left '&' '|' '^'
%left '+' '-'
%left '*' '/' '%'
%right '~' '!' CAST_TO_INT CAST_TO_BOOLEAN


(...)


expression:
                    ConstExpr
              | StreamOp
              | TypeCastExpr
              /* CompareExpr */
              | expression CMP_EQ expression { $$ = Expression.cmpEq
( (Expression) $1, (Expression) $3 ); }
              | expression CMP_NEQ expression { $$ =
Expression.cmpNeq( (Expression) $1, (Expression) $3 ); }
              | expression CMP_LT expression { $$ = Expression.cmpLt
( (Expression) $1, (Expression) $3 ); }
              | expression CMP_LEQ expression { $$ =
Expression.cmpLEq( (Expression) $1, (Expression) $3 ); }
              | expression CMP_GT expression { $$ = Expression.cmpGt
( (Expression) $1, (Expression) $3 ); }
              | expression CMP_GEQ expression { $$ =
Expression.cmpGEq( (Expression) $1, (Expression) $3 ); }
              /* UnaryExpr */
              | '~' expression { $$ = Expression.inv( (Expression)
$2 ); }
              | '!' expression { $$ = Expression.inv( (Expression)
$2 ); }
              | '-' expression { $$ = Expression.neg( (Expression)
$2 ); }
              /* MultiplicativeExpr */
              | expression '*' expression { $$ = Expression.mul
( (Expression) $1, (Expression) $3 ); }
              | expression '/' expression { $$ = Expression.div
( (Expression) $1, (Expression) $3 ); }
              | expression '%' expression { $$ = Expression.mod
( (Expression) $1, (Expression) $3 ); }
              | expression '^' expression { $$ = Expression.xor
( (Expression) $1, (Expression) $3 ); }
              /* AdditiveExpr */
              | expression '+' expression { $$ = Expression.add
( (Expression) $1, (Expression) $3 ); }
              | expression '-' expression { $$ = Expression.minus
( (Expression) $1, (Expression) $3 ); }
              | expression '&' expression { $$ = Expression.and
( (Expression) $1, (Expression) $3 ); }
              | expression '|' expression { $$ = Expression.or
( (Expression) $1, (Expression) $3 ); }
/* brackets */
              | '(' expression ')' { $$ = $2; }
              ;


I hope someone can see what I am doing wrong here.


Thanks in advance


Christian
[The usual problem is that you're not allocating space properly for
the tree nodes you create. And please spell it "grammar". -John]


Post a followup to this message

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