@parser-generator/core
Version:
A Parser Generator that supports LL,SLR,LR1,LALR
244 lines (243 loc) • 8.39 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("mocha");
require("should");
const __1 = require("..");
const definition_1 = require("@parser-generator/definition");
const toolkit_1 = require("./toolkit");
describe("============The calculation of follow set============", function () {
describe("1. 后跟终结符", function () {
let S = new definition_1.NonTerminal("S");
let A = new definition_1.NonTerminal("A");
let grammar = new toolkit_1.SimpleGrammar([
[S, [A, "s"]],
[A, ["a"]]
]);
let calculator = new __1.FollowCalculator(grammar, new __1.FirstCalculator(grammar));
let expectation = new Set(["s"]);
it(``, function () {
let actuality = calculator.getFollowSet(A);
actuality.should.eqls(expectation);
});
});
describe("2. 后跟非ε的非终结符 ", function () {
let S = new definition_1.NonTerminal("S");
let A = new definition_1.NonTerminal("A");
let B = new definition_1.NonTerminal("B");
let C = new definition_1.NonTerminal("C");
let grammar = new toolkit_1.SimpleGrammar([
[S, [A, B, C]],
[A, ["a"]],
[B, ["b1"], ["b2"]],
[C, ["c"]],
]);
let calculator = new __1.FollowCalculator(grammar, new __1.FirstCalculator(grammar));
let expectation = new Set(["b1", "b2"]);
it(``, function () {
let actuality = calculator.getFollowSet(A);
actuality.should.eqls(expectation);
});
});
describe("3. 后跟可ε的非终结符1", function () {
/*
S->ABC
A->a
B->b | 𝝴
C->c
*/
let S = new definition_1.NonTerminal("S");
let A = new definition_1.NonTerminal("A");
let B = new definition_1.NonTerminal("B");
let C = new definition_1.NonTerminal("C");
let grammar = new toolkit_1.SimpleGrammar([
[S, [A, B, C]],
[A, ["a"]],
[B, ["b"], [definition_1.NIL]],
[C, ["c"]]
]);
let calculator = new __1.FollowCalculator(grammar, new __1.FirstCalculator(grammar));
let expectation = new Set(["b", "c"]);
it(``, function () {
let actuality = calculator.getFollowSet(A);
actuality.should.eqls(expectation);
});
});
describe("4. 后跟连续的可ε的非终结符", function () {
/*
S->ABC
A->a
B->b | 𝝴
C->c | 𝝴
*/
let S = new definition_1.NonTerminal("S");
let A = new definition_1.NonTerminal("A");
let B = new definition_1.NonTerminal("B");
let C = new definition_1.NonTerminal("C");
let grammar = new toolkit_1.SimpleGrammar([
[S, [A, B, C]],
[A, ["a"]],
[B, ["b"], [definition_1.NIL]],
[C, ["c"], [definition_1.NIL]]
]);
let calculator = new __1.FollowCalculator(grammar, new __1.FirstCalculator(grammar));
let expectation = new Set(["b", "c", definition_1.EOF]);
it(``, function () {
let actuality = calculator.getFollowSet(A);
actuality.should.eqls(expectation);
});
});
describe("5. 连续重复", function () {
/*
S->AAB
A->a
B->b
*/
let S = new definition_1.NonTerminal("S");
let A = new definition_1.NonTerminal("A");
let B = new definition_1.NonTerminal("B");
let grammar = new toolkit_1.SimpleGrammar([
[S, [A, A, B]],
[A, ["a"]],
[B, ["b"]]
]);
let calculator = new __1.FollowCalculator(grammar, new __1.FirstCalculator(grammar));
let expectation = new Set(["a", "b"]);
it(``, function () {
let actuality = calculator.getFollowSet(A);
actuality.should.eqls(expectation);
});
});
describe("5. 连续重复(地狱级)", function () {
/*
S->AABAAACD
A->a
B->b
C->c
D->d
*/
let S = new definition_1.NonTerminal("S");
let A = new definition_1.NonTerminal("A");
let B = new definition_1.NonTerminal("B");
let C = new definition_1.NonTerminal("C");
let D = new definition_1.NonTerminal("D");
let grammar = new toolkit_1.SimpleGrammar([
[S, [A, A, B, A, A, A, C, D]],
[A, ["a"]],
[B, ["b"]],
[C, ["c"]],
[D, ["d"]]
]);
let calculator = new __1.FollowCalculator(grammar, new __1.FirstCalculator(grammar));
let expectation = new Set(["a", "b", "c"]);
it(``, function () {
let actuality = calculator.getFollowSet(A);
actuality.should.eqls(expectation);
});
});
describe("7. 自递归", function () {
/*
S->AB
A->aA
B->b
*/
let S = new definition_1.NonTerminal("S");
let A = new definition_1.NonTerminal("A");
let B = new definition_1.NonTerminal("B");
let grammar = new toolkit_1.SimpleGrammar([
[S, [A, B]],
[A, ["a", A]],
[B, ["b"]],
]);
let calculator = new __1.FollowCalculator(grammar, new __1.FirstCalculator(grammar));
let expectation = new Set(["b"]);
it(``, function () {
let actuality = calculator.getFollowSet(A);
actuality.should.eqls(expectation);
});
});
describe("8. 循环依赖(青铜)", function () {
/*
A->B
B->A
*/
let A = new definition_1.NonTerminal("A");
let B = new definition_1.NonTerminal("B");
let grammar = new toolkit_1.SimpleGrammar([
[A, [B]],
[B, [A]]
]);
let calculator = new __1.FollowCalculator(grammar, new __1.FirstCalculator(grammar));
it(``, function () {
// should.throws(() => {
calculator.getFollowSet(A);
// });
});
});
describe("8. 循环依赖(白银)", function () {
/*
A->B
B->C
C->A
A:C
C:B
B:A
*/
let A = new definition_1.NonTerminal("A");
let B = new definition_1.NonTerminal("B");
let C = new definition_1.NonTerminal("C");
let grammar = new toolkit_1.SimpleGrammar([
[A, [B]],
[B, [C]],
[C, [A]],
]);
let calculator = new __1.FollowCalculator(grammar, new __1.FirstCalculator(grammar));
it(``, function () {
// should.throws(() => {
calculator.getFollowSet(A);
// });
});
});
describe("8. 循环依赖(黄金)", function () {
let A = new definition_1.NonTerminal("A");
let B = new definition_1.NonTerminal("B");
let C = new definition_1.NonTerminal("C");
let D = new definition_1.NonTerminal("D");
let E = new definition_1.NonTerminal("E");
let grammar = new toolkit_1.SimpleGrammar([
[A, [B]],
[B, [C]],
[C, [D]],
[D, [E]],
[E, [A]]
]);
let calculator = new __1.FollowCalculator(grammar, new __1.FirstCalculator(grammar));
it(``, function () {
// should.throws(() => {
calculator.getFollowSet(A);
// });
});
});
describe("8. 循环依赖(Faker!)", function () {
let A = new definition_1.NonTerminal("A");
let B = new definition_1.NonTerminal("B");
let C = new definition_1.NonTerminal("C");
let D = new definition_1.NonTerminal("D");
let E = new definition_1.NonTerminal("E");
let F = new definition_1.NonTerminal("F");
let grammar = new toolkit_1.SimpleGrammar([
[A, [E]],
[B, [A]],
[C, [A], [B]],
[D, [A]],
[E, [D]],
[F, [E]]
]);
let calculator = new __1.FollowCalculator(grammar, new __1.FirstCalculator(grammar));
it(``, function () {
// should.throws(() => {
calculator.getFollowSet(A);
// });
});
});
});
//# sourceMappingURL=follow.test.js.map