0
votes

Is there recursion in ANTLR in the sense that there is a call stack? Example:

parenset
:   LPAREN
    parenset*
    RPAREN
;

LPAREN: '(';
RPAREN: ')';

Should just verify that there are as many left parenthesis as there right. However in ANTLRWorks 1.4.3, in the interpreter when I type in '((()))', I get

enter image description here

Where are my other right parens?! Am I doing something incorrect? Thanks!

1
If there is no 'call stack' then how would I go about insuring that there are as many left parens as there are right?Tizz
can you give the entire .g fileFoo Bah
grammar Test; options {language = 'CSharp2'; output=AST; }Tizz
Thats all thats missing is the grammar name and the optionsTizz
You can omit the * character at parenset*.Sonson

1 Answers

1
votes

Don't use ANTLRWorks' interpreter: it is notoriously buggy.

If I use the debugger in ANTLRWorks (not the same as the interpreter!) with the grammar:

grammar T;

parenset
  :  LPAREN parenset* RPAREN
  ;

LPAREN : '(';
RPAREN : ')';

and provide the input ((())) I get the following parse-tree:

enter image description here

So, to answer your question:

Where are my other right parens?! Am I doing something incorrect?

No, you're not doing anything wrong: ANTLRWorks' interpreter is messing things up for you. Whenever your grammar contains predicates or recursive rule-invocations, better use the debugger or write your own test class.