help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: %left is not working


From: Hans Aberg
Subject: Re: %left is not working
Date: Tue, 3 Aug 2010 20:56:19 +0200

On 3 Aug 2010, at 18:54, Sasan Forghani wrote:

I have been writing a simple compiler for a CS class and have run into a few problems that I can't figure out how to solve. %left does not seem to be
working as Bison is derivating immediately.

For example:

a := b + c + d

expr : expr ADDOP expr
expr: ref_name
ref_name: IDENTIFIER

ADDOP is specified with the %left

The directed translation in parse tree representation is

E
E         +         E
R         E         +         E
b          c                    d

Since ADDOP is specified with % left shouldn't the tree look like:

E
E        +         E
E        +        E         R
R                  R         d
b                   c

Bison uses LALR(1), so it build it bottom-up and from the right, as in the first diagram.

Another problem I have run into is sending data back when a recursive call
is finished. If the input is a := (b + c) + d, the code I have is as
follows:

stmt : ref_name BL_ASSIGNMENT expr {

fprintf(ptrToIrFile, "mov %s %s\n", $<blIdentifier>3,
                   $<blIdentifier>1);
}

expr : expr BL_ADDOP expr {

   if(strcmp($<blOperator>2, "+"))
       strcpy($<blOperator>2, "add");
   else
       strcpy($<blOperator>2, "sub");

   char bufferT[6];
   changeToString(++amtOfTemps, "T", bufferT);
   fprintf(ptrToIrFile, "%s         %s          %s          %s\n",
                   $<blOperator>2, $<blIdentifier>1, $<blIdentifier>3,
bufferT);

   strcpy($<blIdentifier>1, bufferT);
}

| ( expr)

as long as there isn't ( expr ) in the input input move up without problem. However once input does have an ( expr) part, $<blIdentifier>1 at the ADDOP level has the value of $<blIdentifier>1 at the ASSIGNMENT level. When I add the following action to the ( expr ) production- {strcpy($<blIdentifier>1, $<blIdentifier>2);}, $<blIdentifier>1 at the ADDOP level has the correct value but then the $<blIdentifier>1 at the ASSIGNMENT level value is what I changed $<blIdentifier>1 to be at the ( expr ) level. I'm very confused.

Can't see the details, if you do strcpy() without properly allocating first, there will be problems. Make sure its OK in the lexer, too.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]