Published
Edited
Jul 6, 2020
Insert cell
Insert cell
Insert cell
class BaseParser {
constructor() {
this.tokens = null
this.idx = -1
}
next() {
this.idx += 1
return this.tokens[this.idx]
}

parse() {
return
}
}
Insert cell
Insert cell
// Recursive descent parser

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
// check if the token list is empty
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
}
}
Insert cell
import {Tree, vgraph} from "@joelsunny/parse-tree-visualization"
Insert cell
class TreeViz {
constructor() {
this.ptree = new Tree("root", "PROG");
}

draw(expr, parser) {
let ttree = function main(expr) {
let l = new Lexer(expr)
l.scan();
console.log("begin parsing")
//console.log(ptree);
let ptree = new Tree("root", "PROG");
console.log(ptree);
console.log(parser);
return parser.parse(l.tokens, ptree);
}(expr)

if (ttree != null) {
this.ptree = ttree
}
return vgraph(this.ptree.to_json());
}
}
Insert cell
tviz= new TreeViz()
Insert cell
Insert cell
viewof expr = html`<input type="text" placeholder="Type an expression">`
Insert cell
tviz.draw(expr, new RDParser())
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more