Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character position in scanner using Lex/Flex

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!

like image 860
ChrisDiRulli Avatar asked Feb 14 '10 04:02

ChrisDiRulli


1 Answers

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.

like image 127
ergosys Avatar answered Oct 17 '22 02:10

ergosys