Johannes Luber wrote:
Jim Idle schrieb:
  
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
    

I think you misunderstood me. Here is one rule in my grammar:

collection_initializer
    :   OPEN_BRACE element_initializer_list COMMA? CLOSE_BRACE
    -> ^(OPEN_BRACE element_initializer_list ^(OPTIONAL COMMA?) CLOSE_BRACE)
    ;

A normal parser would maybe need only:

collection_initializer
    :   OPEN_BRACE element_initializer_list COMMA? CLOSE_BRACE
    -> ^(element_initializer_list)
    ;

With a preprocessor one could combine them:

collection_initializer
    :   OPEN_BRACE element_initializer_list COMMA? CLOSE_BRACE
    -> ^(
	#ifdef ALL_TOKENS
	OPEN_BRACE
	#endif

	element_initializer_list

	#ifdef ALL_TOKENS
	^(OPTIONAL COMMA?) CLOSE_BRACE
	#endif
)
    ;

A bit ugly, but it gets the job done. Maybe you have another idea to
accomplish this goal?
  
Well, you should do this with runtime configuration (I show a parameter here but you should use some grammar global config class set externally):

collection_initializer[boolean allTokens]
    :   OPEN_BRACE element_initializer_list COMMA? CLOSE_BRACE
       -> {allTokens}? ^(OPEN_BRACE element_initializer_list ^(OPTIONAL COMMA?) CLOSE_BRACE)
       -> element_initializer_list
;

And you probably don't need that COMMA under a root node ;-)

But the general point is good.

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