[antlr-interest] Multiple pass tree grammars and resetting the input stream (C# target)

Voelkel, Andy andy.voelkel at plantronics.com
Sat Nov 5 17:20:23 PDT 2011


Hi,

I've been trying to figure the best way to deal with a situation where I have to traverse a tree twice, emitting a different StringTemplate on each pass (and doing other housekeeping). After weighing the imperfect alternatives, I opted for a design where the tree grammar has conditionals in the rules which distinguish between pass 1 and pass 2.

I've got it working, but in a very messy way. The problem is how to reset/rewind the input stream. I cannot seem to reset/rewind the input token stream between passes in any other way than to reset the CommonTokenStream itself between passes 1 and 2, but from _outside_ of my parser. This is because, although, the generated code for the parser contains a constructor which take a CommonTokenStream as a argument, it does not store the CommonTokenStream locally, it just passes it to a base class. And once there, it is inaccessible. So I can't seem to find any way to reset the CommonTokenStream from within the TreeParser. The Reset method of TreeParser does not seem to reset the input CommonTokenStream.

This is all a bit of frustrating. Does anyone have any ideas of how to deal with this?

And is there a common way of dealing with situations where multiple passes are required? I sense that my solution is inelegant, but it seems redundant to write two different tree grammars which duplicate much of the same information. I'm beginning to wonder whether tree grammars are really the best way to walk ASTs.

- Andy




________________________________

CONFIDENTIALITY NOTICE: This e-mail transmission, and any documents, files or previous e-mail messages attached to it, may contain information that is confidential and/or legally privileged. If you are not the intended recipient, or a person responsible for delivering it to the intended recipient, please DO NOT disclose the contents to another person, store or copy the information in any medium, or use any of the information contained in or attached to this transmission for any purpose. If you have received this transmission in error, please immediately notify the sender by reply email or at privacy at plantronics.com, and destroy the original transmission and its attachments without reading or saving in any manner.

For further information about Plantronics - the Company, its products, brands, partners, please visit our website www.plantronics.com.


More information about the antlr-interest mailing list