设计一个简单的语法分析器,能够将输入的单词序列分析成语法树,支持语法规则、优先级等特性。代码
时间: 2024-03-12 11:46:55 浏览: 71
一个简单的语法分析器
好的,我可以给你一个简单的语法分析器的代码示例,它可以将输入的表达式转换成语法树。这个示例使用了递归下降解析算法。
```python
class Node:
def __init__(self, value, children=None):
self.value = value
self.children = children or []
def __str__(self):
if not self.children:
return str(self.value)
return f"({self.value} {', '.join(str(c) for c in self.children)})"
class Lexer:
def __init__(self, source):
self.source = source.strip()
self.pos = 0
def next_token(self):
if self.pos >= len(self.source):
return None
if self.source[self.pos].isdigit():
start = self.pos
while self.pos < len(self.source) and self.source[self.pos].isdigit():
self.pos += 1
return int(self.source[start:self.pos])
if self.source[self.pos] in "+-*/()":
token = self.source[self.pos]
self.pos += 1
return token
raise ValueError(f"Invalid character: {self.source[self.pos]}")
class Parser:
def __init__(self, source):
self.lexer = Lexer(source)
self.current_token = self.lexer.next_token()
def parse(self):
return self.parse_expression()
def parse_expression(self):
node = self.parse_term()
while self.current_token in "+-":
operator = self.current_token
self.consume_token()
right = self.parse_term()
node = Node(operator, [node, right])
return node
def parse_term(self):
node = self.parse_factor()
while self.current_token in "*/":
operator = self.current_token
self.consume_token()
right = self.parse_factor()
node = Node(operator, [node, right])
return node
def parse_factor(self):
if self.current_token == "(":
self.consume_token()
node = self.parse_expression()
if self.current_token != ")":
raise ValueError("Expected ')'")
self.consume_token()
return node
if isinstance(self.current_token, int):
node = Node(self.current_token)
self.consume_token()
return node
raise ValueError(f"Expected a number or '('")
def consume_token(self):
self.current_token = self.lexer.next_token()
if __name__ == '__main__':
parser = Parser("2 + 3 * (4 - 1)")
ast = parser.parse()
print(ast)
```
这个示例将输入的表达式转换成语法树,并输出语法树的字符串表示。例如,输入 "2 + 3 * (4 - 1)",输出 "(+ 2 (* 3 (- 4 1)))"。
阅读全文