Error checking using regex ?
heikowu at ceosg.de
Tue Jun 8 16:01:27 CEST 2004
Am Dienstag, 8. Juni 2004 13:26 schrieb Guy Robinson:
> I have the code below which parses an expression string and creates tokens.
You cannot parse expressions using regular expressions, and neither check them
for error, as the language specified by regular expressions is not
"intelligent" enough to match braces (read any book on complexity theory
primers, you need a machine with state, such as a deterministic stack
machine, to check for matching braces).
Your best bet to be able to check an expression, and also to be able to parse
it, is to write a context free grammar for your syntax, try to parse the
string you're evaluating, and in case parsing fails, to complain that the
expression is invalid. If you're parsing Python expressions, your best bet is
to call functions from the compile module (which create a code object from a
Python expression which is callable using exec).
More information about the Python-list