[antlr-interest] How to get and use AST after parse error?

Graham Wideman gwlist at grahamwideman.com
Wed Jun 24 17:38:54 PDT 2009


At 6/24/2009 05:12 PM, you wrote:
>You will probably need to add exception {}
>
>To your rules but most common reason fir getting a single error node  
>is that you rules need reorganizing so they don't throw an exception  
>that nulls out the entire tree when an error is detected, just the  
>tree foe the piece in error.
>
>Jim

Thanks Jim for your observations which directed my attention to the right spot.

After more inspection and trial and error, I think I see what's going on, and it probably wasn't a fair situation under which to expect a sensible result.

My previous description of "failure" was incomplete.  In actuality, the parser proceeded to hit several (over 20) errors (grammar not smart enough to deal with actual input PHP text), and recover each time (or maybe just "recover" :-) ).

Then near the end of the input file, the parser was surprised to see a closing "}" when, due to previous recoveries, it thought there was no block open. This occured in the top-level rule prog(), thus invoking that rule's catch(RecognitionException), which causes the rule to return an errorNode.

(Comedy side note: as it happens, part of the disruption is due to test case happening to use the famous bug-a-boo: identifiers that overlap with PHP keywords... )

So, I guess this is as-designed, though disconcerting, and doesn't return everything that the Parser knows. So I still think there should be a way to get at the AST, even if it's incomplete.

Anyhow, this process has revealed numerous needed improvements to the grammar, and probably the issue I hit here will not be so important when the grammar is closer to adequate.




More information about the antlr-interest mailing list