class RDParser extends BaseParser {
constructor() {
super()
this.tokens = null
this.parseTree = null;
}
parse(tokens, ptree) {
console.log("rd parser") ;
this.tokens = tokens;
this.parseTree = ptree;
console.log(this.tokens);
console.log(this.parseTree);
let temp = this.idx
if(this.EOF()) {
return this.parseTree
} else {
this.backtrack(temp, this.parseTree, this.parseTree.root)
}
if (this.E(this.parseTree.root)) {
if (this.EOF()) {
console.log("Success")
return this.parseTree;
}
else {
console.log("Failed.")
}
}
else {
console.log("Failed")
}
}
E(parent) {
let tr = new Tree("E", "E");
const rt = tr.root
let temp = this.idx
if (this.backtrack(temp, tr, rt) && this.Num(rt) && this.bin_op(rt) && this.E(rt)) {
parent.addChild(rt)
return true
}
else if (this.backtrack(temp, tr, rt) && this.O_PAR(rt) && this.E(rt) && this.C_PAR(rt)) {
parent.addChild(rt)
return true
}
else if (this.backtrack(temp, tr, rt) && this.Num(rt)){
parent.addChild(rt)
return true
}
else {
return false
}
}
backtrack(temp, tr, rt) {
this.idx = temp
tr.delSubTree(rt)
return true
}
Num(parent){
let t = this.next()
if (t.token.type == TOKENS.NUM) {
parent.addChild(new Tree("NUM", t.token.symbol).root)
return true
}
return false
}
bin_op(parent) {
let t = this.next()
if (t.token.type == TOKENS.BIN_OP) {
parent.addChild(new Tree("BIN_OP", t.token.symbol).root)
return true
}
return false
}
O_PAR(parent) {
let t = this.next()
if (t.token.type == TOKENS.O_PAR){
parent.addChild(new Tree( "(", t.token.symbol).root)
return true
}
return false
}
C_PAR(parent) {
let t = this.next()
if (t.token.type == TOKENS.C_PAR) {
parent.addChild(new Tree( ")", t.token.symbol).root)
return true
}
return false
}
EOF() {
let t = this.next()
if (t.token.type == TOKENS.EOF) {
return true
}
return false
}
}