I have written an expression compiler for a template system I am creating for PHP. The purpose of the compiler it to convert an expression written in the template into PHP code. I had the following aims with regard to creating the compiler:
  • The compiler must parse the expression and upon finding an error, flag its location in the original string and stop compiling the expression.
  • The expression compiler must not allow arbitrary text through to the output PHP code as this could be used to inject PHP code into the output expression.


The above is easier said than done and creating a parse tree was a real pain. The compiler is now almost complete and I would appreciate some testers. The below link takes you to a simple interface whereby an expression can be entered and compiled, showing the result. If you enter an invalid expression it should fail and report the location at which it failed.

Please let me know if you find any errors and what you entered to generate the error. Also any expressions that violate the above mentioned conditions.

http://php5.codedv.com/expression_driver.php

Below is a list of rules for the expression language:
  • Templates can contain variables, these are accessed using the variable name which must start with a letter and contain only letters numbers and underscores. e.g: varname1
  • An array type variable can have its indexed accessed as follows: array['stringIndex'] OR array[1] (integer index). The text between the square brackets may be an expression too: array[array2[1] - 4] arrays within arrays can be accessed too array[1][2].
  • An object type variable may access public properties using the following notation: object.property, object.arrayProperty['array_index']
  • Any strings are contained within single quotes with single quotes being escaped with a backslash: 'Adam\'s string'.
  • Expressions can contain any number of nested parentheses.
  • Logical operators supported: AND, OR, NOT
  • Bitwise operators supported: BAND (bitwise AND), BOR (bitwise OR), BXOR (bitwise XOR)
  • Comparison operators: = (equality), <=, <, >=, >
  • Arithmetic operators supported: +, -, / (division) , *, MOD (modulus), - (negetation)


Happy compiling.