开发者

Beginner Recursion in ANTLR, no call stack?

开发者 https://www.devze.com 2023-04-11 07:06 出处:网络
Is there recursion in ANTLR in the sense that there is a call stack?Example: parenset :LPAREN parenset* RPAREN

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

Beginner Recursion in ANTLR, no call stack?

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


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:

Beginner Recursion in ANTLR, no call stack?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消