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

49 views Asked by At

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.

1

There are 1 answers

0
A.Mujeeb On BEST ANSWER

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;