Johannes Luber wrote:
Jim Idle schrieb:
  
Andy Grove wrote:
    
Hi,

I need to generate C and Java from an ANTLR grammar containing  
actions. Is there a preprocessor approach I can use rather than  
maintaining two versions of the grammar?
  
      
I use perforce and maintain a base grammar that has no actions, then 
change only the grammar base. When ready to test I integrate the changes 
via a prestored branch spec. Other SCCS can do the same sort of thing, 
though perforce is streets ahead of anything else at the merge process.

However, occasionally it is a pain to debug remotely when I want to just 
use the ANTLR works debugger before integrating a change, so I have 
written a pre-processor as an experiment (it is in ANTLR3 of course), 
and am trying to decide between the C# lexer base approach and the VB/C 
approach (albeit not having the stupidity of the VB pre-processor.)
    

Having a preprocessor would allow me to have a single grammar for C#,
while allowing to serve it my own needs and the general ones at once.
I've thought about using C#'s preprocessor, but that would at best allow
to circumvent the assignments, but not any initializations.
  
I was referring to the design pattern rather than the implementation. C# has a minimalist approach to what you can do with the pre-processor, VB has some weird and wonderful stuff, and the C pre-processor lets you hang yourself. So, I personally would not want a pre-processor that you can program as we are writing grammars, not pre-processor macros ;-0

#ifdef ANTLR_VER_MAJOR > 3 || ANTLR_VER_MINOR < 2
#abort "This grammar requires ANTLR 3.1.2 and above"
#endif

If I am willing to do the C macro thing. In general though, if you add 
macros, people start embedding code in them and the whole thing is 
macros, so I prefer the minimalist approach of C#.
    

Instead doing it with macros,how about using:

#ifdef ANTLR_3_1_2
  
Same thing. The tool can pre-define anything it likes of course. You need the MAJ and MIN etc as well because you sometimes need to say "This version or above"
language = template;

r1 : %r1predicate(x)%?=>   a=INT bINT c=INT 
    

Can you explain this in more detail? I'm not sure how you arrive at
those and what the purpose actually is.
  
Actually you would need:

language=C;
actions=template-name;

But the idea is just like the runtime use of templates but happens at code generation time. The code generation for the target is exactly the same, but the actions and code related things such as semantic predicates are generated by string template supplied by the grammar author. Then you can have one grammar and different action templates that just return code that is passed on to the target language template.
For now though, m4 is your best bet (it is in the Java compiler even), 
or perhaps something simple with gawk.
    

What is m4? I find only weapon references.
  
Type:

man m4

;-) It is the pre-processor that was used by C compilers of long past.

http://www.gnu.org/software/m4/

Jim


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to