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
英文地址,請注明出處:http://en.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;詳細功能對應的應用場景你是否…