[antlr-interest] Mulit-pass language processing.

Peter C. Chapin pcc482719 at gmail.com
Mon Sep 22 09:25:26 PDT 2008


I have an application where I need to process and rewrite a collection
of source files. I have come to the conclusion that I'll need to use two
passes over the source files. During the first pass I will parse the
files and gather certain bits of information about them. During the
second pass I will parse and rewrite these files taking into account the
information gathered during the first pass. I believe that, in general,
I need to look over all the files before I can reliably rewrite any of them.

I know how to set up ANTLR's rewriting mode. In fact I have a program
that parses my input files using an ANTLR grammar and rewrites them all
using an identity rewriting. Fine so far. Now I'm thinking about how to
gather the necessary preliminary information I need to do the rewriting
I actually want. The problem is that the grammar actions I need are
different in the two cases (gather information vs rewriting). So that
seems to suggest that I'll need two grammars with largely the same
rules, but different actions. I would instantiate parsers from the first
grammar to do the information gathering (one parser for each source
file), and parsers from the second grammar to do the rewriting.

Before embarking on this path, I wanted to ask if there is perhaps a
more "elegant" way.  It seems like my plan would introduce a maintenance
headache given that I'd have two different grammars that are supposed to
accept the same language.

Thanks!

Peter



More information about the antlr-interest mailing list