[antlr-interest] Problem with generated code

Thomas Brandon tbrandonau at gmail.com
Wed Feb 13 11:37:57 PST 2008


On Feb 14, 2008 6:21 AM, Johannes Luber <jaluber at gmx.de> wrote:
> Jamie Penney schrieb:
> > Thanks for the replies everyone,
> >
> > What I ended up doing was changing this into a number of different
> > rules, rather than one rule for all keywords.
> > This created compilable code, but I don't know if it is correct. I kept
> > the old code, so should I go back and
> > change this to use scopes?
>
> Using scopes reduces the code duplication.
>
> Johannes
>
Not really, given the code the OP used. The methods are:
1) Keywords in lexer, ident_or_keyword parser rule
One lexer rule and alternative in parser rule per keyword.
2) Hardcoded parser rules checking IDENT token text
One parser rule per keyword.
3) OPs method
Alternative in contextual_keyword per keyword

1 requires changes in two places, 2 and 3 are pretty similar.
1 will probably be the fastest method as it won't require predicates.
3 will be slightly slower than 2 in at least some cases as it has to
run through the predicates to get into contextual_keyword and then
again in contextual_keyword. Though there's probably not much
practical difference between any of them.

If you want to go back to scopes you'd need to check the generated
code to ensure that scopes do solve the problem.

Tom.


More information about the antlr-interest mailing list