How can I store the filename and line number in each AST node efficiently (esp for filename) in C++.

Ian Kaplan

There are probably a number of ways to do this. One way is to use a string table to store the strings. The AST node has a reference to a type like STRING which is an object that references (points to) strings in the string table. The string table stores each string uniquely.

When the scanner increments the line number used by the parser (for error messages, etc) it can also set the line number in the tree factory.

When a file is opened and given to the parser the file name can also be set in the tree factory. Now when an AST node is created it will be annotated with line number and file name information.