資源簡介
Python 語言寫的 C 語言的詞法分析器。是實(shí)驗(yàn)報(bào)告的一個(gè)實(shí)驗(yàn)。實(shí)驗(yàn)內(nèi)容詳見:https://blog.csdn.net/yuxin1100/article/details/80360837
代碼片段和文件信息
import?re
#?關(guān)鍵字列表
key_ws?=?[‘char‘?‘short‘?‘int‘?‘unsigned‘?‘long‘?‘float‘?‘double‘?‘struct‘?‘union‘?‘void‘?‘enum‘?‘signed‘
??????????‘const‘?‘volatile‘?‘typedef‘?‘a(chǎn)uto‘?‘register‘?‘static‘?‘extem‘?‘break‘?‘case‘?‘continue‘
??????????‘default‘?‘do‘?‘else‘?‘for‘?‘goto‘?‘if‘?‘return‘?‘switch‘?‘while‘?‘sizeof‘?‘txt‘?‘FILE‘
??????????‘fopen‘?‘NULL‘?‘fclose‘?‘exit‘?‘r‘?‘read‘?‘close‘?‘w‘?‘fprintf‘]
#?變量名
ID?=?r‘[a-zA-Z\_][0-9a-zA-Z\_]*‘
#?分隔符
SEPARATOR?=?r‘[:{};)(<>]#‘
#?操作碼
OP?=?[‘+‘?‘-‘?‘*‘?‘/‘?‘%‘?‘>‘?‘<‘?‘=‘?‘!‘?‘|‘?‘&‘?‘~‘?‘^‘]
#?常數(shù)
NUM?=?r‘[0-9\.][0-9]*‘
KEY_List?=?[]
ID_List?=?[]
OP_List?=?[]
SEPARATOR_list?=?[]
STRING_List?=?[]
NUM_List?=?[]
print_List?=?[]
content_List?=?[]
def?main():
????global?ws
????cflag?=?0??#?控制多行注釋
????oflag?=?0
????op_first?=?‘‘
????line?=?read_file(‘/Users/Desktop/test.c‘)
????#?行號
????line_num?=?0
????#?多行注釋起始行號
????line_comment_start?=?0
????comment_start1?=?‘//‘
????comment_start2?=?‘/*‘
??
評論
共有 條評論