Skip to content Skip to sidebar Skip to footer

Ply Lexer For Numbers Always Returns Double

I am having trouble in ply lex with int and double using the following program. DOUBLE_VAL is returned for 1 whereas i expected INT_VAL. On changing order of INT_VAL and DOUBLE_VAL

Solution 1:

Your grammar is matching integers with t_DOUBLE_VAL. Change t_DOUBLE_VAL's expression to only match if a decimal point is present:

def t_DOUBLE_VAL(t):
    '[-+]?[0-9]+(\.([0-9]+)?([eE][-+]?[0-9]+)?|[eE][-+]?[0-9]+)'
    return t

Post a Comment for "Ply Lexer For Numbers Always Returns Double"