[antlr-interest] ast validation question
Sohail Somani
sohail at taggedtype.net
Wed Feb 22 19:50:58 PST 2006
On Wed, 2006-02-22 at 22:11 -0500, Scott Amort wrote:
> Hello All,
>
> I am writing a parser library for a relatively simple textual
> description language, and have progressed fairly well given my relative
> lack of familiarity with compiler tools like ANTLR. This list and the
> sample code and grammars provided have been very helpful.
>
> At this point, I have a functioning lexer and parser, and am now working
> on a tree walker for validation purposes. I am hoping to get some
> opinions on how best to approach this - currently, I envision using the
> tree walking phase to first validate type, then data. The language
> allows tags ( i.e. a '\' followed by text, e.g. \start or \stop).
> Currently the lexer identifies the alphanumeric portion after the
> backlash as a token and sends it on to the parser, which makes sure it
> is found in the correct spots. Now, in the tree walker, I would like to
> make sure that this tag is valid. Is there an ANTLR-specific way to
> accomplish this? Or perhaps I should develop my own code to deal with
> this? If so, what data structure do you think is best to store a fixed
> number of strings, and allow efficient matching or finding?
Couldn't you make these keywords and have them matched as in the parser?
Something like (probably doesnt compile):
class L extends Lexer;
KEYWORDA:"FOO";
KEYWORDB:"BAR";
KEYWORDC:"NO";
SLASH:'\\';
class P extends Parser;
options
{
importVocab=L;
}
{
public static void main(String args[])
{
L l = new L(System.in);
P p = new P(l);
p.tagRule();
}
}
tagRule:SLASH (KEYWORDA | KEYWORDB | KEYWORDC);
The key of course is fixed number of strings.
More information about the antlr-interest
mailing list