|
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语法
- from lark import Lark,Visitor
- latex_grammar = r"""
- start: (command | environment | inline_math | display_math | text | escaped_character)*
- command: "\\" /[a-zA-Z]+/ ("[" math_content "]")? ("{" math_content "}")*
- environment: "\\begin" "{" env "}" math_content* "\\end" "{" env "}"
- env: /[a-zA-Z]+\*?/
- inline_math: "$" math_content "$" | "\\(" math_content "\\)"
- display_math: "$$" math_content "$$" | "\\[" math_content "\\]"
- math_content: (command | environment | text | escaped_character)*
- text: /[^ \t\f\r\n$\\]+/
- escaped_character: "\\" /[$\\,;]/
- %ignore /[ \t\f\r\n]+/
- """
- class EnvChecker(Visitor):
- def environment(self, node):
- begin_env = node.children[0].children[0].value # \begin{env}
- end_env = node.children[-1].children[0].value # \end{env}
- if begin_env != end_env:
- raise ValueError(f"Mismatched environments: {begin_env} vs {end_env}")
- class LatexValidator:
- def __init__(self):
- self.parser = Lark(latex_grammar, parser="lalr", debug=True)
- def validate(self, content):
- tree = self.parser.parse(content)
- print(tree.pretty()) # Print the result tree
- EnvChecker().visit(tree)
- return True
Copy the Code |
|