smalltalk - 教科书上的 PetitParser 算术脚本不起作用。它一直说 ParseOn 为零?

标签 smalltalk pharo operator-precedence arithmetic-expressions petitparser

我正在学习如何在 Pharo、Smalltalk 上使用 PetitParser,并且我正在使用教科书来学习它。教科书中给出了以下脚本。

 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'. 

但是,当我尝试在 Playground 上打印它时,我得到 MessageNotUnderstood: receive of "parseOn:"is nil。 我做错了什么?

最佳答案

如果添加 number 的定义,解析器将生成所需的结果。以下代码执行此操作,并且在其他方​​面与您的代码相同(格式除外)

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'

关于smalltalk - 教科书上的 PetitParser 算术脚本不起作用。它一直说 ParseOn 为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42029970/

相关文章:

ruby-on-rails - 为什么使用 Ruby 而不是 Smalltalk?

smalltalk - 使用 SLL 文件类型的经验,可能是 Serena 或 Synergex PVCS 文件?

smalltalk - PetitParser:如何匹配 token 集

parsing - Happy 运算符优先级错误

smalltalk - 将数字(货币)转换为单词的方法

user-interface - 使用 Moose Glamorous Toolkit 在 Pharo/Smalltalk 中构建类似树/大纲的图形界面

smalltalk - 如何改变变形位置

oop - 从字符串动态创建选择器

python - 标记化规则优先级 Pyparsing

windows - perl 优先于 glob @argv