Compilation principle

xiaoxiao2021-03-06  19

The analysis of the compilation principles of this semester has been finished. The front end of the entire compiler mainly includes two parts of the word methodology and grammar analysis. Now you start working with the front end of the compiler with the C language. The following is a description of the program. 1) This program is the lexical analysis part of the lexical analysis. 2) Only support unsigned integer types. Advanced features such as arrays and pointers are not supported. 3) Do not support functions. Note is "//" of C . 4) Compile with VC . Net.

//Lexical.h# ifefndef _lexical_h_ # define _lexical_h _ // state machine status enumeration typef enum {start = 0, unknown = 0, Comment, Number, Identi, IF, Else, END, REPEAT, UNTIL, ASSIGN, Equal, Plus , MINUS, MULTI, DIVIDE, LESS, GREATER, LPAREN, RPAREN, SEMI, EXPRESSION} LexTokenType; // state changeover structure typedef struct {LexTokenType beg; char Domainbeg; char Domainend; LexTokenType end;} TransState; // keyword description of the structure typedef struct {char strExp [10]; LexTokenType type;} TokenDes; // mark structure typedef struct {char * strName; LexTokenType type;} LexToken; LexToken * GetNextToken (char * pSource); # endif // _ LEXICAL_H _ // Lexical. C # include #include #include #include "lexical.h" // limited state machine status conversion Description TransState TransState TransState Transstate TransState TransState TransState TransState TransState Transstate TransState TransState TransState TransState TransState Transstate TransState Transstate TransState TransState TransState TransState Transstate '9', Number}, {Identi, 'A', 'Z', Identi}, {Identi, 'A', 'Z', Identi}, {Identi, '0', '9', Identi}, { Start, '', '', start}, {start, '/ t', '/ t', start}, {start, '/ n', '/ n', start}, {start, '/ r' , '/ r', start}, {start, '0', '9', number}, {start, 'a', 'z', ide}, {start, 'a', 'z', Identi} , {Start, ' ', ' ', Plus}, {Start, '-', '-', min US}, {start, '*', '*', multi}, {start, '/', '/', divide}, {start, '<', '<', less}, {start, '> ','> ', Greater}, {start,' (',' (')', ')', ', rparen}, {start,'; ','; ', SEMI} , {Start, '=', '=', assign}, {divide, '/', '/', comment}, {comment, '/ n', '/ n', start}, {Comment, '/ N '

1, 127, Comment}, {Comment, 1, '/ N'-1, Comment}, {Comment, -128, -1, Comment}, {Assign,' = ',' = ', Equal},}; // Keywords (reserved words) describe tokendes reserve [] = {{"if", if}, {"else", else}, {"end", end}, {"repeat", repeat}, {"uter", Until},}; lextoken * psource {// Consider speed, no detection of File legitimacy static int Lineno = 0; static int posnow = 0; static int sizeOstateTrans = SizeOf (transstate); static int SizeReserve = sizeof (reserve) / sizeof (TokenDes); static int SizeToken = sizeof (LexToken); LexToken * ptoken = NULL; LexTokenType CurState = START; int PosStart = PosNow; int i, j, tokenlen = 0; char ch ; Ptoken = (lextoken *) malloc (sizetoken); / / omitting error Check while (1) {ch = psource [POSNOW]; if (ch == '/ n') // If you encounter the carriage return character, line number Add 1 LINENO ; for (i = 0; i = trans [i] .domainbeg) && (ch <= Trans [i ] .Domainend) / / Sales this state conversion {curState = trans [i] .end; // Convert to This state break; // jump out of the for loop, ready to see the next character}}}}}} // Did not find a suitable status conversion {if (curState = 0) // If started at START status {IF ( CH == '/ 0') // encountered file tail {free (ptoken); return null;} ptoken-> type = unknown; // Determined as: encountered unknown character ptoken-> strname = null; return ptoken;

} Else // find a new mark {if (curState == Comment) // This situation occurs at the end of the code to end the end of the comment {free (pturn null;} tokenlen = posnow - posstart; ptoken-> strname = (char *) Malloc (PSource Posstart, Tokenlen); psource posstart, tokenlen; ptoken-> strname [tokenlen] = '/ 0'; ptoken-> type = curState; if (ptoken-> TYPE == Identi) // If the mark is considered to be identifier {for (j = 0; j strname, reserve [j] .strexp)) // If Find this identifier {ptoken-> type = reference [j] .type; break;}}} rett ptoken;}}} Return Ptoken;}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} Tate == start) // If converted to the START status posStart = posnow;}}} /// main.c #}} /// main.c # include #include #include "lexical.h" int main ) {Handle File = Createfile ("Source.txt", Generic_read, 0, Null, Open_EXISTING, 0, NULL); long size = getFileSize (file, null); char * pBuffer = (char *) Malloc (size 1) Long sizenow; readfile (file, pbuffer, size, & sizenow, null); pBuffer [size] = '/ 0'; while (ptoken = getNextToken (PBuffer) {Printf ("% 10s,% D / N ", ptoken-> strname, ptoken-> type);

转载请注明原文地址:https://www.9cbs.com/read-39879.html

New Post(0)