是否存在在运行时生成 AST/解析树的解析器?有点像一个库,它会接受一串 EBNF 语法或类似的东西并吐出数据结构?
现在我知道解析器组合器。 (谢谢,乔纳斯)还有一些图书馆(谢谢 eliben)
顺便说一句,我也注意到Parsing Expression Grammars最近,有人实现它听起来很酷(他们说 Perl 6 会有它,但 Perl 回避了我的理解)
最佳答案
看看parser combinators我认为这可以帮助你。可以使用这种技术在运行时创建解析器。一种流行的解析器组合器是 Parsec使用 Haskell作为它的宿主语言。来自 parsec documentation :
Combinator parsers are written and used within the same programming language as the rest of the program. There is no gap between the grammar formalism (Yacc) and the actual programming language used (C)
Parsers are first-class values within the language. They can be put into lists, passed as parameters and returned as values. It is easy extend the available set of parsers with custom made parsers specific for a certain problem
如果您使用 .NET,请查看 parser combinator library for F# .
关于parsing - 动态 (?) 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/185584/