[antlr-interest] simple query language EBNF

Pieter Breed antlr.org at pb.co.za
Thu Dec 13 22:18:47 PST 2007


Hi,

I am trying to get a small special purpose query language working with
ANTLR, and I am having some trouble sorting out the right way to do some
things.

The basic domain problem is this:

you have some keywords: 'from', 'with', 'display', 'filter', 'set'
an example of a valid "query" is this:

from    LastMonth MultipliedBy 3
filter  WeekDays
filter  Not Holidays
set     EachDay 8-hours
with    Expectations

The idea is that ANTLR only takes care of the big structure of the query
(sorting out what string value goes with from, what string value goes with
filter etc) and then I will use these strings and do custom parsing on them.
(Using reflections. Ex, LastMonth is a method on a specific object, it has a
method Multipliedby which takes a parameter 3 and so on)

My ANTLR problem is that I want the raw text "LastMonth MultipliedBy 3" as
output from ANTLR, but I don't know how to specify that rule. I don't know
how say "everything but one of the commandwords". Below I tried to use
string quoting to delimit the text I am interested in, but that also doesn't
work.

This is what I have at the moment (I am troubleshooting at the moment, so I
put the comments in queryLine rule to help with this.):

grammar WorkLogQL;

tokens {
    FROM = 'from';
    WITH = 'with';
    FILTER = 'filter';
    SET = 'set';
    DISPLAY = 'display';
}

queryLine
    :    fromSpec
        //(WS filterSpec)*
        //WS actionSpec
        //WS withSpec
    ;

fromSpec returns [IDateRange result]
    : FROM WS SPECTEXT
        {
            result = ParseDateRangeSpecification($SPECTEXT.value);
        }
    ;

withSpec
    :    WITH WS SPECTEXT
    ;

actionSpec
    : DISPLAY
    |    SET WS SPECTEXT
    ;

filterSpec
    :    FILTER WS SPECTEXT
    ;

SPECTEXT
    :    '\'' .+ '\''
    ;

WS
    :    (' '|'\t'|'\r'? '\n')+ {$channel=HIDDEN;} ;

As is (ie, with the comments) and this input:
from 'Today'

The parser falls over in SPECTEXT. When I am running in ANTLRWorks, in the
Interpreter mode, I get a tree that looks something like this:
<grammar worklogql>
<queryLine>
<fromSpec>
<from> - <MismatchedTokenException>

How can I get this working? Any ideas?

Regards,
Pieter
-- 

Tempus est mensura motus rerum mobilium.
Time is the measure of movement.

   -- Auctoritates Aristotelis

+27 82 567 6207
http://pieterbreed.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071214/badf4503/attachment.html 


More information about the antlr-interest mailing list