Rail 分析的实现思路(python)(1)

本文适用于 Rail 0.1 版本.
工作:输入Rial文件的路径,识别词元,输出实例列表.
是一边写代码一边写文章的,所以有时候改了原本的代码不一定会说.以思路为中心.
Rail是一种信息分布与细节构成的表示语言。详见参考文档.
关于本文的分析对象,参考逻辑行的类型.

从源文件到词元

这个Python脚本实现了文件的读取和词元拆分.以及缩进个数的识别.

def split_mul_ind(inp):word = ""line = [] result = []indent = 0indent_list = []ind_in = Truefor char in inp:if char =="\n":if word:line.append(word)word = ""if line:result.append(line)indent_list.append(indent)line = []indent = 0ind_in = Trueelif char == " ":if ind_in:indent += 1elif word:line.append(word)word = ""else:ind_in = Falseword += charif word: line.append(word)if line:result.append(line)indent_list.append(indent)return (result, indent_list)with (open("test.txt", 'r') as f):result, indent_list = split_mul_ind(f.read())def check(result, indent_list, n):print(f"[{n+1}] {result[n]}: {indent_list[n]}")

测试文件 test.txt 如下.包含了Rail语言的20种语义.


:
: *
*
* = *
< *
< * = *
< * = * ? *
* < *
* < * ! *
+ *
+ * = *
+ * < *
+ * < * ! *
> *
> * = *
> * < *
> * < * ! *
* >
# *

测试方法如下.

for i in range(19):check(result, indent_list, i)

运行结果如下.

[1] [':']: 0
[2] [':', '*']: 0
[3] ['*']: 0
[4] ['*', '=', '*']: 0
[5] ['<', '*']: 0
[6] ['<', '*', '=', '*']: 0
[7] ['<', '*', '=', '*', '?', '*']: 0
[8] ['*', '<', '*']: 0
[9] ['*', '<', '*', '!', '*']: 0
[10] ['+', '*']: 0
[11] ['+', '*', '=', '*']: 0
[12] ['+', '*', '<', '*']: 0
[13] ['+', '*', '<', '*', '!', '*']: 0
[14] ['>', '*']: 0
[15] ['>', '*', '=', '*']: 0
[16] ['>', '*', '<', '*']: 0
[17] ['>', '*', '<', '*', '!', '*']: 0
[18] ['*', '>']: 0
[19] ['#', '*']: 0

语句结构

除了空格会被直接忽略,可以为每种语句结构实现一个类,即实现19个类.

class PlaceHoder:def __init__(line):line.content = ""class Comment:def __init__(line, p1):line.content = (p1)class Element:def __init__(line, p1):line.content = (p1)class Rail:def __init__(line, p1, p2):line.content = (p1, p2)class ReferenceDefination1:def __init__(line, p1):line.content = (p1)class ReferenceDefination2:def __init__(line, p1, p2):line.content = (p1, p2)class ReferenceDefination3:def __init__(line, p1, p2, p3):line.content = (p1, p2, p3)class ReferenceImpletement1:def __init__(line, p1, p2):line.content = (p1)class ReferenceImpletement2:def __init__(line, p1, p2, p3):line.content = (p1, p2, p3)class Category1:def __init__(line, p1):line.content = (p1)class Category2:def __init__(line, p1, p2):line.content = (p1, p2)class Category3:def __init__(line, p1, p2):line.content = (p1, p2)class Category4:def __init__(line, p1, p2, p3):line.content = (p1, p2, p3)class AggregateDefination1:def __init__(line, p1):line.content = (p1)class AggregateDefination2:def __init__(line, p1, p2):line.content = (p1, p2)class AggregateDefination3:def __init__(line, p1, p2):line.content = (p1, p2)class AggregateDefination4:def __init__(line, p1, p2, p3):line.content = (p1, p2, p3)class AggregateImpletement1:def __init__(line, p1, p2):line.content = (p1)class Import:def __init__(line, p1):line.content = (p1)

然后呢,要给这些行创建实例.
我看这个长度是固定的,能到6,所以做个分支结构敷衍了事,要改以后该.
先做个骨架.

def get_ins(line_list):lenth = len(line_list)if lenth == 1:...elif lenth == 2:...elif lenth == 3:...elif lenth == 4:...elif lenth == 5:...elif lenth == 6:...

然后照着参考文档一个一个匹配至实例.

