How can I build my raw expression to differentiate between a sum operator and a signed integer? I'm using PLY Python.
This,unfortunately, didn't work:
t_sum=r'\+'
def t_integer(token):
r'[-+]?\d+'
How can I build my raw expression to differentiate between a sum operator and a signed integer? I'm using PLY Python.
This,unfortunately, didn't work:
t_sum=r'\+'
def t_integer(token):
r'[-+]?\d+'
One way to think about this is, rather than trying to distinguish the purpose of +
on lexing, is to wait until parsing (where we determine meaning). So keep:
t_PLUS = r'\+'
t_MINUS = r'\-'
def t_INTEGER(token):
r'\d+'
return int(token)
And define a grammar:
number -> number PLUS number #sum, binary operator
number -> number MINUS number
number -> PLUS number #signed integer, unary operator
number -> MINUS number
number -> INTEGER
#By writing each as a parse functions like...
def p_number_ADD(p):
"number : number PLUS number"
p[0] = p[1]+p[2]
Note: this does allow the following behaviour +-+2 = +(-(+2)) = -2
(as seen in Python).
If you could detect unary operator by its surroundings (which is true only if your language is simple enough to list all the cases):
a ++ b // binary +, unary +
So we say that ++[a-z]
is unary, and +[a-z]
is binary (and we list -+
, *+
etc. for detecting unary as well).
Then another problem pops up: the surrundings will also attached to the token. In the example: it will be [a]
and [++b]
, instead of [a]
, [+]
and [+b]
.