when I try running this code on pharo, my answers are somewhat off. I try evaluating 1-2+3 but for some reason, it does 1- (2+3) and I do not understand why this is so. Thanks for your time.
number :=  #digit asParser plus token trim ==> [ :token | token inputValue asNumber ].
term := PPUnresolvedParser new.
prod := PPUnresolvedParser new.
term2 := PPUnresolvedParser new.
prod2 := PPUnresolvedParser new.
prim := PPUnresolvedParser new.
term def: (prod , $+ asParser trim , term ==> [ :nodes | nodes first + nodes last ]) / term2.
term2 def: (prod , $- asParser trim , term ==> [ :nodes | nodes first - nodes last ])/ prod.
prod def: (prim , $* asParser trim , prod ==> [ :nodes | nodes first * nodes last ])/ prim.
prod2 def: (prim , $/ asParser trim , prod ==> [ :nodes | nodes first / nodes last ])/ prim.
prim def: ($( asParser trim , term , $) asParser trim ==> [ :nodes | nodes second ]) / number.
start := term end.
start parse: '1 - 2 + 3'
 
                        
Consider the definition of
termThe
/ term2part is anORbetweenand
Let's mentally parse
'1 - 2 + 3'according toterm.We first read
$1and have to decide between the two options above. The first one will fail unlessprodconsumes'1 - 2'. But this is impossible becauseand there is no
$*or$(coming, and'1 - 2'is not parsed by#number.So, we try with
term2, which is defined in a similar wayso now we have a
ORbetween two options.As above, the first option fails, so we next try
prod, thenprimand finallynumber. Thenumberparser begins withwhich consumes the digit
$1and produces the integer1.We are now back in
term2. We consume' - 'and are left with'2 + 3'; which according totermproduces5. So we get1 - 5 = -4.SHORT EXPLANATION
For
termto parse as(1 - 2) + 3,prodshould consume1 - 2, which doesn't becauseprodinvolves no$-.