multiple assignment

Michael Larson <mklarson@gte.net>
28 Feb 2000 03:03:25 -0500

          From comp.compilers

Related articles
multiple assignment mklarson@gte.net (Michael Larson) (2000-02-28)
Re: multiple assignment Jens_Kilian@agilent.com (Jens Kilian) (2000-03-06)
Re: multiple assignment rkrayhawk@aol.com (2000-03-06)
Re: multiple assignment bonzini@gnu.org (2000-03-06)
| List of all articles for this month |

From: Michael Larson <mklarson@gte.net>
Newsgroups: comp.compilers
Date: 28 Feb 2000 03:03:25 -0500
Organization: Compilers Central
Keywords: syntax, design

I have a testbed compiler for a simple C dialect (Renderman Shading
Language). One of the problems I am having with evaluating the parse
tree is with multiple assignement. It should be clean but it really is
a hack for a recusive evaluator.


Say you have an expression like this


a = b = c + 1;


this would result in a node assignment like this using my parser,
similar to the syntax driven parse example in the dragon book.


Printing node list, 9 nodes
Node #0001: 080506E0 type IDENTIFIER a
Node #0002: 080506F8 type IDENTIFIER b
Node #0003: 08050710 type = left 080506E0 right 080506F8
Node #0004: 08050728 type IDENTIFIER c
Node #0005: 08050740 type CONSTANT 1.000000
Node #0006: 08050758 type + left 08050728 right 08050740
Node #0007: 08050770 type = left 08050710 right 08050758
Node #0008: 08050788 type EXPR left 08050770
Node #0009: 080507A0 type STMT left 08050788
                push 1.000000
                push c
                add
                pop b
                push b
                pop a


As you can see the double assign blows the eval method (without a hack),
it will parse the node list bottom up recusively evaluating right nodes
if present then left nodes. I only allow a node to have a type and a
left and right node, singular operations don't have a right node.


Here is the hack...... that makes it work


    case '=':
              eval(pNodes[i].right, pSymbols);
              if ( pNodes[i].left->type == IDENTIFIER)
              {
                        printf("\tpop %s\n", pNodes[i].left->symbol->name);
              }
              else
              {
                        pNode = pNodes[i].left;
                        printf("\tpop %s\n", pNode->right->symbol->name);
                        pNode = eval(pNodes[i].left, pSymbols);
              }
              return(pNodes[i].left);
              break;


Is this a syntax problem? If you do the parse of the above statement
using a left / right node method it seems to result in the same
problem.....


Grammar is
assignment_expr
        : conditional_expr
        | assignment_expr '=' conditional_expr { $$ = node('=',
$1, $3); }
        | ..... MULT_ASSIGN




Thanks,


Mike Larson


Post a followup to this message

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