parglare icon indicating copy to clipboard operation
parglare copied to clipboard

Error for named matches in an imported grammar with default action

Open hosford42 opened this issue 4 years ago • 2 comments

  • parglare version: 0.12.0
  • Python version: 3.6.9
  • Operating System: Ubuntu 18.04 LTS

Description

I have a grammar file I am importing from another one. I am using the default actions for all rules. I want to use named matches in the rules of the imported grammar file. When I add named matches to any rule in the imported file, I get an exception when parsing. Since the grammar worked as expected before I added the named matches, I expected it to work after adding them, as well.

What I Did

To reproduce the error, first, create two files, one named "main.pg" and one named "imported.pg". The contents of the files are as follows:

main.pg:

import "imported.pg";

ROOT: imported.ROOT;

imported.pg:

ROOT: A | B;
A: 'a';
B: b='b';

Now, if you load "imported.pg" into a grammar and use it directly, everything works as expected:

>>> import parglare
>>> grammar = parglare.Grammar.from_file('imported.pg')
>>> parser = parglare.Parser(grammar)
>>> parser.parse('a')
'a'
>>> parser.parse('b').__dict__
{'_pg_end_position': 1, '_pg_start_position': 0, 'b': 'b'}
>>>

However, if you load "main.pg", you get a surprise:

>>> import parglare
>>> grammar = parglare.Grammar.from_file('main.pg')
>>> parser = parglare.Parser(grammar)
>>> parser.parse('a')
'a'
>>> parser.parse('b').__dict__
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-6-495fa134a825>", line 1, in <module>
    parser.parse('b')
  File "/usr/local/lib/python3.6/dist-packages/parglare/parser.py", line 333, in parse
    result = self._call_reduce_action(context, subresults)
  File "/usr/local/lib/python3.6/dist-packages/parglare/parser.py", line 714, in _call_reduce_action
    result = sem_action(context, subresults, **assgn_results)
  File "/usr/local/lib/python3.6/dist-packages/parglare/actions.py", line 167, in obj
    cls = grammar.classes[rule_name]
KeyError: 'imported.B'
>>>

hosford42 avatar Sep 03 '20 20:09 hosford42

Thanks for the report. I've verified it is a bug. It seem it gets triggered only when assignment is used, i.e. the b='b' in 'B' rule seems to be the culprit.

igordejanovic avatar Sep 04 '20 08:09 igordejanovic

Yes, this matches what I observed as well.

hosford42 avatar Sep 04 '20 14:09 hosford42