0

I try to create a compiler of a simplified language and I want to know how to store meta information like line number in my node. This will permit me to make a step-by-step interpretation. So I need to find the line of the current instruction in the concrete source text from the instruction node.

Can someone help me with this problem ?

Thank you by advance.

Simon
  • 5
  • 6

1 Answers1

1

Make a class SourceCoords to hold the information you want.

Add field to the SimpleNode class

  private SourceCoords myCoords ;
  public void setCoords( SourceCoords toSet ) { myCoords = toSet ; }
  public SourceCoords getCoords() { return myCoords ; }

In the options add

NODE_SCOPE_HOOK=true;

In your parser class add the following declarations

void jjtreeOpenNodeScope(Node n) {
    ((SimpleNode)n).setCoords( new SourceCoords( file, getToken(1).beginLine ) ) ;
}
void jjtreeCloseNodeScope(Node n) {
}
Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45