drmfinlay / pyjsgf

JSpeech Grammar Format (JSGF) compiler, matcher and parser package for Python.
MIT License
50 stars 22 forks source link

parsing error for rules with [x] followed by x #41

Open kercos opened 9 months ago

kercos commented 9 months ago

Try the following script:

from jsgf import parser, Grammar
rule1 = parser.parse_rule_string('public <test1> = [la] lady;')
rule2 = parser.parse_rule_string('public <test2> = [la] bady;')
grammar = Grammar()
grammar.add_rule(rule1)
grammar.add_rule(rule2)
print(grammar.compile())
matching1 = grammar.find_matching_rules('lady')
matching2 = grammar.find_matching_rules('bady')
print('Matching "lady":', matching1)
print('Matching "bady":', matching2)

The output will show the following:

#JSGF V1.0;
grammar default;
public <test1> = [la] lady;
public <test2> = [la] bady;

Matching "lady": []
Matching "bady": [Rule(name='test2', visible=True, expansion=Sequence(OptionalGrouping(Literal('la')), Literal('bady')))]

Notice that the string "lady" is not matched by the grammar, even though rule test1 expands to it.

kercos commented 1 month ago

@drmfinlay I was wondering if this issue has been considered...

drmfinlay commented 1 month ago

Hello @kercos,

My apologies, I have been neglecting this library in recent years and had not investigated this issue until just now.

This error looks serious, unfortunately. From what I can tell, there is no look-ahead operation for giving precedence to (higher-level) required expansions that match or match partially preceding (lower-level) optionals.

I'll try to fix this, but it may take some time.