def get_ins(line_list):ins = []for line in line_list:lenth = len(line)if lenth == 1:if line[0] == ':':ins.append(PlaceHoder())else:ins.append(Element(line[0]))elif lenth == 2:if line[0] == ':':ins.append(Comment(line[1]))elif line[0] == '<':ins.append(ReferenceDefination1(line[1]))elif line[0] == '>':ins.append(AggregateDefination1(line[1]))elif line[0] == '+':ins.append(Category1(line[1]))elif line[0] == '#':ins.append(Import(line[1]))else:ins.append(AggregateImpletement(line[0]))elif lenth == 3:if line[1] == "=":ins.append(Rail(line[0], line[2]))else:ins.append(ReferenceImpletement1(line[0], line[2]))elif lenth == 4:if line[0] == '+':if line[2] == '=':ins.append(Category2(line[1], line[3]))else:ins.append(Category3(line[1], line[3]))elif line[0] == '>':if line[2] == '=':ins.append(AggregateDefination2(line[1], line[3]))else:ins.append(AggregateDefination3(line[1], line[3]))else:ins.append(ReferenceDefination2(line[1], line[3]))elif lenth == 5:ins.append(ReferenceImpletement2(line[0], line[2], line[4]))elif lenth == 6:if line[0] == '<':ins.append(ReferenceDefination3(line[1], line[3], line[5]))elif line[0] == '+':ins.append(Category4(line[1], line[3], line[5]))else:ins.append(AggregateDefination4(line[1], line[3], line[5]))return ins

测试

使用之前构造的词元列表进行检查.

INS = get_ins(result)
for line in INS:print(line)

测试结果与预期一致.
在这里插入图片描述
本文的最终代码如下.

def split_mul_ind(inp):word = ""line = [] result = []indent = 0indent_list = []ind_in = Truefor char in inp:if char =="\n":if word:line.append(word)word = ""if line:result.append(line)indent_list.append(indent)line = []indent = 0ind_in = Trueelif char == " ":if ind_in:indent += 1elif word:line.append(word)word = ""else:ind_in = Falseword += charif word: line.append(word)if line:result.append(line)indent_list.append(indent)return (result, indent_list)with (open("test.txt", 'r') as f):result, indent_list = split_mul_ind(f.read())def check(result, indent_list, n):print(f"{n+1} | {result[n]}: {indent_list[n]}")for i in range(19):check(result, indent_list, i)def __init__(line):line.content = ""class PlaceHoder:def __init__(line):passclass Comment:def __init__(line, p1):line.content = (p1)class Element:def __init__(line, p1):line.content = (p1)class Rail:def __init__(line, p1, p2):line.content = (p1, p2)class ReferenceDefination1:def __init__(line, p1):line.content = (p1)class ReferenceDefination2:def __init__(line, p1, p2):line.content = (p1, p2)class ReferenceDefination3:def __init__(line, p1, p2, p3):line.content = (p1, p2, p3)class ReferenceImpletement1:def __init__(line, p1, p2):line.content = (p1, p2)class ReferenceImpletement2:def __init__(line, p1, p2, p3):line.content = (p1, p2, p3)class Category1:def __init__(line, p1):line.content = (p1)class Category2:def __init__(line, p1, p2):line.content = (p1, p2)class Category3:def __init__(line, p1, p2):line.content = (p1, p2)class Category4:def __init__(line, p1, p2, p3):line.content = (p1, p2, p3)class AggregateDefination1:def __init__(line, p1):line.content = (p1)class AggregateDefination2:def __init__(line, p1, p2):line.content = (p1, p2)class AggregateDefination3:def __init__(line, p1, p2):line.content = (p1, p2)class AggregateDefination4:def __init__(line, p1, p2, p3):line.content = (p1, p2, p3)class AggregateImpletement:def __init__(line, p1):line.content = (p1)class Import:def __init__(line, p1):line.content = (p1)def get_ins(line_list):ins = []for line in line_list:lenth = len(line)if lenth == 1:if line[0] == ':':ins.append(PlaceHoder())else:ins.append(Element(line[0]))elif lenth == 2:if line[0] == ':':ins.append(Comment(line[1]))elif line[0] == '<':ins.append(ReferenceDefination1(line[1]))elif line[0] == '>':ins.append(AggregateDefination1(line[1]))elif line[0] == '+':ins.append(Category1(line[1]))elif line[0] == '#':ins.append(Import(line[1]))else:ins.append(AggregateImpletement(line[0]))elif lenth == 3:if line[1] == "=":ins.append(Rail(line[0], line[2]))else:ins.append(ReferenceImpletement1(line[0], line[2]))elif lenth == 4:if line[0] == '+':if line[2] == '=':ins.append(Category2(line[1], line[3]))else:ins.append(Category3(line[1], line[3]))elif line[0] == '>':if line[2] == '=':ins.append(AggregateDefination2(line[1], line[3]))else:ins.append(AggregateDefination3(line[1], line[3]))else:ins.append(ReferenceDefination2(line[1], line[3]))elif lenth == 5:ins.append(ReferenceImpletement2(line[0], line[2], line[4]))elif lenth == 6:if line[0] == '<':ins.append(ReferenceDefination3(line[1], line[3], line[5]))elif line[0] == '+':ins.append(Category4(line[1], line[3], line[5]))else:ins.append(AggregateDefination4(line[1], line[3], line[5]))return insINS = get_ins(result)
for line in INS:print(line)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/85630.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/85630.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【JAVA】数组的使用

文章目录 前言一、数组的基本概念1.1 数组的创建和初始化1.2 数组的基本使用 二、数组是引用类型2.1 初始JVM的内存分布JVM内存划分&#xff08;按功能分区&#xff09; 2.2 基本类型变量与引用类型变量的区别2.3 再谈引用变量2.4 认识null 三、数组作为函数的参数和返回值四、…

Python图像处理与计算机视觉:OpenCV实战指南

引言 在当今数字化时代&#xff0c;图像处理和计算机视觉技术已经渗透到我们生活的方方面面&#xff0c;从智能手机的人脸识别解锁&#xff0c;到自动驾驶汽车的路况感知&#xff0c;再到医疗影像辅助诊断系统。作为这一领域最流行的开源库之一&#xff0c;OpenCV (Open Sourc…

OCCT基础类库介绍:Modeling Algorithm - Features

Features 特征 This library contained in BRepFeat package is necessary for creation and manipulation of form and mechanical features that go beyond the classical boundary representation of shapes. In that sense, BRepFeat is an extension of BRepBuilderAPI …

【前端AI实践】DeepSeek:开源大模型的使用让开发过程不再抓头发

有时候你可能正对着屏幕发呆&#xff0c;不知道怎么下手一个 Vue 的流式请求功能。这时候&#xff0c;DeepSeek 就像是你的“编程外挂”&#xff0c;帮你把模糊的需求变成清晰的代码。 下面我们就以几个常见的开发场景为例&#xff0c;看看 DeepSeek 能帮我们做点啥。 解答技…

SAP S/4HANA 的“Smart Core”:在现实与理想之间实现敏捷扩展

摘要&#xff1a; 在 SAP S/4HANA 的实施过程中&#xff0c;“Clean Core”&#xff08;干净核心&#xff09;已成为热门话题&#xff0c;指的是通过简化和优化系统架构&#xff0c;减少技术债务、提升性能并增强可升级性。尽管这是 SAP 推动云转型的核心理念之一&#xff0c;…

Python 量化金融与算法交易实战指南

https://www.python.org/static/community_logos/python-logo-master-v3-TM.png 金融数据获取与处理 使用yfinance获取市场数据 python 复制 下载 import yfinance as yf import pandas as pd# 下载苹果公司股票数据 aapl yf.Ticker("AAPL") hist aapl.histo…

【StarRocks系列】join查询优化

目录 Join 类型 和 Join 策略 1. Join 类型&#xff08;Join Type&#xff09; 2. Join 策略&#xff08;Join Strategy&#xff09; 分布式 Join 策略 (核心) 1. Colocate Join (本地 Join - 最优): 2. Bucket Shuffle Join: 3. Broadcast Join (复制广播): 4. Shuffl…

【论文解读】ZeroSearch: 零API成本激活大模型Web搜索

1st author: Hao Sun 孙浩 - PhD Candidate Peking University - Homepage paper: [2505.04588] ZeroSearch: Incentivize the Search Capability of LLMs without Searching code: Alibaba-NLP/ZeroSearch: ZeroSearch: Incentivize the Search Capability of LLMs without…

JAVA网络编程中HTTP客户端(HttpURLConnection、Apache HttpClient)

HTTP 客户端是 Java 中实现网络请求的核心工具,主要用于与 Web 服务器交互(如获取网页、提交表单、调用 REST API 等)。Java 生态中有两种主流的 HTTP 客户端实现:​​HttpURLConnection(JDK 原生)​​ 和 ​​Apache HttpClient(第三方库)​​。以下是两者的详细解析、…

