关于python:大连理工大学软件学院编译技术课程MicroC词法分析上机实验

0次阅读

共计 2845 个字符,预计需要花费 8 分钟才能阅读完成。

大连理工大学软件学院编译技术课程——MicroC 词法剖析上机试验

题目

编写词法剖析编译程序

试验目标:对循环语句和条件判断语句编写词法剖析编译程序,只能通过一遍扫描实现。

试验要求:
(1) 关键字:
for if then else while do until int input output
所有关键字都是小写。
(2)运算符和分隔符:
:= + – * / < > <= <> >= ; () #
(2) 其余标识符(ID)和整型常数(NUM),通过以下模式定义:
ID=letter(letter | digit)*
NUM=digit digit*
(4)空格由空白、制表符和换行符组成。空格个别用来分隔 ID、NUM、运算符、分隔符和关键字,词法分析阶段通常被疏忽。

各种词法单元对应的词法记号如下:

词法单元 词法记号 词法单元 词法记号
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

词法分析程序的性能
输出:源程序
输入:二元组(词法记号, 属性值 / 其在符号表中的地位)形成的序列。

例如:对 源程序
int x:=5; if (x>0) then x:=2*x+1/3; else x:=2/x; #
经词法剖析后输入如下序列:
(30, int)(10,’x’)(18, :=) (11,5) (26, ;) (2, if) (27,()……

1. 几点阐明:
(1)关键字表的初值。
关键字作为非凡标识符解决,把它们预先安排在一张表格中(称为关键字表),当扫描程序辨认出标识符,查关键字表。如能查到匹配的单词,则该单词的关键字,否则为个别标识符。要害表为一个字符串数组,其形容如下:
char *keyword[11]={”for”,”if”,”then”,”else”,”while”,”do”,“until”,“int”,“until”,“input”,“output”};

(3) 程序中须要用到的次要变量为 token , id 和 num.
1)id 用来寄存形成词法单元的字符串;
2)num 用来寄存整数(能够扩大到浮点数和迷信计数法示意);
3)token 用来寄存词法单元的词法记号。

do{lexical(); // 将词法单元对应的记号保留到 token 中,属性值保留到 num 或者 id 中
switch(token) {case 11: printf ("(token, %d\n)", num); break; 
case -1: printf("error!\n");break; 
default: printf("(%d,%s)\n", token, id); 
} 
}while (token!=0); 

附加题

擦,咱们的明码设置才奇葩呢。
要求:
(1)必须要有字母(大小写都有)、数字、符号,八个以上字符
(2)明码每三个月从新设置一次,新设置的明码跟以前所有的明码反复的字符不超过三个

没几次搞下来就无奈设本人相熟的字符串了,只好随机创立一个,写到纸上每次抄进去。
假如旧明码为:by99YL17!
利用正规式检测输出的新密码是否符合规定。

上机要求:
上机作业需助教查看后注销能力取得上机分数。

答案

第一题

import string
result=""
def check(temp):
    global result
    if temp in dict_key:
        result=result+"("+str(dict_key[temp])+","+ str(temp)+")"+" "
    else:
        if len(temp)==0 :
            result = result
        elif 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
        # 数字
    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)

第二题

# 状态
# 空: 起始
# l. 小写字母
# L. 大写字母
# c. 符号
# n. 数字
# lL. 小写字母 + 大写字母
# lc. 小写字母 + 符号
# ln. 小写字母 + 数字
# Lc. 符号 + 大写字母
# cn. 符号 + 数字
# Ln. 大写字母 + 数字
# lLc. 小写字母 + 大写字母 + 符号
# lLn 小写字母 + 大写字母 + 数字
# lcn. 小写字母 + 符号 + 数字
# Lcn 大写字母 + 符号 + 数字
# 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("明码和旧明码反复位数在 3 以上")
            break;
        elif ch in origin_pwd:
            --check_err
        else:
            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,行!

正文完
 0