[antlr-interest] Parser not generating error?
Jim Idle
jimi at temporal-wave.com
Fri May 14 09:07:16 PDT 2010
Your statement rule needs to end with the EOF token.
Jim
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] 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
More information about the antlr-interest
mailing list