天津理工大学编译原理实验一
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验报告学院(系)名称:计算机与通信工程学院
实验源代码和心得体会
#include
#include
#include
#define bufsize 1024
//关键字
#define INCLUDE 256
#define AUTO 257
#define BREAK 258
#define CASE 259
#define CHAR 260
#define CONST 261
#define CONTINUE 262
#define DEFAULT 263
#define DO 264
#define DOUBLE 265
#define ELSE 266
#define ENUM 267
#define EXTERN 268
#define FLOAT 269
#define FOR 270
#define GOTO 271
#define IF 272
#define INT 273
#define LONG 274
#define REGISTER 275
#define RETURN 276
#define SHORT 277
#define SIGNED 278
#define SIZEOF 279
#define STA TIC 280
#define STRUCT 281
#define SWITCH 282
#define TYPEDEF 283
#define UNION 284
#define UNSIGNED 285
#define VOLATILE 286
#define WHILE 287
//运算符
#define PLUS 288 // +
#define MINUS 289 // -
#define MUL 290 // * #define DIV 291 // /
#define REMAIN 292 // %
#define LESS 294 // <
#define EQUAL 295 // =
#define MISTAKE 296 // ! #define AND 297 // &
#define OR 298 // |
#define PP 299 // ++
#define MM 300 // --
#define EE 301 // ==
#define GE 302 // >=
#define LE 303 // <=
#define MISE 304 // !=
#define AA 305 // &&
#define OO 306 // ||
#define PE 307 // +=
#define MINUSE 308 // -=
#define MULE 309 // *=
#define DIVE 310 // /=
#define POW 311 // ^ // 界符
#define SEMIC 312 // ;
#define COMMA 313 // ,
#define MULANNO_L 314 // /* #define MULANNO_R 315 // */ #define BRACE_L 316 // { #define BRACE_R 317 // } #define BRAKET_L 318 // (
#define BRAKET_R 319 // )
#define MIDBRA_L 320 // [
#define MIDBRA_R 321 // ]
#define ONE_ANNO 322 // //
//标识符和常量符
#define TAG 400
#define CONINT 401
#define CONFLOAT 402
#define CONCHAR 403
#define CONSTRING 404
//转义字符和字符串
#define CA 500
#define CB 501
#define CF 502
#define CN 503
#define CR 504
#define CT 505
#define CV 506
#define CQUE 508
#define CDQM 509
#define CQM 510
#define ZERO 511
using namespace std;
typedef struct Variate{//变量标识符
int id;
char name[50];
}Variate;
typedef struct Constant{//常量
int id;
char name[50];
}Constant;
typedef struct Sign{
char name[100];
int sym;
char attr[100];
}Sign;
const char *keywordTable[]={"include","auto","break","case","char","const","continue",
"default","do","double","else","enum","extern","float","for",
"goto","if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union","unsigned",
"volatile","while","","##"};//##作用是判断是否结束
const char *operateTable[]={"+","-","*","/","%",">","<","=","!","&","|","++","--",
"==",">=","<=","!=","&&","||","+=","-=","*=","/=","^","##"}; const char *borderTable[]={";",",","/*","*/","{","}","(",")","[","]","//","##"};
const char changeList[12]={'a','b','f','n','r','t','v','\\','?','"','\'','0'};
FILE *in;
FILE *Out;
FILE *Error;
int line=1; //用于输出错误的行数或者其他情况。默认值是从1 开始,为第一行char buf[bufsize]; //存储读取的一行的字符串
char firchar; //头文件下第一个字符
char Char;
int start=0;
int VariateNum=0;//记录变量的个数,减去1
int ConstantNum=0;//记录常量的个数
int SignNum=0;//记录标记的个数
int notation=1;//记录是否找到多行注释的另一半*/ ,默认值是1 即为有另一半
int isNotation=0;//判断是否是在注释行内0 不是,1 单行注释2多行注释
bool last=false;
int late=0;
Variate var;
Constant con;