test_grammar.py 937 B

123456789101112131415161718192021222324252627
  1. import os.path as osp
  2. import sys
  3. mydir = osp.normpath(osp.dirname("__file__"))
  4. sys.path.append(osp.normpath(osp.join(mydir)))
  5. import expr_parser
  6. def test_grammar():
  7. """Show off check_sets to check a grammar"""
  8. p = expr_parser.ExprParser()
  9. missing_lhs, missing_rhs, token_set, right_recursive, dup_rhs = p.check_sets()
  10. print("LHS nonterminals missing from the RHS of some rule:", missing_lhs)
  11. print("RHS nonterminals that aren't the LHS of some rule:", missing_rhs)
  12. print("set of tokens:", token_set)
  13. print("Rules which have the same RHS (so might be combined):", dup_rhs)
  14. print(
  15. "Right recursive rules which aren't (yet) handled efficiently", right_recursive
  16. )
  17. assert token_set == set("NUMBER LPAREN ADD_OP RPAREN MULT_OP".split())
  18. assert missing_lhs == set([])
  19. assert missing_rhs == set([])
  20. assert right_recursive == set([])
  21. if __name__ == "__main__":
  22. test_grammar()