Open HMRDevil opened 6 months ago
There the associativity is incorrect. In the first example this is clearly visible:
1-2-3
equals to:
Subtraction (
[a] => IntValue (
[value] => 1
)
[b] => Subtraction (
[a] => IntValue (
[value] => 2
)
[b] => IntValue (
[value] => 3
)
)
)
That equals to: Subtraction(1, Subtraction(2, 3))
That equals to: (1 - (2 - 3))
We get:
1) 2 - 3
= -1
2) 1 - -1
= 2
It should be inverted it and make it left-associative instead of right) Just a bug, lol)
here are examples of correct grammar with left-associativity https://phplrt.org/docs/examples/advanced-math
@SerafimArts Hello again. I looked at the example from the documentation on the link, and I can say with confidence that it does not work. For clarity, I deleted the extra tokens:
%token T_INT \d+
%token T_PLUS \+
%token T_MUL \*
%token T_BRACE_OPEN \(
%token T_BRACE_CLOSE \)
%skip T_WHITESPACE \s+
%pragma root Expression
Expression
: BinaryExpression()
;
BinaryExpression
: AdditiveExpression()
;
AdditiveExpression
: (MultiplicativeExpression() (<T_PLUS>))* MultiplicativeExpression()
;
MultiplicativeExpression
: (UnaryExpression() (<T_MUL>))* UnaryExpression()
;
UnaryExpression
: ::T_BRACE_OPEN:: Expression() ::T_BRACE_CLOSE::
| Literal()
;
Literal
: <T_INT>
;
By entering 1, we completely switch to the literal. BinaryExpression -> no match -> AdditiveExpression -> no match -> MultiplicativeExpression -> no match -> UnaryExpression -> match. We get a Literal [1]
By entering 1+2, we get an AdditiveExpression, because 1) MultiplicativeExpression => UnaryExpression => Literal [1] and 2) MultiplicativeExpression => UnaryExpression => Literal [2]
By entering 1+2+3, we get an AdditiveExpression ( a .Literal [1] + b. AdditiveExpression ( a. Literal [2] + b. Literal [3])). But if we add new rules:
AdditiveExpression
: AdditiveExpression <T_PLUS> MultiplicativeExpression
| (MultiplicativeExpression() (<T_PLUS>))* MultiplicativeExpression()
;
, we get recursion, because tokens don't change (T_INT T_PLUS T_INT) and AdditiveExpression is BinaryExpression.
I have already tried, it seems to me, all the ways with changes/permutations, etc. of the rules in the current implementation. And today I had the idea to "pack" a Binary Expression into a UnaryExpression.
Hello, Kirill!
I noticed a bug (not a bug, but a feature) that negatively affects calculations.
Description
When 2+ operators with the same priority sequentially follow each other in the original expression, the nodes are arranged like this. From the grammar side, this is true, because it obeys the rules:
Example with subtraction is more illustrative: 1-2-3 Expected: -4 Result: 2
1-2-3-4 Expected: -8 Result: -2
I tried to change the grammatical rules, changing them, swapping them, but in vain. Either it doesn't work, or it goes into recursion.
Possible Solutions
I am ready to do PR, but I doubt the correctness of my solutions
I need help)))
@SerafimArts