大连理工大学软件学院编译技术课程——MicroC词法分析上机实验
大連理工大學(xué)軟件學(xué)院編譯技術(shù)課程——MicroC詞法分析上機(jī)實驗
題目
編寫詞法分析編譯程序
實驗?zāi)康?#xff1a;對循環(huán)語句和條件判斷語句編寫詞法分析編譯程序,只能通過一遍掃描完成。
實驗要求:
(1) 關(guān)鍵字:
for if then else while do until int input output
所有關(guān)鍵字都是小寫。
(2)運算符和分隔符:
: = + - * / < > <= <> >= ; ( ) #
(2) 其他標(biāo)識符(ID)和整型常數(shù)(NUM),通過以下模式定義:
ID=letter(letter | digit)*
NUM=digit digit*
(4)空格由空白、制表符和換行符組成。空格一般用來分隔ID、NUM、運算符、分隔符和關(guān)鍵字,詞法分析階段通常被忽略。
各種詞法單元對應(yīng)的詞法記號如下:
| for | 1 | : | 17 |
| if | 2 | := | 18 |
| then | 3 | < | 20 |
| else | 4 | <> | 21 |
| while | 5 | <= | 22 |
| do | 6 | > | 23 |
| letter(letter+digit)* | 10 | >= | 24 |
| digit digit* | 11 | = | 25 |
| + | 13 | ; | 26 |
| - | 14 | ( | 27 |
| * | 15 | ) | 28 |
| / | 16 | # | 0 |
| until | 29 | int | 30 |
| input | 31 | output | 32 |
詞法分析程序的功能
輸入:源程序
輸出:二元組(詞法記號,屬性值/其在符號表中的位置)構(gòu)成的序列。
例如:對源程序
int x:=5; if (x>0) then x:=2*x+1/3; else x:=2/x; #
經(jīng)詞法分析后輸出如下序列:
(30, int)(10,’x’)(18, :=) (11,5) (26, 😉 (2, if ) (27,( )……
1.幾點說明:
(1)關(guān)鍵字表的初值。
關(guān)鍵字作為特殊標(biāo)識符處理,把它們預(yù)先安排在一張表格中(稱為關(guān)鍵字表),當(dāng)掃描程序識別出標(biāo)識符,查關(guān)鍵字表。如能查到匹配的單詞,則該單詞的關(guān)鍵字,否則為一般標(biāo)識符。關(guān)鍵表為一個字符串?dāng)?shù)組,其描述如下:
char *keyword[11]={”for”, ”if”, ”then” ,”else”,”while”, ”do”, “until”, “int”, “until”, “input”, “output” };
(3) 程序中需要用到的主要變量為 token , id和num.
1)id用來存放構(gòu)成詞法單元的字符串;
2)num用來存放整數(shù)(可以擴(kuò)展到浮點數(shù)和科學(xué)計數(shù)法表示);
3)token用來存放詞法單元的詞法記號。
附加題
擦,我們的密碼設(shè)置才奇葩呢。
要求:
(1)必須要有字母(大小寫都有)、數(shù)字、符號,八個以上字符
(2)密碼每三個月重新設(shè)置一次,新設(shè)置的密碼跟以前所有的密碼重復(fù)的字符不超過三個
沒幾次搞下來就無法設(shè)自己熟悉的字符串了,只好隨機(jī)創(chuàng)建一個,寫到紙上每次抄進(jìn)去。
假設(shè)舊密碼為:by99YL17!
利用正規(guī)式檢測輸入的新密碼是否符合規(guī)定。
上機(jī)要求:
上機(jī)作業(yè)需助教檢查后登記才能獲得上機(jī)分?jǐn)?shù)。
答案
第一題
import string result="" def check(temp):global resultif temp in dict_key:result=result+"("+str(dict_key[temp])+","+ str(temp)+")"+" "else:if len(temp)==0 :result = resultelif temp[0].isdigit():result = result + "(" + "11" + "," + temp + ")" + " "else:result = result +"("+"10"+","+ temp+")"+" " dict_key = {'for': 1, 'if': 2, 'then': '3', "else": 4, "while": 5,"do": 6, "letter(letter+digit)*": 10, "digit digit*": 11,"until": 29, "input": 31, ":": 17,"int": 30, "output": 32} dict_op ={"+": 13, "-": 14, "*": 15, "/": 16,">": 23, ">=": 24,"=": 25, ";": 26, "(": 27, ")": 28, "#": 0, ":=": 18, "<": 20, "<>": 21, "<=": 22} with open("test.txt", "r", encoding='UTF-8') as f:data = f.read() temp="" print(data) for i in data:if i in string.ascii_letters:temp=temp+i# 數(shù)字elif i.isdigit():temp = temp + i# 空格elif i.isspace():if temp!="":check(temp)temp=""# 字符else:check(temp)temp = ""if i in dict_op:result = result + "(" + str(dict_op[i]) + "," + str(i) + ")"+" " print(result)第二題
# 狀態(tài) # 空:起始 # l.小寫字母 # L.大寫字母 # c.符號 # n.數(shù)字 # lL.小寫字母+大寫字母 # lc.小寫字母+符號 # ln.小寫字母+數(shù)字 # Lc.符號+大寫字母 # cn.符號+數(shù)字 # Ln.大寫字母+數(shù)字 # lLc.小寫字母+大寫字母+符號 # lLn 小寫字母+大寫字母+數(shù)字 # lcn.小寫字母+符號+數(shù)字 # Lcn 大寫字母+符號+數(shù)字 # lLcn 滿足條件 import re import string origin_pwd="by99YL17!" pwd = input("輸入密碼:") print(pwd) check_err=3 status="" if len(pwd)<9:print("密碼太短了!!!") else:for ch in pwd:if(check_err<0):print("密碼和舊密碼重復(fù)位數(shù)在3以上")break;elif ch in origin_pwd:--check_errelse:if ch.isupper():if "L" not in status:status+="L"elif ch.islower():if "l" not in status:status+="l"elif ch.isdigit():if "n" not in status:status+="n"else:if "c" not in status:status+="c" print(status) if "l" in status and "L" in status and "n" in status and "c" in status and check_err>=0:print("滿足條件") else:print("不滿足條件")python,行!
總結(jié)
以上是生活随笔為你收集整理的大连理工大学软件学院编译技术课程——MicroC词法分析上机实验的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 农村电子商务专家学者以及来自全国各地的淘
- 下一篇: Project 2003,2007下载