一个简单的词法分析器可以采用有限状态自动机来实现。下面是一个Python实现的示例代码:
```python
import re
# 定义关键字、运算符、界限符和注释符号
keywords = ['else', 'if', 'int', 'return', 'void', 'while']
operators = ['+', '-', '*', '/', '<', '<=', '>', '>=', '==', '!=', '=']
delimiters = [';', ',', '(', ')', '[', ']', '{', '}']
comments = ['/*', '*/']
# 定义正则表达式
letter = r'[a-zA-Z]'
digit = r'[0-9]'
id_pattern = r'{0}({0}|{1})*'.format(letter, digit)
num_pattern = r'[-+]?({0}+(\.{0}*)?(e[-+]?{0}+)?)'.format(digit)
comment_pattern = r'/\*(.|\n)*?\*/'
# 定义词法分析器类
class Lexer:
def __init__(self, source):
self.source = source # 待分析的源代码
self.tokens = [] # 存储分析结果的列表
self.errors = [] # 存储错误信息的列表
self.index = 0 # 当前分析的字符位置
def tokenize(self):
while self.index < len(self.source):
c = self.source[self.index]
if c.isspace(): # 跳过空白字符
self.index += 1
elif c.isalpha(): # 处理标识符和关键字
match = re.match(id_pattern, self.source[self.index:])
lexeme = match.group()
self.index += len(lexeme)
if lexeme in keywords:
self.tokens.append(('keyword', lexeme))
else:
self.tokens.append(('identifier', lexeme))
elif c.isdigit() or c == '+' or c == '-': # 处理常数
match = re.match(num_pattern, self.source[self.index:])
lexeme = match.group()
self.index += len(lexeme)
self.tokens.append(('number', lexeme))
elif c in operators: # 处理运算符
self.tokens.append(('operator', c))
self.index += 1
elif c in delimiters: # 处理界限符
self.tokens.append(('delimiter', c))
self.index += 1
elif self.source[self.index:self.index+2] in comments: # 处理注释符号
match = re.match(comment_pattern, self.source[self.index:])
if match:
self.index += len(match.group())
else:
self.errors.append(('error', 'unmatched comment'))
break
else:
self.errors.append(('error', 'unknown character'))
self.index += 1
# 测试代码
if __name__ == '__main__':
source = '''int main() {
int x = 123;
if (x < 0) {
x = -x;
}
return x;
}'''
lexer = Lexer(source)
lexer.tokenize()
print(lexer.tokens)
print(lexer.errors)
```
上述代码实现了一个简单的词法分析器,可以识别关键字、标识符、常数、运算符和界限符,并输出每个词法单元的类型和值。当遇到不可识别的符号时,程序会将其视为词法错误,并将错误信息添加到错误列表中。
对于输入的源代码,程序会按照有限状态自动机进行分析,并将分析结果存储在tokens列表中。程序还提供了一个errors列表,用于存储词法错误信息。在测试代码中,我们使用一个简单的C程序作为测试样例,程序可以正确地识别其中的词法单元,并输出它们的类型和值。