lark-parser / lark

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

in parse raise UnexpectedEOF(expected_terminals, state=frozenset(i.s for i in to_scan)) lark.exceptions.UnexpectedEOF: Unexpected end-of-input. Expected one of: * NEWLINE #1253

Closed Keenonthedaywalker closed 1 year ago

Keenonthedaywalker commented 1 year ago

Hello, I'm new to lark and parsers in general, so I don't really know how to ask this question

I'm trying to parse a file that is filled with names, and the code I have should be working(Thanks to a kind soul on Stackoverflow), but whenever I run the code, it gives me this error:
` in parse raise UnexpectedEOF(expected_terminals, state=frozenset(i.s for i in to_scan)) lark.exceptions.UnexpectedEOF: Unexpected end-of-input. Expected one of:

Here is the code that I am using


from pathlib import Path
from pprint import pprint
from collections import defaultdict

parser = lark.Lark(r"""
start: (term)*
term: key "=" value "\n"
key: CNAME | SIGNED_NUMBER
value: CNAME | SIGNED_NUMBER | ESCAPED_STRING | map
map: "{" (term)* "}"
%import common.CNAME
%import common.ESCAPED_STRING
%import common.SIGNED_NUMBER
%import common.WS
%ignore WS
%ignore /#.*/
""")

class TreeTransformer(lark.Transformer):
  def start(self, items):
       return dict(items)

   def term(self, items):
       return (items[0], items[1])

   def CNAME(self, item):
       return item.value

   def SIGNED_NUMBER(self, item):
       return int(item.value)

   def ESCAPED_STRING(self, item):
       return item.value[1:-1]

   def map(self, items):
       return dict(items)

   def key(self, item):
       return item[0]

   def value(self, item):
       return item[0]

data = Path("Essos.txt").read_text()
tree = parser.parse(data)
res = TreeTransformer().transform(tree)
pprint(res)

names_by_culture = defaultdict(list)
for info in res.values():
    names_by_culture[info["culture"]].append(info["name"])
pprint(dict(names_by_culture)) ```

And that's basically it, I apologise if this is not the most in-depth explanation, but like I said I don't really know how to ask this.
MegaIng commented 1 year ago

Does Esso.txt end in a newline? I.e. is there a blank line at the end? The current grammar requires that.

Keenonthedaywalker commented 1 year ago

It was just that easy! Thanks, it works now.