Trying to understand non-deterministic behaviour
See original GitHub issueHi. 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:
- Created 5 years ago
- Reactions:1
- Comments:13 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, we’ve corrected this bug recently. I’m glad to hear that it seems to be working for others as well.
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 (notstr
,bytes
ordatetime
) is missing its__hash__
function, and somewhere the code is iterating over sets/dictionaries of this class.