[antlr-interest] parentheses sequence validation

Rui Alberto L. Gonçalves rui-l-goncalves at ptinovacao.pt
Tue Jan 25 08:28:57 PST 2005


Hi,
I'm just starting with antlr. Sorry if I'm asking a really basic
question.

I'm trying to write a grammatic that should validade if
a sequence of parentheses is well formed.
Here's my grammatic:
................................................
 class ExpressionParser extends Parser;
options { buildAST=true; }
                                                                                                                            expr : LPAREN^ (expr)* RPAREN!;
                                                                                                                            class ExpressionLexer extends Lexer;
options {
  filter=false;
  interactive  = true;
}
                                                                                                                           LPAREN : '(' ;
RPAREN : ')' ;
                                                                                                                             WS    : (' ' | '\t' | '\r' | '\n') {$setType(Token.SKIP);} ;

.....................................................

this is working well, except when the input sequence
ends with extra right parentheses.

Ex: ((()())()) : Validated OK
(() : The error is thrown: expecting RPAREN, found : OK

BUT when trying validate the expression: ()) no error is thrown!!!

Why this is happening ? Thanks for any help.

Rui

-- 



More information about the antlr-interest mailing list