[antlr-interest] Java In / Java Out: Going once ...

eric nelson wirecracker at gmail.com
Mon Nov 7 18:39:27 PST 2005


Sorry, but I still feel like someone must know how to arrange the java.g and
Java.stg code so that we can parse Java source input and produce Java source
output without Java Beans, etc. (although thanks for that suggestion :)
Mabye everyone has been too busy to take a look at this, but really it
should be simple for someone who understands ANTLR and StringTemplate
(unlike me ;) Sorry if I'm being too pushy about this question, but I think
its a good one. If I'm thinking about this totally wrong, at least just tell
me I'm crazy or something k? ;) ... original Q:
 I'm trying to modify the java.g grammar file so that I can use
StringTemplate for outputting the file. The input is a Java file. I would
like the output to be Java also. So, yes, basically no change in content ...
mabye some spacing will differ here or there. I am still learning to use
ANTLR and StringTemplate and I've written a Java.stg file based on what I
think I know so far. Exerpts from java.g and Java.stg appear below. One of
the questions I have is how to handle the "|" (or) symbol (i.e.
classDefinition | interfaceDefinition). The way I have it right now there
would be a list of "modifiers", a list of "classDefinitions", and a list of
interfaceDefinitions (then naturally I would have a question about the " |
s:SEMI!" ... but one question at a time ;) So, the problem in Java.stg is
how to match up the correct "modifiers" with the corresponding
"interfaceDefinition" or "classDefinition". Or what if there is a
classDefinition with no modifier, etc. I am quite convinced that my
Java.stgtemplate (typeDefinition in particular) and/or my
java.g code has erred somewhere. Any thoughts? This is HUGE since this will
help me understand how to approach the "field" rule in java.g .... if anyone
has some hints on a "field" template that would also be great!
 Thanks :) - e
 p.s. In case the above paragraph was too cryptic, what I want to do is
modify the java.g file and Java.stg files to process parsed Java input to
formatted Java output.
  java.g exerpt (java 1.3 grammar)

//////////////////////////////////////////////////////////////////////////////////////////////////
// A type definition in a file is either a class or interface definition.
typeDefinition returns [StringTemplate code=template("typeDefinition")]
options {defaultErrorHandler = true;}
{StringTemplate mod=null, cd=null, id=null, semi=null;}
: mod=m:modifiers! {code.setAttribute("modifiers", mod);}
( cd=classDefinition[#m] { code.setAttribute("classDefinitions", cd);}
| id=interfaceDefinition[#m] {code.setAttribute("interfaceDefinitions",
id);}
)
| s:SEMI! {semi=template("lexer_semi");semi.setAttribute("semi",
s.getText());code.setAttribute("semi",
semi);}
;

//////////////////////////////////////////////////////////////////////////////////////////////////
  Java.stg exerpt

//////////////////////////////////////////////////////////////////////////////////////////////////
typeDefinition(modifiers,classDefinitions,interfaceDefinitions,semi) ::= <<
<modifiers><classDefinitions><interfaceDefinitions><semi>
>>

modifiers(modifiers) ::= "<modifiers>"

modifier(modifier) ::= "<modifier>"

classDefinition(ident,superClassClause,implementsClause,classBlock) ::= <<
class <ident> <superClassClause> <implementsClause>
<classBlock>
>>

interfaceDefinition() ::= <<
not yet completed
>>

//////////////////////////////////////////////////////////////////////////////////////////////////
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20051107/09600b56/attachment.html


More information about the antlr-interest mailing list