0

I want to use multiple regular expressions as follows (pseudo code):

[0-9]+|[0-9]+.[0-9]+ - number
+|-|*|/ - sign

[number][sign][number]=[number] - math expression

The closest thing i found was this, but the code is in JavaScript, while i want to use lex / flex for it.

Is it possible using the normal RegEx syntax?

Community
  • 1
  • 1
argamanza
  • 1,122
  • 3
  • 14
  • 36

1 Answers1

1

(F)lex provides the possibility to define what are, in effect, macros. The definitions go in the definitions section (before the first %%) and the simple syntax is described in the flex manual, with eχamples.

So you could write

number [0-9]+|[0-9]+.[0-9]+
sign [+*/-]
%%
{number}{sign}{number}={number}     { /* do something */ }

But that would rarely be a good idea, and it is certainly not the intended use of (f)lex. Normally, you would use flex to decompose the input into a sequence of five tokens: a number, an operator, another number, an =, and a final number. You would use a parser, which repeatedly calls the flex-generated scanner, to construct an object representing the equation (or to verify the equation, if that was the intent.)

If you use the regex proposed in your question, you will almosrt certainly end up rescanning the matched equation in order to extract its components; it is almst always better to avoid the rescan.

rici
  • 234,347
  • 28
  • 237
  • 341