JavaCC conditionals implement given a grammar

92 Views Asked by At

So I'm pretty new to JavaCC, I have this in JavaCC for a conditional grammar. I have to implement it in such a way that if the condition is true in the grammar I established, the rest of commands (block()) execute, but if not, just skip this part. How can I do that?

void conditional(): { }
    {
      < IF > condition()  block()
      [<ELSE>  block()]< FI>
      
    }
1

There are 1 best solutions below

0
Maurice Perry On

There is very little information about your project and the way to approach the question of conditional greately depends on the context.

For instance, if your program is compiling code for a target machine like, say JVM bytecode, you would do something like this:

void conditional(): {
    ForwardJump jump, savedJump;
} {
    <IF> condition() { jump = generateJumpIfFalse(); } block()
    [<ELSE> {
        savedJump = jump;
        jump = generateJump();
        fixForwardJump(savedJump);
    }  block()] {
        fixForwardJump(jump);
    } <FI>
}

This assumes that condition() will generate the code that calculates a boolean value and that that boolean value is then pushed to the stack. generateJumpIfFalse() generates a conditional jump that pops a boolean from the stack and, if false, jumps to a location that is not yet known because the block that follows has not yet been compiled. Forward jumps have to be updated once this location is known.; that's wat fixForwardJump does.

Now if your program is an interpretor, You want the parser to produce some structure that your java code can then execute.

In the present case, you manipulate two basic kind of structure: Statements and Expression. They could be the same thing, but the difference is than an Expression returns a value when executed, whereas Statement does not.

In the case of an interpretor, you often want the syntactic methods to return some substructure of the whole input programme; so you would have something like this:

Statement conditional(): {
    Expression cond;
    Statement ifBlock;
    Statement elseBlock = null;
} {
    <IF> cond=condition() ifBlock=block()
    [<ELSE> elseBlock=block()] <FI>
    {
        return new ConditionalStatement(cond, ifBlock, elseBlock);
    }
}

Let's say that Statement and Expression are interfaces of the following kind:

public interface Statement {
    public void execute(MachineState state);
}

public interface Expression {
    public Object evaluate(MachineState state);
}

Class ConditionalStatement must of course implement the Statement interface. It would look like this:

public class ConditionalStatement implements Statement {
    private final Expression cond;
    private final Statement ifStatement;
    private final Statement elseStatement;

    public ConditionalStatement(Expression cond, Statement ifStatement, Statement elseStatement) {
        this.cond = cond;
        this.ifStatement = ifStatement;
        this.elseStatement = elseStatement;
    }

    @Override
    public void execute(MachineState state) {
        Object value = cond.evaluate(state);
        if (value == Boolean.TRUE) {
            ifBlock.execute(state);
        } else if (elseBlock != null) {
            elseBlock.execute(state);
        }
    }
}

Of course, it can get a lot more complicated than that.