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

Commit6b49f65

Browse files
committed
CFG: Always terminating when building parse trees
Creates a CYK table to check if the string is in the language before creating the parse tree.
1 parent18fbcde commit6b49f65

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

‎src/ContextFreeGrammar/ContextFreeGrammar.ts‎

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,48 +42,63 @@ export class ContextFreeGrammar {
4242
}
4343
}
4444

45+
//#region Computations
4546
publicparse(string:string):ParseTree|null{
46-
if(this.start!=null){
47-
constqueue=newArray<[Array<Token>,Array<Token>,string]>([newArray<Token>(),[Token.nonTerminal(this.start)],string]);
48-
while(queue.length>0){
49-
const[tree,[token, ...rest],string]=queue.shift();
50-
if(token.kind===TokenKind.NonTerminal){
51-
for(constsequenceofthis.productions.get(token.identifier)){
52-
queue.push([
53-
[...tree,token],
54-
[...sequence, ...rest],
55-
string
56-
]);
57-
}
58-
}
59-
elseif(token.kind===TokenKind.Terminal){
60-
if(string.startsWith(token.identifier)){
61-
if(string.length===token.identifier.length&&rest.length===0){
62-
returnthis.constructTree([...tree,token]);
63-
}
64-
elseif(rest.length>0){
65-
queue.push([
66-
[...tree,token],
67-
rest,
68-
string.substring(token.identifier.length)
69-
]);
70-
}
71-
}
47+
if(this.start==null){
48+
returnnull;
49+
}
50+
if(string.length>0){
51+
constcyk=this.cyk(string);
52+
if(cyk==null||!cyk.has(string)||!cyk.get(string).has(this.start)){
53+
returnnull;
54+
}
55+
}
56+
else{
57+
if(!this.nullableNonTerminals().some(t=>t===this.start)){
58+
returnnull;
59+
}
60+
}
61+
62+
constqueue=newArray<[Array<Token>,Array<Token>,string]>([newArray<Token>(),[Token.nonTerminal(this.start)],string]);
63+
while(queue.length>0){
64+
const[tree,[token, ...rest],string]=queue.shift();
65+
if(token.kind===TokenKind.NonTerminal){
66+
for(constsequenceofthis.productions.get(token.identifier)){
67+
queue.push([
68+
[...tree,token],
69+
[...sequence, ...rest],
70+
string
71+
]);
7272
}
73-
elseif(token.kind===TokenKind.Empty){
74-
if(string.length===0&&rest.length===0){
73+
}
74+
elseif(token.kind===TokenKind.Terminal){
75+
if(string.startsWith(token.identifier)){
76+
if(string.length===token.identifier.length&&rest.length===0){
7577
returnthis.constructTree([...tree,token]);
7678
}
7779
elseif(rest.length>0){
7880
queue.push([
7981
[...tree,token],
8082
rest,
81-
string
83+
string.substring(token.identifier.length)
8284
]);
8385
}
8486
}
8587
}
88+
elseif(token.kind===TokenKind.Empty){
89+
if(string.length===0&&rest.length===0){
90+
returnthis.constructTree([...tree,token]);
91+
}
92+
elseif(rest.length>0){
93+
queue.push([
94+
[...tree,token],
95+
rest,
96+
string
97+
]);
98+
}
99+
}
86100
}
101+
87102
returnnull;
88103
}
89104
privateconstructTree(tokens:ReadonlyArray<Token>):ParseTree;
@@ -122,7 +137,6 @@ export class ContextFreeGrammar {
122137
thrownewError("Failed to construct the parse tree.");
123138
}
124139

125-
//#region Computations
126140
publicgeneratingSymbols():Array<Token>;
127141
publicgeneratingSymbols(step:(symbols:Array<Token>)=>void):Array<Token>;
128142
publicgeneratingSymbols(step?:(symbols:Array<Token>)=>void):Array<Token>{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp