[antlr-interest] yet another first antlr grammar

Johnicholas Hines johnicholas.hines at gmail.com
Wed Jun 14 07:37:31 PDT 2006


Hi, this is my first time using antlr. I've used flex and bison before
(in a compilers class when I was in college).

I'm trying to write a little language that is basically "starless
regular expressions with a range operator". The language is input for
a driver program that will call the same scripts over and over with
different parameters, telling the driver program what scripts to call
and with what parameters.

My question is: Do you think I can implement the range operator with a
tree parser?

Also, if anyone is interested, could you briefly look at my grammar
and give me any suggestions that come to mind? (Am I doing things in a
nonidiomatic way? a stupid way?)

The intended semantics of this language is:
AND(x, y, z) means "concatenations of words from the languages x, y and z"
OR(x, y, z) means "the union of the languages x, y and z"
"ABC" means "the language containing only the word 'ABC'"
1 means "the language containing only the word '1' "

1, ..., 5 is an abbreviation for "1, 2, 3, 4, 5"
10, 20, ..., 50 is an abbreviation for "10, 20, 30, 40, 50"

So:
AND("test ", "-x", OR(1, ..., 10), " -y", OR(10, 20, ..., 1000))
would mean:
test -x1 -y10
test -x1 -y20
...
test -x10 -y1000

Here is my grammar file:
---
{
    language = "Cpp";
}
class DriverLexer extends Lexer;
options
{
    charVocabulary = '\3'..'\377';
}
ELLIPSIS: "...";
INTEGER: ( '0'..'9' )+;
STRING: '"'! ( ESCAPE | ~'"' )* '"'! ;
LPAREN: '(';
RPAREN: ')';
AND: "AND";
OR: "OR";
COMMA: ',';
WS:
        ( ' '
        | '\t'
        | '\n' { newline(); }
        | '\r'
        )+
        { $setType(antlr::Token::SKIP); }
    ;
protected
ESCAPE:
        '\\'
         ( 'n' { $setText("\n"); }
         | 'r' { $setText("\r"); }
         | 't' { $setText("\t"); }
         | '"' { $setText("\""); }
         )
    ;
class DriverParser extends Parser;
options
{
    buildAST = true;
}
expr: INTEGER
    | STRING
    | ELLIPSIS
    | AND^ LPAREN! expr (COMMA! expr)* RPAREN!
    | OR^ LPAREN! expr (COMMA! expr)* RPAREN!
    ;


More information about the antlr-interest mailing list