Skip to content

Commit 9a0000d

Browse files
authored
bpo-36256: Fix bug in parsermodule when parsing if statements (GH-12477)
bpo-36256: Fix bug in parsermodule when parsing if statements In the parser module, when validating nodes before starting the parsing with to create a ST in "parser_newstobject" there is a problem that appears when two arcs in the same DFA state has transitions with labels with the same type. For example, the DFA for if_stmt has a state with two labels with the same type: "elif" and "else" (type NAME). The algorithm tries one by one the arcs until the label that starts the arc transition has a label with the same type of the current child label we are trying to accept. In this case, the arc for "elif" comes before the arc for "else"and passes this test (because the current child label is "else" and has the same type as "elif"). This lead to expecting a namedexpr_test (305) instead of a colon (11). The solution is to compare also the string representation (in case there is one) of the labels to see if the transition that we have is the correct one.
1 parent aedc273 commit 9a0000d

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

Lib/test/test_parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,10 @@ def test_try_stmt(self):
319319
self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
320320
"finally: pass\n")
321321

322+
def test_if_stmt(self):
323+
self.check_suite("if True:\n pass\nelse:\n pass\n")
324+
self.check_suite("if True:\n pass\nelif True:\n pass\nelse:\n pass\n")
325+
322326
def test_position(self):
323327
# An absolutely minimal test of position information. Better
324328
# tests would be a big project.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bug in parsermodule when parsing a state in a DFA that has two or more
2+
arcs with labels of the same type. Patch by Pablo Galindo.

Modules/parsermodule.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,12 @@ validate_node(node *tree)
675675
for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
676676
short a_label = dfa_state->s_arc[arc].a_lbl;
677677
assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels);
678-
if (_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type) {
678+
679+
const char *label_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str;
680+
if ((_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type)
681+
&& ((ch->n_str == NULL) || (label_str == NULL)
682+
|| (strcmp(ch->n_str, label_str) == 0))
683+
) {
679684
/* The child is acceptable; if non-terminal, validate it recursively. */
680685
if (ISNONTERMINAL(ch_type) && !validate_node(ch))
681686
return 0;
@@ -688,17 +693,24 @@ validate_node(node *tree)
688693
/* What would this state have accepted? */
689694
{
690695
short a_label = dfa_state->s_arc->a_lbl;
691-
int next_type;
692696
if (!a_label) /* Wouldn't accept any more children */
693697
goto illegal_num_children;
694698

695-
next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
696-
if (ISNONTERMINAL(next_type))
699+
int next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
700+
const char *expected_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str;
701+
702+
if (ISNONTERMINAL(next_type)) {
697703
PyErr_Format(parser_error, "Expected node type %d, got %d.",
698704
next_type, ch_type);
699-
else
705+
}
706+
else if (expected_str != NULL) {
707+
PyErr_Format(parser_error, "Illegal terminal: expected '%s'.",
708+
expected_str);
709+
}
710+
else {
700711
PyErr_Format(parser_error, "Illegal terminal: expected %s.",
701712
_PyParser_TokenNames[next_type]);
713+
}
702714
return 0;
703715
}
704716

0 commit comments

Comments
 (0)