编译原理 实验四 第1关:使用C/C++语言编写PL/0编译程序的语义分析程序

该博客介绍了如何使用C/C++语言编写PL/0编译程序的语义分析部分,生成四元式中间代码。任务包括结合之前的词法分析和语法分析程序,编程完成后在平台上进行测试,通过多个测试用例验证程序的正确性。
摘要由CSDN通过智能技术生成

任务描述

基于第二章的词法分析程序和第三章的语法分析程序,使用C/C++语言编写PL/0编译程序的语义分析程序,并生成四元式形式的中间代码。

编程要求

完成上述编程任务,将C/C++语言源程序复制粘贴到右侧代码编辑器,点击“评测”按钮,运行程序,系统会自动进行结果对比。

测试说明

平台会对你编写的代码进行测试:

测试输入:

  1. const a = 10;
  2. var b, c;
  3. //单行注释
  4. /*
  5. * 多行注释
  6. */
  7. procedure p;
  8. if a <= 10 then
  9. begin
  10. c := b + a;
  11. end;
  12. begin
  13. read(b);
  14. while b # 0 do
  15. begin
  16. call p;
  17. write(2 * c);
  18. read(b);
  19. end;
  20. end.

预期输出:

  1. 语义正确
  2. 中间代码:
  3. (1)(syss,_,_,_)
  4. (2)(const,a,_,_)
  5. (3)(=,10,_,a)
  6. (4)(var,b,_,_)
  7. (5)(var,c,_,_)
  8. (6)(procedure,p,_,_)
  9. (7)(j<=,a,10,$8)
  10. (8)(+,b,a,c)
  11. (9)(ret,_,_,_)
  12. (10)(read,b,_,_)
  13. (11)(j#,b,0,$13)
  14. (12)(j=,b,0,$17)
  15. (13)(call,p,_,_)
  16. (14)(*,2,c,T1)
  17. (15)(write,T1,_,_)
  18. (16)(read,b,_,_)
  19. (17)(syse,_,_,_)
  20. 符号表:
  21. const a 10
  22. var b 0
  23. var c 0
  24. procedure p

测试输入:

  1. const a = 10;
  2. var a, b, c;
  3. procedure p;
  4. if a <= 10 then
  5. begin
  6. c := b + a;
  7. end;
  8. begin
  9. read(b);
  10. while b # 0 do
  11. begin
  12. call p;
  13. write(2 * c);
  14. read(b);
  15. end;
  16. end.

预期输出:

  1. (语义错误,行号:2)

测试输入:

  1. const a = 10;
  2. var b, c;
  3. //单行注释
  4. /*
  5. * 多行注释
  6. */
  7. procedure p;
  8. if a <= 10 then
  9. begin
  10. c := b + a;
  11. end;
  12. begin
  13. read(p);
  14. while b # 0 do
  15. begin
  16. call p;
  17. write(2 * c);
  18. read(b);
  19. end;
  20. end.

预期输出:

  1. (语义错误,行号:16)

测试输入:

  1. const a = 10;
  2. var b, c;
  3. //单行注释
  4. /*
  5. * 多行注释
  6. */
  7. procedure p;
  8. if a <= 10 then
  9. begin
  10. c := b + a;
  11. end;
  12. begin
  13. read(p);
  14. while b # 0 do
  15. begin
  16. call q;
  17. write(2 * c);
  18. read(b);
  19. end;
  20. end.

预期输出:

  1. (语义错误,行号:19)

测试输入:

  1. const a = 10;
  2. var a, b, c;
  3. //单行注释
  4. /*
  5. * 多行注释
  6. */
  7. procedure p;
  8. if a <= 10 then
  9. begin
  10. c := b + a;
  11. end;
  12. begin
  13. read(p);
  14. while b # 0 do
  15. begin
  16. call q;
  17. write(2 * d);
  18. read(b);
  19. end;
  20. end.

预期输出:

  1. (语义错误,行号:2)
  2. (语义错误,行号:16)
  3. (语义错误,行号:19)
  4. (语义错误,行号:20)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
 
 
/* ------------ 全局变量声明定义 ------------ */
/* 单词表 */
typedef struct WordTable{
    int type;           // 所属类型
    char id[20];        // 标识识别符值
    int lineNum;        // 所在的行 
    int flag = 0;
};
WordTable wordTable[100];   // 单词表
char word[20] = "\0";       // 存储单个字符,添加到 wordTable 中
 
/* 符号表 */
typedef struct SymbolTable { 
    char type[20];
    char variable[20];
    int value;
};
SymbolTable symbolTable[50];    // 符号表
 
/* 内部代码 */
typedef struct InterCode {
    char op[20];
    char arg1[20];
    char arg2[20];
    char result[20];
};
InterCode interCode[50];
 
/* 标签表 */
typedef struct LabelTable {
    char name[10];
    int value = -1;
};
LabelTable lableTable[50];  // 标签表
 
int count_grammarError = 0;     // 语法错误个数
int count_semanticError = 0;    // 语义错误个数
int grammarErrorLine = 0;       // 语法错误行号
int quaternionLine = 0;         // 四元式行号
 
int wordIndex = 0;              // 记录当前 wordTable 的下标
int symbolIndex = 0;            // 记录当前 symbolTable 的下标
int lableIndex = 0;             // 记录当前 lableTable 的下标
 
 
char allWord[39][20] ={
    "begin","call","const","do","end","if","odd","procedure",
    "read","then","var","while","write","(",")",",",";",".",
    "+","-","*","/","=","#","<",">","<=",">=",":=","0","1","2","3",
    "4","5","6","7","8","9",
};
char reservedWord[20][20] ={ // 保留字
    "begin","call","const","do","end","if","odd","
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值