好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

天津理工大学编译原理实验一(共23页).doc

23页
  • 卖家[上传人]:文库****9
  • 文档编号:211024132
  • 上传时间:2021-11-15
  • 文档格式:DOC
  • 文档大小:279KB
  • / 23 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 精选优质文档-----倾情为你奉上实验报告学院(系)名称:计算机与通信工程学院姓名学号专业计算机科学与技术班级实验项目实验一:词法分析课程名称编译原理课程代码实验时间2016/03/172016/03/22实验地点软件实验室7-219批改意见成绩教师签字: 实验内容:实现标准C语言词法分析器实验要求:(1)单词种别编码要求 基本字、运算符、界符:一符一种 标识符:统一为一种; 常量:按类型编码;(2)词法分析工作过程中建立符号表、常量表 并以文本文件形式输出3)词法分析的最后结果以文本文件形式输出实验源代码和心得体会#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 STATIC 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 GREATER 293 // >#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 CBSL 507 #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;Sign sign;Variate VarArr[bufsize];Constant ConArr[bufsize];Sign SigArr[bufsize];//获取读取的文件本行的第一个字符 ,直到找到一个非空格字符 char getfirstc(FILE *in){ char ch=fgetc(in);//fgetc() 函数的作用是读取文件的当前行的一个字符,返回读取的字符 while(ch== ||ch==\n||ch==\t) { if(ch==\n) { line++; fputc(\n,Out);//向输出的文本文件中打印换行 } ch=fgetc(in); } return ch;}//处理读取的本行内容void dealhead(char *buf) { char ch[10]; char cha; char str[bufsize]; int i=0; int j=0; int temp=0; while(i\n",line); break; } } if(temp==1) { ch[j]=buf[i]; j++; if(buf[i]==<) { ch[j]=\0; break; } } else if(temp==2) { ch[j]=buf[i]; j++; if(buf[i+1]== ) { ch[j]=\0; i++; break; } } } i+。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.