编写如下 pascal 子集的词法分析程序
时间: 2023-08-08 17:06:14 浏览: 167
基于C语言实现小子集程序的词法分析【100013134】
以下是一个简单的Pascal子集的词法分析程序示例:
```python
# 定义保留字集合
reserved = {
'program': 'PROGRAM',
'var': 'VAR',
'integer': 'INTEGER',
'real': 'REAL',
'begin': 'BEGIN',
'end': 'END'
# 其他保留字
}
# 定义Token类
class Token:
def __init__(self, type, value):
self.type = type
self.value = value
def __str__(self):
return 'Token({type}, {value})'.format(
type=self.type,
value=repr(self.value)
)
# 定义词法分析器类
class Lexer:
def __init__(self, text):
self.text = text
self.pos = 0
self.current_char = self.text[self.pos]
def error(self):
raise Exception('Invalid character')
def advance(self):
self.pos += 1
if self.pos > len(self.text) - 1:
self.current_char = None
else:
self.current_char = self.text[self.pos]
def skip_whitespace(self):
while self.current_char is not None and self.current_char.isspace():
self.advance()
def get_integer(self):
result = ''
while self.current_char is not None and self.current_char.isdigit():
result += self.current_char
self.advance()
return int(result)
def get_real(self):
result = ''
while self.current_char is not None and (self.current_char.isdigit() or self.current_char == '.'):
result += self.current_char
self.advance()
return float(result)
def get_id(self):
result = ''
while self.current_char is not None and (self.current_char.isalnum() or self.current_char == '_'):
result += self.current_char
self.advance()
token_type = reserved.get(result, 'ID')
return Token(token_type, result)
def get_next_token(self):
while self.current_char is not None:
if self.current_char.isspace():
self.skip_whitespace()
continue
if self.current_char.isdigit():
return Token('INTEGER', self.get_integer())
if self.current_char.isalpha() or self.current_char == '_':
return self.get_id()
if self.current_char == '.':
self.advance()
return Token('DOT', '.')
if self.current_char == ':' and self.text[self.pos+1] == '=':
self.advance()
self.advance()
return Token('ASSIGN', ':=')
if self.current_char == ';':
self.advance()
return Token('SEMI', ';')
if self.current_char == ',':
self.advance()
return Token('COMMA', ',')
if self.current_char == '+':
self.advance()
return Token('PLUS', '+')
if self.current_char == '-':
self.advance()
return Token('MINUS', '-')
if self.current_char == '*':
self.advance()
return Token('MUL', '*')
if self.current_char == '/':
self.advance()
return Token('DIV', '/')
if self.current_char == '(':
self.advance()
return Token('LPAREN', '(')
if self.current_char == ')':
self.advance()
return Token('RPAREN', ')')
self.error()
return Token('EOF', None)
```
这个词法分析器可以处理整数、实数、保留字、标识符、运算符和分隔符。你可以在这个基础上进行扩展,例如添加其他类型的字面量或运算符。
阅读全文