循环'在 ANTLR 中迭代

发布于 2024-10-20 03:40:40 字数 1447 浏览 2 评论 0原文

我正在尝试使用 ANTLR 制作 Pascal 解释器,目前在遍历 AST 树时处理循环时遇到一些问题。 例如 for 循环被解析为:(

parametricLoop
    : FOR IDENTIFIER ASSIGN start = integerExpression TO end = integerExpression DO
    statement
    -> ^( PARAMETRIC_LOOP IDENTIFIER $start $end statement )
    ;

带有 DOWNTO 的变体被忽略)。 我怎样才能让 walker 根据需要多次重复循环的执行?我知道我应该使用 input.Mark() 和 input.Rewind() 。但它们到底应该放在哪里呢?我当前的错误变体看起来是这样的(目标语言是C#):(

parametricLoop
    :
        ^(
            PARAMETRIC_LOOP
            IDENTIFIER
            start = integerExpression
            {
                Variable parameter = Members.variable($IDENTIFIER.text);
                parameter.value = $start.result;
            }
            end = integerExpression
            {
                int end_value = $end.result;
                if ((int)parameter.value > end_value) goto EndLoop;
                parametric_loop_start = input.Mark();
            }
            statement
            {
                parameter.value = (int)parameter.value + 1;
                if ((int)parameter.value <= end_value)
                    input.Rewind(parametric_loop_start);
            )
            {
                EndLoop: ;
            }
        ;

希望一切都是可以理解的)。应在语句首次执行之前检查重复条件。 我尝试将 Mark 和 Rewind 放置在不同的代码块中,包括 @init 和 @after,甚至将尾随 goto 放置到循环头,但每次循环要么迭代一次,要么抛出异常,例如遇到意外的令牌,例如 ':= '(作业)。我不知道如何使其正常工作,也找不到任何有效的示例。有人可以建议这个问题的解决方案吗?

I'm trying to make a Pascal interpreter using ANTLR and currently have some troubles with processing loops while walking the AST tree.
For example for loop is parsed as:

parametricLoop
    : FOR IDENTIFIER ASSIGN start = integerExpression TO end = integerExpression DO
    statement
    -> ^( PARAMETRIC_LOOP IDENTIFIER $start $end statement )
    ;

(variant with DOWNTO is ignored).
In what way can I make walker to repeat the loop's execution so much times as needed? I know that I should use input.Mark() and input.Rewind() for that. But exactly where should they be put? My current wrong variant looks so (target language is C#):

parametricLoop
    :
        ^(
            PARAMETRIC_LOOP
            IDENTIFIER
            start = integerExpression
            {
                Variable parameter = Members.variable($IDENTIFIER.text);
                parameter.value = $start.result;
            }
            end = integerExpression
            {
                int end_value = $end.result;
                if ((int)parameter.value > end_value) goto EndLoop;
                parametric_loop_start = input.Mark();
            }
            statement
            {
                parameter.value = (int)parameter.value + 1;
                if ((int)parameter.value <= end_value)
                    input.Rewind(parametric_loop_start);
            )
            {
                EndLoop: ;
            }
        ;

(Hope everything is understandable). The condition of repeating should be checked before the statement's first execution.
I tried to play with placing Mark and Rewind in different code blocks including @init and @after, and even put trailing goto to loops head, but each time loop either iterated one time or threw exceptions like Unexpected token met, for example ':=' (assignement). I have no idea, how to make that work properly and can't find any working example. Can anybody suggest a solution of this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

中二柚 2024-10-27 03:40:40

我没有使用过 ANTLR,但在我看来,你在解析程序时试图执行该程序,但这并不是解析器的真正设计目的(简单的算术表达式可以在解析期间执行,但正如你所发现,循环是有问题的)。我强烈建议您仅使用解析来构建 AST。因此parametricLoop 的解析器代码应该只构造一个表示循环的树节点,其子节点表示变量、条件和主体。然后,在一个单独的常规 C# 类(向其提供解析器生成的 AST)中,通过以某种方式遍历树来执行代码,然后您可以完全自由地按顺序在节点之间来回跳转来模拟循环执行。

I haven't used ANTLR, but it seems to me that you are trying to execute the program while you're parsing it, but that's not really what parsers are designed for (simple arithmetic expressions can be executed during parsing, but as you have discovered, loops are problematic). I strongly suggest that you use the parsing only to construct the AST. So the parser code for parametricLoop should only construct a tree node that represents the loop, with child nodes representing the variables, conditions and body. Afterwards, in a separate, regular C# class (to which you provide the AST generated by the parser), you execute the code by traversing the tree in some manner, and then you have complete freedom to jump back and forth between the nodes in order to simulate the loop execution.

怀念你的温柔 2024-10-27 03:40:40

我使用 ANTLR 3.4,找到了一个与 CommonTreeNodeStream 类一起使用的解决方案。

基本上,我分割了树解析器的新实例,它又分析了所有子树。我的示例代码定义了一个 while 循环:

tree grammar Interpreter;
...
@members
{
  ...
  private Interpreter (CommonTree node, Map<String, Integer> symbolTable)
  {
    this (new CommonTreeNodeStream (node));
    ...
  }
  ...
}
...
stmt    :   ...
        |   ^(WHILE c=. s1=.) // ^(WHILE cond stmt)
            {
              for (;;)
              {
                Interpreter condition = new Interpreter (c, this.symbolTable);
                boolean     result    = condition.cond ();
                if (! result)
                  break;

                Interpreter statement = new Interpreter (s1, this.symbolTable);
                statement.stmt ();
              }
            }
...
cond returns [boolean result]
                          : ^(LT e1=expr e2=expr) {$result = ($e1.value < $e2.value);}
                          | ...

I work with ANTLR 3.4 and I found a solution which works with Class CommonTreeNodeStream.

Basically I splitted off new instances of my tree parser, which in turn analyzed all subtrees. My sample code defines a while-loop:

tree grammar Interpreter;
...
@members
{
  ...
  private Interpreter (CommonTree node, Map<String, Integer> symbolTable)
  {
    this (new CommonTreeNodeStream (node));
    ...
  }
  ...
}
...
stmt    :   ...
        |   ^(WHILE c=. s1=.) // ^(WHILE cond stmt)
            {
              for (;;)
              {
                Interpreter condition = new Interpreter (c, this.symbolTable);
                boolean     result    = condition.cond ();
                if (! result)
                  break;

                Interpreter statement = new Interpreter (s1, this.symbolTable);
                statement.stmt ();
              }
            }
...
cond returns [boolean result]
                          : ^(LT e1=expr e2=expr) {$result = ($e1.value < $e2.value);}
                          | ...
原来分手还会想你 2024-10-27 03:40:40

刚刚解决了类似的问题,有几点:

  1. 似乎你需要使用 BufferedTreeNodeStream 而不是 CommonTreeNodeStream,CommonTreeNodeStream 对我来说永远不起作用(挣扎了很长时间才找到)

  2. 使用eek对我来说似乎更清楚

这是我的列表命令代码,很确定你的可以是轻松更改为这种样式:

list returns [Object r]
    :   ^(LIST ID
          {int e_index = input.Index;}
          exp=.
          {int s_index = input.Index;}
          statements=.
         )
        {
            int next = input.Index;
            input.Seek(e_index);
            object list = expression();
            foreach(object o in (IEnumerable<object>)list)
            {
                model[$ID.Text] = o;
                input.Seek(s_index);
                $r += optional_block().ToString();
            }
            input.Seek(next);
        }

Just solved a similar problem, several points:

  1. Seems you need to use BufferedTreeNodeStream instead of CommonTreeNodeStream, CommonTreeNodeStream never works for me (struggled long time to find out)

  2. Use seek seems to be more clear to me

Here's my code for a list command, pretty sure yours can be easily changed to this style:

list returns [Object r]
    :   ^(LIST ID
          {int e_index = input.Index;}
          exp=.
          {int s_index = input.Index;}
          statements=.
         )
        {
            int next = input.Index;
            input.Seek(e_index);
            object list = expression();
            foreach(object o in (IEnumerable<object>)list)
            {
                model[$ID.Text] = o;
                input.Seek(s_index);
                $r += optional_block().ToString();
            }
            input.Seek(next);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文