kjell@saturn.ucsc.edu (Kjell Post) (03/24/89)
I am currently writing a multipass compiler where the parser builds an Abstract Syntax Tree which is later traversed in several passes. I would like to keep track of the linenumbers so I can refer to a specific line when an error is detected during the tree traversal. There are two approaches I can think of: 1. Store the linenumbers in the leaf nodes only. When an error occur, go first right then all the way down the left spine to find a leaf. This doesn't work in all cases; for instance stmt ::= RETURN ';' { ... } doesn't have a leaf node and no line number can be associated with that construct. 2. Store the linenumbers in all nodes. The problem here is how to synthesize a linenumber from two subtrees in an non-ad-hoc way. Example expr ::= expr '+' expr { ... } The linenumber stored in $$ should probably be the linenumber where the '+' appeared. But some productions have right-hand sides without any terminals and so $$'s linenumber must be synthesized from one of the nonterminals. Empty subtrees must of course be avoided so this method requires careful thinking. Is there some clean, obvious way that I have missed? --Kjell (kjell@saturn.ucsc.edu) [At parse time, I'd set the line number in a node from the current line number kept by the lexer. Crude, but as likely as anything else to be right. -John] -- Send compilers articles to ima!compilers or, in a pinch, to Levine@YALE.EDU Plausible paths are { decvax | harvard | yale | bbn}!ima Please send responses to the originator of the message -- I cannot forward mail accidentally sent back to compilers. Meta-mail to ima!compilers-request