-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.js
50 lines (42 loc) · 1.25 KB
/
item.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const { production } = require('./cfg');
function item(production, intersectedStates) {
const i = {
production,
intersectedStates,
};
const hash = JSON.stringify(i);
return { ...i, hash };
}
function getNextUnprocessedSymbol(i) {
return i.production.rhs[i.intersectedStates.length - 1];
}
function getLastIntersectedState(i) {
return i.intersectedStates[i.intersectedStates.length - 1];
}
function getFirstIntersectedState(i) {
return i.intersectedStates[0];
}
function isCompletelyIntersected(i) {
return i.intersectedStates.length - 1 === i.production.rhs.length;
}
function spansAcceptingPath(i, fsa) {
return fsa.initialStates.has(getFirstIntersectedState(i)) && fsa.acceptingStates.has(getLastIntersectedState(i));
}
function toProduction(i, cfg) {
const lhs = `${i.production.lhs}_${getFirstIntersectedState(i)},${getLastIntersectedState(i)}`;
const rhs = i.production.rhs.map(
(symbol, index) => cfg.terminals.has(symbol)
? symbol
: `${symbol}_${i.intersectedStates[index]},${i.intersectedStates[index + 1]}`
);
return production(lhs, rhs);
}
module.exports = {
item,
getNextUnprocessedSymbol,
getLastIntersectedState,
getFirstIntersectedState,
isCompletelyIntersected,
spansAcceptingPath,
toProduction
};