-
I hope this isn't an inappropriate place to ask about this, but if it is I apologize. I'm implementing a compiler for the Ada programming language, not a custom one. The language is quite complex and contains a significant number of rules. I've done my best to rewrite them in the Pest grammar format. The syntax summary can be found here, and my rewrite of the grammar is available here. Pest identifies the ExplicitDereference, IndexedComponent, Slice, SelectedComponent, AttributeReference, FunctionCall, GeneralizedReference, GeneralizedIndexing, ImplicitDereference, Name, Prefix, ReductionAttributeReference, ReferenceObjectName, IndexableContainerObjectPrefix, FunctionName, and FunctionPrefix as left-recursive. However, I'm not sure how to rewrite these rules to eliminate that, given that I need to retain the syntax from the original syntax summary. In order, the rules that are left-recursive are below, though it might be helpful to read the full grammar for missing constructs:
I'm not asking for others to remove all the left-recursion for me (though if someone wants to, they can), but instead to provide suggestions (I've only written one other parser and that was in Uni). I've thought about just flattening some of the rules (and for all I know, I've mis-defined some of them because some of them aren't defined in BNF but in narrative descriptions, which is harder to translate), but I'm not sure if that would even work and I feel like I'd be majorly duplicating things all over the place, which would make the grammar harder to maintain. Does anyone have any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I guess there are three general approaches:
|
Beta Was this translation helpful? Give feedback.
-
Thanks for those! I've heard of left factoring (or at least I think that's what it's called in reference to your third method) but am unsure what the other two are called. Looking at all the rules that Pest claims have left recursion, the common prefixes are, well, the Since |
Beta Was this translation helpful? Give feedback.
-
Okay so I just resolved it, and it was a lot easier than I thought. I just translated:
Into:
I know that this changes the syntax of the grammar but if I did it right it shouldn't affect the language in any way. |
Beta Was this translation helpful? Give feedback.
Okay so I just resolved it, and it was a lot easier than I thought. I just translated:
Into: