[antlr-interest] Grammar for antlr 3b7/3b8

Ilia Kantor ilia at obnovlenie.ru
Fri Apr 27 14:50:27 PDT 2007


Hello, 

I'm making a simple grammar to process commands like
#r->a{1}{2}

It should work fine with both #r->a and #r->a{1} too, so syntax predicates are used.

Unfortunately after 2+ days of struggling I still can't make it work. 
Is that ANTLR problems or do I miss anything?

The input is:#r->a{1}{2}
The output should put {..} into separate subdirs, but it actually ignores it and gives them as plain text!

If you uncomment/comment last 2 strings, everything works just fine!
What's the matter eh ?

in the grammar,
rule rule_long_def_part matches #...  , that's just what is needed.
seems, the problem is digged somewhere in curly_block.



============ Grammar cleaned up ===================

grammar GetScopeValueClean;

options {
        output=AST;
}


tokens {
        TEXT;
        GET_PROPERTY_VALUE;
        GET_PROPERTY_ARGUMENTS;
}

LCURL   :       '{';
RCURL   :       '}';

GET_SCOPE_VALUE :       '#';

SEMI:   ';';

DOT     :       '.';

MINUS
        :       '-';

GT      :       '>';

SPECIAL_CHAR
        :       '`' | '!' | '@' | '$' | '%' | '^' | '&' | '*' | '(' | ')' |
        '+' | '=' | '[' | ']' | ':' | '\'' | '"' | '\\' | '|' | ',' | '<' |
         '/' | '?';

fragment WS_CHAR  :       (' '|'\r'|'\t'|'\u000C'|'\n' )    ;

WS      :       WS_CHAR+;

// not SPECIAL_CHAR not WS_CHAR
WORD:
        (~ ('`' | '!' | '@' | '$' | '%' | '^' | '&' | '*' | '(' | ')' |
        '+' | '=' | '[' | ']' | ':' | '\'' | '"' | '\\' | '|' | ',' | '<' | '/' | '?'
        | '{' | '~' | '}' | '#' | ';' | '.' | '-' | '>' | ' '|'\r'|'\t'|'\u000C'|'\n' ) )+;


document: exprs;

exprs: expr+;

expr: expr_simple | curly_block;

exprs_simple: expr_simple+;

expr_simple: rule_call | text;


rule_call: GET_SCOPE_VALUE WORD ((rule_long_def_part)=>rule_long_def_part)* -> ^(GET_SCOPE_VALUE WORD rule_long_def_part*) |
        GET_SCOPE_VALUE LCURL WORD rule_long_def_part* RCURL -> ^(GET_SCOPE_VALUE WORD rule_long_def_part*)
        ;


get_property_arguments: LCURL exprs? RCURL -> ^(GET_PROPERTY_ARGUMENTS exprs?);

rule_long_def_part
        :         MINUS GT WORD ((get_property_arguments) => get_property_arguments)* -> ^(GET_PROPERTY_VALUE WORD get_property_arguments*) |
                  DOT WORD  -> ^(GET_SCOPE_VALUE WORD)
        ;


text: DOT | MINUS | WORD | WS | GT | SPECIAL_CHAR;

// uncomment this and comment lower string to see all working fine
//curly_block: SEMI;
curly_block: LCURL exprs? RCURL -> TEXT[$LCURL] exprs? TEXT[$RCURL];


More information about the antlr-interest mailing list