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

Error for named matches in an imported grammar with default action #127

Open
hosford42 opened this issue Sep 3, 2020 · 2 comments
Open
Labels

Comments

@hosford42
Copy link

  • 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'
>>>
@igordejanovic
Copy link
Owner

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.

@hosford42
Copy link
Author

Yes, this matches what I observed as well.

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

No branches or pull requests

2 participants