This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 regels
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)