YAML解析-是lex还是手动滚动? [英] YAML parsing - lex or hand-rolled?

查看:101
本文介绍了YAML解析-是lex还是手动滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的YAML解析器,我从yaml.org阅读了规范, 在开始之前,我想知道编写手动分析器是否更好,或者 使用lex(flex/bison).我看着libyaml(C库)- 似乎不使用lex/yacc. YAML(不包括 flow样式)似乎更面向行,是吗? 更容易编写手动解析器,或使用flex/bison 谢谢.

I am trying to write a simple YAML parser, I read the spec from yaml.org, before I start, I was wondering if it is better to write a hand-rolled parser, or use lex (flex/bison). I looked at the libyaml (C library) - doesn't seem to use lex/yacc. YAML (excluding the flow styles), seems to be more line-oriented, so, is it easier to write a hand-rolled parser, or use flex/bison Thanks.

推荐答案

此答案基本上是对以下问题的答案:我应该滚动自己的解析器还是使用解析器生成器?"与YAML无关.但尽管如此,它会回答"您的问题.

您需要问的问题不是用给定的语言/语法完成这项工作",而是我有信心实现这一点".事实是,您要解析的大多数格式都只能与生成的解析器一起使用.另一个事实是,使用简单的手写递归下降解析器解析甚至复杂的语言也是可行的. .

The question you need to ask is not "does this work with this given language/grammar", but "do I feel confident to implement this". The truth of the matter is that most formats you want to parse will just work with a generated parser. The other truth is that it is feasible to parse even complex languages with a simple hand written recursive descent parser.

除其他外,我还编写了 EDDL (C和结构化元素)的递归下降解析器以及bison/flex INI解析器.我选择了这些示例,因为它们违背了直觉,并且外部要求决定了此决定.

I have written among others, a recursive descent parser for EDDL (C and structured elements) and a bison/flex parser for INI. I picked these examples, because they go against intuition and exterior requirements dictated the decision.

由于我建立了技术水平,所以有可能,为什么您会选择一个?这确实是一个很难回答的问题,这是关于此主题的一些想法:

Since I established on a technical level it is possible, why would you pick one over the other? This is really hard question to answer, here are some thoughts on the subject:

  • 写一个好的词法分析器真的很困难.在大多数情况下,使用flex生成词法分析器是有意义的.除非您确实有异国情调的输入格式,否则手动滚动自己的词法分析器几乎没有用.
  • 使用野牛或类似的生成器使用于解析的语法清晰可见.这样做的主要好处是,在五年内维护您的解析器的开发人员将立即看到所使用的语法并将其与任何规范进行比较.
  • 使用递归下降解析器可以使解析器中发生的事情非常清楚.这提供了轻松解决哈里冲突的简便方法.您可以编写一个简单的if,而不是将整个语法重新排列为LALR1.
  • 在开发解析器时,您可以使用手写的解析器修饰细节",而使用野牛则几乎是不可能的.在野牛中,语法必须起作用,否则生成器将无能为力.
  • 野牛很棒地指出了语法中的形式缺陷.不幸的是,您一个人来修复它们.手动滚动解析器时,您只会在解析器读取废话时发现缺陷.

这不是一个确定的答案,但它会为您指明正确的方向.既然您似乎在编写解析器很有趣,所以我认为您应该同时编写两种类型的解析器.

This is not a definite answer for one or the other, but it points you in the right direction. Since it appears that you are writing the parser for fun, I think you should have written both types of parser.

这篇关于YAML解析-是lex还是手动滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