C 语言程序片段: { int i; int j; float[10][10] a; i = 0; while ( i < 10 ) { j = 0; while ( j < 10 ) { a[i][j] = 0.0; j = j + 1; } i = i + 1; } i = 0; while ( i < 10 ) { a[i][i] = 1.0; i = i + 1; } } 分析并理解编译器前端输入(C 语言程序片段)和输出(对应的中间代码片段) 的一一对应关系。
时间: 2023-05-29 08:06:27 浏览: 64
输入(C语言程序片段):
1. 定义了两个整型变量i和j以及一个浮点型二维数组a[10][10];
2. 初始化数组a中所有元素为0;
3. 将数组a的对角线上的元素赋值为1。
输出(对应的中间代码片段):
1. 声明整型变量i和j;
2. 分配10*10的浮点型二维数组a;
3. 将i赋值为0;
4. 进入while循环,循环条件为i<10;
5. 在循环体内,将j赋值为0;
6. 进入while循环,循环条件为j<10;
7. 在循环体内,将a[i][j]赋值为0.0;
8. 将j自增1;
9. 退出while循环;
10. 将i自增1;
11. 退出while循环;
12. 将i赋值为0;
13. 进入while循环,循环条件为i<10;
14. 在循环体内,将a[i][i]赋值为1.0;
15. 将i自增1;
16. 退出while循环。
可以看出,输入和输出之间是一一对应的,输出是对输入的翻译和转换。输入是高级语言,输出是对应的中间代码,中间代码是可以被计算机执行的。这种对应关系是编译器实现的基础。
相关问题
通过以下 C 语言程序片段,逐条语句跟踪程序的执行流程(画出语法分 析树结构, 写入实验报告): C 语言程序片段: { int i; int j; float[10][10] a; i = 0; while ( i < 10 ) { j = 0; while ( j < 10 ) { a[i][j] = 0.0; j = j + 1; } i = i + 1; } i = 0; while ( i < 10 ) { a[i][i] = 1.0; i = i + 1; } }
程序的语法分析树结构如下:
```
├── Block
├── Declaration: int i
├── Declaration: int j
├── Declaration: float[10][10] a
├── Assignment: i = 0
├── While: i < 10
├── Assignment: j = 0
├── While: j < 10
├── Assignment: a[i][j] = 0.0
├── Assignment: j = j + 1
└── Assignment: i = i + 1
└── Assignment: i = 0
├── While: i < 10
├── Assignment: a[i][i] = 1.0
└── Assignment: i = i + 1
```
程序的执行流程如下:
1. 声明变量 i 和 j,以及 10x10 的浮点数数组 a。
2. 将 i 赋值为 0。
3. 进入第一个 while 循环,当 i 小于 10 时执行循环体。
4. 将 j 赋值为 0。
5. 进入第二个 while 循环,当 j 小于 10 时执行循环体。
6. 将 a[i][j] 赋值为 0.0。
7. 将 j 增加 1。
8. 结束第二个 while 循环。
9. 将 i 增加 1。
10. 回到第三步,直到 i 大于等于 10 时退出第一个 while 循环。
11. 将 i 赋值为 0。
12. 进入第二个 while 循环,当 i 小于 10 时执行循环体。
13. 将 a[i][i] 赋值为 1.0。
14. 将 i 增加 1。
15. 回到第十二步,直到 i 大于等于 10 时退出第二个 while 循环。
16. 程序结束。
通过以下 C 语言程序片段,逐条语句跟踪程序的执行流程(画出语法分 析树结构,C 语言程序片段: { int i; int j; float[10][10] a; i = 0; while ( i < 10 ) { j = 0; while ( j < 10 ) { a[i][j] = 0.0; j = j + 1; } i = i + 1; } i = 0; while ( i < 10 ) { a[i][i] = 1.0; i = i + 1; } }
程序代码如下:
```
int i;
int j;
float a[10][10];
i = 0;
while ( i < 10 ) {
j = 0;
while ( j < 10 ) {
a[i][j] = 0.0;
j = j + 1;
}
i = i + 1;
}
i = 0;
while ( i < 10 ) {
a[i][i] = 1.0;
i = i + 1;
}
```
程序的执行流程如下:
1. 定义整型变量 i。
2. 定义整型变量 j。
3. 定义浮点型二维数组 a。
4. 给 i 赋值为 0。
5. 进入 while 循环,判断 i 是否小于 10,是,执行循环体。
6. 给 j 赋值为 0。
7. 进入 while 循环,判断 j 是否小于 10,是,执行循环体。
8. 给 a[i][j] 赋值为 0.0。
9. 给 j 赋值为 j + 1。
10. 退出内层循环。
11. 给 i 赋值为 i + 1。
12. 退出外层循环。
13. 给 i 赋值为 0。
14. 进入 while 循环,判断 i 是否小于 10,是,执行循环体。
15. 给 a[i][i] 赋值为 1.0。
16. 给 i 赋值为 i + 1。
17. 退出循环。
18. 程序结束。
语法分析树结构如下:
```
Program
└── CompoundStmt
├── DeclStmt
│ ├── TypeSpec: int
│ ├── VarDecl: i
│ └── TypeSpec: int
│ └── VarDecl: j
├── DeclStmt
│ ├── TypeSpec: float
│ ├── ArrDecl: a[10][10]
│ └── EmptyExpr
├── ExprStmt
│ ├── VarExpr: i
│ └── IntegerLiteral: 0
├── WhileStmt
│ ├── BinaryOperator: i < 10
│ │ ├── VarExpr: i
│ │ └── IntegerLiteral: 10
│ └── CompoundStmt
│ ├── ExprStmt
│ │ ├── VarExpr: j
│ │ └── IntegerLiteral: 0
│ ├── WhileStmt
│ │ ├── BinaryOperator: j < 10
│ │ │ ├── VarExpr: j
│ │ │ └── IntegerLiteral: 10
│ │ └── CompoundStmt
│ │ ├── ExprStmt
│ │ │ ├── ArraySubscriptExpr: a[i][j]
│ │ │ │ ├── VarExpr: a
│ │ │ │ ├── VarExpr: i
│ │ │ │ └── VarExpr: j
│ │ │ └── FloatingLiteral: 0.0
│ │ └── ExprStmt
│ │ ├── VarExpr: j
│ │ └── BinaryOperator: j + 1
│ │ ├── VarExpr: j
│ │ └── IntegerLiteral: 1
│ └── ExprStmt
│ ├── VarExpr: i
│ └── BinaryOperator: i + 1
│ ├── VarExpr: i
│ └── IntegerLiteral: 1
└── WhileStmt
├── BinaryOperator: i < 10
│ ├── VarExpr: i
│ └── IntegerLiteral: 10
└── CompoundStmt
├── ExprStmt
│ ├── ArraySubscriptExpr: a[i][i]
│ │ ├── VarExpr: a
│ │ ├── VarExpr: i
│ │ └── VarExpr: i
│ └── FloatingLiteral: 1.0
└── ExprStmt
├── VarExpr: i
└── BinaryOperator: i + 1
├── VarExpr: i
└── IntegerLiteral: 1
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)