登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> SoftHub关联区 >> 主题: [ast/python]和 antlr 一样能支持文法的代码解析器     [回主站]     [分站链接]
[ast/python]和 antlr 一样能支持文法的代码解析器
clq
浏览(58) - 2024-08-23 16:57:34 发表 编辑

关键字:

[ast/python]和 antlr 一样能支持文法的代码解析器

https://github.com/lark-parser/lark

https://blog.csdn.net/liuyuan_jq/article/details/137432643
参考

https://github.com/lark-parser/lark/blob/master/examples/calc.py
Python-Lark是一个强大的、现代的解析库,专为Python开发者设计,用于处理各种语法结构的文本解析任务。这个库的出现旨在简化和优化文本解析的过程,使开发者能够高效地构建解析器,处理从简单的命令行语法到复杂的编程语言语法等各种输入。

使用Python解析TypeScript代码的工具和库
除了ast模块之外,Python还提供了一些其他的工具和库,可以帮助我们解析 比如ply和pyleri,
1.+使用ast模块解析TypeScript代码
2.+使用ply和pyleri解析TypeScript代码
3.+使用lark解析TypeScript代码


--
https://github.com/search?q=parsing%20toolkit&type=repositories

BNF(Backus-Naur Form)是一种用于描述语言结构的文法表示方法。它由美国数学家约翰·伯纳德·纳尔逊(John Backus Naur)于1962年提出,用于描述自然语言。

https://blog.csdn.net/gitblog_00063/article/details/138744646
Chevrotain的核心在于它的灵活性和效率。你可以直接在JavaScript中编写和修改语法规则,这使得开发过程更加敏捷,调试也更为方便。此外,Chevrotain还提供了丰富的API和DSL(领域特定语言),用于创建和操作词法分析器和解析器。一个值得注意的特性是其内置的支持LL(K)和通过第三方插件实现的LL(*)解析,这使得处理复杂语法结构变得轻松。

它有个在线编辑器,非常的方便
https://chevrotain.io/playground/

https://devpress.csdn.net/react/62ed776cc6770329307f263e.html


-----------------------------------------------
-----------------------------------------------
以下是一个使用Python编写的代码分析器示例,可以对多种编程语言的源代码进行分析:

import ast
import codestyle
import codex
import dx
import git
import keyword
import re
import tokenize
import traceback
import yaml


def analyze_code(code_path):
# 读取代码文件内容
with codex.open(filename=code_path, encoding="utf-8") as f:
code = f.read()

# 解析代码
lines = ast.parse(code)

# 查找关键字
keywords = keyword.kwlist(code)

# 代码格式检查
formatted_code = codestyle.check_style(code)

# 代码结构分析
structure = ast.parse(formatted_code)

# 代码树分析
tree = ast.tree(structure)

# 打印树节点
for node in tree.body:
if isinstance(node, ast.Assign):
print(f"Assignment: {node.targets}")
elif isinstance(node, ast.AnnAssign):
print(f"Annotation: {node.targets}")
elif isinstance(node, ast.Import):
print(f"Import: {node.module}")

# 打印注释
comments = [co.comments[0] for co in ast.walk(tree)]
for comment in comments:
print(f"{comment.body.strip()}")

# 打印错误信息
errors = []
for usage in ast.walk(tree):
if isinstance(usage, ast.Import):
errors.append(f"{usage.module} is imported but not defined")
elif isinstance(usage, ast.ImportFrom):
errors.append(f"{usage.module} is imported from but not defined")

if errors:
print("\nErrors:")
for error in errors:
print(f"{error}")

# 示例:分析Python代码
analyze_code("my_program.py")

# 输出

该示例使用Python内置的ast、codestyle和codex库来分析代码文件。分析包括:代码格式检查、代码树分析、注释分析以及错误检查。

在这个示例中,分析的代码是“my_program.py”文件。输出将显示该文件中的关键字、注释以及错误信息。

你可以根据需要修改示例以分析其他编程语言的代码。例如,要分析Java代码,可以使用Java ast库和Java Codex库,以及Java代码格式检查工具和Java注释工具。


总数:0 页次:1/0 首页 尾页  
总数:0 页次:1/0 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1