Your statement rule needs to end with the EOF token. Jim
> -----Original Message----- > From: [email protected] [mailto:antlr-interest- > [email protected]] On Behalf Of Charles Chan > Sent: Friday, May 14, 2010 9:06 AM > To: antlr-interest > Subject: [antlr-interest] Parser not generating error? > > Hi, all. I am trying to write a parser for an input similar to the > following: > > A = 'TEST1' and B = 'TEST2' > > The problem I am having is that the parser is not returning error in > the following scenarios: > > 1. A dangling left paren: > > A = 'TEST1' and B = 'TEST2' ( > > 2. A dangling ID: > > A = 'TEST1' and B = 'TEST2' A > > Is there some options I need to turn on to get these kinds of errors? > > Thanks! > Charles > > > grammar Script; > > options { > output=AST; > } > > @parser::header { > > import java.util.Map; > import java.util.HashMap; > import java.math.BigDecimal; > } > > @parser::members { > public Map<String, Object> values = new HashMap<String, Object>(); > > public static void main(String[] args) throws Exception { > ScriptLexer lexer = new ScriptLexer(new ANTLRFileStream(args[0])); > CommonTokenStream tokenStream = new CommonTokenStream(lexer); > ScriptParser parser = new ScriptParser(tokenStream); > parser.values.put("A", "TEST1"); > parser.values.put("B", "TEST2"); > parser.values.put("C", "TEST3"); > System.out.println(parser.statement().value); > } > > protected void mismatch(IntStream input, int ttype, BitSet follow) > throws RecognitionException { > throw new MismatchedTokenException(ttype, input); > } > > public Object recoverFromMismatchedSet(IntStream input, > RecognitionException e, BitSet follow) throws RecognitionException { > throw e; > } > } > > @rulecatch { > catch (RecognitionException ex) { > throw ex; > } > } > > /*------------------------------------------------------------------ > * PARSER RULES > *------------------------------------------------------------------*/ > > statement returns [boolean value]: e=orcondition { $value = $e.value; } > ; > > orcondition returns [boolean value] > : e=andcondition { $value = $e.value; } (OR^ e=andcondition { > $value = $value || $e.value; })* > ; > > andcondition returns [boolean value] > : e=expression { $value = $e.value; } (AND^ e=expression { $value > = $value && $e.value; })* > ; > > expression returns [boolean value] > : ID operator rhs > { > Object idValue = (Object)values.get($ID.text); > Object rhsValue = $rhs.value; > $value = idValue.equals(rhsValue); > } > | LEFT_PAREN! orcondition RIGHT_PAREN! > { > $value = $orcondition.value; > } > ; > > operator : GTE | LTE | GT | LT | EQ | NOT_EQ > ; > rhs returns [Object value] > : e=INTEGER { $value = Integer.valueOf($e.text); } | > e=FLOAT { $value = new BigDecimal($e.text); } | > e=STRING_LITERAL { $value = $e.text.substring(1, > $e.text.length() - 1); } | > TRUE { $value = Boolean.TRUE; } | > FALSE { $value = Boolean.FALSE; } > ; > > /*------------------------------------------------------------------ > * LEXER RULES > *------------------------------------------------------------------*/ > AND : 'and' > ; > OR : 'or' > ; > LT : '<' > ; > GT : '>' > ; > LTE : '<=' > ; > GTE : '>=' > ; > EQ : '=' > ; > NOT_EQ : '!=' > ; > TRUE : 'true' > ; > FALSE : 'false' > ; > ID : ( 'a' .. 'z' | 'A' .. 'Z' | '.' | '_' )+ > ; > STRING_LITERAL : '\'' ( ( '\'' '\'' )=> '\'' '\'' | ~'\'' )* '\'' > ; > LEFT_PAREN : '(' > ; > RIGHT_PAREN : ')' > ; > WS : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = > HIDDEN; } > ; > SIGN : '+' | '-' > ; > > INTEGER: '0' | SIGN? '1'..'9' '0'..'9'*; > FLOAT: INTEGER '.' '0'..'9'+; > > List: http://www.antlr.org/mailman/listinfo/antlr-interest > Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your- > email-address 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.
