Created
October 3, 2014 03:59
-
-
Save thomasrussellmurphy/0bb9f503f352311c02cd to your computer and use it in GitHub Desktop.
PLY Lex INT or FLOAT Correctly!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Have to start with a general numeric token to avoid PLY lexing problems | |
r_NUM_CONST = r'[0-9]*\.?[0-9]+((E|e)(\+|-)?[0-9]+)?' | |
@Token(r_NUM_CONST) | |
def t_NUM_CONST(self, token): | |
try: | |
token.value = int(token.value) | |
token.type = 'INT_CONST' | |
return token | |
except ValueError: | |
pass | |
try: | |
token.value = float(token.value) | |
token.type = 'FLOAT_CONST' | |
return token | |
except ValueError: | |
pass | |
self.t_error("Not INT or FLOAT: " + token.value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment