views:

128

answers:

1

Hi there,

In Lex/Flex is there a way to get the position in the character stream (from the start of the file) that a token appears at? Kind of like yylineno except that it returns the character position as an integer?

If not, what's the best way to get at this? Do I need to keep my own counter?

Thanks!

+1  A: 

You can track the character position using yyleng and YY_USER_ACTION. yyleng has the length of the current token, YY_USER_ACTION is invoked before matching a token. In YY_USER_ACTION, add yyleng to a position variable. You'll need to reset the variable at each end-of-line token unless you want the character position from the start of the input stream. In your rule action, the variable - yyleng is the starting position of the token.

ergosys
That's great! Thanks!
ChrisDiRulli