Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for python syntax

Tags:

python

syntax

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?

like image 972
user1497830 Avatar asked Nov 30 '22 23:11

user1497830


2 Answers

Such a regular expression cannot exist, because regular expressions are, by definition, not powerful enough to recognize Turing complete languages (such as python).

like image 122
Matthew Adams Avatar answered Dec 03 '22 12:12

Matthew Adams


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.

like image 37
pepr Avatar answered Dec 03 '22 14:12

pepr