4
I have to do a lexical parser in python using the PLY. Use REGEX to pick up tokens:
Example:
t_FLUTUANTE = r'flutuante'
The expression above gives a match with the word flutuante
and return me a token (how ply returns tokens per hour will not be necessary to explain how it works).
The Problem:
My difficulty is in getting the tokens of negative numbers, positive sum and subtraction.
My test entry is:
a := -1
y := +2
b := 2
c := 3+4
z := 20 + 42
funcao(-1)
funcao( -1)
funcao(a, -1)
If my regex is:
[+-]?\d+
The pouch evening:
But note that in the variable c
, got +4
, and that can’t happen because in reality it was supposed to be a sum.
Well if I modify a little I get a better result.
((\D)[+-]\d+)|\d+
Your exit is:
Actually a better result. But there are some spaces between the functions and the variable a
, including the regex took on one of the functions the match (-1
.
How To Get It Right?
I’m using the site https://www.regextester.com/ to test my expressions, I am disabling multiline (m
) on the website because in Ply I could not activate it.
It worked perfectly, thank you very much, I will study more this Lookbehind because I did not understand very well. But thank you.
– MICHEL GOMES DE SOUZA