Taking precedence

It has finally been done, PHP-Front has perfect operator precedence!

And why can this be written down as a fact? Because of the work of Martin! He has worked on the grammar-engineering-tools tool for generating SDF priorities. When I worked on them I ran into some problems because of the decisions that where made during the initial development (read: when the tools had to be finished yesterday). This resulted in a common format for PHP precedence rules that was neither YACC-like nor SDF-like. Martin has rewritten the tools to use most of SDF representation and this worked very good according to all the solved issues. The operator precedence is now encoded into PHP-Front as separate modules for both version 4 and version 5, both of them over 6k LOC.

The (technical) details about how these files are generated will probably make a long and interesting blog-post. Since Martin says it is hard to find such a subject, not to mention (again) that it is his work, who am I to take this subject away from him? Maybe he can find some time after preparing the LDTA-presentation for Thursday. I won't be able to make it, but if you are close to Delft you might want to sneak into the Research Colloqium. (If you do, please tape it!)

Besides bringing perfect precedence to PHP-Front, this work has also resulted in a new issue. It was not very hard to figure it out, I just misinterpreted the note in the documentation. So I immediately fixed it by removing a special case which did not have to exist, just to put the icing on the cake.


Khwab said...

echo '5Text' . 1 + 2 . '50';

This should work like " echo ( 5Text . 1 ) + ( 2 . '50' ); " as concatenation operator has more precedence than addition operator.

Thats "echo 5Text1 + 250;"

that means " 5 + 250 " as when we convert 5Text1 to integer it gives 5.

Thats 255 but when we run, the result comes 750, can anyone explain how?



Eric Bouwers said...

Hi Khwab,

The expression '5Text' . 1 + 2 . '50'; given to the echo statement is evaluated from left to right. This means that it first concatenates '5Text' and '1' which results in '5Text1'. This is then added to '2' with a result of '7'. This is concatenated in front of '50', which gives the results you see.

The reason for this left-right-evaluation is that the operators '+' and '.' share the same perecedence, and are left-associative. You can find more details on this here.