Adding AST from another Parser as a child to the current one.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   James_Cerra
Posted On:   Tuesday, March 15, 2005 06:01 PM

I have a grammar that I broke up into two parts so it is easier to write. I wrote a lexer ("PatternLexer") and a parser ("PatternParser") for the first one and a lexer ("URILexer") and a parser ("URIParser") for the container grammar. I wanted to put the AST from the `Pattern` grammar as a child in the `URI` grammar. Eventually I came up with the following: rootExpr : (URIEXPR | patternExpr)* ; patternExpr : USEP! { PatternParser patternParser = new PatternParser(getInputState()); patternParser.rootExpr(); currentAST.root = patternParser.getAST(); } ; That seems to work; however, I am concerned about how much of a hack it seems to be. The v   More>>

I have a grammar that I broke up into two parts so it is easier to write. I wrote a lexer ("PatternLexer") and a parser ("PatternParser") for the first one and a lexer ("URILexer") and a parser ("URIParser") for the container grammar. I wanted to put the AST from the `Pattern` grammar as a child in the `URI` grammar. Eventually I came up with the following:


			
				rootExpr    : (URIEXPR | patternExpr)* ;
				
patternExpr : USEP! {
PatternParser patternParser = new PatternParser(getInputState());
patternParser.rootExpr();
currentAST.root = patternParser.getAST();
} ;

That seems to work; however, I am concerned about how much of a hack it seems to be. The variable `currentAST` is generated by Antlr and doesn't seem to be well documented. Also I don't like modifying automatically generated variables like this in case the next implementation of Antlr changes variables. (I'd rather use a documented method of Antlr's API.)


So, are my concerns unfounded? (I hope so.) Is there another way of making the AST generated from `PatternParser` a child of `patternExpr`?


P.S: I'm very new to Antlr - this is my first grammar and I've used Antlr for less than a week - so please excuse any faux pas by me (and explain it too).

   <<Less

Re[2]: Adding AST from another Parser as a child to the current one.

Posted By:   James_Cerra  
Posted On:   Sunday, March 20, 2005 05:11 PM

Thanks. I haven't tested that because I retooled my design to work around the problem (and ended up halving the size and complexity of my code). The solution is still good to know. I gather that the ANTLR variable `##` means ""current AST""? Is there a list of all special ANTLR methods/macros (the docs seem incomplete)?

Re: Adding AST from another Parser as a child to the current one.

Posted By:   Anonymous  
Posted On:   Friday, March 18, 2005 08:05 AM

Hi,
I had a similar problem. The solution apparently is this:

patternExpr : USEP! {
PatternParser patternParser = new PatternParser(getInputState());
patternParser.rootExpr();
## = patternParser.getAST(); /* Replace the current AST with the other parser AST */
} ;
About | Sitemap | Contact