lark-parser / lark

Lark is a parsing toolkit for Python, built with a focus on ergonomics, performance and modularity.
MIT License
4.81k stars 409 forks source link

I can't create a parser from my grammar #724

Closed joepalermo closed 3 years ago

joepalermo commented 3 years ago

What is your question?

I can't create a parser from my grammar. I would appreciate any help debugging my grammar.

Here's the traceback:

Traceback (most recent call last):
  File "/Users/jpalermo/.conda/envs/mm_for_gpt/lib/python3.7/site-packages/lark/parsers/lalr_parser.py", line 60, in get_action
    return states[state][token.type]
KeyError: '_RPAR'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jpalermo/workspace/math/hol-rainbow/metamath_preprocessing/parse_expressions.py", line 36, in <module>
    parser = Lark(lark_grammar, parser='lalr', debug=True)  # possible values: ('earley', 'lalr', 'cyk', None)
  File "/Users/jpalermo/.conda/envs/mm_for_gpt/lib/python3.7/site-packages/lark/lark.py", line 270, in __init__
    self.grammar = load_grammar(grammar, self.source, re_module)
  File "/Users/jpalermo/.conda/envs/mm_for_gpt/lib/python3.7/site-packages/lark/load_grammar.py", line 985, in load_grammar
    return GrammarLoader(re_).load_grammar(grammar, source)
  File "/Users/jpalermo/.conda/envs/mm_for_gpt/lib/python3.7/site-packages/lark/load_grammar.py", line 824, in load_grammar
    tree = self.canonize_tree.transform( self.parser.parse(grammar_text+'\n') )
  File "/Users/jpalermo/.conda/envs/mm_for_gpt/lib/python3.7/site-packages/lark/parser_frontends.py", line 108, in parse
    return self._parse(token_stream, start)
  File "/Users/jpalermo/.conda/envs/mm_for_gpt/lib/python3.7/site-packages/lark/parser_frontends.py", line 58, in _parse
    return self.parser.parse(input, start, *args)
  File "/Users/jpalermo/.conda/envs/mm_for_gpt/lib/python3.7/site-packages/lark/parsers/lalr_parser.py", line 36, in parse
    return self.parser.parse(*args)
  File "/Users/jpalermo/.conda/envs/mm_for_gpt/lib/python3.7/site-packages/lark/parsers/lalr_parser.py", line 89, in parse
    action, arg = get_action(token)
  File "/Users/jpalermo/.conda/envs/mm_for_gpt/lib/python3.7/site-packages/lark/parsers/lalr_parser.py", line 67, in get_action
    raise UnexpectedToken(token, expected, state=state, puppet=puppet)
lark.exceptions.UnexpectedToken: Unexpected token Token('_RPAR', ')') at line 2, column 106.
Expected one of: 
    * _OR
    * _NL

I'm reading my grammar in from a text file and then invoking Lark as follows:

parser = Lark(lark_grammar, parser='lalr', debug=True)

Here's the grammar (note that it has about 1500 production rules):

lark_grammar.txt

MegaIng commented 3 years ago

"/\" is not a valid string literal. It has an escaped ", meaning the actual string literals continues a bit. Somehow your grammar is designed in a way that it doesn't break the lexer, but only the parser. A syntax highlighter would help there.

Also, these kinds of questions are better over at the gitter chat.

erezsh commented 3 years ago

As @MegaIng said, you should use "/\\" instead.

MegaIng commented 3 years ago

@erezsh that is what he is using. I assume you mean "/\\"

erezsh commented 3 years ago

Whoops, I forgot to quote it.

joepalermo commented 3 years ago

Thanks. That resolved it.

joepalermo commented 3 years ago

@MegaIng I can't access that link

erezsh commented 3 years ago

https://gitter.im/lark-parser/Lobby