user1142580
user1142580

Reputation: 163

Where to handle Parsing of Floating Point Number - Scanner or Parser?

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?

Upvotes: 3

Views: 928

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754100

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.

Upvotes: 2

Related Questions