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.

Trying to understand non-deterministic behaviour

See original GitHub issue

Hi. Frist, I have to admit that I am new both to Lark and Python (I came from the land of C). I’m trying to write a simple assembler.

I’ve prepared the following Lark grammar:

start: source+
source: [LABEL_DEF+] MNEMONIC [[IMM_OPERAND|OPERAND|SYMBOL][","IMM_OPERAND|","OPERAND|","SYMBOL]] [COMMENT] _NL -> instruction
      | META [META_ARG] [COMMENT] _NL -> meta
      | [COMMENT] _NL

LABEL_DEF: SYMBOL":"
SYMBOL: NAME
META_ARG: DEC_NUMBER|HEX_NUMBER
OPERAND: DEC_NUMBER|HEX_NUMBER
IMM_OPERAND: IMM_DEC_NUMBER|IMM_HEX_NUMBER
MNEMONIC: "nop"|"stp"
META: ".org"
DEC_NUMBER: /[1-9]\d*l?/i
IMM_DEC_NUMBER: /#[1-9]\d*l?/i
HEX_NUMBER: /0x[\da-f]*l?/i
IMM_HEX_NUMBER: /#0x[\da-f]*l?/i
COMMENT: ";"/.*/
WHITE: " "|"\t"
%import common.CNAME -> NAME
%import common.LETTER
%import common.INT -> NUMBER
%import common.NEWLINE -> _NL
%ignore WHITE
%ignore COMMENT
%ignore _NL

(disregard the fact that there are only two mnemonics defined for now)

I’ve also got a test file that looks like:

.org 0xC00

foo:	nop 1
	nop 0x1,1	; fdas
foo2:
foo3:
	nop #0x1,1
	nop lab1
	stp #2

; comment
	nop		; fds
; fds

The parser works consistently on this example. However, if I change the first line to be a comment, to look like:

;.org 0xC00

Then approximately half of the time, I’ll get the expected parsing output, and the other half of the time I get an empty token at the beginning:

[]
[Token(LABEL_DEF, 'foo:'), Token(MNEMONIC, 'nop'), Token(OPERAND, '1')]
[Token(MNEMONIC, 'nop'), Token(OPERAND, '0x1'), Token(OPERAND, '1')]
[Token(LABEL_DEF, 'foo2:'), Token(LABEL_DEF, 'foo3:'), Token(MNEMONIC, 'nop'), Token(IMM_OPERAND, '#0x1'), Token(OPERAND, '1')]
[Token(MNEMONIC, 'nop'), Token(SYMBOL, 'lab1')]
[Token(MNEMONIC, 'stp'), Token(IMM_OPERAND, '#2')]
[Token(MNEMONIC, 'nop')]

Any hints on how to handle this?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
erezshcommented, Jan 14, 2019

Yes, we’ve corrected this bug recently. I’m glad to hear that it seems to be working for others as well.

1reaction
federicotdncommented, Dec 3, 2018

Glad it helped! If parts of the code are still non-deterministic when using a fixed PYTHONHASHSEED, then the problem may be that a class (not str, bytes or datetime) is missing its __hash__ function, and somewhere the code is iterating over sets/dictionaries of this class.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nondeterministic algorithm - Wikipedia
In computer programming, a nondeterministic algorithm is an algorithm that, even for the same input, can exhibit different behaviors on different runs, ...
Read more >
Difference between Deterministic and Non ... - GeeksforGeeks
The non-deterministic algorithms can show different behaviors for the same input on different execution and there is a degree of randomness to ...
Read more >
Why is non-determinism a useful concept?
Rather, nondeterminism is a useful concept when trying to understand computation. For instance, we now know that, from a computability ...
Read more >
The art of non-deterministic behaviour - Youris.com
Our perspective is to create agents or use artificial life to develop scenarios where non-deterministic behaviour occurs, which may actually add ...
Read more >
Nondeterminism - YouTube
Theory of Computationhttps://uvatoc.github.io/week714.5: NondeterminismNathan Brunelle and David EvansUniversity of Virginia.
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