简体   繁体   中英

Regular expression to parse list of Python instructions between commas

I am writing a parser and, at some point, I am expecting a list of Python instructions and/or values written between commas. For example,

n >= 5, n < 20, m == len([1, 2, 3]), m + 1 == func(myObject.myMethod());

This is just an example, in practice I would like to be able to parse any valid Python expression.

My grammar has a rule of the form:

EXPRESSION ("," EXPRESSION)* ";"

where EXPRESSION is a regular expression that can accept any Python instruction. Of couse, since the comma could appear as part of my expression, I don't know how I could use it as a separator. Still, seems to me like there must be a regular expression, since any human can clearly parse the four expressions in the example above. Any ideas?

My current regex is EXPRESSION = [^,;]+ but this of course only works for very simple cases; the example above cannot be parsed with this.

This is not possible, as a comma in a Python instruction may be misinterpreted as a separator, but still yield valid Python instructions.

Here is an example:

a = 1, b = ((2, 3), 4), a, b = b, a, b = a

This can be interpreted as:

a = 1
b = ((2, 3), 4), a
b = b, a
b = a

Or as:

a = 1
b = ((2, 3), 4)
a, b = b
a, b = a

Or as:

a = 1
b = ((2, 3), 4)
a, b = b, a
b = a

All are valid pieces of code. There is no way you can know which is the intended one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM