[antlr-interest] How to use lexer getCharPositionInLine() function in Cpp target

Gokulakannan Somasundaram gokul007 at gmail.com
Wed Oct 17 05:50:26 PDT 2012


Hmmm... This is a hack in C- grammar. So it should not be expected to be
supported in C++ grammar.
Either store the data what you want to extract out of token inside $text.

If you are worried about copying, then use
this->get_state()->set_tokenStartCharIndex( theStart );

Thanks.

On Wed, Oct 17, 2012 at 6:49 AM, Richard Gildea <rgildea at gmail.com> wrote:

> Another problem I have found with the Cpp target compared to the C target
> is when trying to use a construct such as $start=theStart (see e.g.
> http://antlr.markmail.org/message/izyhuzbooerfw4tu), which gives the
> following compiler error:
>
> error: expression is not assignable
>              this->get_state()->get_tokenStartCharIndex() = theStart;
> this->emit();
>              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
>
> Cheers,
>
> Richard
>
>
> On 16 October 2012 14:56, Richard Gildea <rgildea at gmail.com> wrote:
>
>> Hi Gokul,
>>
>> This does not work either, as I now get the following compiler error in
>> the generated lexer rule functions:
>>
>> error: use of undeclared identifier 'm_ctx'
>>
>> Cheers,
>>
>> Richard
>>
>> On 15 October 2012 17:56, Gokulakannan Somasundaram <gokul007 at gmail.com>wrote:
>>
>>> You don't need to write it inside the Cpp file. Just write it in the
>>> grammar.
>>>
>>> Gokul.
>>>
>>>
>>> On Tue, Oct 16, 2012 at 8:56 AM, Gokulakannan Somasundaram <
>>> gokul007 at gmail.com> wrote:
>>>
>>>> Yeah. That looks like a bug. I will need to change the stg file. For
>>>> now, you can directly write m_ctx->getCharPositionInLine() in place of getCharPositionInLine()
>>>> and let me know, whether it works.
>>>>
>>>> Thanks,
>>>> Gokul.
>>>>
>>>> On Tue, Oct 16, 2012 at 8:28 AM, Richard Gildea <rgildea at gmail.com>wrote:
>>>>
>>>>> Hi Gokul,
>>>>>
>>>>> The problem is that the predicate also appears in one of the generated
>>>>> TLexerCyclicDFA functions, leading to the following compilation error:
>>>>>
>>>>> error: no member named 'getCharPositionInLine' in 'cifLexerCyclicDFA33'
>>>>>                             if ( (( this->getCharPositionInLine() > 0
>>>>> )) )
>>>>>                                     ~~~~  ^
>>>>>
>>>>> Manually editing the generated code to read
>>>>> ctx->getCharPositionInLine() fixes the compilation.
>>>>>
>>>>> Cheers,
>>>>>
>>>>> Richard
>>>>>
>>>>>
>>>>> On 15 October 2012 17:13, Gokulakannan Somasundaram <
>>>>> gokul007 at gmail.com> wrote:
>>>>>
>>>>>> getCharPositionInLine() function is present inside Lexer. You can
>>>>>> just call it as similar to Java.
>>>>>> just replace the MACRO GETCHARPOSITIONINLINE() with
>>>>>> this->getCharPositionInLine() ?
>>>>>>
>>>>>> I can't understand second part of your question.
>>>>>>
>>>>>> Thanks,
>>>>>> Gokul.
>>>>>>
>>>>>>
>>>>>> On Tue, Oct 16, 2012 at 2:43 AM, Richard Gildea <rgildea at gmail.com>wrote:
>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> I am attempting to convert my parser to use the Cpp target after
>>>>>>> previously
>>>>>>> using the C target for some time, and have come across a problem
>>>>>>> trying to
>>>>>>> call the function TLexer::getCharPositionInLine() inside a gated
>>>>>>> semantic
>>>>>>> predicate in one of my lexer rules. With the C target I was using a
>>>>>>> construct such as
>>>>>>>
>>>>>>> ({ GETCHARPOSITIONINLINE() > 0 }?=> ';')
>>>>>>>
>>>>>>> to identify a semicolon character in the first column of a line. When
>>>>>>> trying to use a similar construct with the Cpp target, in the
>>>>>>> generated
>>>>>>> code this predicate is used both inside the lexer rule, where the
>>>>>>> correct
>>>>>>> usage would be this->getCharPositionInLine(), and inside a
>>>>>>> cyclicDFA, where
>>>>>>> the usage would be ctx->getCharPositionInLine(). Is there a way to
>>>>>>> use such
>>>>>>> a predicate with the Cpp target?
>>>>>>>
>>>>>>> Cheers,
>>>>>>>
>>>>>>> Richard
>>>>>>>
>>>>>>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>>>>>> Unsubscribe:
>>>>>>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>


More information about the antlr-interest mailing list