使用解析器组合器抑制'seq(p,many(p))'构造中'many'的空结果

问题描述 投票:1回答:1

我正在尝试按照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测序组合器接受maybemany(使用maybe)的结果为零,就会发生这种情况。

使用Parser Combinators在输出中排除这些额外噪声的正常方法是什么?

github repo

functional-programming postscript parser-combinators
1个回答
0
投票

叹。我似乎可以围绕它实施。我在seq中添加了特殊代码来检测空的右侧并丢弃它。关于其他问题......

© www.soinside.com 2019 - 2024. All rights reserved.