Error report in multipass compilers

Kjell Post <>
Thu, 23 Mar 89 14:34:24 PST

          From comp.compilers

Related articles
Error report in multipass compilers (Kjell Post) (1989-03-23)
| List of all articles for this month |

Date: Thu, 23 Mar 89 14:34:24 PST
From: Kjell Post <>

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 (
[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]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.