Bill Steer wrote:
I know just enough about ANTLR to be dangerous.  Like another recent poster, I've read the book and am trying to get the hang of it, but think I have this simple grammar messed up.

One of the kinds of statements my grammar, attached, tries to handle is:

REGISTER = avg(REGISTER, REGISTER, REGISTER);

where REGISTER is just a multi-part identifier, with the parts separated by an '!'.  The lexer seems to work ok, judging by the Tokens it returns in another part of my code.  But, at runtime, the parser gets a NoViableAltException on a statement like that.

Can anybody offer any pointers?  Thanks very much.
These two rules are ambiguous:

IDENT	:	LETTER (LETTER | DIGIT)* ;
REGISTER:	IDENT ('!'IDENT)* ;
Just use IDENT, then look for the sequence of '!' IDENT in the parser. You are getting the NVA because you are asking for REGISTER but you are getting IDENT. The parser does not influence the lexer in any way. Also your number rules are ambiguous and all over the place ;-) Not sure why you are trying to distinguish exponents as CONSTANT?

Anyway, here it is fixed up, but you should make sure you understand the changes:

Jim

grammar t;

options {
language = Java;
output=AST;
}

prog:    (stat SEMI)+ ;

/** Statement */

stat:    identOrReg EQUALS expr

     ;

elist
scope {
    ArrayList regList;
}
    :    regs+=expr (COMMA regs+=expr)* ;

expr
    :    e=multdiv
        (    '+'^ e=multdiv
        |    '-'^ e=multdiv
        )*
        ;

multdiv
    :    e=atom
        (    '*'^ e=atom
        |    '/'^ e=atom
        )*
    ;

atom
    :    CONSTANT
    |    INT
    |    FLOAT
    |    identOrReg
    |    ('avg'^ | 'Avg'^) LPAREN elist RPAREN
    |    ('min'^ | 'Min'^) LPAREN elist RPAREN
    |    ('max'^ | 'Max'^) LPAREN elist RPAREN
    |    LPAREN expr RPAREN
    ;
   
identOrReg
    : IDENT ('!' IDENT)*
    ;

WS : (' '
| '\t'
| '\n'
| '\r')
{ $channel = HIDDEN; }
;
LPAREN    : '(' ;
RPAREN    : ')' ;
STAR    : '*' ;
DIV        : '/' ;
PLUS    : '+' ;
MINUS    : '-' ;
SEMI    : ';' ;
COMMA    : ',' ;
EQUALS    : '=' ;
fragment
DIGIT    : '0'..'9' ;
fragment
LETTER    : 'a'..'z' | 'A'..'Z' | '_' ;
COMMENT    : '/*' .* '*/' { $channel = HIDDEN; } ;
LINE_COMMENT: '//' ~('\n' | '\r')* '\r'? '\n' { $channel = HIDDEN; } ;
fragment INT :;
fragment FLOAT :;
fragment CONSTANT :;
NUMBER    : DIGIT+
            (
                  '.' DIGIT+ { $type = FLOAT; } ( EXPONENT {$type = CONSTANT; } )?
                | {$type = INT;} ( EXPONENT { $type = CONSTANT; } )?
            )
        | '.' DIGIT+ { $type = FLOAT; } (EXPONENT {$type = CONSTANT; } )?
;
fragment
EXPONENT:    ('e' | 'E') ('+' | '-')? DIGIT+ ;
EXP        :    '**' ;
AND        :    '&' ;
OR        :    '|' ;
XOR        :    '^' ;
IDENT    :    LETTER (LETTER | DIGIT)* ;


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---



List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to