Hi there. I am having trouble with the error handling.
I have a grammar for recoignize linear expression. And it works great!.
The grammar for a linear expresion is the following:

tokens
{
        PLUS    = '+';
        MINUS   = '-';
        MUL             = '*';
        DIV             = '/';
}

linexpr : (MINUS|PLUS)? linterm ((PLUS|MINUS) linterm)*;
linterm : factor? ID;

expr returns [double value]
        : e=term {$value = $e.value;}
        (       PLUS e=term {$value += $e.value;}
        |       MINUS e=term {$value -= $e.value;}
        )*;

term returns [double value]
        : f=factor {$value = $f.value;}
        (       MUL f=factor {$value *= $f.value;}
        |       DIV f=factor {$value /= $f.value;}
        )*;

factor returns [double value]
        : DOUBLE {$value = Double.parseDouble($DOUBLE.text);}
        | '(' e=expr ')'{$value = $e.value;};
        
ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

DOUBLE
        :   ('0'..'9')+
        |       ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
     |   '.' ('0'..'9')+ EXPONENT?
     |   ('0'..'9')+ EXPONENT
     ;

fragment EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

NEWLINE:'\r'? '\n' { $channel = HIDDEN; };

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


But the problem ocurrs when, for example, i have:
"x x x"

Then the parsers stop after processing the first "x".
¿How do i correctly emit an invalid syntax error?.
I Try with the catch EarlyExitException, but it doesn't works.
I Want, inside my java aplicacition to catch this, and show to the final 
user.
Something like this...
//line is equals to the user input...

             CharStream cs = new ANTLRStringStream(line);
             LinearExpressionLexer lexer = new LinearExpressionLexer(cs);
             CommonTokenStream tokens = new CommonTokenStream(lexer);
             LinearExpressionParser parser = new 
LinearExpressionParser(tokens);
             res = parser.linexpr (); // and here, it's suppose to fail, 
but it isn't.
Actually, the linexpr does returns some kind of data whose type is a 
custom class called LinearExpresion. I omit to put the return in the 
linearexpr parser rule to simplify things.

Hope anyone can help me.
Greettings and thanks for advance.
Víctor.

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

-- 
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.

Reply via email to