I want to write a python code generator, and it would be helpful if I had the regular expression that describes all valid python programs.
Does such a regular expression exist? What is it?
Such a regular expression cannot exist, because regular expressions are, by definition, not powerful enough to recognize Turing complete languages (such as python).
If you generate a string with fragment of a Python code, and you want to check whether it is syntactically correct, try the built-in function compile
. It returns the resulting code object or it raises the SyntaxError
exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With