lark-parser / lark

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

No terminal defined #598

Closed priyeshshukla closed 4 years ago

priyeshshukla commented 4 years ago

UnexpectedCharacters: No terminal defined for '[' at line 531 col 15

  pin (acc[0]) {
          ^

Expecting: {'CNAME', 'RPAR', 'LBRACE', 'ESCAPED_STRING', 'DBLQUOTE', 'SEMICOLON', 'LPAR', 'COMMA', 'RBRACE', 'SIGNED_NUMBER', 'COLON'}

Previous tokens: Token(CNAME, 'acc')

I need help with this issue, using lark-parser for the first time. Played with grammar but maybe I am doing something wrong.

the grammar is below

    ?start: group

    group: name argument_list group_body
    group_body: "{" (statement)* "}"

    argument_list: "(" [value ("," value)*] ")" | "(" [value "[" value "]"] ")"

    ?statement: attribute ";"
        | group
        | define ";"

    ?value: name
        | number
        | number unit -> number_with_unit
        | numbers
        | string -> escaped_string

    numbers: "\"" [number ("," number)*] "\""

    unit: CNAME

    ?attribute: simple_attribute
        | complex_attribute

    simple_attribute: name ":" value

    complex_attribute: name argument_list

    define: "define" "(" name "," name "," name ")"

    name : CNAME
    string: ESCAPED_STRING_MULTILINE

    number: SIGNED_NUMBER

    COMMENT: /\/\*(\*(?!\/)|[^*])*\*\//
    NEWLINE: /\\?\r?\n/

    _STRING_INNER: /.*?/
    _STRING_ESC_INNER_MULTILINE: (_STRING_INNER | NEWLINE)+ /(?<!\\)(\\\\)*?/ 

    ESCAPED_STRING_MULTILINE : "\"" _STRING_ESC_INNER_MULTILINE "\""

    %import common.WORD
    %import common.ESCAPED_STRING
    %import common.CNAME
    %import common.SIGNED_NUMBER
    %import common.WS

    %ignore WS
    %ignore COMMENT
    %ignore NEWLINE
erezsh commented 4 years ago

I used your exact grammar and it works:

l = Lark(g, parser='lalr')
print(l.parse('pin (acc[0]) {a: 4;}'))
$ python issue598.py
Tree(group, [Tree(name, [Token(CNAME, 'pin')]), Tree(argument_list, [Tree(name, [Token(CNAME, 'acc')]), Tree(number, [Token(SIGNED_NUMBER, '0')])]), Tree(group_body, [Tree(simple_attribute, [Tree(name, [Token(CNAME, 'a')]), Tree(number, [Token(SIGNED_NUMBER, '4')])])])])

Please put more effort into your questions.

julienmalard commented 4 years ago

@priyeshshukla Could you share the full text you are trying to parse? I think that might be the problem.