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

Commit724ec16

Browse files
committed
CFG: Term transformation with description of steps
And more steps.
1 parent70db032 commit724ec16

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

‎src/ContextFreeGrammar/ContextFreeGrammar.ts‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -516,41 +516,42 @@ export class ContextFreeGrammar {
516516
}
517517

518518
publicterm():ContextFreeGrammar;
519-
publicterm(step:(cfg:ContextFreeGrammar)=>void):ContextFreeGrammar;
520-
publicterm(step?:(cfg:ContextFreeGrammar)=>void):ContextFreeGrammar{
519+
publicterm(step:StepCallback<ContextFreeGrammar>):ContextFreeGrammar;
520+
publicterm(step?:StepCallback<ContextFreeGrammar>):ContextFreeGrammar{
521521
constproductions=newMap<string,Array<Array<Token>>>([...this.productions].map(([nt,p])=>[nt,p.map(s=>[...s])]));
522522

523523
constterminalProductions=newMap<string,string>([...productions]
524524
.filter(([_,p])=>p.length===1&&p[0].length===1&&p[0][0].kind===TokenKind.Terminal)
525-
.map(([nt,p])=>[p[0][0].identifier,nt])
526-
);
525+
.map(([nt,p])=>[p[0][0].identifier,nt]));
527526

528-
for(const[_,production]of[...productions].sort(([a,_a],[b,_b])=>sortBySymbolButFirst(a,b,this.start))){
529-
letchangeHappened=false;
527+
for(const[nonTerminal,production]of[...productions].sort(([a,_a],[b,_b])=>sortBySymbolButFirst(a,b,this.start))){
530528
for(constsequenceofproduction){
531529
if(sequence.length>=2){
532530
for(leti=0;i<sequence.length;i++){
533531
if(sequence[i].kind===TokenKind.Terminal){
534-
letreplacement=terminalProductions.get(sequence[i].identifier);
532+
constterminal=sequence[i].identifier;
533+
letisNew=false;
534+
letreplacement=terminalProductions.get(terminal);
535535
if(replacement==null){
536-
replacement=countUpLetterSymbol("T",s=>!productions.has(s));
537-
terminalProductions.set(sequence[i].identifier,replacement);
538-
productions.set(replacement,newArray<Array<Token>>(newArray<Token>(Token.terminal(sequence[i].identifier))));
536+
replacement=countUpLetterSymbol(nonTerminal,s=>!productions.has(s));
537+
terminalProductions.set(terminal,replacement);
538+
productions.set(replacement,newArray<Array<Token>>(newArray<Token>(Token.terminal(terminal))));
539+
isNew=true;
539540
}
540541
sequence.splice(i,1,Token.nonTerminal(replacement));
541-
changeHappened=true;
542+
step?.(
543+
newContextFreeGrammar(
544+
productions.keys(),
545+
this.terminals,
546+
productions,
547+
this.start,
548+
),
549+
`Replaced a terminal "${terminal}" in production${nonTerminal} with the${isNew ?"new " :""}non-terminal${replacement}.`
550+
);
542551
}
543552
}
544553
}
545554
}
546-
if(changeHappened){
547-
step?.(newContextFreeGrammar(
548-
productions.keys(),
549-
this.terminals,
550-
productions,
551-
this.start,
552-
));
553-
}
554555
}
555556

556557
returnnewContextFreeGrammar(

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp