Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In lex, how do I differentiate between '-' (subtraction) operator and an integer '-3'?

I am writing lex for a specific language where operations are carried out in prefix notation :

(+ a b) --> (a + b)

An integer is defined as follows : An integer can have a negative sign (–) but no positive sign. It can be with or without space(s) between the sign and the digits. If the value is 0, only a single digit 0 is allowed. Otherwise, it is the same as common integer definitions (no leading 0’s).

Order of expressions in the lex is as follows ( first match rule) :

  • Regex for integer :[\-]?[ ]*((0)|([1-9][0-9]*))
  • Regex for subtraction operator : "-"

With these definitions, I would like to parse the string - 5 3 ie. (5-3)

Current output

Integer : - 5, 

Integer : 3

Desired output:

Operator : '-'

Integer : 5

Integer : 3
like image 287
ronakshah725 Avatar asked Feb 13 '16 23:02

ronakshah725


1 Answers

You don't. You return - and INTEGER separately to the parser, and let the parser handle unary minus.

like image 66
user207421 Avatar answered Dec 09 '22 05:12

user207421