Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite4c6400

Browse files
committed
CFG: CYK algorithm now with steps
1 parent394184b commite4c6400

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

‎src/ContextFreeGrammar/ContextFreeGrammar.ts‎

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,11 @@ export class ContextFreeGrammar {
218218
}
219219

220220
publiccyk(string:string):CYKTable;
221-
publiccyk(string:string,step:(table:CYKTable)=>void):CYKTable;
222-
publiccyk(string:string,step?:(table:CYKTable)=>void):CYKTable{
221+
publiccyk(string:string,step:StepCallback<[number,CYKTable]>):CYKTable;
222+
publiccyk(string:string,step?:StepCallback<[number,CYKTable]>):CYKTable{
223223
constcfg=this.cnf();
224224
consttable=newCYKTable(string);
225-
constcallStep=()=>step?.(newCYKTable(table));
225+
constcallStep=(height:number,desc:string)=>step?.([height,newCYKTable(table)],desc);
226226

227227
constgetSequencesFor=(part:string):Array<Array<string>>=>{
228228
constsequences=newArray<Array<string>>();
@@ -238,44 +238,50 @@ export class ContextFreeGrammar {
238238

239239
for(constiofstring){
240240
if(!table.has(i)){
241-
table.set(
242-
i,
243-
newSet<string>(
244-
[...cfg.productions]
245-
.filter(([_,p])=>p.some(s=>s.length===1&&s[0].kind===TokenKind.Terminal&&s[0].identifier===i))
246-
.map(([nt,_])=>nt)
247-
)
241+
constproducers=newSet<string>(
242+
[...cfg.productions]
243+
.filter(([_,p])=>p.some(s=>s.length===1&&s[0].kind===TokenKind.Terminal&&s[0].identifier===i))
244+
.map(([nt,_])=>nt)
248245
);
246+
table.set(i,producers);
247+
if(producers.size>0){
248+
callStep(1,`Add non-terminals that directly can produce the string "${i}".`);
249+
}
250+
else{
251+
callStep(1,`No non-terminals can directly produce the string "${i}".`);
252+
}
249253
}
250254
}
251-
callStep();
252255

253256
for(letl=2;l<=string.length;l++){
254257
for(leti=0;i<=string.length-l;i++){
255258
constpart=string.substr(i,l);
256259
if(!table.has(part)){
257260
constseqs=getSequencesFor(part);
258-
table.set(
259-
part,
260-
newSet<string>(
261-
[...cfg.productions]
262-
.filter(([_,p])=>
263-
p.some(s=>
264-
seqs.some(seq=>
265-
seq.every((t,i)=>
266-
i<s.length&&
267-
s[i].kind===TokenKind.NonTerminal&&
268-
s[i].identifier===t
269-
)
261+
constproducers=newSet<string>(
262+
[...cfg.productions]
263+
.filter(([_,p])=>
264+
p.some(s=>
265+
seqs.some(seq=>
266+
seq.every((t,i)=>
267+
i<s.length&&
268+
s[i].kind===TokenKind.NonTerminal&&
269+
s[i].identifier===t
270270
)
271271
)
272272
)
273-
.map(([nt,_])=>nt)
274-
)
273+
)
274+
.map(([nt,_])=>nt)
275275
);
276+
table.set(part,producers);
277+
if(producers.size>0){
278+
callStep(l,`Add productions that can produce the string "${part}".`);
279+
}
280+
else{
281+
callStep(l,`No productions can produce the string "${part}".`);
282+
}
276283
}
277284
}
278-
callStep();
279285
}
280286

281287
returntable;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp