Simple lexical analyzer implementation
Compile analyzerTwo methodsOne is to recognize words through DFA, and the other is to recognize words by writing programs directly. This program uses DFA to recognize words.
DFA Implementation MethodThe general idea is the same as that in the book. In the program, two-dimensional arrays represent the state transition matrix and one-dimensional arrays represent the final state.
To implement a lexical editingFunctionIt mainly includes the following:
Identifies identifiers, keywords, numbers, and operators, filters comments, and identifies program errors.
Instructions for use:
The input of this program is read from the in.txt file in the current directory, and the output is a series of Binary
# Include
# Include
# Include
// Use a pointer instead of a two-dimensional array, so that you do not need to specify the string length, but you cannot modify the content of char * key [] = {"int", "char ", "float", "double", "void", "const", "for", "if", "else", "then", "while", "switch ", "break", "main", "return"}; char buffer [20]; // store the currently recognized word char * identifier [50]; char * num [50]; int ident_num; // number of characters int number; // number int judgement_num (char c) {if (c> = '0' & c <= '9') return 0; else if (c = '. ') return 1; return-1;} int judge (char c) {if (c =' _ ') re Turn 2; else if (c> = '0' & c <= '9') return 1; else if (c> = 'A' & c <= 'Z' | c> = 'A' & c <= 'Z') return 0; return-1;} // next_ I and next_j indicate the current status of the move table and the received characters. // width indicates the number of States that may be encountered in each status, length indicates the size of the final state set int DFA (int begin, int move [], int width, int final [], int length, int (* judge) (char )) {int len = 0, next_ I, next_j, tmp; char next_char; memset (buffer, 0, sizeof (buffer); next_char = getchar (); next_ I = begin; while (next_j = judge (next_char ))! =-1) {tmp = next_ I; next_ I = move [next_ I * width + next_j]; if (next_ I =-1) {printf ("move [% d] [% d] has not next state \ n", tmp, next_j); return-1;} buffer [len ++] = next_char; next_char = getchar ();} ungetc (next_char, stdin); buffer [len] = '\ 0'; for (int I = 0; I
'| C =' <') {next_char = getchar (); if (next_char =') printf ("(rlop, % c =) \ n ", c); else {printf ("(rlop, % c) \ n", c); ungetc (next_char, stdin) ;}} else if (c = '! ') {Next_char = getchar (); if (next_char =') printf ("(! =, _) \ N "); else {printf (" (not, _) \ n "); ungetc (next_char, stdin );}} else if (c = '|') {next_char = getchar (); if (next_char = '|') printf ("(or, |) \ n "); else {ungetc (next_char, stdin) ;}} else if (c = '&') {next_char = getchar (); if (next_char = '&') printf ("(and, &) \ n"); else {ungetc (next_char, stdin );}} else if (c = '| c =' ('| c = ') '| c =' ['| c ='] '| c ='; '| c = ', '| c =' {'| c ='} ') {printf ("(% c, _) \ n", c );}} void is_digit () {int begin = 0; int move [] = {1,-,-1}; int final [] = }; int result =-1; result = DFA (begin, move, 2, final, 2, judgement_num); if (result =-1) {printf ("digit DFA error \ n"); exit (-1);} else if (result = 0) {// printf ("% s \ n ", buffer); for (int I = 0; I