r/Compilers • u/apoetixart • May 04 '26
EOF Token at which line?
Let's say my source code file is of 5 lines, should the lexer emit the EOF at line 5, or at line 6.
EOF(line = 5)
Or
EOF(line = 6)
"Depends on your preference" Suggest me what experienced people would do
22
Upvotes
2
u/Big-Rub9545 May 04 '26
I’m aware this doesn’t really answer the question, but I typically don’t associate any particular line, offset or column information with it.
It’s a unique enough token type that I handle it on its own.
E.g., instead of “error at (15:1)”, I might put “error at end”.
7
u/evincarofautumn May 04 '26
Basically, leave it where it ends up. In a file with n content lines, if the file ends in a newline, EOF will naturally be at the start of line n + 1. If the last content line has no final newline, EOF will naturally be after the last character in line n. Lines are numbered from 1 and columns are numbered from 0 because they refer to the coordinates of the bottom left of the character cell, that is, the height from the top margin of the file to the text baseline, and the width from the left margin to the start of the character.
You can also just consider it in terms of where you want an editor to jump to. If you use the conventional GNU error message format,
Line:is a whole line,Top-Bottom:is an inclusive range of whole lines,Line:Column:is a point in a line,Line:Left-Right:is an inclusive span (from before Left to after Right) within a single line, andTop.Left-Bottom.Right:is a span over multiple lines.5
u/un_virus_SDF May 04 '26
It depends wether there is a linefeed or not.
For exemple following strict ISO c, all line must be ended by a newline (what most editor do for you), and that's because eof is returned as a token when fgets fail (or any former equivalent).
So i'd say to treat eof as a token.
8
u/SmashStrider May 04 '26
Preferably at line = 6
4
u/j1sk1ss May 04 '26
Treat it as a regular token. I mean, if there was a new line symbol, set it to 6
4
u/TrgtBBB May 04 '26
I do it at line = 6 personally. Creates lot less confusion
0
u/[deleted] May 04 '26
[deleted] — view deleted comment
5
u/[deleted] May 04 '26
[removed] — view removed comment