[antlr-interest] Line and column numbers in rewrite rules
Andreas Stefik
stefika at gmail.com
Thu Jun 11 15:08:53 PDT 2009
Hello folks,
I'm working on an expression reader in one of my grammars and am working
with transporting line and column numbers from my grammar to a different
place in my compiler architecture. I apologize if this has been asked
before. I did a search, but did not immediately see an answer. A simplified
version of the grammar is below
expression returns[ExpressionValue eval]
:
| ^(PLUS left = expression right = expression)
{ }
| INT
{
int bl = $INT.getLine();
int bc = $INT.getCharPositionInLine();
int ec = bc + $INT.text.length();
//etc
}
;
Now, when the item I want to get line and column numbers from is a terminal,
things are easy, as I can just call getLine and getCharPositionInLine.
However, when I have rules, it's not immediately clear to me how I go about
getting them without doing some hacking behind the scenes. What I've been
doing is something like this:
CLASS ID?
{
currentClass.setLineBegin($CLASS.getLine());
currentClass.setColumnBegin($CLASS.getCharPositionInLine());
}
method_declaration*
END
{
currentClass.setLineEnd($END.getLine());
currentClass.setColumnEnd($END.getCharPositionInLine());
}
This works fine when I've got terminals at the beginning and end of a
particular rule, but does not work when I have rewrite rules in a grammar
(e.g., ^(PLUS left = expression right = expression) )
What am I missing here? Is there an easy way to get line and column number
information from each subrule in a rewrite?
Any ideas are appreciated,
Andy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090611/44f1e1d2/attachment.html
More information about the antlr-interest
mailing list