资源简介
本文为转载资源,希望大家积极下载指正。
代码片段和文件信息
__author__ = ‘baoli1100‘
import scanner
import copy
from collections import deque
tokens = []
curpos = 0
curToken = None
curLine = 0
syntaxTree = {}
syntaxTreeNode = {}
ultiSym = []
unultiSym = []
syntaxQueue = []
production = []
predict = []
derive = {}
nodenum = 0
emp = []
haveError = False
curTKErr = False
errorList = []
symName = {‘=‘: ‘EQ‘
‘<‘: ‘LT‘
‘+‘: ‘PLUS‘
‘-‘: ‘MINUS‘
‘*‘: ‘TIMES‘
‘/‘: ‘OVER‘
‘[‘: ‘LPAREN‘
‘]‘: ‘RPAREN‘
‘.‘: ‘DOT‘
‘;‘: ‘SEMI‘
‘‘: ‘COMMA‘
‘(‘: ‘LMIDPAREN‘
‘)‘: ‘RMIDPAREN‘
‘:=‘: ‘ASSIGN‘
‘..‘: ‘UNDERANGE‘
}
def getNextToken():
global curpos curToken curTKErr
curpos += 1
curToken = tokens[curpos]
curTKErr = False
def getSym():
global ultiSym unultiSym
ultiSym = open(‘ultiSym.txt‘).read().split()
ultiSym = [sym[:-1] if sym[len(sym) - 1] == ‘‘ else sym for sym in ultiSym]
unultiSym = open(‘unultiSym.txt‘).read().split()
unultiSym = [sym[:-1] if sym[len(sym) - 1] == ‘‘ else sym for sym in unultiSym]
def getProduction():
global production
production = []
f = open(‘production.txt‘)
lines = f.readlines()
lines = [l for l in lines if l != ‘\n‘]
left = ““
for l in lines:
l = l.strip()
if ‘::=‘ in l:
l = l.split(‘::=‘)
left = l[0].strip()
right = l[1].split()
else:
right = l.split(‘|‘)[1].split()
production.append((left tuple(right)))
def getPredict():
global predict
predict = open(‘predict.txt‘).readlines()
predict = [line.strip().split(‘‘) for line in predict]
def getDerive():
global derive
for word in unultiSym:
for token in ultiSym:
for i in range(len(production)):
prod = production[i]
if prod[0] == word and len([t for t in predict[i] if match(t token) == True]) != 0:
derive[(word token)] = prod[1]
def match(curSym tokenSym):
if curSym == tokenSym: return True
tcurSym = copy.deepcopy(curSym);
ttokenSym = copy.deepcopy(tokenSym)
if tcurSym in symName: tcurSym = symName[curSym]
if tcurSym == ttokenSym: return True
if ttokenSym in ultiSym: ttokenSym = tokenSym.lower()
return True if tcurSym == ttokenSym else False
def addChild(father son):
global syntaxTree
if father not in syntaxTree:
syntaxTree[father] = []
syntaxTree[father].append(son)
def init():
global nodenum emp
nodenum = 0
emp = []
getSym()
getProduction()
getPredict()
getDerive()
def error(line expect found):
global curTKErr haveError errorList
haveError = True
if curTKErr: return
curTKErr = True
inf = ‘Error founded at %d line!\n‘ % line + ‘Expected ‘ + ‘ or ‘.join(expect) + ‘\n‘ + ‘Founded ‘ + found + ‘\n‘
errorList.append(i
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-04-25 23:03 EasySNLCompiler-master\
文件 497 2016-05-22 19:18 EasySNLCompiler-master\change.txt
目录 0 2019-04-25 23:09 __MACOSX\
目录 0 2019-04-25 23:09 __MACOSX\EasySNLCompiler-master\
文件 212 2016-05-22 19:18 __MACOSX\EasySNLCompiler-master\._change.txt
文件 2617 2019-04-25 22:53 EasySNLCompiler-master\scanner.py
目录 0 2019-04-25 23:03 EasySNLCompiler-master\test\
文件 258 2016-05-22 19:18 EasySNLCompiler-master\test\t1.txt
目录 0 2019-04-25 23:09 __MACOSX\EasySNLCompiler-master\test\
文件 212 2016-05-22 19:18 __MACOSX\EasySNLCompiler-master\test\._t1.txt
文件 205 2016-05-22 19:18 EasySNLCompiler-master\test\t3.txt
文件 212 2016-05-22 19:18 __MACOSX\EasySNLCompiler-master\test\._t3.txt
文件 104 2019-04-25 23:03 EasySNLCompiler-master\test\t2.txt
文件 212 2019-04-25 23:03 __MACOSX\EasySNLCompiler-master\._test
文件 854 2016-05-22 19:18 EasySNLCompiler-master\unultiSym.txt
文件 212 2016-05-22 19:18 __MACOSX\EasySNLCompiler-master\._unultiSym.txt
文件 962 2016-05-22 19:18 EasySNLCompiler-master\predict.txt
文件 212 2016-05-22 19:18 __MACOSX\EasySNLCompiler-master\._predict.txt
文件 43520 2016-05-22 19:18 EasySNLCompiler-master\鐘舵€佽浆鎹㈣〃.doc
文件 212 2016-05-22 19:18 __MACOSX\EasySNLCompiler-master\._鐘舵€佽浆鎹㈣〃.doc
目录 0 2019-04-25 23:01 EasySNLCompiler-master\__pycache__\
文件 2276 2019-04-25 23:01 EasySNLCompiler-master\__pycache__\scanner.cpython-36.pyc
文件 5235 2019-04-25 23:03 EasySNLCompiler-master\parser.py
文件 329 2016-05-22 19:18 EasySNLCompiler-master\ultiSym.txt
文件 212 2016-05-22 19:18 __MACOSX\EasySNLCompiler-master\._ultiSym.txt
文件 2613 2016-05-22 19:18 EasySNLCompiler-master\production.txt
文件 212 2016-05-22 19:18 __MACOSX\EasySNLCompiler-master\._production.txt
目录 0 2019-04-25 23:03 EasySNLCompiler-master\.idea\
文件 600 2019-04-25 22:51 EasySNLCompiler-master\.idea\EasySNLCompiler-master.iml
目录 0 2019-04-25 22:51 EasySNLCompiler-master\.idea\libraries\
文件 123 2019-04-25 22:51 EasySNLCompiler-master\.idea\libraries\R_User_Library.xm
............此处省略11个文件信息
评论
共有 条评论