I have a parse tree for my compiler, and i was wondering what traversal of my parse tree will give me the same order in which my Source Code was tested for the generation,
I think it should be Pre-order, but i am told it is in-order , can someone tell me why.
Also, if i want to find out that in my parser, when was some declaration of identifiers was done, (say nonterminal declaration has the production for that) than what order of traversal shall i opt for. Pre-order?
You want to do an in-order traversal. If you do an in-order traversal on this AST, you’re visiting constructs in the order they’re written in the source.
Not quite sure what you mean here, but if you’re referring to finding the location (line, character, or column number) at which a symbol is declared, then that information should have been stored with the identifier AST node when it was inserted into the tree. Most parsers keep track of that information upon matching some token.
However, it would be more efficient to store the location information along with the symbol table entry, instead of traversing the tree whenever you need it.