javacc-jtree

JJTree 介绍

Node的生命周期

1.创建一个node节点时候自动指定一个唯一整形参数,这个整形参数表示唯一的节点类型,在simplenode类里面很有用,JJTREE自动产生一个parserTreeConstants.java文件和声明一些常用变量来作为node标示;同样还有数组jjtNodeName[]

2. node's jjtOpen() 方法调用

3.如果设置了 NODE_SCOPE_HOOK 参数, 那么用户声明的 openNodeScope()方法被调用并将 node 作为参数传进去. 这方法能初始化node的属性或者调用它的方法 .比如能保存node第一个token在这个节点

4.如果遇到在node解析时候抛出一个错误,那么JJTREE不能被关闭,用户定义的 closeNodeHook() 也不能被调用

5. 或者这个node的参数正则表达式是false,用户定义的closenodehook()也不能正常关闭;

6. 或者所有孩子节点都是整形数定义node节点,或者全部的包含在条件表达式的节点被压入堆栈, 他们之间的顺序是没有被标示的

7.node'的 jjtClose() 方法被调用

8. 节点node 被压入堆栈 stack.

9. 如果设置了 NODE_SCOPE_HOOK 参数,用户定义的 closenNodeScope()调用而且把node作为参数传进去

10.如果node不是root根节点他将作为孩子节点,并且调用jjtSetParent()方法设置父节点

 

 

/* Generated By:JJTree: Do not edit this line. .\JJTEg4State.java */

class JJTEg4State {
  private java.util.Stack nodes;
  private java.util.Stack marks;

  private int sp;		// 堆栈的node节点的数量number of nodes on stack
  private int mk;		// 当前的node的index
  private boolean node_created;

  JJTEg4State() {
    nodes = new java.util.Stack();
    marks = new java.util.Stack();
    sp = 0;
    mk = 0;
  }

  /* 决定当前的node是否关或者压入,当用户在执行动作时候调用.  */
  boolean nodeCreated() {
    return node_created;
  }

  /*初始化statck,当reinit()调用时候自动进行调用 */
  void reset() {
    nodes.removeAllElements();
    marks.removeAllElements();
    sp = 0;
    mk = 0;
  }

  /* Returns the root node of the AST.  It only makes sense to call
     this after a successful parse. */
  Node rootNode() {
    return (Node)nodes.elementAt(0);
  }

  /* Pushes a node on to the stack. */
  void pushNode(Node n) {
    nodes.push(n);
    ++sp;
  }

  /* Returns the node on the top of the stack, and remove it from the
     stack.  */
  Node popNode() {
    if (--sp < mk) {
      mk = ((Integer)marks.pop()).intValue();
    }
    return (Node)nodes.pop();
  }

  /* 返回statck最顶层的那个node节点 */
  Node peekNode() {
    return (Node)nodes.peek();
  }

  /* 返回当前node的孩子数量,比如node有很多,ASTADD ASTMULT的这种node*/
  int nodeArity() {
    return sp - mk;
  }


  void clearNodeScope(Node n) {
    while (sp > mk) {
      popNode();
    }
    mk = ((Integer)marks.pop()).intValue();
  }


  void openNodeScope(Node n) {
    marks.push(new Integer(mk));
    mk = sp;
    n.jjtOpen();
  }


  /* 把node作为父节点,从堆栈里面讲num个节点作为子节点,然后将node重新压入堆栈 */
  void closeNodeScope(Node n, int num) {
    mk = ((Integer)marks.pop()).intValue();
    while (num-- > 0) {
      Node c = popNode();
      c.jjtSetParent(n);
      n.jjtAddChild(c, num);
    }
    n.jjtClose();
    pushNode(n);
    node_created = true;
  }


  /* A conditional node is constructed if its condition is true.  All
     the nodes that have been pushed since the node was opened are
     made children of the the conditional node, which is then pushed
     on to the stack.  If the condition is false the node is not
     constructed and they are left on the stack. */
  void closeNodeScope(Node n, boolean condition) {
    if (condition) {
      int a = nodeArity();
      mk = ((Integer)marks.pop()).intValue();
      while (a-- > 0) {
	Node c = popNode();
	c.jjtSetParent(n);
	n.jjtAddChild(c, a);
      }
      n.jjtClose();
      pushNode(n);
      node_created = true;
    } else {
      mk = ((Integer)marks.pop()).intValue();
      node_created = false;
    }
  }
}

 参数:

 

BUILD_NODE_FILES (default: true)

Generate sample implementations for SimpleNode and any other nodes used in the grammar.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值