[antlr-interest] Separation of specification and code
Ian Kaplan
iank at bearcave.com
Mon Mar 7 11:44:54 PST 2005
> What he'd like is to be able to keep the specification clean and the
> code separate. He mentioned Grammatica which uses the EBNF to
> produce a parser, but you implement the actions by extending the
> class for each leaf in the generated code.
>
> Have I made myself clear? Is this something that antlr supports?
> Pointers to where I can easily resolve this are as (if not more)
> welcome as direct answers.
I think that this is an interesting point. In a parser I recently
did for a query language I developed the grammar first and then
added the actions. The emacs grammar coloring helps, but the
actions and arguments pretty much obscure the grammar once they are
added. So I understand the desire to separate grammar and actions.
This said, I'm not sure how to do this in practice. For example,
in some cases you want to pass an argument "downward" in the parse.
For many parsers in all cases the grammar production returns a value
(the attribute grammar people sometimes call this a synthesized
attribute). Downward arguments become method arguments and returned
values, of course, become method results. Since these are part of
the core signature for the method that handles that grammar
production, it seems like at a minimum you have to include this
along with the grammar.
If all of the method signatures are correct you could in theory
subclass the ANTLR parser.
While the separation of grammar and actions sounds nice, it has
problems too. The advantage of having the actions there with the
grammar is that you can immediately see what is being done when the
production is processed. This may out weigh the problem of the
actions obscuring the grammar. Especially since someone wrote a
nice Emacs module that can be loaded to colar the grammar (if you're
not an Emacs user I can only say: there is one true editor and its
name is Emacs and Stallman is its prophet).
Ian
More information about the antlr-interest
mailing list