[antlr-interest] What's the right way to do this?

Voelkel, Andy andy.voelkel at plantronics.com
Thu Oct 27 15:26:08 PDT 2011


Hi,

I'm developing tools for generating various C source files from a simple DSL. I've got the lexing/parsing/tree-creation part of it working to my satisfaction, and am comfortable with my level of understanding (though I will continue to work my way all the way through Terence's book). But I'm confused about the best approach to generating one of the output files I need, which should look like this:

toplevel name = {
  { // begin group1
    { // begin block1
      item 1,
      item 2
    }, // end block1
    { // begin block2
      item 1,
      item 2
    } // end block2
  }, // end group1
  { // begin block3
    item 1,
    item 2,
    item 3
  }, // end block3
  { // begin group2
    { // begin block4
      item 1,
      item 2
    }, // end block4
    { // begin block5
      item 1,
      item 2
    } // end block5
  } // end group2
};

The problem is putting the "," characters on all elements in the nested structure except for the last one at that level, and I'm wondering what the best approach to the problem is. It seems like I could pass a "last one" parameter down to subrules when the item invoking the subrule is the last in the list of a rule, and use that as a way to avoid printing the ",". Alternatively, I get the feeling that this could be handled by the right StringTemplate magic, but I haven't found the right StringTemplate tutorials to get comfortable with this problem, which is obviously recursive, and where the items in a list are not all of the same type. The toplevel structure can contain groups or blocks, and groups can contain other groups:

group: 'group'^ ID '{'! (block | group)+ '}'! ;
block: 'block'^ ID '{'! (assign)+ '}'! ;

What would be the right way to get the output I'm looking for?

- 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