Forgot password?
 Create new account
View 492|Reply 2

LaTeX检查器探讨

[Copy link]

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

hbghlyj Posted at 2025-3-31 05:45:28 |Read mode
Last edited by hbghlyj at 2025-3-31 07:48:17github.com/lark-parser/lark/blob/master/docs/ … part-1---the-grammar 使用Extended Backus-Naur Form (EBNF)来描述上下文无关文法 Context-Free Grammars (CFG)
ENBF定义了符号串生成非终止符的规则。
Lark解析器创建一个LatexValidator类来验证LaTeX语法
  1. from lark import Lark,Visitor
  2. latex_grammar = r"""
  3.     start: (command | environment | inline_math | display_math | text | escaped_character)*
  4.     command: "\\" /[a-zA-Z]+/ ("[" math_content "]")? ("{" math_content "}")*
  5.     environment: "\\begin" "{" env "}" math_content* "\\end" "{" env "}"
  6.     env: /[a-zA-Z]+\*?/
  7.     inline_math: "$" math_content "$" | "\\(" math_content "\\)"
  8.     display_math: "$$" math_content "$$" | "\\[" math_content "\\]"
  9.     math_content: (command | environment | text | escaped_character)*
  10.     text: /[^ \t\f\r\n$\\]+/
  11.     escaped_character: "\\" /[$\\,;]/
  12.     %ignore /[ \t\f\r\n]+/
  13. """
  14. class EnvChecker(Visitor):
  15.     def environment(self, node):
  16.         begin_env = node.children[0].children[0].value  # \begin{env}
  17.         end_env = node.children[-1].children[0].value   # \end{env}
  18.         if begin_env != end_env:
  19.             raise ValueError(f"Mismatched environments: {begin_env} vs {end_env}")
  20. class LatexValidator:
  21.     def __init__(self):
  22.         self.parser = Lark(latex_grammar, parser="lalr", debug=True)
  23.     def validate(self, content):
  24.         tree = self.parser.parse(content)
  25.         print(tree.pretty())  # Print the result tree
  26.         EnvChecker().visit(tree)
  27.         return True
Copy the Code

Related threads

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

 Author| hbghlyj Posted at 2025-3-31 07:36:21
Last edited by hbghlyj at 2025-3-31 07:49:09测试:
  1. LatexValidator = LatexValidator()
  2. LatexValidator.validate(r"""\begin{document}
  3. \begin{equation}
  4.     E = mc^2
  5. \end{equation}
  6. \begin{equation*}
  7.     F = ma
  8. \end{equation*}
  9. \end{document}
  10. """)
Copy the Code
输出:
start
  environment
    env document
    math_content
      environment
        env     equation
        math_content
          text  E
          text  =
          text  mc^2
        env     equation
      environment
        env     equation*
        math_content
          text  F
          text  =
          text  ma
        env     equation*
    env document

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

 Author| hbghlyj Posted at 2025-3-31 07:50:13
测试:
  1. LatexValidator = LatexValidator()
  2. LatexValidator.validate(r"""\begin{document}
  3. \begin{equation}
  4.     E = mc^2
  5. \end{equation}
  6. \begin{equation*}
  7.     F = ma
  8. \end{equation}
  9. \end{document}
  10. """)
Copy the Code
输出:ValueError: Mismatched environments: equation* vs equation

手机版Mobile version|Leisure Math Forum

2025-4-20 12:16 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list