PetitParser arithmatic script from textbook is not working. It keeps saying ParseOn is nill?

95 Views Asked by At

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?

1

There are 1 best solutions below

0
Leandro Caniglia On

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'