Question

I need to transform text from following structure:

{ A1 A2 { B1 B2 { C1 C2 } } }

to the following developed/flattened structure:

{ A1 }
{ A2 B1 }
{ A2 B2 C1 }
{ A2 B2 C2 }

I use the following Antlr grammar to parse the file:

grammar tree;
node : '{' (STRING | node)* '}';
STRING : ('A' .. 'Z' | '0' .. '9')+;
WS : ( ' ' | '\t' | '\n' | '\r') -> channel(HIDDEN);

Is it possible to perform the Ast transformation by only using inline Antlr rewrite rules (using ->)?

Was it helpful?

Solution

By looking at channel(HIDDEN), it seems you're using ANTLR4, which does not have the rewrite functionality anymore: ANTLR 4 tree inject/rewrite operator. Besides, if you'd be using ANTLR3, I doubt you'd be able to rewrite the AST is such a way.

You should see this as two operations: 1) parse the input, and 2) walk/visit the parse tree and [by using plain programming] rewrite the parsed tree into something else. This last step is done by yourself in tree listener- or visitor of which ANTLR generates base classes for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top