From 48ab262bc5162ccf137c38004b27e562c0ca00e9 Mon Sep 17 00:00:00 2001 From: imenfaith Date: Mon, 23 Apr 2018 23:17:08 +0100 Subject: [PATCH 1/4] removed the useless filter method the filter method here does not serve any purpose. --- src/main.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main.js b/src/main.js index 9ba3f45..ccd1d4a 100644 --- a/src/main.js +++ b/src/main.js @@ -19,8 +19,6 @@ var firstFollow = (function() { // remove spaces, filter empty lines, and break the rules into (production, derivations) return grammar.split(/;|\n/).map(function(line) { return line.trim(); - }).filter(function(line) { - return line; }).map(function(rule) { var data = rule.split(/->|→/); if (data.length < 2 || !data[0]) { From 1fb3183e1520575e5124a0fd56c0645dafa1c87d Mon Sep 17 00:00:00 2001 From: imenfaith Date: Mon, 23 Apr 2018 23:24:29 +0100 Subject: [PATCH 2/4] unused property removed the unused property grammar --- src/main.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.js b/src/main.js index ccd1d4a..84a118d 100644 --- a/src/main.js +++ b/src/main.js @@ -41,7 +41,6 @@ var firstFollow = (function() { var Grammar = function(str) { var that = this; - this.grammar = str; this.productionList = GetProductions(str); if (!this.productionList.length) { throw 'Empty grammar'; From b93c8ad6c8e105941b87571703d3dedd808c0d05 Mon Sep 17 00:00:00 2001 From: imenfaith Date: Tue, 24 Apr 2018 00:06:08 +0100 Subject: [PATCH 3/4] removed the unaccessed if statement the if statement is never accessed since all symbols are nonTerminals, so the condition cannot be true. --- src/main.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main.js b/src/main.js index 84a118d..5c782fc 100644 --- a/src/main.js +++ b/src/main.js @@ -122,10 +122,8 @@ var firstFollow = (function() { var symbol = prod.symbol; var derivationList = prod.derivation; - if (!that.isNonTerminal(symbol)) { - that.addToFirstSet(symbol, symbol); - } - else { + + // set to true when the first derivation has epsilon in its first set, // it remains true as long as the subsequent derivations also contain '' in their first sets. var addEpsilon = false; @@ -185,7 +183,7 @@ var firstFollow = (function() { if (addEpsilon) { that.addToFirstSet(symbol, ''); } - } + }); // we stop iterating and consider that the sets are complete when after a iteration nothing was added to them. } while(this._changed); From 6777780c310b0928a896654e31dec2b397c851f6 Mon Sep 17 00:00:00 2001 From: imenfaith Date: Tue, 24 Apr 2018 00:18:05 +0100 Subject: [PATCH 4/4] removed the || {} the firstSet object is already created for all Terminals and nonTerminals: prod.derivation.forEach(function(d) { if (d === '') { return; } else if (that.isNonTerminal(d){ that.firstSet[d] = {}; that.followSet[d] = {}; } else { that.firstSet[d] = {}; that.firstSet[d][d] = 1; } }); --- src/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.js b/src/main.js index 5c782fc..bb2cb6d 100644 --- a/src/main.js +++ b/src/main.js @@ -133,7 +133,7 @@ var firstFollow = (function() { } else { if (index === 0 || addEpsilon) { - var fSet = that.firstSet[derivation] || {}; + var fSet = that.firstSet[derivation] ; // only add epsilon to firt(X) if epsilon is in all k for X -> Y1, Y2...Yk addEpsilon = '' in fSet; @@ -183,7 +183,7 @@ var firstFollow = (function() { if (addEpsilon) { that.addToFirstSet(symbol, ''); } - + }); // we stop iterating and consider that the sets are complete when after a iteration nothing was added to them. } while(this._changed);