2

I am learning how to use PetitParser on Pharo, Smalltalk and I am using a textbook to learn it. In the textbook, the following script is given.

 term := PPDelegateParser new.
 prod := PPDelegateParser new.
 prim := PPDelegateParser new.
 term setParser: (prod , $+ asParser trim , term ==> [ :nodes | nodes first + nodes last ]) / prod.
 prod setParser: (prim , $*asParser trim , prod ==> [ :nodes | nodes first*nodes last ]) / prim.
 prim setParser: ($( asParser trim , term , $) asParser trim ==> [ :nodes | nodes second ]) / number.
 start := term end.
 start parse:'1+2*3'. 

however,when i try to print it in the playground i get MessageNotUnderstood: receiver of "parseOn:" is nil. What did I do wrong?

boss revs
  • 331
  • 2
  • 4
  • 13
  • 1
    What is `number`? I don't see it defined in the snippet. – Leandro Caniglia Feb 03 '17 at 20:37
  • you are right, thankyou. If you could look at this question, same code but i added a division and multiplication method aswell. http://stackoverflow.com/questions/42034072/petitparser-evaluator-not-working-properly – boss revs Feb 03 '17 at 22:37

1 Answers1

2

If you add the definition of number, the parser produces the desired result. The following code does that and is otherwise identical to yours (except for the formatting)

number := #digit asParser plus token trim
    ==> [:token | token inputValue asNumber].
term := PPDelegateParser new.
prod := PPDelegateParser new.
prim := PPDelegateParser new.
term
    setParser: prod , $+ asParser trim , term
        ==> [:nodes | nodes first + nodes last]
        / prod.
prod
    setParser: prim , $* asParser trim , prod
        ==> [:nodes | nodes first * nodes last]
        / prim.
prim
    setParser: $( asParser trim , term , $) asParser trim
        ==> [:nodes | nodes second]
        / number.
start := term end.
^start parse: '1+2*3'
Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51