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
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
} 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