[antlr-interest] patching a tree (recoverability)

Paul J. Lucas pauljlucas at mac.com
Fri Nov 19 15:00:59 PST 2004


	Suppose I want to parse and compile as much as possible.  An
	example is having:

		funcDecl
		    : DECLARE! FUNCTION^ IDENT '('! paramList ')'! funcBody ';'!
		    	{
			    ##.setType( FUNC_DECL );
			}
		    ;

	I want to recover if something doesn't parse in funcBody and
	produce a tree not having a body.  The reason is that the
	compiler (tree-walker) could compile the program from the AST as
	much as possible.  In this case, it could at least enter the
	function signature for the failed-to-compile function into the
	symbol table so that some later function can call the failed
	function and not generate a "function not declared" error.

	But how to communicate the "failed-ness" from the parser to the
	tree-parser?  Is there a standard-practice "ANTLR way" to do
	this?  If not, I've been thinking along the lines of
	introducing an "ERROR" token (yes, like yacc) and "patching" it
	into the tree.

		funcBody
		    : '{'! expr '}'!
		    ;
		    exception
		    catch [ TokenStreamRecognitionException e ] {
		    	## = #([ERROR,"ERROR"]);
		    }

	Then in the tree-parser I can do:

		functionDecl
		    : #( FUNC_DECL IDENT paramList
			( funcBody
			    {
			    	// The normal case
			    }
			| ERROR
			    {
			    	// At least enter the signature into the
				// symbol table.
			    }
			)
		       )
		    ;

	Comments?

	- Paul



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 





More information about the antlr-interest mailing list