Character position in scanner using Lex/Flex
In Lex/Flex is there a way to get the position i开发者_开发技巧n 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!
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.
精彩评论