4 ms·
Compare the first image in the README to the corresponding section of the "original grammar": if_stmt: | 'if' named_expression ':' block elif_stmt
by jamessb 3y ago
Compare the first image in the README to the corresponding section of the "original grammar":
if_stmt:
| 'if' named_expression ':' block elif_stmt
| 'if' named_expression ':' block [else_block]
elif_stmt:
| 'elif' named_expression ':' block elif_stmt
| 'elif' named_expression ':' block [else_block]
else_block:
| 'else' ':' block
Whilst I can read this, I need to jump around to find the definitions of labels, and it requires mental effort to remember how each label was defined. The effort would be greater for a more complex example.
The diagram essentially avoids this by inlining the definitions of `elif_stmt` and `else_block`. This makes it much faster for me to read.
- lifthrasiir 3y agoThat only means that the railroad diagram is not a direct translation of the original grammar. Conversely, there are more than enough BNF variants that can do the equivalent inlining. I will personally denote your example as: if_stmt: 'if' named_expression ':' block ('elif' named_expression ':' block)* ('else' ':' block)? ...which is no more complex than the original diagram, and reads much easily at least for me. In fact, that "original grammar" does support the equivalent syntax because it's a direct input to Python's PEG code generator. I don't know why `if_stmt` was described in that way, but such duplication is often done for readability and hardly surprising.