boss revs
boss revs

Reputation: 351

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

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?

Upvotes: 2

Views: 71

Answers (1)

Leandro Caniglia
Leandro Caniglia

Reputation: 14858

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'

Upvotes: 2

Related Questions