[antlr-interest] Need help with simple grammar
Johannes Luber
jaluber at gmx.de
Wed Apr 11 06:06:08 PDT 2007
Steve Karam wrote:
> I've never worked with any sort of parsing engine before; I think I am
> getting this, but still getting caught up with the huge amount of
> syntax. Could anyone help me with a basic grammar file that can handle
> these basic types of commands?
>
> get data from file
> put data in file
> change data in file
> get metadata about file
> change metadata about file
> get dependencies of file
> get dependents of file
> get statistics of file
>
> Basically it boils down to: command what preposition target. There
> will of course be more than this in the end, but I really need this
> basic understanding first before I can even think about what else I'll
> need to tackle.
>
> What would a grammar file for this look like? I'd prefer using C++, but
> I don't mind Java if that's all you know!
>
A basic ANTLR3 grammar without semantic checks (i.e. invalid sentences
still allowed) would be:
grammar CommandLanguage;
tokens {
GET='get';
PUT='put';
CHANGE='change';
DATA='data';
METADATA='metadata';
DEPENDENCIES='dependencies';
DEPENDENTS='dependents';
STATISTICS='statistics';
FROM='from';
IN='in';
ABOUT='about';
OF='of';
FILE='file';
}
command
: sentence*;
sentence
: verb object preposition target
;
verb
: GET
| PUT
| CHANGE
;
object
: DATA
| METADATA
| DEPENDENCIES
| DEPENDENTS
| STATISTICS
;
preposition
: FROM
| IN
| ABOUT
| OF
;
target
: FILE
;
You still have to change the target rule to allow arbitrary filenames
and include predicates which filter the invalid sentences out (supposing
all your examples were entirely inclusive). I suggest to buy the Beta
Book (http://www.pragmaticprogrammer.com/titles/tpantlr/index.html) for
more in-depth information.
Best regards,
Johannes Luber
More information about the antlr-interest
mailing list