Walter - Discrepancy between EqualExpression spec and implementation

Walter Bright newshound at digitalmars.com
Wed Jun 7 00:47:34 PDT 2006


Jarrett Billingsley wrote:
> I'm writing a small scripting language with a syntax based on D, and as 
> such, I'm basing some of the parsing code off the the DMD frontend source. 
> I've found a discrepancy between the spec and the code, though, and I'm not 
> sure if it's a bug or if it was changed in the code on purpose and the spec 
> just hasn't been updated.
> 
> For EqualExpressions, the following grammar is given:
> 
> EqualExpression:
>     RelExpression
>     EqualExpression == RelExpression
>     EqualExpression != RelExpression
>     EqualExpression is RelExpression
>     EqualExpression !is RelExpression
> 
> Notice that the left-hand operand of the operators is an EqualExpression.
> 
> However, in the code (parse.c line 4365), instead of using parseEqualExp() 
> to get the left-hand operand as I would have expected, it instead uses 
> parseRelExp().  Meaning that the code implements the following grammar 
> instead:
> 
> EqualExpression:
>     RelExpression
>     RelExpression == RelExpression
>     RelExpression != RelExpression
>     RelExpression is RelExpression
>     RelExpression !is RelExpression
> 
> I'm pretty sure only Walter can answer this one! 

It's implemented as (simplified):

Expression *Parser::parseEqualExp()
{   Expression *e;
     Expression *e2;
     e = parseRelExp();
     while (1)
     {   enum TOK value = token.value;

         switch (value)
         {
             case TOKequal:
             case TOKnotequal:
                 nextToken();
                 e2 = parseRelExp();
                 e = new EqualExp(value, loc, e, e2);
                 continue;

             default:
                 break;
         }
         break;
     }
     return e;
}

which means that:
	a == b == c == d
is parsed as:
	(((a == b) == c) == d)
which I think you'll see matches the grammar. The while loop makes it 
left-associative.



More information about the Digitalmars-d mailing list