引言
编译原理实验要求实现简单的词法分析器,正好最近在学习python,就使用python实现Java的词法分析器,功能比较简单,算是一个小小的实验吧。
1.基本符号表设计
采用字典的形式存储基本符号表,字典的键是Java的关键字,字典的值是符种,具体设计如下:
# 基本符号表
tables = {
# 关键字
'abstract': 0, 'assert': 1, 'boolean': 2, 'break': 3, 'byte': 4, 'case': 5, 'catch': 6, 'char': 7, 'class': 8,
'const': 9, 'continue': 10, 'default': 11, 'do': 12, 'double': 13, 'else': 14, 'enum': 15, 'extends': 16,
'final': 17, 'finally': 18, 'float': 19, 'for': 20, 'goto': 21, 'if': 22, 'implements': 23, 'import': 24,
'instanceof': 25, 'int': 26, 'interface': 27, 'long': 28, 'native': 29, 'new': 30, 'package': 31,
'private': 32, 'protected': 33, 'public': 34, 'return': 35, 'short': 36, 'static': 37, 'strictfp': 38,
'super': 39, 'switch': 40, 'synchronized': 41, 'this': 42, 'throw': 43, 'throws': 44, 'transient': 45,
'try': 46, 'void': 47, 'volatile': 48, 'while': 49, 'identify': 50,
# 操作符
'+': 89, '-': 90, '*': 91, '/': 92, '%': 93, '++': 94,
'--': 95, '+=': 117, '-=': 118, '/=': 120, '==':