I have the following parser
END = Literal(';').suppress()
POINT = Literal('.')
COMMA = Literal(',').suppress()
COLON = Word(':', exact=1).suppress()
EQUAL = Literal('=').suppress()
VARNAME = Word(alphanums, max=3)
DIGIT = Word(nums, exact=1)
SIGN = oneOf('+ -')
OPER = oneOf('+ - * / ^ ')
NATNUM = DIGIT + ZeroOrMore(DIGIT)
REALNUM = Combine(Optional(SIGN) + NATNUM + Optional(POINT)*1 + NATNUM)
EXRPESS = Forward()
EXRPESS << Combine((REALNUM | VARNAME) + ZeroOrMore(OPER*1 + EXRPESS), adjacent=False)
And the expression, something like 2*y + 7
, and it is parsing ok, unfortunately as well as 2y + 7
. So how to change EXPRESS to raise an exception if 2
and y
come together?
Problem was in both of that parsers
VARNAME = Word(alphas, max=1)
NATNUM = Word(nums) # 1234567890
So 2y was the varname, but not Num and Var as i expect. Thank You.