【python】PLY词法分析实验「编译原理」

编写程序,能够把如下程序中的词法单元都识别出来

int asd = 0;
int bc = 10;
while ( asd < bc)
{
	if(bc - asd < 2)
		printf("they are close.");
	asd = asd + 1;
}

代码

import ply.lex as lex

# C语言保留字
reserved = {
    'if' : 'IF',
    'then' : 'THEN',
    'else' : 'ELSE',
    'while' : 'WHILE',
    'int' : 'INT',
    'printf' : 'PRINTF'
}
tokens = ['LPAREN', 'RPAREN', 'DIVIDE', 'TIMES', 'MINUS', 'PLUS', 'NUMBER', 'FENHAO', 'DENGHAO', 'XIAOYU', 'DAYU', 'ZUODAKUOHAO', 'YOUDAKUOHAO', 'YINHAO', 'DIANHAO', 'ID'] + list(reserved.values())

# ID规则
def t_ID(t):
    r'[a-zA-Z_][a-zA-Z_0-9]*'
    t.type = reserved.get(t.value,'ID') # Check for reserved words
    return t

# 规则
t_PLUS    = r'\+'
t_MINUS   = r'-'
t_TIMES   = r'\*'
t_DIVIDE  = r'/'
t_LPAREN  = r'\('
t_RPAREN  = r'\)'
t_FENHAO  = r';'
t_DENGHAO = r'='
t_XIAOYU = r'<'
t_DAYU = r'>'
t_ZUODAKUOHAO = r'{'
t_YOUDAKUOHAO = r'}'
t_YINHAO = r'"'
t_DIANHAO = r'\.'

# 规则
def t_NUMBER(t):
	# 识别数字
    r'\d+'
    t.value = int(t.value)    
    return t

# 对于空行的规则
def t_newline(t):
    r'\n+'
    t.lexer.lineno += len(t.value)

# 忽略空格
t_ignore  = ' \t'

# 输出错误的规则
def t_error(t):
    print ("Illegal character '%s'" % t.value[0])
    t.lexer.skip(1)

# Build the lexer
lexer = lex.lex()

# 测试数据
data = '''
int asd = 0;
int bc = 10;
while ( asd < bc)
{
	if(bc - asd < 2)
		printf("they are close.");
	asd = asd + 1;
}
'''

lexer.input(data)

while True:
	tok = lexer.token()
	if not tok: break 
	print (tok)

结果

在这里插入图片描述


新手上路,有错请指正。

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,词法分析器是一个编译器非常重要的一部分,它用于将输入的程序代码分解成各个单词(Token),并进行标识符、关键字、常量等信息的识别与归类。在Python,可以使用Lex/Yacc工具来实现词法分析器的设计。下面是一个简单的示例代码,用于实现一个简单的词法分析器: 1. 首先,安装Pythonply库: ``` pip install ply ``` 2. 然后,编写词法分析器的规则: ```python import ply.lex as lex # 定义关键字 keywords = { 'if': 'IF', 'else': 'ELSE', 'while': 'WHILE', 'for': 'FOR', 'int': 'INT', 'float': 'FLOAT', 'char': 'CHAR', 'void': 'VOID', } # 定义符号 symbols = [ 'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'LPAREN', 'RPAREN', 'LBRACE', 'RBRACE', 'SEMICOLON', 'COMMA', ] # 定义规则 tokens = list(keywords.values()) + symbols + [ 'ID', 'NUM', 'CHARACTER', ] t_PLUS = r'\+' t_MINUS = r'-' t_TIMES = r'\*' t_DIVIDE = r'/' t_LPAREN = r'\(' t_RPAREN = r'\)' t_LBRACE = r'\{' t_RBRACE = r'\}' t_SEMICOLON = r';' t_COMMA = r',' t_ignore = ' \t' def t_ID(t): r'[a-zA-Z_][a-zA-Z0-9_]*' t.type = keywords.get(t.value, 'ID') return t def t_NUM(t): r'\d+' t.value = int(t.value) return t def t_CHARACTER(t): r'\'[a-zA-Z0-9_]\'' t.value = t.value[1] return t def t_newline(t): r'\n+' t.lexer.lineno += len(t.value) def t_error(t): print("Illegal character '%s'" % t.value[0]) t.lexer.skip(1) lexer = lex.lex() ``` 3. 最后,编写测试代码,用于对输入的程序代码进行词法分析: ```python if __name__ == '__main__': code = """ int main() { int a = 10; int b = 20; int c = a + b; printf("%d", c); return 0; } """ lexer.input(code) while True: tok = lexer.token() if not tok: break print(tok) ``` 运行上述测试代码后,就可以看到词法分析器的输出结果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Khalil三省

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值