创建项目

This commit is contained in:
lichx 2024-06-21 13:15:53 +08:00
commit 0da66d6491
6 changed files with 1564 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

102
GrammarAnalysis.py Normal file
View File

@ -0,0 +1,102 @@
import re
class GrammarAnalysis:
keyword = {
r'begin': 1,
r'end': 2
}
symbol = {
r'+': 13,
r'-': 14,
r'*': 15,
r'/': 16,
r';': 17,
r'=': 18,
r'<': 19,
r'<>': 20,
r'<=': 21,
r'>': 22,
r'>=': 23,
r'(': 24,
r')': 25,
r'#': 0
}
regex = {
r'^[a-zA-Z][a-zA-Z0-9]*$': 10,
r'^\d+$': 11
}
def readfile(self, filename: str) -> str:
s = ''
with open(filename, 'r', encoding='utf-8') as f:
for x in f.readlines():
s = s.strip()
if len(x) != 0:
s = x
break
return s
def strsplit(self, s: str) -> list[str]:
lt = []
l = 0
r = 1
while r < len(s):
if not (s[r].isalpha() or s[r].isdigit()):
k = s[l:r].strip()
if len(k) != 0:
lt.append(k)
k = s[r].strip()
if len(k) != 0:
lt.append(k)
l = r + 1
r = l
else:
r += 1
if l < len(s):
lt.append(s[l:r])
return lt
def analyze(self, lt: list[str]) -> list[(int, str)]:
ans = []
i = 0
while i < len(lt):
s = lt[i]
if s in self.keyword:
ans.append((self.keyword[s], s))
elif s in self.symbol:
if i + 1 < len(lt) and s + lt[i + 1] in self.symbol:
ans.append((self.symbol[s + lt[i + 1]], s + lt[i + 1]))
i += 1
else:
ans.append((self.symbol[s], s))
else:
no_answer = True
for key, value in self.regex.items():
if re.match(key, s):
ans.append((value, s))
no_answer = False
break
if no_answer:
ans.append((-1, 'error'))
i += 1
return ans
def result_print(self, lt: list[(int, str)]) -> None:
with open('output.txt', 'w', encoding='utf-8') as f:
for key, value in lt:
if key == -1:
print(value)
f.write(value + '\n')
else:
print(key, value, sep=' ')
f.write(f'{key} {value}\n')
def f(self) -> None:
s = self.readfile('testfile.txt')
s = self.strsplit(s)
s = self.analyze(s)
self.result_print(s)
GrammarAnalysis().f()

1157
GrammaticalAnalysis.py Normal file

File diff suppressed because it is too large Load Diff

168
RE2NFA.py Normal file
View File

