[antlr-interest] [C Target]: comment processing

Jim Idle jimi at temporal-wave.com
Thu Jan 24 07:07:23 PST 2008


The C target does support the same way of retrieving off-channel tokens
that the java target does, but the semantics are C of course J See the C
target examples in the downloadable examples pack on the download page.
This has Visual Studio 2005 .sln files, and if you are a UNIX man, then
just convert all the grammar files and compile all the .c files into one
binary. This will give you some of the API “look and feel”.

 

To see how to achieve the getTokens functionality, look at the MACRO
definitions in the generated code. You will see that you have a MACRO
INPUT, which is the the input stream (whether lexer, parser or tree
parser).

 

So, you can do this:

 

… $tok=rule ….

 

pANTLR3_LIST commentTokens;

pANTLR3_VECTOR allTokens;

 

 

allTokens                 = INPUT->getTokens             (INPUT);  //
Returns pointer to the list of all tokens

commentTokens  = INPUT->getTokenRange  (INPUT, $tok->start, $tok->stop);
// Other semantics for $xxx apply of course

 

 

The API for the LIST and VECTOR are all documented.

 

You can pick out the comments even from the tree parser, so you should
not need together the comment anywhere, just scan back through the
tokens from the token you wish to associate comments with, looking for
COMMENT types. Of course, the difficulty of associating the comments
with the correct piece of code is a different problem.

 

However, you probably need scopes if you want to actually collect things
(which I don’t think you need to do).

 

Jim

 

 

 

From: Hong-Thai NGUYEN [mailto:Hong-Thai.Nguyen at imag.fr] 
Sent: Thursday, January 24, 2008 2:41 AM
To: ANTLR maillist
Subject: [antlr-interest] [C Target]: comment processing

 

Hi,

I'm using ANTLR for creating a compiler in linguistic processing
(système-Q). The target language is ANSI C.

We'd keep all comments in code source and attach them to related items
(like JavaDoc, i got example of sub-parser to process JavaDoc type
comment from Ter, thanks).

But, we'd try to keep ANTLR grammar as clear as possible, we wouldn't
pollute with "comment"  rule every where. So, we're using Channel to
identify comment-liked Tokens, then calculate which items are related to
which comments by line number. It's a good solution ? Even that, antlr3c
don't give API (ex. getTokens(), ...) like in Java Target.

 

I've an other issue, if ANTLR support some kind of "global" rule like:

global_rule:

item comment;

where item = ALL matching rule, so that i can grant directly comment to
every propriety items. How could we implement that in ANTLR ?

 

Thanks,

 

============================

Hong-Thai NGUYEN

PhD Student

Email: Hong-Thai.Nguyen at imag.fr

GETALP-LIG

385 rue de la Bibliothe`que

38041 Grenoble cedex

France

Tel.:   + 33 4 7651 4817

Fax.:  + 33 4 7644 6675

GSM.: + 33 6 27 04 86 22

IM (Yahoo): thaichat04, (Sky): thaichat04

Page: http://www-clips.imag.fr/geta/User/hong-thai.nguyen/Welcome.html


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080124/31cf1202/attachment-0001.html 


More information about the antlr-interest mailing list