Search code examples
javascriptjqueryparsingcontext-free-grammarearley-parser

how to identify the next possible node from grammar using tokenStream?


I am creating a textarea which have intellisense like most of the IDEs. My approach is to use earley parser algorithm.

I am using the early-parser-js library.

Below is the grammer:

S -> NP VP
VP -> VP PP | V NP | V
PP -> P NP
NP -> Det N | N | Pn | Det A N | A NP
A -> Adv A | A A
Adv -> too | very | quite
Pn -> she | he
A -> fresh | tasty | silver
N -> fish | fork | apple
V -> eats 
Det -> a | an | the
P -> with

Now, If I write "she" in textarea, my code should suggest next possible node like "eats", "fish", "fork" etc.


Solution

  • I acheived this using the below code

     var results = [];
            var lastColumn = this.chart.length -1 ;
            for(var j in this.chart[lastColumn])
            {
             if(!this.chart[lastColumn][j].expectedNonTerminal(grammar))
             {
            results.push(this.chart[lastColumn][j].rhs.toString());
             }
            }
    
            return results;