[antlr-interest] BASIC-like goto implementation

Juan Fernando Herrera J. juanfhj at gmail.com
Sun Oct 17 11:53:24 PDT 2010


I'm trying to implement a tree grammar that recognizes the goto
statement in BASIC with numbered code lines. So far I'm toying with
goto and print. My parser-lexer grammar looks like:

prog:   stat+;

stat:   expr NEWLINE  -> expr
    |   NEWLINE       ->
    ;

expr:    INT^ instruction
    ;

instruction: 'goto'^ INT
    | 'print'^ INT
    ;

As you can see, I'm building a tree whose first level are the numeric
line labels, and the children are BASIC instructions.

The tree grammar I'm trying has the following function:

@members {
  HashMap labeledLines = new HashMap();
  public void getLabels() {
      CommonTree root = (CommonTree)input.getTreeSource();
      for (int i = 0; i < root.getChildCount(); i++) {
          labeledLines.put(root.getChild(i).getText(), i);
      }
      System.out.println(labeledLines);
  }
}

My getLabels() function manages to get the (CommonTree) children index
i, of each labeled child. Next I'd like to use seek() to go to the
corresponding BufferedTreeNodeStream node. How can I translate the
child index 'i' into an index that is usable with seek?

Thanks

JH


More information about the antlr-interest mailing list