Re: Getting a Counter
Hans Ã…berg <[email protected]> Fri, 1 Oct 2021 09:52:57 +0200
| Newsgroups | gmane.comp.parsers.bison.general |
|---|---|
| Message-ID | <[email protected]> |
> On 1 Oct 2021, at 07:16, Guenther Sohler <[email protected]> wrote: > > Hi, > In my parser i don't want flex to return comments in my code as flex tokens. > Instead i want to store them in a different list together with their > position. > > Lateron i am using their position to included them in the code again. > For that reason i am interested to know the Token position in the parsed > stream. > Ideally i need the number in the exact same scope as its displayed during > ambiguity resolution. > After some googling i could not find such a feature. > Does anybody know such a variable next to yylval or yytext ? As the lexer must buffer the input, it is necessary to compute the stream position in the lexer. As Akim pointed out, in Flex, this can be done with a user action (C++ here): std::istream::pos_type current_position = 0; %{ #define YY_USER_ACTION yylloc.columns(length_utf8(yytext)); current_position += yyleng; %} This example also shows how to compute the UTF-8 column number for the Bison parser, if one wants that. If files are opened recursively, one needs a stack for the current position as well.