3

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

 2 years ago
source link: https://segmentfault.com/a/1190000040685360
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

大连理工大学软件学院编译技术课程——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、运算符、分隔符和关键字,词法分析阶段通常被忽略。

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

词法单元词法记号词法单元词法记号for1:17if2:=18then3<20else4<>21while5<=22do6>23letter(letter+digit)*10>=24digit digit*11=25+13;26-14(27*15)28/16#0until29int30input31output32

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

例如:对源程序
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,行!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK