How do c/c++ compilers know which line an error is on

c, c++, parsing

Solution

actually most of it is covered in the dragon book. Compilers do Lexing/Parsing i.e.: transforming the source code into a tree representation. When doing so each keyword variable etc. is associated with a line and column number.

However during parsing the exact origin of the failure might get lost and the information might be off.

Problem

There is probably a very obvious answer to this, but I was wondering how the compiler knows which line of code my error is on. In some cases it even knows the column. The only way I can think to do this is to tokenize the input string into a 2D array. This would store [lines][tokens]. C/C++ could be tokenized into 1 long 1D array which would probably be more efficient. I am wondering what the usual parsing method would be that would keep line information.

Original source