Skip to content

Instantly share code, notes, and snippets.

@thomasrussellmurphy
Created October 3, 2014 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasrussellmurphy/0bb9f503f352311c02cd to your computer and use it in GitHub Desktop.
Save thomasrussellmurphy/0bb9f503f352311c02cd to your computer and use it in GitHub Desktop.
PLY Lex INT or FLOAT Correctly!
# 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