ANTLR v4 Header / Options Documentation

2,249 views
Skip to first unread message

Ryan Nideffer

unread,
Mar 3, 2014, 11:10:00 AM3/3/14
to antlr-di...@googlegroups.com
I'm unable to find documentation regarding the header elements (is this the right term?) for ANTLR 4 grammars. I'm referring to @header, @lexer::, @options, etc.. What are these sections used for, what are the valid options. I'm trying to learn ANTLR4 and translating these from ANTLR3 examples has been frustrating since I can't seem to find documentation, with all links that seem related ending up pointing me at buying the book.

Thanks,
Ryan

Terence Parr

unread,
Mar 3, 2014, 11:46:06 AM3/3/14
to antlr-di...@googlegroups.com
Hi. I think you need Section 15.4, Actions and Attributes in the book but can see them in action in doc:


is this page hard to find?

Ter
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ryan Nideffer

unread,
Mar 3, 2014, 12:08:04 PM3/3/14
to antlr-di...@googlegroups.com
Terence, thanks for the quick reply! That page was easy to find and I've read over it more than once. Perhaps I'm looking for detail that doesn't exist, but I was hoping to find a definitive reference along with explanations. For instance, the table at the top giving the general form of a grammar gives options, tokens, @actionName. Further down the Tokens Section shows usage of @lexer::members, the Actions section shows @header, @members and @after. With all these I'm left to guess at the exact usage given the short examples - plus I don't know if there are other attributes of @lexer::,  for example, that I'm not seeing on this page.

What led me down this path is my attempt to follow along with exercises in Martin Fowler's Domain Specific Languages using ANTLRv3, specifically the following that is reported as an error in v4:

grammar Hello;

@header {
    package com.ryan;
}

@lexer::header {
    package com.ryan;
}

antlr4 complains that I'm redefining 'header' with the @lexer::header block.

Ryan

Terence Parr

unread,
Mar 3, 2014, 12:45:47 PM3/3/14
to antlr-di...@googlegroups.com
On Mar 3, 2014, at 9:08 AM, Ryan Nideffer wrote:

Terence, thanks for the quick reply! That page was easy to find and I've read over it more than once. Perhaps I'm looking for detail that doesn't exist, but I was hoping to find a definitive reference along with explanations.

heheheh, then I guess the book is for you! ;)  Got $25?

For instance, the table at the top giving the general form of a grammar gives options, tokens, @actionName. Further down the Tokens Section shows usage of @lexer::members, the Actions section shows @header, @members and @after. With all these I'm left to guess at the exact usage given the short examples - plus I don't know if there are other attributes of @lexer::,  for example, that I'm not seeing on this page.

What led me down this path is my attempt to follow along with exercises in Martin Fowler's Domain Specific Languages using ANTLRv3, specifically the following that is reported as an error in v4:

grammar Hello;

@header {
    package com.ryan;
}

@lexer::header {
    package com.ryan;
}

antlr4 complains that I'm redefining 'header' with the @lexer::header block.

works for me:

/tmp $ a4 Hello.g4 
warning(125): Hello.g4:11:4: implicit definition of token 'A' in parser

Terence

Ryan

unread,
Mar 3, 2014, 1:15:54 PM3/3/14
to antlr-di...@googlegroups.com
Lol..ordered. Thanks.

--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/fpwchLlykko/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages