[antlr-interest] HI, i have a simple problem
Cesar Arrieta
mis.negocios.cba at gmail.com
Fri Sep 21 13:50:38 PDT 2007
Hi..here a post my MyCalc.g3 file..it is working fine..my problem is that i
dont know how to make my program tell if an error ocurrs..for example i want
that parser.expr() throw an exception or return a boolean telling if the
parse went fine or not?? no just sending messages to the console.
I hope you could help me, Thanks.
well this is the file MyCalc.g3:
and valids inputs are 8*b1*9+5*B2
and wrong inputs are 8*v*b1+da*b1b
grammar MiCalc;
tokens {
PLUS = '+' ;
MINUS = '-' ;
MULT = '*' ;
DIV = '/' ;
PUNTO = '.';
}
@header {
package lib;
import Jama.Matrix;
}
@lexer::header {
package lib;
}
@members {
public static void main(String[] args) throws Exception {
//This is a file for the input
//valid input 8 * b1
//wrong input 8dsa*b1b
String texto="texto";
MiCalcLexer lex = new MiCalcLexer(new ANTLRFileStream(texto));
CommonTokenStream tokens = new CommonTokenStream(lex);
MiCalcParser parser = new MiCalcParser(tokens);
parser.expr();
}
}
/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/
expr : term ( ( PLUS | MINUS ) term )* ;
term : factorN (((MULT | DIV) factorN) | ((MULT | DIV) factorBi) )*
| factorBi (((MULT) factorBi) | ((MULT | DIV) factorN))*;
factorN : (NUMBER | FLOAT);
factorBi: BINDEX NUMBER;
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
NUMBER : (DIGIT)+ ;
FLOAT : (NUMBER (PUNTO) NUMBER)+;
/*
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; }
;
*/
fragment DIGIT : '0'..'9' ;
BINDEX : ('b' | 'B');
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070921/f19842b2/attachment.html
More information about the antlr-interest
mailing list