[antlr-interest] A Simple Question on Channels

Andreas Stefik stefika at gmail.com
Thu Apr 2 19:31:46 PDT 2009


Hello list,

I am an academic working on a new compiler architecture. I've written a
number of compilers in the past in other languages, including JavaCC and
Lex/Yacc, and am pretty well versed in compiler theory, and thought that it
might be fun and interesting to try out ANTLR. As such, I've written some
practice grammars to get my feet wet, am nearly finished reading Terence's
fantastic book, and have started on a first draft of a grammar for my little
language in ANTLR. So far, I've run into very few problems, and am
positively in love with the visualization system built into ANTLRWorks, but
if the list will indulge a new user, I do have one question, related to how
"channels" are used, and how I can manipulate them.

Here's the basic problem I'm trying to figure out:

In my programming language, I would like to use newlines as a delimiter in
some cases, but also to have it thrown away in "most" cases. My initial
guess as to how to do this would be something like this:

NEWLINE    :     '\r'?'\n' {$channel = HIDDEN;};

Where a newline is tracked, and not thrown away, but put away for usage if I
decide I need it. Now, ideally, what I would LIKE to do, is something like
the following:

expression_statement
    :    expression NEWLINE
    ;

Now, I know I can't do this, as the parser reads only from the default
channel, not the hidden channel, but even if it did read from the hidden
channel, there would be all sorts of garbage in the way anyway (other,
unrelated, white space). But, as the above statement implies, I would like
to determine if there is a newline token on the hidden channel that is
between whatever tokens end "expression" and begin whatever rule is after my
newline.

After looking through the documentation, my best "guess" is that I need to
write an action, that will get the actual token stream from the hidden
channel (which I'm not completely sure how to do), then check all the hidden
tokens between that and the beginning of the next rule (Which I'm also not
completely sure how to do). If a NEWLINE is found, I would issue an ACCEPT;
as that's the end of the rule, otherwise it isn't. However, all else being
equal, I would really prefer if there was a way to temporarily tell my
grammar to search for _only_ newlines in the hidden channel, but otherwise
to process normally.

If anyone has any thoughts or suggestions on the right way to go about this,
I would appreciate it.

Sincerely,

Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090402/86fc5141/attachment.html 


More information about the antlr-interest mailing list