我如何在javacc中实现循环(For) [英] how do I implement loops (For) in javacc

查看:127
本文介绍了我如何在javacc中实现循环(For)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为包含循环的脚本语言做了一个解释器,使用javacc我定义了语法,但是我没有办法备份到行上重复执行"for"块. 如何备份令牌管理器,以便可以一遍又一遍地重新解析并重新评估循环体?

I made an interpreter for a script language containing a loop for, using javacc I have defined the grammar but I can't have a way to back up to line to repeat the execution of the block "for". how back up the token manager so that loop-bodies can be re-parsed, and thus reevaluated, over and over again ?.

void For(): {ArrayList<String> lst;Token n,v;int i=0;} {

      "for" "(" n=<ID> ":"  v=<ID> ")" "{"
    (actions()";"  )+
    "}"


    }

推荐答案

我们可以使用如下所示的JavaCode Production

We can use the JavaCode Production like below,

前提是action()生产负责每一行. 请将分号移到actions()生产中.

provided the action() production takes care of each line. Please Move semicolon to the actions() Production.

TOKEN : {
   <LCURLY : "{" >
   <RCURLY : "}" >
}


void For(): {ArrayList<String> lst;Token n,v;int i=0;} {
    "for" "(" n=<ID> ":"  v=<ID> ")" <LCURLY>
         loopBody ();
    <RCURLY>
}

JAVACODE
void loopBody () {
    Token t = getNextToken();
    while (t.kind != RCURLY) {
       actions();
       t = getNextToken();
    }
}

希望这会有所帮助.

这篇关于我如何在javacc中实现循环(For)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