Token Attribute ANTLR4 (C++ Target)

77 views
Skip to first unread message

Cleverson Ledur

unread,
Jul 25, 2016, 3:08:59 PM7/25/16
to antlr-discussion

Hello,

I am trying to access a token line number value through "line" attribute. I am using the ANTLR4 C++ target.

I tried the following combinations based on forum informations:

1)
idexpression
:
    unqualifiedid
{ std::cout << $unqualifiedid.ctx->getLine(); }
   
| qualifiedid
;
this returns:
 error: ‘class antlrcpptest::CPP14Parser::UnqualifiedidContext’ has no member named ‘getLine’  std::cout << std::dynamic_pointer_cast<IdexpressionContext>(_localctx)->unqualifiedidContext->getLine();


2)
idexpression
:
    unqualifiedid
{ std::cout << $unqualifiedid.ctx->line; }
   
| qualifiedid
;
this returns:
error: ‘class antlrcpptest::CPP14Parser::UnqualifiedidContext’ has no member named ‘line’ std::cout << std::dynamic_pointer_cast<IdexpressionContext>(_localctx)->unqualifiedidContext->line;


3)
idexpression
:
    unqualifiedid
{ std::cout << $unqualifiedid.line; }
   
| qualifiedid
;
this returns:
error(65): CPP14.g4:108:45: unknown attribute line for rule unqualifiedid in $unqualifiedid.line


4)
idexpression
:
    unqualifiedid
{ std::cout << $unqualifiedid.getLine(); }
   
| qualifiedid
;

this returns:
error(67): CPP14.g4:108:31: missing attribute access on rule reference unqualifiedid in $unqualifiedid



On the other hand, i am accessing the "text" attribute just by using "$unqualifiedid.text" (and this works).

Is this a bug in ANTLR4 C++ target? I am using the last JAR file uploaded on 20 July. (http://www.soft-gems.net/index.php/tools/49-the-antlr4-c-target-is-here)

How could I access line information?

Thank you in advance!

Mike Lischke

unread,
Jul 26, 2016, 11:22:28 AM7/26/16
to antlr-di...@googlegroups.com
Hi Cleverson,

I am trying to access a token line number value through "line" attribute. I am using the ANTLR4 C++ target.

I tried the following combinations based on forum informations:

1)
idexpression
:
    unqualifiedid
{ std::cout << $unqualifiedid.ctx->getLine(); }
   
| qualifiedid
;
this returns:
 error: ‘class antlrcpptest::CPP14Parser::UnqualifiedidContext’ has no member named ‘getLine’  std::cout << std::dynamic_pointer_cast<IdexpressionContext>(_localctx)->unqualifiedidContext->getLine();

Line number information is only available from the token source (while a new token is being produced) and in the tokens. That's true for both Java and C++ target.

On the other hand, i am accessing the "text" attribute just by using "$unqualifiedid.text" (and this works).

.text is a bit special as almost every class has a getText() member, including RuleContext.


Reply all
Reply to author
Forward
0 new messages