easy_parser_generator.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from evaluator import *
  2. DESCRIPTION = "Test if the model can understand a vague BNF-style grammar and write a Python function that evaluates expressions based on the grammar rules."
  3. TAGS = ['code', 'python']
  4. question = """
  5. Given this BNF-style grammar write a Python function `evaluate(str)` that evaluates the expression
  6. <expression> ::= <term> | <expression> '+' <term> | <expression> '-' <term>
  7. <term> ::= <factor> | <term> '*' <factor> | <term> '/' <factor>
  8. <factor> ::= <number> | '(' <expression> ')' | <prefix-op> <factor>
  9. <prefix-op> ::= 'sin' | 'cos'
  10. <number> ::= <digit> | <number> <digit>
  11. <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
  12. For example you can evaluate this
  13. sin(3 + 2 * 4) - cos(15) = -0.240302
  14. """
  15. test_case, answer = make_python_test([("round(evaluate('sin(35+2*4)-cos(15)'), 5)", "-0.07209")])
  16. TestSimpleBNF = question >> LLMRun() >> ExtractCode() >> PythonRun(test_case) >> SubstringEvaluator(answer)
  17. if __name__ == "__main__":
  18. print(run_test(TestSimpleBNF))