This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

29 Zeilen
701 B

  1. #
  2. # This example demonstrates relative imports with rule rewrite
  3. # see multiples.lark
  4. #
  5. #
  6. # if b is a number written in binary, and m is either 2 or 3,
  7. # the grammar aims to recognise m:b iif b is a multiple of m
  8. #
  9. # for example, 3:1001 is recognised
  10. # because 9 (0b1001) is a multiple of 3
  11. #
  12. from lark import Lark, UnexpectedInput
  13. parser = Lark.open('multiples.lark', parser='lalr')
  14. def is_in_grammar(data):
  15. try:
  16. parser.parse(data)
  17. except UnexpectedInput:
  18. return False
  19. return True
  20. for n_dec in range(100):
  21. n_bin = bin(n_dec)[2:]
  22. assert is_in_grammar('2:{}'.format(n_bin)) == (n_dec % 2 == 0)
  23. assert is_in_grammar('3:{}'.format(n_bin)) == (n_dec % 3 == 0)