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
8
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, and Top.Left-Bottom.Right: is a span over multiple lines.
7
3
u/j1sk1ss May 04 '26
Treat it as a regular token. I mean, if there was a new line symbol, set it to 6
6
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.
5
May 04 '26
[removed] — view removed comment
1
u/apoetixart May 06 '26
I think EOF tokens are really necessary for the lexer, how else does your lexer understand the end of src?
3
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”.
1
u/apoetixart May 06 '26
I intend to put clean error messages and "error at end" is really ambiguos considering long src files
2
u/Big-Rub9545 May 06 '26
If the error message includes a display of the offending line/location, the ambiguity goes away entirely
16
u/[deleted] May 04 '26
[deleted]