As for the verification, the following symbol tokens are valid:
operator: [
A simple check can then check if the input string matches any combination of these patterns. Since the funcs token funcs fairly accurate, and it does not collide with other tokens, this check should be fairly stable without the need to implement any syntax / grammar:
$tokens = array( 'operator' => '[/*+-]', 'funcs' => '(a\(|b\()', 'brackets' => '[()]', 'numbers' => '\d+(\.\d+)?', 'space' => '[ ]', ); $pattern = ''; foreach($tokens as $token) { $pattern .= sprintf('|(?:%s)', $token); } $pattern = sprintf('~^(%s)*$~', ltrim($pattern, '|')); echo $pattern;
Only if the entire input line matches the token-based template does it check. It can still be syntactically incorrect PHP, suppose you can make sure that it is built only on the specified tokens:
~^((?:[
If you build the template dynamically - as in the example, you can more easily change the language tokens.
In addition, this may be the first step to your own tokenizer / lexer. Then, the token stream can be passed to the parser, which can parse and interpret it. This piece of user187291 wrote about .
As an alternative to writing the full lexer + syntax, and you need to check the syntax, you can also formulate your grammar based on tokens, and then perform marker grammar based on expressions on the marker representation of the input.
Signs are words that you use in your grammar. You will need to more accurately describe the brackets and the definition of the function in tokens, and the tokenizer must follow the clearer rules that the token replaces another token. The concept is set out in another question of mine . It also uses regex to formulate grammar and check syntax, but it still does not parse. In your case, eval will be the parser that you are using.