Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

parsing

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.

like image 428
jmasterx Avatar asked Feb 04 '14 14:02

jmasterx


1 Answers

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.

like image 79
Alexander Oh Avatar answered Oct 03 '22 20:10

Alexander Oh