Nordlöw
Nordlöw

Reputation: 12138

Stack Overflow when Pyparsing Ada 2005 Scoped Identifiers using Reference Manual Grammar

I'm currently implementing an Ada 2005 parser using Pyparsing and the reference manual grammar rules. We need this in order to analyze and transform parts of our aging Ada-codebase to C/C++.

Most things work.

However, one little annoying problem remains:

The grammar rule name when parsing scoped identifiers (rule selected_component) such as the expression "Global_Types.Integer2" fails because it is part of a left-associative grammar rule cycle.

I believe this rule is incorrectly written: the sub-rule direct_name should be placed after the sub-rule direct_name. In fact it should be placed last in the list of alternatives. Otherwise direct_name and in turn name matches and "Global_Types" only and then expects the string to end after that. Not what I want.

Therefore I now move the rule direct_name to the end of name-alternatives...but then I instead get an Pyparsing infinite recursion and Python spits out maximum recursion depth exceeded.

I believe the problem is caused either by the fact that

Recursive expressions with pyparsing may be related to my problem.

The problem also seems closely related to Need help in parsing part of python grammar which unfortunately doesn't have answers yet.

Here's the Ada 2005 grammar rule cycle that causes infinite recursion:

Note that this problem is not an Ada-specific issue but is related to all grammars containing left-recursive rules.

Upvotes: 3

Views: 612

Answers (1)

trashgod
trashgod

Reputation: 205775

For reference, as noted in GNAT: The GNU Ada Compiler, §2.2 The Parser, "The Ada grammar given [in] the ARM is ambiguous, and a table-driven parser would be forced to modify the grammar to make it acceptable to LL (1) or LALR (1) techniques."

Upvotes: 1

Related Questions