Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding and removing rules from a grammar #3

Open
jarble opened this issue Feb 15, 2016 · 1 comment
Open

Adding and removing rules from a grammar #3

jarble opened this issue Feb 15, 2016 · 1 comment

Comments

@jarble
Copy link

jarble commented Feb 15, 2016

I'm developing an application that requires the grammar rules to be modified after the grammar is created. After creating a grammar like this one, would it be possible to add or modify individual rules in the grammar?

var grammar = new tinynlp.Grammar([
   'R -> N',
   'S -> S add_sub M | M',
   'M -> M mul_div T | T',
   'N -> S lt_gt S | S',
   'T -> num | ( S )',
]);

Would I be able to add a new rule to the grammar using grammar.add('A -> B') or something similar?

@jarble jarble changed the title Adding and removing rules from a parse tree Adding and removing rules from a grammar Feb 15, 2016
@jarble jarble changed the title Adding and removing rules from a grammar Adding and modifying rules in a grammar Feb 15, 2016
@jarble jarble changed the title Adding and modifying rules in a grammar Adding and removing rules from a grammar Feb 15, 2016
@lagodiuk
Copy link
Owner

@jarble Excuse me for a bit delayed response.

Currently, Grammar object is implemented in such way, that it encapsulates a data structure with all grammar productions - indexed by nonterminal symbols (the left hand side of grammar rules).
For example, grammar productions S -> S add_sub M | M and N -> S lt_gt S | S are internally represented in the following way:

{
  'S': [['S', 'add_sub', 'M'], ['M']],
  'N': [['S', 'lt_gt', 'S'], ['S']],
}

Please, check the source code of the constructor of Grammar objects for more implementation details: https://github.com/lagodiuk/earley-parser-js/blob/master/earley-oop.js#L4-L23

Given code can be reused for parsing of the new rules, and addition of them to the dictionary with already existing rules.

I will try to find a time within nearest days - to implement requested feature. However, a pull request with this feature will be appreciated as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants