How can I store the filename and line number in each AST node efficiently (esp for filename) in C++.
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.