@ -0,0 +1,168 @@
import functools
class Edge:
# 符号 开始状态 结束状态
def __init__(self, label: chr, start: int, end: int):
self.label = label
self.start = start
self.end = end
class EdgeGroup:
def __init__(self, start: int, end: int, numOfState: int, edges: list[Edge]):
self.edges = edges
self.start = start
self.end = end
self.numOfState = numOfState
def append(self, edges: list[Edge]):
self.edges += edges
class NFA:
# ( ) | * . others
# priority * . |
def Priority(self, a: chr, b: chr) -> int: # a > b 1 ,a == b 0 ,a < b -1
if a == b:
return 0
pri = ['|', '.', '*', '(']
return 1 if pri.index(a) > pri.index(b) else -1
def EdgePriority(self, a: Edge, b: Edge) -> int:
a = a.label
b = b.label
if a == '~':
return -1
if b == '~':
return 1
return 1 if ord(b) < ord(a) else -1
def AddConcat(self, s: str) -> str:
lt = list(s)
i = 0
while i < len(lt):
if lt[i] in '|.(':
pass
elif i + 1 < len(lt) and (lt[i + 1].isalpha() or lt[i + 1] == '('):
lt.insert(i + 1, '.')
i += 1
return ''.join(lt)
def ToPostfix(self, s: str) -> str:
tempSt = []
operSt = []
for c in s:
match c:
case '(':
operSt.append(c)
case ')':
while operSt[-1] != '(':
tempSt.append(operSt.pop())
operSt.pop()
case '*' | '|' | '.':
if len(operSt) == 0 or operSt[-1] == '(':
operSt.append(c)
else:
while len(operSt) != 0 and self.Priority(c, operSt[-1]) != 1:
tempSt.append(operSt.pop())
operSt.append(c)
case _:
tempSt.append(c)
while len(operSt) != 0:
tempSt.append(operSt.pop())
return ''.join(tempSt)
def ToNFA(self, s: str) -> EdgeGroup:
stack = []
for c in s:
match c:
case '|':
edgeGroups = stack[-2:]
stack = stack[:-2]
eg = EdgeGroup(0, edgeGroups[0].numOfState + edgeGroups[1].numOfState - 3,
edgeGroups[0].numOfState + edgeGroups[1].numOfState - 2, [])
for edge in edgeGroups[0].edges:
if edgeGroups[0].end == edge.end:
edge.end = eg.end
eg.append([edge])
for edge in edgeGroups[1].edges:
if edgeGroups[1].end == edge.end:
edge.end = eg.end
else:
edge.end += edgeGroups[0].numOfState - 2
edge.start += edgeGroups[0].numOfState - 2 if edge.start != 0 else 0
eg.append([edge])
stack.append(eg)
case '.':
edgeGroups = stack[-2:]
stack = stack[:-2]
eg = EdgeGroup(0, edgeGroups[0].numOfState + edgeGroups[1].numOfState - 2,
edgeGroups[0].numOfState + edgeGroups[1].numOfState - 1, edgeGroups[0].edges)
for edge in edgeGroups[1].edges:
edge.start += edgeGroups[0].numOfState - 1
edge.end += edgeGroups[0].numOfState - 1
eg.append([edge])
stack.append(eg)
case '*':
edgeGroup = stack[-1]
stack.pop()
eg = EdgeGroup(0, edgeGroup.numOfState, edgeGroup.numOfState + 1, [])
for edge in edgeGroup.edges:
edge.start += 1
edge.end = edgeGroup.start+1 if edge.end == edgeGroup.end else edge.end + 1
eg.append([edge])
eg.append([Edge('~', 0, 1), Edge('~', 1, eg.end)])
stack.append(eg)
case _:
stack.append(EdgeGroup(0, 1, 2, [Edge(c, 0, 1)]))
return stack[0]
def printEdge(self, eg: Edge, st: str = '', ed: str = '') -> None:
# 0~>1
print(eg.start - 1 if st == '' else st, '-', eg.label, '->', eg.end - 1 if ed == '' else ed, sep='', end=' ')
def printNFA(self, eg: EdgeGroup) -> None:
dt = {}
for edge in eg.edges:
if edge.start in dt:
dt[edge.start].append(edge)
else:
dt[edge.start] = [edge]
i = 0
while i in dt:
edges = dt[i]
edges.sort(key=functools.cmp_to_key(self.EdgePriority))
if i == 0:
print('X', end=' ')
for edge in edges:
self.printEdge(edge, st='X')
print('\nY')
i += 1
continue
print(i - 1, end=' ')
if i + 1 not in dt:
for edge in edges:
self.printEdge(edge, ed='Y')
else:
for edge in edges:
self.printEdge(edge)
print()
i += 1
pass
def f(self, s: str) -> None:
s = self.AddConcat(s)
print(s)
s = self.ToPostfix(s)
print(s)
s = self.ToNFA(s)
self.printNFA(s)
nfa = NFA()
nfa.f("(a|b)*baa")
nfa.f(input().lower())
# print(nfa.AddConcat("(a*b)k"))

117
output.txt Normal file
View File

@ -0,0 +1,117 @@
CONSTTK const
INTTK int
IDENFR const1
ASSIGN =
INTCON 1
<无符号整数>
<整数>
COMMA ,
IDENFR const2
ASSIGN =
MINU -
INTCON 100
<无符号整数>
<整数>
<常量定义>
SEMICN ;
CONSTTK const
CHARTK char
IDENFR const3
ASSIGN =
CHARCON _
<常量定义>
SEMICN ;
<常量说明>
INTTK int
IDENFR change1
<变量定义>
SEMICN ;
CHARTK char
IDENFR change3
<变量定义>
SEMICN ;
<变量说明>
INTTK int
IDENFR gets1
<声明头部>
LPARENT (
INTTK int
IDENFR var1
COMMA ,
INTTK int
IDENFR var2
<参数表>
RPARENT )
LBRACE {
IDENFR change1
ASSIGN =
IDENFR var1
<因子>
<项>
PLUS +
IDENFR var2
<因子>
<项>
<表达式>
<赋值语句>
SEMICN ;
<语句>
RETURNTK return
LPARENT (
IDENFR change1
<因子>
<项>
<表达式>
RPARENT )
<返回语句>
SEMICN ;
<语句>
<语句列>
<复合语句>
RBRACE }
<有返回值函数定义>
VOIDTK void
MAINTK main
LPARENT (
RPARENT )
LBRACE {
PRINTFTK printf
LPARENT (
STRCON Hello World
<字符串>
RPARENT )
<写语句>
SEMICN ;
<语句>
PRINTFTK printf
LPARENT (
IDENFR gets1
LPARENT (
INTCON 10
<无符号整数>
<整数>
<因子>
<项>
<表达式>
COMMA ,
INTCON 20
<无符号整数>
<整数>
<因子>
<项>
<表达式>
<值参数表>
RPARENT )
<有返回值函数调用语句>
<因子>
<项>
<表达式>
RPARENT )
<写语句>
SEMICN ;
<语句>
<语句列>
<复合语句>
RBRACE }
<主函数>
<程序>

12
testfile.txt Normal file
View File

@ -0,0 +1,12 @@
const int const1 = 1, const2 = -100;
const char const3 = '_';
int change1;
char change3;
int gets1(int var1,int var2){
change1 = var1 + var2;
return (change1);
}
void main(){
printf("Hello World");
printf(gets1(10, 20));
}