Do not change a loop variable inside a for-loop block

110 views Asked by At

I want to implement the rule coding in my parser generated by javaCC : Do not change a loop variable inside a for-loop block.

the Rule Production javacc of for-loop block is :

void MyMethod () : {}
{
"(" Argument () ")" {}
(Statement ()) *

}

void Statement () : {}
{
expressionFOR()
}

void expressionFOR() :{}
{
<For> <id> "= " 1  <to> 100
int J
int kk =SUM( , J)
......

}

thank you very much in advance

1

There are 1 answers

0
Theodore Norvell On

Assuming you are using JJTree with MULTI=false and VISITOR=true, you could write a visitor along this line

public void visit(SimpleNode node, Object data) {
    if( this is a for loop node ) {
        push the for loop variable onto a stack of variables
        node.childrenAccept(this, null) ;
        pop the stack }
    else {
        if( this is an assignment statement node
            and the target variable is on the stack )
            report rule violated
        node.childrenAccept(this, null) ;
   }
}