实验三算符优先分析法 通过读文件实现的编译原理的java代码和python代码怎么写
时间: 2024-02-09 12:11:15 浏览: 29
下面是算符优先分析法的java代码和python代码,以及读取文件并进行分析的示例:
Java代码实现:
1. 定义符号栈和操作数栈:
```java
Stack<Character> opStack = new Stack<>();
Stack<Integer> numStack = new Stack<>();
```
2. 读取文件内容并进行分析:
```java
// 读取文件内容
File file = new File("input.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
// 对每一行进行分析
int i = 0;
while (i < line.length()) {
char ch = line.charAt(i);
if (Character.isDigit(ch)) {
// 数字直接入操作数栈
int num = ch - '0';
i++;
while (i < line.length() && Character.isDigit(line.charAt(i))) {
num = num * 10 + line.charAt(i) - '0';
i++;
}
numStack.push(num);
} else {
// 符号进行比较
if (opStack.isEmpty() || compare(opStack.peek(), ch) == -1) {
// 当前符号优先级较高,直接入栈
opStack.push(ch);
i++;
} else if (compare(opStack.peek(), ch) == 0) {
// 当前符号与栈顶符号优先级相同,弹出栈顶符号
opStack.pop();
i++;
} else {
// 当前符号优先级较低,弹出栈顶符号并进行运算
char op = opStack.pop();
int b = numStack.pop();
int a = numStack.pop();
int res = calculate(a, b, op);
numStack.push(res);
}
}
}
}
// 结束时对栈中剩余符号依次进行运算
while (!opStack.isEmpty()) {
char op = opStack.pop();
int b = numStack.pop();
int a = numStack.pop();
int res = calculate(a, b, op);
numStack.push(res);
}
// 输出最终结果
System.out.println(numStack.pop());
```
3. 比较符号优先级和运算:
```java
private static int compare(char op1, char op2) {
int p1 = precedence(op1);
int p2 = precedence(op2);
if (p1 < p2) {
return -1;
} else if (p1 > p2) {
return 1;
} else {
return 0;
}
}
private static int precedence(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
private static int calculate(int a, int b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
default:
return 0;
}
}
```
Python代码实现:
1. 定义符号栈和操作数栈:
```python
opStack = []
numStack = []
```
2. 读取文件内容并进行分析:
```python
# 读取文件内容
with open("input.txt", "r") as f:
for line in f:
# 对每一行进行分析
i = 0
while i < len(line):
ch = line[i]
if ch.isdigit():
# 数字直接入操作数栈
num = int(ch)
i += 1
while i < len(line) and line[i].isdigit():
num = num * 10 + int(line[i])
i += 1
numStack.append(num)
else:
# 符号进行比较
if not opStack or compare(opStack[-1], ch) == -1:
# 当前符号优先级较高,直接入栈
opStack.append(ch)
i += 1
elif compare(opStack[-1], ch) == 0:
# 当前符号与栈顶符号优先级相同,弹出栈顶符号
opStack.pop()
i += 1
else:
# 当前符号优先级较低,弹出栈顶符号并进行运算
op = opStack.pop()
b = numStack.pop()
a = numStack.pop()
res = calculate(a, b, op)
numStack.append(res)
# 结束时对栈中剩余符号依次进行运算
while opStack:
op = opStack.pop()
b = numStack.pop()
a = numStack.pop()
res = calculate(a, b, op)
numStack.append(res)
# 输出最终结果
print(numStack.pop())
```
3. 比较符号优先级和运算:
```python
def compare(op1, op2):
p1 = precedence(op1)
p2 = precedence(op2)
if p1 < p2:
return -1
elif p1 > p2:
return 1
else:
return 0
def precedence(op):
if op in ['+', '-']:
return 1
elif op in ['*', '/']:
return 2
else:
return 0
def calculate(a, b, op):
if op == '+':
return a + b
elif op == '-':
return a - b
elif op == '*':
return a * b
elif op == '/':
return a // b
else:
return 0
```
示例文件input.txt内容:
```
2+3*4-5
```
输出结果:
```
9
```