0

I have to validate a syntax from math expressions like:

(ind(10)+15)-10
1000-(perg(25)*2)
25/var(1)
12*2-(58+1)/5

ind, perg and var are functions.

So, I'm trying this:

$var = '(\((var[\(])[0-9]+\)\)|(var[\(])[0-9]+\))';
$perg = '(\((perg[\(])[0-9]+\)\)|(perg[\(])[0-9]+\))';
$ind = '(\((ind[\(])[0-9]+\)\)|(ind[\(])[0-9]+\))';
$number = '[0-9]+(\.|,){0,1}[0-9]{0,2}';
$ope = '(\+|\-|\*|\/)';


$x = '('.$ind.'|'.$var.'|'.$perg.'|'.$number.')';
$a = $ope;
$s = '/^('.$x.$a.'(?R)|\('.$x.$a.'(?R)\)|(?R)'.$a.$x.'|\((?R)'.$a.$x.'\)|'.$x.')$/';

print_r(preg_match($s, '(perg(6)*perg(4)*)*1000000'));

And it's throwing this error:

Warning: preg_match(): Compilation failed: recursive call could loop indefinitely at offset 359

I gonna try to explain how I made this solution.

I though in this regular expression: 'X A S' or 'S A X'

X-> Values;    
A-> math operator;    
S-> a subexpression.    

A-> + or - or * or /;    
X-> ind(NUMBER) or perg(NUMBER) or ind(NUMBER) or NUMBER_FLOAT;    
S-> X A S or (X A S) or S A X or (S A X) or X    

0 Answers0