This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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