diff --git a/parso/pgen2/generator.py b/parso/pgen2/generator.py index e64577e..d5355d3 100644 --- a/parso/pgen2/generator.py +++ b/parso/pgen2/generator.py @@ -282,6 +282,10 @@ def _calculate_first_plans(nonterminal_to_dfas, first_plans, nonterminal): # We only need to check the first dfa. All the following ones are not # interesting to find first terminals. state = dfas[0] + for transition, next_ in state.ilabel_to_plan.items(): + # It's a string. We have finally found a possible first token. + new_first_plans[transition] = [next_.next_dfa] + for nonterminal2, next_ in state.nonterminal_arcs.items(): # It's a nonterminal and we have either a left recursion issue # in the grammar or we have to recurse. @@ -303,9 +307,5 @@ def _calculate_first_plans(nonterminal_to_dfas, first_plans, nonterminal): ) new_first_plans[t] = [next_] + pushes - for transition, next_ in state.ilabel_to_plan.items(): - # It's a string. We have finally found a possible first token. - new_first_plans[transition] = [next_.next_dfa] - first_plans[nonterminal] = new_first_plans return new_first_plans diff --git a/test/test_pgen2.py b/test/test_pgen2.py index 6ea96a4..fe22d5b 100644 --- a/test/test_pgen2.py +++ b/test/test_pgen2.py @@ -285,3 +285,6 @@ def test_ambiguities(): with pytest.raises(ValueError, match='ambiguous'): generate_grammar('''foo: bar | baz\nbar: 'x'\nbaz: "x"\n''', tokenize.PythonTokenTypes) + + with pytest.raises(ValueError, match='ambiguous'): + generate_grammar('''foo: bar | 'x'\nbar: 'x'\n''', tokenize.PythonTokenTypes)