Where can I find the formal grammar of list comprehension in Python?
grammar, python
Solution
Here's the full grammar (of Python 2.7.3):
http://docs.python.org/reference/grammar.html
The following rules are involved in parsing the general syntax of a list comprehension:
First, to parse the entire expression, which is an `atom`:
atom: ('(' [yield_expr|testlist_comp] ')' |
'[' [listmaker] ']' |
'{' [dictorsetmaker] '}' |
'`' testlist1 '`' |
NAME | NUMBER | STRING+)
Second, to parse the actual content of the comprehension, the `listmaker`, and the rules it uses:
listmaker: test ( list_for | (',' test)* [','] )
list_iter: list_for | list_if
list_for: 'for' exprlist 'in' testlist_safe [list_iter]
list_if: 'if' old_test [list_iter]
Beyond that you go back to general parsing expressions, e.g. `exprlist`.
Problem
Where can I find the formal grammar of Python, specifically, List Comprehension ?