question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strangeness with 'expected' value from exception

See original GitHub issue

This a cut-down example from a parser intended to accept disk format definitions. I do not understand why it fails to report the ‘libdisk’ token in the expected list from the thrown exception. The parse tree certainly looks reasonable, yet it insists on calling out an ANON token. If there’s a difference in the way the grammar is formed for that one terminal I’m not seeing it.

#!/usr/bin/python3

from lark.lark import Lark
from lark import Transformer, UnexpectedToken, UnexpectedCharacters, UnexpectedEOF
import pprint

GRAMMAR = r"""
start: diskdef+

diskdef: "diskdef" defname pair+ "end"

defname: CNAME

pair: intparm intval -> intparm
    | strparm strval -> strparm
    | osparm OSVAL -> osparm

intparm: "seclen" -> seclen
       | "tracks" -> tracks

strparm: "libdsk:format" -> libdisk

osparm: "os" -> os

?intval: INT

?strval: CNAME

OSVAL: "2.2"|"3"|"isx"|"p2dos"|"zsys"

%import common.WS
%ignore WS

%import common.INT
%import common.CNAME
%import common.LETTER
%import common.DIGIT
"""
parser = Lark(GRAMMAR, parser='lalr')

def do_parse(data):
    try:
        j = parser.parse(data)
        print(j.pretty())
    except UnexpectedToken as u:
        print("Unxpected token at line: %d, column: %d\n" % (u.line, u.column))
        txt = u.get_context(data)
        print(txt)
        print("Expected one of:")
        for tok in u.expected:
            print(tok)
    except UnexpectedCharacters as u:
        print(u)
    except UnexpectedEOF as u:
        print(u)

good_data = r"""
diskdef pcw
  seclen 512
  tracks 40
  os 3
  libdsk:format pcw180
end
"""
do_parse(good_data)

bad_data = r"""
diskdef pcw
  foobar 512
  tracks 40
  os 3
  libdsk:format pcw180
end
"""
do_parse(bad_data)

Parse Tree:

start
  diskdef
    defname     pcw
    intparm
      seclen
      512
    intparm
      tracks
      40
    osparm
      os
      3
    strparm
      libdisk
      pcw180

Error report:

Unxpected token at line: 3, column: 3

  foobar 512
  ^

Expected one of:
OS
SECLEN
TRACKS
__ANON_0  <----- Why?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
snhirschcommented, Feb 4, 2022

Are you getting a backslash when doing

print( parser.get_terminal(tok).pattern.value )

?

No - that works correctly.

To name the terminal, you need to define it. Like

rule: NAMED_TERMINAL
NAMED_TERMINAL: "some_value"

Thanks very much for the clarification! Really appreciate the work you’ve done on this package.

0reactions
erezshcommented, Feb 4, 2022

Are you getting a backslash when doing

print( parser.get_terminal(tok).pattern.value )

?

To name the terminal, you need to define it. Like

rule: NAMED_TERMINAL
NAMED_TERMINAL: "some_value"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Expected Value: Strange/unexpected result
I want to calculate the expected value of 1X. Since X>0, I am expecting that E(1X)>0, but Mathematica gives some negative value.
Read more >
Strangeness - an overview
The strange (s) quark is attributed a strangeness value of −1. For example, the proton and neutron contain no strange quarks, and thus...
Read more >
How to distinguish unexpected (boneheaded) exceptions ...
In my experience, no, it doesn't end up adding value. Something doesn't work as expected - for any reason, doesn't matter what -...
Read more >
Handling and Verifying Exceptions in JUnit 5
The most commonly used method is with the expected field in @Test . An alternative way of handling exceptions is by using a...
Read more >
Chapter 5: Error Handling
When the program is running, and the function is called like that, the code that called it will get a string value, as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found