Support for floating point literals.

This commit is contained in:
Stefano Sanfilippo 2014-11-29 22:37:57 +01:00
parent 8c813602d4
commit ad80fc5130
2 changed files with 22 additions and 4 deletions

View File

@ -22,7 +22,6 @@
#include "Parser.hpp"
#include <string>
#include <cstdlib>
static void meta(const char *);
@ -191,9 +190,16 @@ CHAR [a-zA-Z_]
return token::ID;
}
{DIGIT}+ {
lval->intval = strtol(yytext, NULL, 10);
return token::NUMBER;
[-+]?(({DIGIT}*".")?{DIGIT}+|{DIGIT}+".")([eE][-+]?{DIGIT}+)? {
std::string value(yytext);
if (value.find(".") != std::string::npos) {
lval->floatval = std::stod(value);
return token::FLOAT;
} else {
lval->intval = std::stol(value);
return token::NUMBER;
}
}
<INITIAL,shift>. {

12
examples/float.mc Normal file
View File

@ -0,0 +1,12 @@
# Test all possible numerical forms (integer and float)
Lei ha clacsonato
2.0 a posterdati
-2.0 a posterdati
2. a posterdati
.232 a posterdati
-2 a posterdati
-.2233 a posterdati
+2 a posterdati
+2.233e-23 a posterdati