我正在尝试按照Hutton和Meijer,“Monadic Parser Combinators”构建解析器组合器。我的实现是在PostScript中,但我认为我的问题对于组合器解析器是通用的,而不是我的具体实现。
作为一个小练习,我使用解析器来识别正则表达式。
(pc9.ps)run
/Dot (.) char def
/Meta (*+?) anyof def
/Character (*+?.|()) noneof def
/Atom //Dot
//Character plus def
/Factor //Atom //Meta maybe seq def
/Term //Factor //Factor many seq def
/Expression //Term (|) char //Term xthen many seq def
/regex { string-input //Expression exec ps } def
(abc|def|ghi) regex
quit
它正在工作,但输出有很多[]
空数组,当我尝试使用bind
处理程序来处理值时,这些数组确实受到阻碍。
$ gsnd -q -dNOSAFER pc9re2.ps
stack:
[[[[[97 []] [[98 []] [[99 []] []]]] [[[100 []] [[101 []] [[102 []]
[]]]] [[[103 []] [[104 []] [[105 []] []]]] []]]] null]]
只要seq
测序组合器接受maybe
或many
(使用maybe
)的结果为零,就会发生这种情况。
使用Parser Combinators在输出中排除这些额外噪声的正常方法是什么?
叹。我似乎可以围绕它实施。我在seq
中添加了特殊代码来检测空的右侧并丢弃它。关于其他问题......