jscc-parser
Version:
A LALR(1) Parser Generator for JavaScript written in JavaScript.
404 lines (371 loc) • 20.2 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: lexdfa.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: lexdfa.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*
* Universal module definition for lexdfa.
*/
(function(root, factory) {
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
define(['require', './global', './log/log', './enums/EDGE', './classes/Dfa'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require);
} else {
root.jscclexdfa = factory(function(mod) {
return root["jscc" + mod.split("/").pop()];
});
}
}(this,
/**
* @param {reqParameter} require
* @param {...*} others
* @returns {jscc.lexdfa}
*/
function(require, others) {
//>>excludeStart("closure", pragmas.closure);
var jscc = {};
var has = /** @type {hasObject} */ (require("./localHas"));
//>>excludeEnd("closure");
var log, global = /** @type {jscc.global} */ (require("./global")),
EDGE = require("./enums/EDGE"),
Dfa = /** @type {function(new:jscc.classes.Dfa, ?DfaOptions=)} */ (require("./classes/Dfa"));
/**
* @suppress {uselessCode}
*/
(function() {
if (has("node")) {
log = /** @type {jscc.log} */ (require("./log/logNode"));
} else {
log = /** @type {jscc.log} */ (require("./log/log"));
}
})();
/**
* @constructor
*/
jscc.lexdfa = function() {
};
/**
* DFA-related functions.
* @module {jscc.lexdfa} jscc/lexdfa
* @requires module:jscc/global
* @requires module:jscc/log/log
*/
jscc.lexdfa.prototype = {
/**
* Creates a new {@link jscc.global.Dfa} object and
* adds it to the provided array.
* @param {Array<!jscc.classes.Dfa>} where - The array to which to add the
* new Dfa object.
* @returns {number} The prior length of the provided array.
* @memberof jscc.lexdfa
*/
create_dfa: function(where) {
var dfa = new Dfa({
line: new Array(global.MAX_CHAR),
accept: -1,
nfa_set: [],
done: false,
group: -1
});
where.push(dfa);
return where.length - 1;
},
/**
* Determines whether the dfa_states array has a
* Dfa object whose {@link jscc.global.Dfa#nfa_set}
* property contains the same values as those in the
* items parameter.
* @param {Array<!jscc.classes.Dfa>} dfa_states - The array of Dfa
* objects to check.
* @param {Array<number>} items - The set to match.
* @returns {number} The index in dfa_states of the first
* matching item, or -1 if no match exists.
* @memberof jscc.lexdfa
*/
same_nfa_items: function(dfa_states, items) {
var i, j;
for (i = 0; i < dfa_states.length; i++) {
if (dfa_states[i].nfa_set.length == items.length) {
for (j = 0; j < dfa_states[i].nfa_set.length; j++) {
if (dfa_states[i].nfa_set[j] != items[j]) {
break;
}
}
if (j == dfa_states[i].nfa_set.length) {
return i;
}
}
}
return -1;
},
/**
* Determines the next Dfa object in the provided array
* whose {@link jscc.classes.Dfa#done} value is false.
* @param {Array<!jscc.classes.Dfa>} dfa_states - The array to check.
* @returns {number} The index of the first array element for
* which done is false, or -1 if no such element exists.
* @memberof jscc.lexdfa
*/
get_undone_dfa: function(dfa_states) {
for (var i = 0; i < dfa_states.length; i++) {
if (!dfa_states[i].done) {
return i;
}
}
return -1;
},
/**
* Performs a move operation on a given input character from a
* set of NFA states.
* @param {Array<!number>} state_set - The set of epsilon-closure
* states on which base the move should be performed.
* @param {Array<!jscc.classes.Nfa>} machine - The NFA state machine.
* @param {number} ch - A character code to be moved on.
* @returns {Array<!number>} If there is a possible move, a new
* set of NFA-states is returned, else the returned array has a
* length of 0.
* @author Jan Max Meyer
* @memberof jscc.lexdfa
*/
move: function(state_set, machine, ch) {
var hits = [];
var tos = -1;
try {
do {
tos = state_set.pop();
if (machine[tos].edge == EDGE.CHAR) {
if (machine[tos].ccl.get(ch)) {
hits.push(machine[tos].follow);
}
}
} while (state_set.length > 0);
} catch (e) {
log.error("\n state_set= " + state_set + " machine= " + machine + " ch= " + ch);
throw e;
}
return hits;
},
/**
* Performs an epsilon closure from a set of NFA states.
* @param {Array<!number>} state_set - The set of states on which
* base the closure is started. The whole epsilon closure will
* be appended to this parameter, so this parameter acts as
* input/output value.
* @param {Array<!jscc.classes.Nfa>} machine - The NFA state machine.
* @returns {Array<!number>} An array of accepting states, if
* available.
* @author Jan Max Meyer
* @memberof jscc.lexdfa
*/
epsilon_closure: function(state_set, machine) {
var stack = [];
var accept = [];
var tos = -1;
for (var i = 0; i < state_set.length; i++) {
stack.push(state_set[i]);
}
do {
tos = stack.pop();
if (machine[tos].accept >= 0) {
accept.push(machine[tos].accept);
}
if (machine[tos].edge == EDGE.EPSILON) {
if (machine[tos].follow > -1) {
for (var i = 0; i < state_set.length; i++) {
if (state_set[i] == machine[tos].follow) {
break;
}
}
if (i == state_set.length) {
state_set.push(machine[tos].follow);
stack.push(machine[tos].follow);
}
}
if (machine[tos].follow2 > -1) {
for (var i = 0; i < state_set.length; i++) {
if (state_set[i] == machine[tos].follow2) {
break;
}
}
if (i == state_set.length) {
state_set.push(machine[tos].follow2);
stack.push(machine[tos].follow2);
}
}
}
} while (stack.length > 0);
return accept.sort();
},
/**
* Constructs a deterministic finite automata (DFA) from a
* nondeterministic finite automata, by using the subset
* construction algorithm.
* @param {!Array<!jscc.classes.Nfa>} nfa_states - The NFA-state machine
* on which base the DFA will be constructed.
* @returns {!Array<!jscc.classes.Dfa>} An array of DFA-objects forming the
* new DFA-state machine. This machine is not minimized here.
* @author Jan Max Meyer
* @memberof jscc.lexdfa
*/
create_subset: function(nfa_states) {
var dfa_states = [];
var stack = [0];
var current = this.create_dfa(dfa_states);
var trans;
var next = -1;
var lowest_weight;
if (nfa_states.length == 0) {
return dfa_states;
}
this.epsilon_closure(stack, nfa_states);
dfa_states[current].nfa_set = dfa_states[current].nfa_set.concat(stack);
while ((current = this.get_undone_dfa(dfa_states)) > -1) {
dfa_states[current].done = true;
lowest_weight = -1;
for (var i = 0; i < dfa_states[current].nfa_set.length; i++) {
if (nfa_states[dfa_states[current].nfa_set[i]].accept > -1
&& nfa_states[dfa_states[current].nfa_set[i]].weight < lowest_weight
|| lowest_weight == -1) {
dfa_states[current].accept = nfa_states[dfa_states[current].nfa_set[i]].accept;
lowest_weight = nfa_states[dfa_states[current].nfa_set[i]].weight;
}
}
for (var i = global.MIN_CHAR; i < global.MAX_CHAR; i++) {
trans = [].concat(dfa_states[current].nfa_set);
trans = this.move(trans, nfa_states, i);
if (trans.length > 0) {
this.epsilon_closure(trans, nfa_states);
}
if (trans.length == 0) {
next = -1;
} else if ((next = this.same_nfa_items(dfa_states, trans)) == -1) {
next = this.create_dfa(dfa_states);
dfa_states[next].nfa_set = trans;
}
dfa_states[current].line[i] = next;
}
}
return dfa_states;
},
/**
* Minimizes a DFA, by grouping equivalent states together.
* These groups form the new, minimized dfa-states.
* @param {!Array<!jscc.classes.Dfa>} dfa_states - The DFA-state machine on
* which base the minimized DFA is constructed.
* @returns {!Array<!jscc.classes.Dfa>} An array of DFA-objects forming the
* minimized DFA-state machine.
* @author Jan Max Meyer
* @memberof jscc.lexdfa
*/
minimize_dfa: function(dfa_states) {
var groups = [[]];
var accept_groups = [];
var min_dfa_states = [];
var old_cnt = 0;
var cnt = 0;
var new_group;
var i, j, k;
if (dfa_states.length == 0) {
return min_dfa_states;
}
// Forming a general starting state:
// Accepting and non-accepting states are pushed in separate groups first
for (i = 0; i < dfa_states.length; i++) {
if (dfa_states[i].accept > -1) {
for (j = 0; j < accept_groups.length; j++) {
if (accept_groups[j] == dfa_states[i].accept) {
break;
}
}
if (j == accept_groups.length) {
accept_groups.push(dfa_states[i].accept);
groups.push([]);
}
groups[j + 1].push(i);
dfa_states[i].group = j + 1;
} else {
groups[0].push(i);
dfa_states[i].group = 0;
}
}
// Now the minimization is performed on base of these default groups
do {
old_cnt = cnt;
for (i = 0; i < groups.length; i++) {
new_group = [];
if (groups[i].length > 0) {
for (j = 1; j < groups[i].length; j++) {
for (k = global.MIN_CHAR; k < global.MAX_CHAR; k++) {
// This verifies the equality of the first state
// in this group with its successors
var groupZeroLineK = dfa_states[groups[i][0]].line[k];
var groupJLineK = dfa_states[groups[i][j]].line[k];
if (groupZeroLineK != groupJLineK &&
(groupZeroLineK == -1 ||
groupJLineK == -1) ||
(groupZeroLineK > -1 &&
groupJLineK > -1 &&
dfa_states[groupZeroLineK].group
!= dfa_states[groupJLineK].group)) {
// If this item does not match, put it to a new group
dfa_states[groups[i][j]].group = groups.length;
new_group = new_group.concat(groups[i].splice(j, 1));
j--;
break;
}
}
}
}
if (new_group.length > 0) {
groups[groups.length] = [];
groups[groups.length - 1] = groups[groups.length - 1].concat(new_group);
cnt += new_group.length;
}
}
} while (old_cnt != cnt);
// Updating the dfa-state transitions; each group forms a new state.
for (i = 0; i < dfa_states.length; i++) {
for (j = global.MIN_CHAR; j < global.MAX_CHAR; j++) {
if (dfa_states[i].line[j] > -1) {
dfa_states[i].line[j] = dfa_states[dfa_states[i].line[j]].group;
}
}
}
for (i = 0; i < groups.length; i++) {
min_dfa_states.push(dfa_states[groups[i][0]]);
}
return min_dfa_states;
}
};
return new jscc.lexdfa();
}));
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-jscc.html">jscc</a></li><li><a href="module-jscc_bitset_BitSet32.html">jscc/bitset/BitSet32</a></li><li><a href="module-jscc_bitset_BitSetBool.html">jscc/bitset/BitSetBool</a></li><li><a href="module-jscc_bitset_BitSetJava.html">jscc/bitset/BitSetJava</a></li><li><a href="module-jscc_classes_Dfa.html">jscc/classes/Dfa</a></li><li><a href="module-jscc_classes_Item.html">jscc/classes/Item</a></li><li><a href="module-jscc_classes_Nfa.html">jscc/classes/Nfa</a></li><li><a href="module-jscc_classes_Param.html">jscc/classes/Param</a></li><li><a href="module-jscc_classes_Production.html">jscc/classes/Production</a></li><li><a href="module-jscc_classes_State.html">jscc/classes/State</a></li><li><a href="module-jscc_classes_Symbol.html">jscc/classes/Symbol</a></li><li><a href="module-jscc_classes_TableEntry.html">jscc/classes/TableEntry</a></li><li><a href="module-jscc_enums_ASSOC.html">jscc/enums/ASSOC</a></li><li><a href="module-jscc_enums_EDGE.html">jscc/enums/EDGE</a></li><li><a href="module-jscc_enums_EXEC.html">jscc/enums/EXEC</a></li><li><a href="module-jscc_enums_LOG_LEVEL.html">jscc/enums/LOG_LEVEL</a></li><li><a href="module-jscc_enums_MODE_GEN.html">jscc/enums/MODE_GEN</a></li><li><a href="module-jscc_enums_SPECIAL.html">jscc/enums/SPECIAL</a></li><li><a href="module-jscc_enums_SYM.html">jscc/enums/SYM</a></li><li><a href="module-jscc_first.html">jscc/first</a></li><li><a href="module-jscc_global.html">jscc/global</a></li><li><a href="module-jscc_integrity.html">jscc/integrity</a></li><li><a href="module-jscc_io_io.html">jscc/io/io</a></li><li><a href="module-jscc_lexdbg.html">jscc/lexdbg</a></li><li><a href="module-jscc_lexdfa.html">jscc/lexdfa</a></li><li><a href="module-jscc_log_log.html">jscc/log/log</a></li><li><a href="module-jscc_parse.html">jscc/parse</a></li><li><a href="module-jscc_printtab.html">jscc/printtab</a></li><li><a href="module-jscc_regex.html">jscc/regex</a></li><li><a href="module-jscc_util.html">jscc/util</a></li></ul><h3>Classes</h3><ul><li><a href="-_anonymous_-jscc.BitSet32.html">BitSet32</a></li><li><a href="-_anonymous_-jscc.BitSetBool.html">BitSetBool</a></li><li><a href="-_anonymous_-jscc.BitSetJava.html">BitSetJava</a></li><li><a href="-_anonymous_-jscc.classes.Dfa.html">classes.Dfa</a></li><li><a href="-_anonymous_-jscc.first.html">first</a></li><li><a href="-_anonymous_-jscc.global.html">global</a></li><li><a href="-_anonymous_-jscc.integrity.html">integrity</a></li><li><a href="-_anonymous_-jscc.ioBrowser.html">ioBrowser</a></li><li><a href="-_anonymous_-jscc.ioNashorn.html">ioNashorn</a></li><li><a href="-_anonymous_-jscc.ioNode.html">ioNode</a></li><li><a href="-_anonymous_-jscc.ioRhino.html">ioRhino</a></li><li><a href="-_anonymous_-jscc.lexdbg.html">lexdbg</a></li><li><a href="-_anonymous_-jscc.lexdfa.html">lexdfa</a></li><li><a href="-_anonymous_-jscc.logBrowser.html">logBrowser</a></li><li><a href="-_anonymous_-jscc.logJava.html">logJava</a></li><li><a href="-_anonymous_-jscc.logNode.html">logNode</a></li><li><a href="-_anonymous_-jscc.printtab.html">printtab</a></li><li><a href="-_anonymous_-jscc.util.html">util</a></li><li><a href="jscc_debug.html">jscc/debug</a></li><li><a href="jscc_nfaStates.html">jscc/nfaStates</a></li><li><a href="jscc_tabgen.html">jscc/tabgen</a></li><li><a href="%257Bjscc.classes%257D.jscc.classes.State.html">jscc.classes.State</a></li></ul><h3>Namespaces</h3><ul><li><a href="jscc.debug.html">debug</a></li></ul><h3>Interfaces</h3><ul><li><a href="jscc.bitset.html">bitset</a></li><li><a href="jscc.io.html">io</a></li><li><a href="jscc.log.html">log</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Aug 01 2016 13:35:55 GMT-0500 (Central Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>