UNPKG

@parser-generator/grammar-interpreter

Version:

A Parser Generator that supports LL,SLR,LR1,LALR

154 lines (149 loc) 5.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const definition_1 = require("@parser-generator/definition"); class IfStmt { constructor(children) { this.children = children; } toString() { return "(" + this.children.join(" ") + ")"; } } let S = new definition_1.NonTerminal("S"); let Stmt = new definition_1.NonTerminal("Stmt"); let If = new definition_1.NonTerminal("If"); let Expr = new definition_1.NonTerminal("Expr"); let Other = new definition_1.NonTerminal("Other"); /* (0) S->Stmt */ let p0 = new definition_1.Production(0, S, [ new definition_1.SymbolWrapper(Stmt) ], undefined, undefined); /* (1) Stmt->If<%(e)=>e[0]%> */ let p1 = new definition_1.Production(1, Stmt, [ new definition_1.SymbolWrapper(If) ], undefined, (e) => e[0]); /* (2) Stmt->Other<%(e)=>e[0]%> */ let p2 = new definition_1.Production(2, Stmt, [ new definition_1.SymbolWrapper(Other) ], undefined, (e) => e[0]); /* (3) If->ifExprStmt<%(e)=> new IfStmt(e)%> */ let p3 = new definition_1.Production(3, If, [ new definition_1.SymbolWrapper("if"), new definition_1.SymbolWrapper(Expr), new definition_1.SymbolWrapper(Stmt) ], undefined, (e) => new IfStmt(e)); /* (4) If->ifExprStmtelseStmt<%(e)=> new IfStmt(e)%> */ let p4 = new definition_1.Production(4, If, [ new definition_1.SymbolWrapper("if"), new definition_1.SymbolWrapper(Expr), new definition_1.SymbolWrapper(Stmt), new definition_1.SymbolWrapper("else"), new definition_1.SymbolWrapper(Stmt) ], undefined, (e) => new IfStmt(e)); /* (5) Expr->(Expr)<%(e)=>e[0]%> */ let p5 = new definition_1.Production(5, Expr, [ new definition_1.SymbolWrapper("Expr") ], undefined, (e) => e[0]); /* (6) Other->Other<%(e)=>e[0]%> */ let p6 = new definition_1.Production(6, Other, [ new definition_1.SymbolWrapper("Other") ], undefined, (e) => e[0]); S.prods = [p0]; Stmt.prods = [p1, p2]; If.prods = [p3, p4]; Expr.prods = [p5]; Other.prods = [p6]; let table = new definition_1.ParsingTable(); table.put(0, Stmt, new definition_1.Goto(1)); table.put(0, If, new definition_1.Goto(2)); table.put(0, Other, new definition_1.Goto(3)); table.put(0, "if", new definition_1.Shift(4)); table.put(0, "Other", new definition_1.Shift(5)); table.put(1, definition_1.EOF, new definition_1.Accept()); table.put(2, definition_1.EOF, new definition_1.Reduce(p1)); table.put(3, definition_1.EOF, new definition_1.Reduce(p2)); table.put(4, Expr, new definition_1.Goto(6)); table.put(4, "Expr", new definition_1.Shift(7)); table.put(5, definition_1.EOF, new definition_1.Reduce(p6)); table.put(6, Stmt, new definition_1.Goto(8)); table.put(6, If, new definition_1.Goto(9)); table.put(6, Other, new definition_1.Goto(10)); table.put(6, "if", new definition_1.Shift(11)); table.put(6, "Other", new definition_1.Shift(12)); table.put(7, "if", new definition_1.Reduce(p5)); table.put(7, "Other", new definition_1.Reduce(p5)); table.put(8, definition_1.EOF, new definition_1.Reduce(p3)); table.put(8, "else", new definition_1.Shift(13)); table.put(9, definition_1.EOF, new definition_1.Reduce(p1)); table.put(9, "else", new definition_1.Reduce(p1)); table.put(10, definition_1.EOF, new definition_1.Reduce(p2)); table.put(10, "else", new definition_1.Reduce(p2)); table.put(11, Expr, new definition_1.Goto(14)); table.put(11, "Expr", new definition_1.Shift(7)); table.put(12, definition_1.EOF, new definition_1.Reduce(p6)); table.put(12, "else", new definition_1.Reduce(p6)); table.put(13, Stmt, new definition_1.Goto(15)); table.put(13, If, new definition_1.Goto(2)); table.put(13, Other, new definition_1.Goto(3)); table.put(13, "if", new definition_1.Shift(4)); table.put(13, "Other", new definition_1.Shift(5)); table.put(14, Stmt, new definition_1.Goto(16)); table.put(14, If, new definition_1.Goto(9)); table.put(14, Other, new definition_1.Goto(10)); table.put(14, "if", new definition_1.Shift(11)); table.put(14, "Other", new definition_1.Shift(12)); table.put(15, definition_1.EOF, new definition_1.Reduce(p4)); table.put(16, definition_1.EOF, new definition_1.Reduce(p3)); table.put(16, "else", new definition_1.Shift(17)); table.put(17, Stmt, new definition_1.Goto(18)); table.put(17, If, new definition_1.Goto(9)); table.put(17, Other, new definition_1.Goto(10)); table.put(17, "if", new definition_1.Shift(11)); table.put(17, "Other", new definition_1.Shift(12)); table.put(18, definition_1.EOF, new definition_1.Reduce(p4)); table.put(18, "else", new definition_1.Reduce(p4)); const core_1 = require("@parser-generator/core"); exports.parser = new core_1.LRParser(table); /* #GRAMMAR S -> Stmt ; Stmt -> If | Other <% (e)=>e[0] %> ; If -> 'if' Expr Stmt | 'if' Expr Stmt 'else' Stmt <% (e)=> new IfStmt(e) %>; Expr -> 'Expr' <% (e)=>e[0] %>; Other -> 'Other' <% (e)=>e[0] %>; 'if' Expr Stmt {EOF} 'if' Expr Stmt 'else' Stmt {EOF} 'if' Expr 'if' Expr Stmt {EOF} 'if' Expr 'if' Expr Stmt {'else'} 'else' Stmt {EOF} 'if' Expr Stmt 'else' Stmt */ class Token { constructor(lexeme) { this.lexeme = lexeme; } key() { return this.lexeme; } toString() { return this.lexeme; } } class Lexer { constructor(lexemes) { this.i = 0; this.lexemes = lexemes; } peek() { return this.i >= this.lexemes.length ? new Token(definition_1.EOF) : new Token(this.lexemes[this.i]); } next() { return this.i < this.lexemes.length ? new Token(this.lexemes[this.i++]) : new Token(definition_1.EOF); } } let ast = exports.parser.parse(new Lexer(["if", "Expr", "if", "Expr", "Other", "else", "Other"])); console.log(ast.toString()); //(if Expr (if Expr Other else Other)) //# sourceMappingURL=test.js.map