C# Process.Start多个参数传递及各个参数之间的空格处理

最近做一个软件集成的事情&#xff0c;有多个之前做的软件&#xff0c;集成到一起自己用&#xff0c;使用了 Process.Start&#xff08;“*.exe”&#xff09;的方式&#xff0c;然而遇到了传递参数的问题。 这里汇总后的程序叫main.exe&#xff0c;要汇总的软件之一是pro1.…

【Python】Excel表格操作:ISBN转条形码

一、效果 原始文件&#xff1a; 输出文件&#xff1a; 二、代码 import os import logging from openpyxl import load_workbook from openpyxl.drawing.image import Image as ExcelImage from barcode import EAN13 from barcode.writer import ImageWriterlogging.basicCo…

【Fargo】mediasoup发送2:码率分配、传输基类设计及WebRtcTransport原理

Fargo 使用了mediasoup的代码,搬运了他的架构架构精妙,但是似乎是为了sfu而生,【Fargo】mediasoup发送1:控制与数据分离的分层设计和原理我本地用来发送测试,因此需要进一步梳理: 通过分析这段代码,我来详细解释: 一、sfu 需要码率级别的分配控制 1. DistributeAvail…

矩阵置零C++

给定一个 m x n 的矩阵&#xff0c;如果一个元素为 0 &#xff0c;则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。 思路&#xff1a; 1、让首行首列记录哪一行哪一列有0 2、于是可以直接遍历非首行首列的元素&#xff0c;若该元素对应的首行首列为0&#xff0c;说明…

大内存对电脑性能有哪些提升

在科技飞速发展的今天&#xff0c;电脑已经成为我们生活和工作中不可或缺的伙伴。无论是日常办公、追剧娱乐&#xff0c;还是进行复杂的游戏和专业设计&#xff0c;电脑的性能都至关重要。而在影响电脑性能的众多因素中&#xff0c;内存大小常常被人们忽视。 多任务处理更流畅…

【StarRocks系列】Update语句

目录 简要流程 详细流程 1. UPDATE 语句执行流程 2. 如何更新表的数据 3. 是否支持事务 总结关键点 简要流程 前端处理&#xff08;FE&#xff09;&#xff1a; 解析 SQL 并验证主键条件生成包含主键列表和新值的更新计划按主键哈希分发到对应 BE 后端执行&#xff08…

计算机三级Linux应用与开发

第 1 章 计算机体系结构与操作系统 1.1 计算科学与计算机系统 冯诺依曼体系的结构要点&#xff1a; 计算机数制采用二进制&#xff0c;程序指令和数据统一存储&#xff0c;计算机应按照程序顺序执行。按照冯诺依曼结构设计的计算机由 控制器&#xff0c;运算器&#xff0c;存…

Web攻防-XSS跨站Cookie盗取数据包提交网络钓鱼BEEF项目XSS平台危害利用

知识点&#xff1a; 1、Web攻防-XSS跨站-手工代码&框架工具&在线平台 2、Web攻防-XSS跨站-Cookie盗取&数据提交&网络钓鱼 演示案例-WEB攻防-XSS跨站-Cookie盗取&数据提交&网络钓鱼&Beef工具 1、XSS跨站-攻击利用-凭据盗取 条件&#xff1a;无防…

自力更生式养老VS三大新型养老:在时代裂变中重构银发生存法则

在岁月长河中&#xff0c;父母曾为子女遮风挡雨&#xff0c;当他们步入暮年&#xff0c;养老问题成为家庭与社会共同关注的焦点。 “父母的养老终究是自力更生”&#xff0c;这句话道出了养老的本质内核。 然而&#xff0c;在自力更生的基础上&#xff0c;选择合适的养老方式…

计算机网络学习笔记:Wireshark观察TCP通信

文章目录 前言一、前置准备二、三报文握手过程抓包2.1、第一次握手2.2、第二次握手2.3、第三次握手 三、通信过程抓包3.1、报文 44379 – 客户端发数据&#xff08;PSH, ACK&#xff09;3.2、 报文 44380 – 服务端确认收到数据&#xff08;ACK&#xff09;3.3、报文 44469 – …

在Linux中,Iptables能做什么?

概述 背景说明 在运维工作中&#xff0c;Iptables是一个不可或缺的工具&#xff0c;它提供了强大的网络流量控制和管理能力。 问题呈现 iptables是一个不可获取的工具&#xff0c;你对其了解多少&#xff1f;该工具你是否真的会用&#xff1f;详细功能对应的应用场景你是否…