Files
MY_COMPILER/frontend/ast/exp/RelExp.java
2025-12-10 17:58:17 +08:00

51 lines
1.5 KiB
Java

package frontend.ast.exp;
import frontend.ast.Node;
import frontend.ast.NodeStack;
import frontend.ast.SyntaxType;
import frontend.lexer.TokenStream;
import frontend.lexer.TokenType;
import frontend.ast.token.TokenNode;
import error.Errors;
public class RelExp extends Node {
public RelExp(TokenStream ts) {
super(SyntaxType.REL_EXP, ts);
}
public void parse(Errors errors) {
NodeStack stack = new NodeStack();
while (true) {
AddExp addexp = new AddExp(this.ts);
addexp.parse(errors);
stack.push(addexp);
if (isRelOp()) {
stack.push(new TokenNode(ts)); // relop
} else {
break;
}
}
if (stack.size() == 1) {
this.addChild((AddExp)stack.pop());
} else {
RelExp temp = this;
while(stack.size() > 1) {
RelExp rexp = new RelExp(this.ts);
AddExp addexp = (AddExp)stack.pop();
TokenNode relop = (TokenNode)stack.pop();
temp.addChild(rexp);
temp.addChild(relop);
temp.addChild(addexp);
temp = rexp;
}
temp.addChild((AddExp)stack.pop());
}
}
public boolean isRelOp() {
TokenType t = getCurrToken().getType();
return t == TokenType.LSS || t == TokenType.GRE
|| t == TokenType.LEQ || t == TokenType.GEQ;
}
}