在哪里处理浮点数的解析 - 扫描器或解析器?
我正在设计一种小型领域特定语言。 作为扫描器/解析器生成器,我使用 Flex/Bisonc++。 现在,生成的 DSL 编译器前端能够解析八进制、十进制和十六进制数。 唯一剩下的就是对 C/C++ 中规定的浮点数 (FPN) 的支持。
浮点数语法的正则表达式位于 http://rosettacode.org/wiki/Literals/Floating_point#C
a) 我知道可以在扫描仪或/和解析器中完成的解析, 但我不知道在性能和效率方面什么是最好的。
b) 一个额外的约束是,我想避免多次接触输入的每个字符,即我想避免使用 STL 或其他字符串到浮点转换函数 通过在解析过程中实现即时转换。 这可能吗?
I'm in the process of designing a small domain specific language.
As scanner/parser generators I use Flex/Bisonc++.
Now, the generated DSL-compiler frontend is capable of parsing octal, decimal and hex numbers.
The only thing remaining is the support for floating point numbers (FPN) as notated in C/C++.
There's a RegExp for the floating point number syntax at
http://rosettacode.org/wiki/Literals/Floating_point#C
a) I know that parsing that can be done in the scanner or/and in the parser,
but I don't know what's best - in terms of performance and efficiency.
b) One additional constraint is, that I want to avoid touching each character of the input more than once, i.e. I want to avoid using STL or other string-to-float conversion functions
by implementing an on-the-fly conversion during the parsing process.
Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让扫描器(
flex
代码)识别和转换浮点数是完全可行的,并且通常是明智的,就像它应该识别和转换整数一样。如果您在语法(bison
代码)而不是扫描器中编写了整数识别代码,那么也许您也在那里编写了浮点识别,但这不是通常完成的方式。您可能需要对标记类型使用更复杂(读作“复杂”)的数据类型;阅读%union
。It is perfectly feasible, and normally sensible, to have the scanner (
flex
code) recognize and convert the floating point numbers, just as it should be recognizing and converting the integers. If you've written the integer recognition code in the grammar (bison
code) rather than the scanner, then maybe you write the floating-point recognition there too, but it is not the way it is normally done. You will probably need to use a more sophisticated (read 'complex') data type for the token types; read up on%union
.