[antlr-interest] Grammar dependencies (was Re: Expected token apparently not expected?!)

Ric Klaren klaren at cs.utwente.nl
Fri Sep 10 03:26:15 PDT 2004


On Fri, Sep 10, 2004 at 06:12:27AM -0000, David Bullock wrote:
> I guess the problem is that if you use literals in the parser, then 
> the lexer needs to get these somehow.  This means that the grammar 
> needs to be 'pushed back' to the lexer.
> 
> > In l.g do exportVocab = L;
> > In p.g do importVocab = L; and exportVocab = P;
> 
> Instead I did:
>  
>   p.g   exportVocab = L;
>   l.g   importVocab = P;
> 
> and compiled p.g first.

Oh yeah that's a good point. I usually define all tokens in the lexer using
the tokens section. So the parser can be a clean 'consumer' of the vocab of
the lexer.

e.g. in the lexer:

class L extends Lexer;
options { .. }
tokens {
  IF="if";
  ELSE="else";
  ..
  LBRACE="{";
  RBRACE="}";  
  ..
  PLUS="+";
  MINUS="-";
  .. etc. ..
} 

If you do it like this you can use the texts as well as the token in the
parser:

if_else: IF expr LBRACE expr RBRACE ELSE LBRACE expr RBRACE;
or:
if_else: "if" expr "{" cmds "}" "else" "{" cmds "}";

I personally like the last way of writing things down, but that's a matter
of taste ;)

> IMHO it would be better to dispense with importing and exporting 
> vocabularies, and have antlr.Tool accept multiple grammar files on 
> the command-line, to achieve similar results as the 'all classes 
> defined in one grammar file' semantics.

I agree with you that the current import/exportVocab stuff is not exactly
pretty, we should take care to do better in ANTLR 3 (I think it was already
on the hate list). Although it offers some flexibility too. E.g. you can
make the tokenTypes.txt files yourself and only use imports. Currently it's
also easy to make mistakes due to typos in tokens (they get silently
added).

Cheers,

Ric
-- 
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893755  ----
-----+++++*****************************************************+++++++++-------
  "You can't expect to wield supreme executive power just because some
   watery tot throws a sword at you!"
  --- Monty Python and the Holy Grail



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 





More information about the antlr-interest mailing list