Where to handle Parsing of Floating Point Number
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, ie 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?
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
.
上一篇: 用maven构建一个scala应用程序(将java源代码混合在一起)
下一篇: 在哪里处理浮点数的解析