Compilation Principles Courses

xiaoxiao2021-03-20  206

Compilation principle course design. Pascal lexical analyzer. Now I think about what I have learned again!

/ ******* Pascal lexical analyzer version 1.0 ******** **** Author: Hainucrzay ****** **** Email: hainucrazy@gmail.com ** ** ** All Rights Reserved (C) 2005.12 ** ************************************************** **** /

//pre_define.h

#include #include #include #include #include

#define LT 1 # Define Le 6 # Define EQ 10 # Define Ne 14 # Define GT 15 # Define GE 20 # Define Bh 21 // Branch Section ';' # Define Ca 22 // Comma Comma ',' # define Co 23 // COLON colon ':' # Define MU 24 // Multiple '*' # define lb 26 // Left bracket '(' #define rb 27 // right backet ')' # define as 30 // Assign: = # define mi 44 // minus '-' # define ps 45 // Plus ' ' # define di 46 // DIVID '/' // Structure arrays: struct keyword {// symbol table char * WORD; Int value;} keytab [] = {"and", 0, "begin", 2, "bool", 3, // boolean "Case", 4, "const", "do", 7, "ELSE "8," end "," false ", 11, // false" for ", 12," function ", 13," ing ", 16," integer ", 17, // integer" label ", 18, "Mod", 25, "NOT", 28, "OR", 29, "Procedure", 31, "Program", 32, "True", 37, // true "Then", 38, "Type" , 39, "Until", 40, "Var", 41, "While", 42, "With", 43,};

//scanner.cpp

#include "pre_define.h"

// Character array token, used to store various character char token [20] in a word essay; char * err1 = "Identifier overflowed! / N"; // nkeys: Calculate the number of elements in the keytable [] arrayint Nkeys = sizeof keytab / sizeof keytab [0]; int Lookup (char * keyword); // Dual Different Method Finding Keywords INT Main (int Argc, char * argv []) {file * srcfile = NULL; int CH; int i; // token [i]: max (i) = 20 int flag; // determines whether the search function successfully returns int countID = 50; // The category of the keyword has been given, and the identifier class code is unified Code INT countnum = 51; // Integer number of category unified code

IF (Argc! = 2) {Printf ("USAGE:% S% S", Argv [0], "src-file-name / n");} else {// 1ELSE SRCFILE = FOPEN (Argv [1], "r"); if (srcfile == null) {Printf ("can not open text file /"% s / "!", argv [1]);} else {// 2ELSE COUT << Endl << "* ****** PASCAL lexical analyzer v1.0 ************ / n "<<" ******* Author: Hainucrazy Li Yang ******* *** / n "<<" ******* STUDENTNO: 2003724035 *********** / N "<<" ****** email: hainucrazy@gmail.com ******** / n "<< Endl <<" The code of the program has been splited in Token As Follow: "<< Endl <<" General Style: (, [ Property]) "<< Endl; while ((ch = fgetc (srcfile))! = EOF) {// 1WHile IF (Isspace (CH)) // IF is a Whitespace Character Continue; ELSE IF (Isalpha (CH)) / * IT Must Be a Identifier * / {// 1ELSEIF token [0] = CH; CH = fgetc (srcfile); i = 1; WHILE (IsalNum (CH)) // an alphapleric character {token [i] = ch ; i ; ch = fgetc (srcfile);} if (i <20) {token [i] = '/ 0'; fseek (srcfile, -1, seek_cur); // Retract IF ((Flag = Lookup (token))> = 0) // Reference to the keywords printf ("% s / t (% d, -) / n", token, keytab [flag] .value); else // is A Identifier Printf ("% S / T (% D,% S) / N", token, countid, token;} else printf ("% s", err1);} // 1ELSE IF ELSE IF (Isdigit (ch )) {// 2ELSEIF token [0] = CH; ch = fgetc (srcfile); i = 1; while (isdigit (ch)) {token [i] = ch; i ; ch = fgetc (srcfile);} / / while if (i <20) {token [i] = '

/ 0 '; fseek (srcfile, -1, seek_cur); // Retract if (' 0 '== token [0]) Printf ("% s / t (% s) / n", "invalid num", token ); // else - 0 is the 1st Digit Num else Printf ("% S / T (% D,% s) / n", token, countnum, token;} else printf ("% s", err1) } // 2ELSEIF Else Switch (CH) {CASE '<': CH = fgetc (srcfile); if (CH == '=') Printf ("% S / T (% D, -) / N", " <= ", Le); ELSE IF ("% S / T (% D, -) / N "," <> ",", "," <>, ne); else {fseek (srcfile, -1 , Seek_cur); // Retract Printf ("% C / T (% D, -) / N", '<', lt);} Break; case ':': CH = fgetc (srcfile); if (CH = = '=') Printf ("% S / T (% D, -) / n", ": =", as); else {fseek (srcfile, -1, seek_cur); Printf ("% C / T % D, -) / N ", ':', CO);} Break; Case '>': CH = fgetc (srcfile); if (CH == '=') Printf ("% S / T (% D) , -) / n "," ge "); else {fseek (srcfile, -1, seek_cur); Printf ("% C / T (% D, -) / n ", '>', gt);} Break Case '=': Printf ("% C / T (% D, -) / N", '=', EQ); Break; Case ';': Printf ("% C / T (% D, -) / N", ';', BH); Break; Case ',': Printf ("% C / T (% D, -) / n ", ',', ca); break; cas '(': Printf ("% C / T (% D, -) / n ", '(', lb); Break; Case ')': Printf "% C / T (% D, -) / N", ')', RB); Break; Case '-': Printf ("% C / T (% D, -) / N", '-', Mi); Break; Case ' '

: Printf ("% C / T (% D, -) / N", ' ', PS); Break; Case '*': Printf ("% C / T (% D, -) / N", ' * ', Mu); Break; Case' / ': Printf ("% C / T (% D, -) / N",' / ', di); Break; default: printf ("Symbol (% C) Not Defined yet / n ", ch); break;} // switch} // 1WHILE} // 2ELSE} // 1ELSE fclose (srcfile); Return 1;} int Lookup (char * keyword)

{

INT LOW = 0, high = nkeys - 1, mid; int {mid = (low high) / 2; / * int strcmp (lpctstr lpstr1, * lpctstr lpstr2); * Returns ZERO If the strings are identical. * / if ((rtnval = strcmp (keyword, keytab [mid] .word)) == 0) Return Mid; Else IF (Rtnval> 0) / * Returns A Positive Value if the * / low = MID 1; / * string pointed to by lpstr1 is greater * / / / * THAN THAT POINTED TO by lpstr2. * / else high = mid - 1; / * returns a negetive value if the * / / * string pointed to by LPSTR1 IS LESS * / / * THAN THAT POINTED TO by lpstr2. * /} Return -1;

}

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

New Post(0)