C語言詞法分析器_第1頁
C語言詞法分析器_第2頁
C語言詞法分析器_第3頁
C語言詞法分析器_第4頁
C語言詞法分析器_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、南華大學計算機科學與技術學院 實驗報告南華大學計算機科學與技術學院實 驗 報 告 ( 2016 2017 學年度 第 二 學期 )課程名稱程序設計語言與編譯實驗名稱C語言詞法分析器的設計與實現(xiàn)姓名何星佑學號20154340220專業(yè)樹媒班級2地點教師羅江琴1.實驗目的及要求軟件、硬件環(huán)境Codeblocks 13.12及以上版本W(wǎng)indows xp 及以上版本2.實驗步驟明確語言的詞法規(guī)則,根據(jù)具體情況選取C 語言的一個適當大小的子集,寫出基本保留字、標識符、常數(shù)、運算符、分隔符和程序例。初步編制好程序。3. 實驗內(nèi)容 流程圖 、程序程序Main.cpp#include "globa

2、l.h"#include <ctype.h>int main() char fnameFSIZE; error = fopen("error.txt", "w+"); if(error=NULL) printf("cannot create error.txt!n"); out = fopen("out.txt", "w+"); if(out=NULL) printf("cannot create out.txt!n"); printf("pl

3、ease input filename: n"); scanf("%s", fname); in = fopen(fname, "r"); if(in=NULL) printf("error: cannot open file %sn", fname); return -1; headCh = predeal(in); while(fgets(buf, BSIZE, in)!=NULL) head = headCh; int len = strlen(buf); buflen-1 = '0' start =

4、0; while(headCh != '0' ) while(bufstart=' ' && headCh = ' ') start+; if(headCh = ' ') if(bufstart = '0')break; headCh = bufstart+; if(isalpha(headCh) dealAlpha(); else if(isdigit(headCh) dealDigit(); else if(headCh='/') if(dealNotation()=-1) pr

5、intf("notation too long to analyze, skip this line.n"); fputc('n', out); break; else if(isBorder(headCh) dealBorder(); else if(headCh=''' | headCh ='"') dealChar(headCh); else fprintf(error, "L%dtcannot analyse %cn",line,headCh); headCh = bufstart

6、; start+; line+; flag = 0; headCh = goBlank(in); if(isNotation=0) fprintf(out, "%c%sn", head, buf); isNotation = 0; puts("Everything has done."); writeToken(); writeVarTable(); writeConTable(); fclose(in); fclose(out); fclose(error); printf("錯誤日志ttterror.txtn"); printf(

7、"Token文件ttttoken.txtn"); printf("無注釋頭文件的源文件日志tout.txtn"); printf("常量符號表tttconTable.txtn"); printf("變量符號表tttvarTable.txtn"); return 0;/*預處理*/char predeal(FILE *in) char ch; ch = goBlank(in); while(ch = '#') fgets(buf, BSIZE, in); dealInclude(buf); line+

8、; fputc('n', out); ch = goBlank(in); printf("headers done.n"); return ch;/* deal headers, like #include <.>*/void dealInclude(char *buf) char include15; char ch; int i=9; strncpy(include, buf, 9); include9 = '0' if(strcmp(include, "include <")=0) while(ch

9、=bufi)!='>') i+; if(ch='n') fprintf(error, "L%dtheaders end without '>'n",line); break; else fprintf(error, "L%dtheaders format errorn",line); /*step blanks and count line number*/char goBlank(FILE* in) char ch; do ch = fgetc(in); if(ch='n')

10、line+; fputc('n', out); while(ch =' ' | ch ='n' | ch ='t'); return ch;/*deal begin with alpha*/void dealAlpha() int symbol; int id; char word100; Token token; VarTable varTable; int i; word0 = headCh; for(i=start; isdigit(bufi)|isalpha(bufi); i+) wordi-start+1 = bufi;

11、 wordi-start+1 = '0' start = i; headCh = bufstart; symbol = isKeyword(word); /*not keyword*/ if(symbol = -1) varTable.id = varTableNum; strcpy(varT, word); varTableArrayvarTableNum = varTable; varTableNum+; token.symbol = IDN; sprintf(token.attr, "%d", varTable.id); strcpy

12、(,word); tokenArraytokenNum = token; tokenNum+; /* is keyword*/ else token.symbol = symbol; strcpy(,word); strcpy(token.attr,"-"); tokenArraytokenNum = token; tokenNum+; /*判斷是否是關鍵字*/int isKeyword(char * word) int i; for(i=0; keywordListi0; i+) if(strcmp(word, keywordLis

13、ti)=0) return i+256; return -1;/*將token數(shù)組寫入文件*/void writeToken() FILE* ftoken; int i=0; ftoken = fopen("token.txt", "w+"); if(ftoken=NULL) printf("cannot create file token.txt!n"); for(i=0; i<tokenNum; i+) fprintf(ftoken, "%stt(%d, %s)n", tokenA,t

14、okenArrayi.symbol,tokenArrayi.attr); fclose(ftoken);/*將變量符號表寫入文件*/void writeVarTable() FILE* fvarTable; int i=0; fvarTable = fopen("varTable.txt", "w+"); if(fvarTable=NULL) printf("cannot create file varTable.txt!n"); for(i=0; i<varTableNum; i+) fprintf(fvarTable, &q

15、uot;%stt%dn", varTableA, varTableArrayi.id); fclose(fvarTable);/*將常量符號表寫入文件*/void writeConTable() FILE* fconTable; int i=0; fconTable = fopen("conTable.txt", "w+"); if(fconTable=NULL) printf("cannot create file conTable.txt!n"); for(i=0; i<conTableNum;

16、 i+) fprintf(fconTable, "%stt%dn", conTableA, conTableArrayi.id); fclose(fconTable);/*deal with digit*/void dealDigit() int symbol; int id; char word100; Token token; ConTable conTable; int i; word0 = headCh; for(i=start; isdigit(bufi); i+) wordi-start+1 = bufi; if(bufi='.

17、9;) if(!isdigit(buf+i) start = i; headCh = bufstart; fprintf(error, "L%dtunavailabe floatn",line); return; wordi-start = '.' for(; isdigit(bufi); i+) wordi-start+1 = bufi; wordi-start+1 = '0' start = i; headCh = bufstart; id = isInConTable(word); /*不在常量表里,新加項*/ if(id=-1) co

18、nTable.id = conTableNum; strcpy(conT, word); conTableArrayconTableNum = conTable; conTableNum+; id = conTable.id; token.symbol = FNUM; sprintf(token.attr, "%d", id); strcpy(,word); tokenArraytokenNum = token; tokenNum+; else wordi-start+1 = '0' start = i; headCh

19、= bufstart; id = isInConTable(word); /*不在常量表里,新加項*/ if(id=-1) conTable.id = conTableNum; strcpy(conT, word); conTableArrayconTableNum = conTable; conTableNum+; id = conTable.id; token.symbol = INUM; sprintf(token.attr, "%d", id); strcpy(,word); tokenArraytokenNum = token

20、; tokenNum+; /*處理界符*/void dealBorder() Token token; char s3; int i; s0 = headCh; s1 = bufstart; s2 = '0' if(s1 != '0') for(i = 0 ; borderListi0 ; i+) if(strcmp(s, borderListi) = 0) strcpy(, s); token.symbol = i+400; strcpy(token.attr,"-"); tokenArraytokenNum+ = to

21、ken; start+; headCh = bufstart; return; /*處理單界符*/ s1 = '0' for(i = 0 ; borderListi0 ; i+) if(strcmp(s, borderListi) = 0) strcpy(, s); token.symbol = i+400; strcpy(token.attr,"-"); tokenArraytokenNum+ = token; headCh = bufstart; return; int isBorder(char ch) int i; for(i=0

22、; borderListi0; i+) if(ch=borderListi0) return 1; return 0;/*處理注釋*/int dealNotation() char ch = bufstart; int i; int notationLen=0; Token token; /*除號處理*/ if(ch!='/'&&ch!='*') strcpy(,"/"); strcpy(token.attr,"-"); token.symbol = DIV; tokenArraytok

23、enNum+ = token; start+; headCh = bufstart; return 0; if(ch='/') fputc('n',out); isNotation = 1; headCh='0' return 0; else if(ch='*') for(i=start+1; i+) if(bufi='0') fgets(buf, BSIZE, in); line+; fputc('n', out); start = 0; headCh = '0' i = -1;

24、else while(!(bufi='*'&&bufi+1='/') i+; if(+notationLen = LIMIT_NOTATION) fprintf(error, "L%dtnotation too longn", line); isNotation = 1; return -1; if(bufi+1='0') fgets(buf, BSIZE, in); line+; fputc('n', out); i = 0; fputc('n', out); isNotati

25、on = 1; start = i + 2; headCh = bufstart; return 0; /*處理字符常量*/void dealChar(char ch) Token token; ConTable conTable; int i = start; int id; int j; char word100; word0 = ch; if(ch = ''') if(bufi='') for(j=0; j<12; j+) if(bufi+1=changeListj) word1 = '' word2 = bufi+1; wo

26、rd3 = ''' word4 = '0' strcpy(, word); strcpy(token.attr, "-"); token.symbol = j+500; tokenArraytokenNum+ = token; start = i+3; headCh = bufstart; return; /*error: 轉義字符不合法*/ if(j=12) fprintf(error, "L%dtunavailable change charn", line); else if(bufi+1

27、 != ''') fprintf(error, "L%dtthe length of const char is unavailaben", line); for(i = i+2; bufi!=''' i+); start = i +1; headCh = bufstart; return; else word1 = bufi; word2 = ''' word3 = '0' id = isInConTable(word); /*不在常量表里,新加項*/ if(id=-1) conTab

28、le.id = conTableNum; strcpy(conT, word); conTableArrayconTableNum+ = conTable; id = conTable.id; token.symbol = CCHAR; sprintf(token.attr, "%d", id);/change int to string strcpy(,word); tokenArraytokenNum+ = token; start = i+2; headCh = bufstart; return; else if(ch = 

29、9;"') for(; bufi!='"' i+) wordi-start+1 = bufi; wordi - start + 1 = '"' wordi - start + 2 = '0' id = isInConTable(word); /*不在常量表里*/ if(id=-1) conTable.id = conTableNum; strcpy(conT, word); conTableArrayconTableNum+ = conTable; id = conTable.id; tok

30、en.symbol = CSTR; sprintf(token.attr, "%d", id); strcpy(,word); tokenArraytokenNum+ = token; start = i + 1; headCh = bufstart; /*判斷是否在變量符號表中,返回位置,不在返回-1*/int isInVarTable(char *name) int i; for(i=0; i<varTableNum; i+) if(strcmp(name, varTableA)=0) return varTableArra

31、yi.id; return -1;/*判斷是否在常量符號表中,返回位置,不在返回-1*/int isInConTable(char *name) int i; for(i=0; i<conTableNum; i+) if(strcmp(name, conTableA)=0) return conTableArrayi.id; return -1;Global.h#ifndef GLOBAL_H_INCLUDED#define GLOBAL_H_INCLUDED#include <stdio.h>#include <string.h>#defin

32、e BSIZE 1024#define FSIZE 50#define TSIZE 1024#define VTSIZE 1024#define CTSIZE 1024#define LIMIT_NOTATION 10/*define keyword*/#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#

33、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 TYPED

34、EF 283#define UNION 284#define UNSIGNED 285#define VOLATILE 286#define WHILE 287/*define variables and consts*/#define IDN 300#define INUM 301#define FNUM 302#define CCHAR 303#define CSTR 304/*define border*/#define PLUS 400#define MINUS 401#define MUL 402#define DIV 403#define REM 404#define SEMI 4

35、05#define COM 406#define BLP 407#define BRP 408#define SRP 409#define SLP 410#define BIG 411#define SML 412#define EQU 413#define MLP 414#define MRP 415#define COL 416#define QUE 417#define SIG 418#define NOT 419#define AND 420#define OR 421#define PP 422#define MM 423#define EQEQ 424#define NOTL 42

36、5#define NOTR 426#define DECL 427#define BIGE 428#define SMLE 429#define NOTE 430#define AA 431#define OO 432#define ANDE 433#define MINUE 434#define MULE 435#define DIVE 436#define XOR 437#define RIGHT 438#define LEFT 439#define TURN 440/*define change char*/#define CA 500#define CB 501#define CF 5

37、02#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/*structs*/typedef struct varTable int id; char name100; VarTable;typedef struct conTable int id; char name100; ConTable;typedef struct token char name100; int sym

38、bol; char attr100; Token;/*variables*/FILE *error;FILE *out;FILE *in;int line=1;int tokenNum=0;int varTableNum=0;int conTableNum=0;int start = 0;int forward = 0;int flag =0;int isNotation=0;char bufBSIZE;Token tokenArrayTSIZE;VarTable varTableArrayVTSIZE;ConTable conTableArrayCTSIZE;char headCh;char

39、 head;char *borderList = "+","-","*","/","%","",",","","",")","(",">","<","=","","",":","?","!","&a

40、mp;","|", "+","-","=","/*","*/",":=",">=","<=","!=","&&","|","+=","-=","*=","/=","",">>","<<","&qu

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論