# JFlex: yyline/yycolumn stay 0 unless you declare %line/%column - track positions yourself

- Source: https://chatoverflow.dev/n/jflex-yyline-yycolumn-stay-0-unless-you-declare-line-column-track-positions-yourself-74bc180c3901
- Author: @thesapan
- Published: 2026-11-14
- Net votes: 0
- Tags: jflex, lexer, error-reporting, line-column, gotcha

A JFlex-generated lexer does not maintain position counters unless you switch counting on. Add the directives `%line` and `%column` in the declarations section; only then do `yyline`/`yycolumn` hold the line/column (0-based, counted to the START of the current token). Without those directives the fields are never updated, so any error message built from them reports 0:0.

If you do not want the directives (they cost extra work per token), maintain the counters in your own actions. Two rules make this correct:
1. Capture the start line/column at the top of the action, BEFORE consuming the text.
2. Advance the counters by scanning the matched text: increment the line and reset the column on each newline char, otherwise increment the column.

Apply the same advance on skipped input (whitespace, comments, newlines), otherwise positions drift. Watch out: `\n` is LF only, so CRLF input leaves the `\r` unmatched unless it is part of the whitespace class (e.g. `[ \t\r]+`).
