CACU interpreter C script code

xiaoxiao2021-03-06  66

#define Lit 1 # Define Lod 2 # Define Sto 3 # Define Cal 4 # Define Int 5 # Define JMP 6 # Define JPC 7 # define opr 8 # define Nul EndArray / * Demonstration Stack Computer Target Instruction Set Instruction Format: FL A1 . LIT: Take the constant to the top of the running stack, the A domain is a constant value 2. LOD: Put the variable on the top of the stack. The A domain is a variable relative position in the illustrated layer, and L is the layer difference value of the calling layer and the explanatory layer. 3. STO: Sends the content of the top of the stack into a variable unit. A, the meaning of the L domain with the LOD instruction. 4. Cal: The command of the calling process. A is the target program entry address of the called process, L is a layer difference 5. INT: Develops the data area in the running stack for the calling process (or main program). The number of units A domain is open. 6. JMP: Unconditional transfer command, A is the steering address. 7. JPC: Condition transfer instruction, when the boolean value of the top of the stack is non-real, the address of the stencil is the address of the A domain, otherwise it will be performed. 8. OPR: Relationship operation and arithmetic operation instructions. The specific operation is given by the A domain value to 0 RETURN 1 - (reverse) 2 3 - (reduction) 4 * 5/6 True 7 8 == 9 <> 10 <11> = 12> 13 <= * ////

/ * ENBF in the PL0 language indicates that :: = . :: = [] [] :: = { } : = VAR {, :: = { | } :: = {; }; :: = func ; :: = | | | | | | :: = : = :: = beg {, :: = :: = [ | -] { }; :: = { } :: = | '(' Expression ') : = | - : = * | / : == | # | <| <= |> |> = :: = if Then :: while DO : = Read '(' {, } ')' : = Write '(' {, } ')' : = a | b ... x | y | z :: <>> :: = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 * / '' // Variables There are three types, num, string, func / * error: 99 variable names excessive 32 floors When the number is too deep 5 function name, add a divided number 11 Undefined identifier 15 Not a function 16 function parameters No "," Split 34 function call parentheses is not 48 reserved words That too much 49 in the real number of e-numbers * / # define sp 0 # define dot 1 # define eql 2 # define plus 3 # define

# Define EOF.

# Define Maxidt 8 # Define Maxvar 100 // Every Block Can Have 100 Variables # Define Norw 16 # Define TXMAX 100 // Up to 100 Identifiers # define Al 10 # Define Nmax 14 # Define Amax 2047 # Define Levmax 3 # Define CXMAX 2000 // Up to 2000 instructions # define stacksize 5000 # Define Maxchar 5000

/ * ------------------------------------------ ----- * / type, foot; int adj bend; char * strval; char name [maxidt]; char kind; // v (variable) OR F (FUNC) S ( String) or n (num)} TBL; typedef struct {INT F; // Instruct command number INT L; // Level hierarchical int a; // addition} instruction; typedef struct {int size; //how many syms in set Now char Sym [Maxvar] [MAXIDT]; // Sym Set} Symset; INT STRCPY (Char *, Char *); Int strcmp (char *, char *);

/ * ----------------------------- Variable Declaration ---------------- ------- * / ldouble g_num; char * g_str;

INT LL, CC, Err, Num, KK, CX, Line [81]; INT S; CHAR ID [MAXIDT]; // Current Identifier CHAR SYM [MAXIDT]; // Current identifier type Char A [MaxIDT]; CHAR CH;

CHAR text [MAXCHAR]; int PText;

INSTRUCTION CODE [CXMAX 1]; TBL Table [TXMAX 1] = {0};

#define func 3864 # Define if 43 # define the 8436 # Define else 3573

#define whil 9445 # Define do 36 # define beg 234 # Define end 343 # Define shif 7447

#define Writ 9748 # Define Read 7323

#define and 263 # Define or 67 # define not 668

#define Usesym 99

#define beginsym 120 # Define ifsym 121 # Define WhileSym 122 # Define ReadSym 123 # Define WriteSym 124 # Define Dosym 127

Char * Word [] = {0, "Beg", "Else", "End", "IF", "FUNC", "Read", "" "Var", "Whil", "WRIT", " Do "," * "}; int wsym [] = {0, Begsym, ElseSYM, EndSym, IFSYM, Funcsym, ReadSym, THENSYM, VARSYM, WHILESYM, WRITESYM, DOSYM," * "}

#define period 100 # define LT 101 # define gt 102 # define le 103 # define ge 104 # define lp 105 # define plus 107 # define Times 107 # define rp 108 # define semi 109 # define minus 110 # define slash 111 # define And 112 # define or 113 # define NOT 114 # Define Ne 115 # Define EQ 116 # Define Comma 117int Ssym [] = = {Period, LT, GT, Le, GE, LP, Plus, Times, RP, SEMI, Minus, Slash , And, OR, NOT, NE, EQ, COMMA};

#define varsym 118 # Define funcsym 119 # Define endarrayint declbegsys [] = {varsym, funcsym, endarray};

INT stats [] = {Beginsym, IFSYM, WhileSym, ReadSym, Writesym, Endarray};

#define Ident 125 # Define Num 126 # Define LP 105INT Facbegsys [] = {Ident, Num, LP, EndaRray};

// after a black, follow sym can appearint fsysa [] = {Period, Varsym, Funcsym, Beginsym, IFSYM, WhileSym, Endarray};

#define become 127

INT What_IDENTIFY (CHAR CAAR CASE '.': Return Period; Break; Case '<': Return LT; Break; Case '>': Return Gt; Break; Case '(': Return LP; Break; Case ' ': Return Plus; Break; Case '*': Return Times; Break; Case ')': Return Rp; Break; Case ';': Return Semi; Break; Case '-': returnus; Break; Case '/': Return Slash; Break; Case '!': Return Ne; Break; Case '=': Return Eq; Break; Default: Return ";}} void gen (int, int, int); Void Error (int N); void getsym (); void constdeclaration (int * DX, int * tx, int lev); Void Vardeclaration (int * dx, int * tx, int lev); void Enter (CHAR K, INT * DX, INT * TX, INT Lev); Void INSERT (int sym1, symset * fsys, symset * fsys1); void test (Symset * S1, Symset * S2, INT N); INT IN (int sm, symset * fsys) void Statement (Symset * fsys, int TX, int lev); void listcode; int position (char * id, int TX); Void Expression (Symset * fsys, int TX, int lev); void Term Symset * fsys, int TX, int lev; void condition (Symset * fsys, int TX, int lev); void factor (Symset * fsys, int TX, int lev); void getch (); / * ---- - ------------------------ BLOCK (Lev, TX, FSYS) ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- * /

Void Block (int LEV, INT TX, SYMSET * fsys) {Int TX0, TX1, CX0, DX; SYMSET FSYS1 = {0}; DX = 3; Tx0 = Tx; Table [TX] .adr = CX; GEN (JMP , 0, 0); if (Lev> Levmax) error (32); do {if (SYM == Usesym) {getsym (); do {usedeclaration; if (SYM = SEMI) Getsym Else Error (5);} while (SYM == Ident);} if (SYM == Varsym) {getsym (); do {Vardeclaration (& DX, & TX, Lev); while (Sym == Comma) {Getsym (); Vardeclaration (& DX, & TX, Lev);} if (SYM == SEMI) getsym (); Else Error (5);} while (SYM == Ident);} while (SYM, FUNCSYM) {// process The first GetSym (); if (SYM == Ident) {ENTER ('f', & dx, & tx, lev); getsym ();} else error (4); if (SYM == SEMI) getsym (); // After the process is first plus Else Error (5); Insert (Semi, Fsys, & Fsys1); // "SEMICOLON" and the fsys now and front the Follow Sym Block (Lev 1, TX, & FSYS1); // IF (SYM == SEMI) {GetSym (); // Ident and Procsym CAN FOLLOW "DECLBEGSYS // Sub-program Description Multi-Variable Description, Function Description, Start Symbol //// :: = | | | | | Writing Statement |> // Ident, IF, While, Reads, Readn, Writes, Writen, Beg, // Continue to be subroutine description // INSERT (Ident, & statbegsys, & fsys1); Insert (Funcsym, & fsys1, & fsys1 Test (fsys, & fsys1, 6);} else error (5);} Once the outermost function of the block, Ident, IF, While, Reads, Readn, Writes, Writen, Beg, // cannot Reproduction Sub-division INSERT (Ident, & statbegsys, & fsys1); test (& fsys1, & decagsys, 7);} while (in (SYM, DECLBEGSYS));

Code [TABLE [TX0] .adr] .a = cx; table [tx0] .adr = cx; table [tx0] .size = dx; cx0 = cx; gen (int, 0, dx); insert (SEMI, FSYS , & fsys1); INSERT (EndSym, & fsys1, & fsys1); Statement (fsys1, tx, lev); Gen (opR, 0 ,0); test (fsys, fsys, 8); listcode (cx0);

/ * ----------------------------------- Statement (fsys, tx, lev) ----- ---------------- / Void Statement (Symset * fsys, int TX, int lev) {symset fsys1 = {0}; INT I, CX1, CX2; IF (SYM = = Ident) {/ / May be a digital string function i = position (id, tx); if (i == 0) {Error (11);} else if (table [i] .kind! = 'V') {Error (12); I = 0;} getsym (); if (SYM == Become) getsym (); Else Error (13); Expression (fsys, tx, lev); if (i! = 0) GEN ( STO, Lev-Table [I] .level, Table [i] .adr);} else f (SYM == ReadSym) {getsym (); if (SYM == LP) error (34); // function call Brackets do not pair else do {getsym (); if (SYM == lp) error (34); // if (SYM == Ident) i = position (id, tx); Else i = 0; if (i == 0 Error (35); Else {Gen (OPR, 0, 16); GEN (STO, Lev-Table [i] .level, table [i] .adr);} getsym ();} while (SYM == Comma ); If (sym! = Rp) {Error (33); while (in (SYM, FSYS) == 0) getsym ();} else getsym ();} else if (Sym == WriteSym) {Getsym () ; If (SYM == LP) {DO {getsym (); INSERT (rp, Fsys, & fsys1); INSERT (Comma, fsys1, & fsys1); Expression (fsys1, tx, lev); Gen (opr, 0,14);} while (SYM == Comma); if (SYM! = rp) error 33); Else getsym ();} gen (opr, 0,15);} else if (SYM == Callsym) {getsym (); if (Sym! = Ident) error (14); else {i = position ID, TX); if (i == 0) Error (11); Else IF (Table [i] .kind! = 'P') Error (15); else {gen (CAL, Lev-Table [i]. Level, Table [i] .adr); getsym ();

IF (SYM == LP) {while (Sym! = rp) {getsym (); if (SYM == Ident) {/ / may be numeric string function i = position (id, tx); if (i == 0) {Error (11);} else if (Table [i] .kind! = 'V') {Error (12); i = 0;} // Parameter outlet Gen (LOD, Lev-Table [i] , getsym (); if (Sym! = Comma) {Error (16);} getsym ();}}} getsym ();}}}} getsym ();}}}} getsym ();}}}} getsym ();}}}} Getsym ();}}}} {Getsym (); insert (THENSYM, FSYS, & FSYS1); Condition (fsys1, tx, lev); if (SYM == Tensym) getsym (); Else Error (16); CX1 = CX; GEN (JPC, 0, 0); statement (fsys, tx, lev); code [cx1] .a = cx; // jump out address} else if (SYM == Beginsym) {getsym (); INSERT (SEMI, FSYS, & fsys1); Insert Endsym, fsys1, & fsys1); statement (fsys1, tx, lev); while (in (Sym, Statbegsys) || (SYM == SEMI)) {IF (SYM == SEMI) getsym (); Else Error (10) Statement (fsys1, tx, lev);}} (SYM == endsym) getsym (); Else Error (17);} else if (SYM == WhileSym) {CX1 = CX; GetSym (); INSERT (dosym, fsys, & fsys1); condition (fsys1, tx, lev); CX2 = CX; GEN (JPC, 0, 0); if (SYM == DOSYM) getsym (); Else Error (18); Statement FSYS, TX, LEV); GEN (JMP, 0, CX1); / * HAS been change * / code [cx2] .a = cx; // jump out loop}

}

/ * ---------------------- Condition (fsys, tx, lev) -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---- / Void Condition (Symset Fsys, Int TX, INT Lev) {Int Relop; Symset Fsys1 = {0}; INSERT (EQ, Fsys, & fsys1); Insert (ne, fsys1, & fsys1); Insert (LS , FSYS1, & FSYS1); INSERT (Le, Fsys1, & fsys1); Insert (gt, fsys1, & fsys1); INSERT (GE, FSYS1, & FSYS1); Expression (fsys1, tx, lev); if ((Sym! = EQ) && (SYM! = ne) && (SYM! = LS) && (SYM! = le) && (SYM! = GT) && (SYM! = gege)) Error (20); else {RELOP = SYM; GetSym () Expression (FSYS, TX, Lev); if (Relop == Eq) GEN (OPR, 0, 8); if (Relop == Ne) Gen (opr, 0,9); if (Relop == LS) GEN (OPR, 0, 10); if (Relop == Le) GEN (OPR, 0, 11); if (Relop == GT) GEN (OPR, 0, 12); if (Relop == GE) GEN (OPR 0, 13);}} / * ------------------- Expression (fsys, tx, lev) ------------- --------- * /

Void Expression (Symset Fsys, Int TX, INT Lev) {Int Adddop; Symset Fsys1; INSERT (Plus, Fsys, & Fsys1); Insert (Minus, Fsys1, & Fsys1); if ((Sym == Plus) || (Sym = = Minus)) {addop = SYM; getsym (); Term (fsys1, tx, lev); if (addop == minus) GEN (OPR, 0, 1);} else Term (fsys1, tx, lev); while ((SYM == Plus) || (SYM == Minus)) {addop = SYM; GetSym (); Term (fsys1, tx, lev); if (addop == plus) GEN (OPR, 0, 2); Else Gen (opr, 0, 3);}}

/ * ----------------- Term (fsys, tx, lev) ----------------------- * /

Void Term (Symset * fsys, int mark; system fsys1 = {0}; INSERT (Times, Fsys, & fsys1); INSERT (Slash, Fsys1, & fsys1); Factor (fsys1, tx, lev) WHILE ((SYM == Times) || (SYM == Slash)) {mulop = SYM; getsym (); FACTOR (fsys1, tx, lev); if (mulop == Times) GEN (OPR, 0, 4 ); Else Gen (opr, 0,5);}}

/ * ------------------ Factor (fsys, tx, lev) ------------------- * / Void Factor (Symset Fsys, Int TX, INT Lev) {Symset Fsys1 = {0}; INT I; TEST (Facbegsys, Fsys, 24); While (Sym, Facbegsys) == 1) {IF (SYM == Ident) {i = position (id, tx); if (i == 0) error (11); else {if (table [i] .kind == 'v') {gen (LOD, Lev-Table [i ] .level, table [i] .adr;}} (Table [i] .kind == 'f') error (21);} getsym (); //} else if (SYM == Num) {IF (Num> Amax) {Error (31); Num = 0;} GEN (LIT, 0, NUM); getsym ();} else if (SYM == lp) {getsym (); insert (rp, fsys, & fsys1 ); Expression (fsys1, tx, lev); if (SYM == rp) getsym (); Else Error (22);} test (fsys, facbegsys, 23);}} / * ---------- ----------- Vardeclaration (& DX, & TX, Lev) ------------------- * /

Void Vardeclaration (INT * DX, INT * TX, INT LEV) {if (SYM == Ident) {ENTER ('V', DX, TX, Lev); GetSym ();} else error (4);

/ * ------------------------------------- -------------------- * /

Void Enter (CHAR K, INT * DX, INT * TX, INT LEV) {IF (TX == TXMAX) {Error (99); Return;} (* TX) ; Table [(* TX)]. Kind = K; STRCPY (Table [(* TX)]. Name, g_id); if (k == 'v') {Table [(* TX)]. level = lev; table [(* tx)]. ADR = (* dx); (* dx) ;} if (k == 'f') table [(* tx)]. level = lev;}

/ * ------------------------ TEST (S1, S2, N) -------------- --------------- * /

Void Test (Symset S1, Symset S2, INT N) {

IF (in (SYM, S1) == 0) // Not in an acceptable SYM collection, error {Error (N); // Continue to take SYM until the acceptable SYM / / skip is not in S1 SYM, SYM // S1 skipping in S2 is the successor SYM, S2 is the successor SYM // of the blocks included in the block S1, which must be a certain error / / subordinate to S1 ((((( IN (SYM, S1) == 0) && (in (SYM, S2) == 0) && ch! = '.') getsym ();}} / * ------------- -------------- Gen (x, y, z) ------------------------------------------------------------------------- - * /

// void Gen (CHAR X, INT Y, INT Z) VOID GEN (INT X, INT Y, INT Z) {IF (CX> CXMAX) {Print ("Program Too Long! / N"); EXIT (0);} code [cx] .f = x; code [cx] .l = y; code [cx] .a = z; cx ;}

/ * ---------------------------- Getsym () ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------- * /

Void getsym () {INT I, J, K; BOOL Isblank = (CH == '|| CH ==' / n '|| CH ==' / t '||' / r '); While (Isblank && Ch ! = '.') {Getch (); isblank = (CH == '|| CH ==' / n '|| CH ==' / r ');}

IF (CH == EOF || CH == ') Return; Bool Find = False; Char TP [Al] ;; IF ((CH> =' a ') && (CH <=' z ')) { K = 0-1; id = string (); do {INT P; if (k = 'a') && (CH <= 'z') || (CH> = '0') && (ch <= '9'));

ID [K ] = '/ n'; ptext--;

i = 1; j = norw; for (k = i; k

IF (! find) {sym = ident;}

Return;

}

IF ((CH> = '0') && (CH <= '9')) {// Combine a Number K = 0; NUM = 0;

INT E; SYM = NUM; Do {Num = 10 * Num CH-'0 '; K ; getCH ();} while ((ch> =' 0 ') && (ch <=' 9 ')); if (CH == 'E' || CH == 'E') {E = 0; getCH (); if ((CH> = '0') && (CH <= '9')) E = Ch- ' 0 'Else Error (49); getCh (); if ((CH> =' 0 ') && (CH <=' 9 ')) E = CH-'0' Else Error (49); E = 10 * E CH-'0 ';} Num = Num IPOW (10, E); // Pushing Power Index PText -; if (k> nmax) Error (30);

Return;}

// NOT A-Z and NOT 0-9 IF (CH == '=') {SYM = Eq; Return;}

IF (CH == ') {getch (); if (CH ==' = ') {SYM = BECOME;} else Sym = NUL; RETURN;} IF (CH ==' <') {Getch () ; If (ch == '=') {SYM = Le;} else Sym = Ls; Return;} if (CH == '>) {getCH (); if (CH ==' = ') {SYM = Else Sym = gt; return;} SYM = What_identify (ch);

/ * --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------- * /

Void getch () {ch = text [PTEXT ];

/ * ------------------- Error (N) ------------ * / void error (int N) {INT i = 0 PRINTF ("*****"); Printf ("% 2D", N); Err ;}

/ * ------------------------------------------- * / void listcode (int CX0) {INT i; if (listswitch == 1) for (i = Cx0-1; i

/ * ---------------------------- INSERT (Symi, Fsys, & fsysi) ----------- --------- /// Add a new sym and a exising fsys to fsys1void insert (int sym1, symset * fsys, symset * fsys1) {INT i = 0, j = 0;

IF (fsys1-> size! = 0) fsys1-> size -; // endArray outlet

While (fsys [i]! = endArray) {structure (fsys1-> SYM [i], fsys-> SYM [I]); i ; fsys1-> size ; if (i 1 == maxvar) {Error (98 }}

FSYS1-> SYM [I] = endArray;

i = 0; while ((Sym1! = fsys1-> SYM [i])) && (fsys1-> SYM [i]! = endArray) {

i ;

IF ((Sym1! = fsys1-> SYM [i]) // Found or not found to SYM && (i 1 SYM [I-1] = SYM1; fsys1-> SYM [I] = endArray; // INSERT the SYM1 INTO IT}}

/ * -------------------------------- INT in (SM, FSYS) --------- --------- * / int in (char * sm, symset * fsys) // Whether the first SYM is in fsys, not, return 0 {INT i; i = 0;

While (SM! = fsys-> SYM [I] && fsys-> SYM [I]! = endArray) i ; if (SM == fsys-> SYM [I]) i = 1; ELSE I = 0; Return ( i);

/ * ----------------------------- int position (id, tx) ----------- ------- * / int position (char * id, int TX) // From TX to find the position in the symbol table, if the head is left, the head is failed {INT I; STRCPY (Table [0] .name , ID); i = tx; while (strcmp (table [i] .name, id)! = 0) i--; return (i);

/ * ----------------------- Int base (l, b) ------------------ ----- * / int base (int 1; b1 = b; while (l> 0) {b1 = s [b1]; l -;} return (b1);}

/ * -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - * / void interpret () {INT P, B, T; INSTRUCTION I; Printf ("start pl / 0: / n"); t = p = 0; b = 1; s [1] = S [2 ] = s [3] = 0; do {= code [p] .a; il = code [p] .l; if = code [p] .f; p ; if (if == lit) {t ; S [T] = IA;} if (if == opr) Switch (Ia) {case 0: T = b-1; // Stack top finger needle to P = S = S [T 3] in the base register / Code pointer points to the value B = S [T 2]; // The base pointer points to the base of the base, a value Break; Case 1: S [t] = 0-s [T]; Break; case 2: t -; s [t] = s [t] s [t 1]; Break; case 3: t -; s [t] = s [t] -s [t 1 ]; Break; Case 4: T -; S [T] = S [T] * S [T 1]; Break; Case 5: t -; s [t] = s [t] / s [t 1]; Break; Case 6: IF (s [t]! = 0) s [t] = 1; ELSE S [T] = 0; Break; Case 8: T -; IF (s [t] = = S [T 1]) s [t] = 1; ELSE S [T] = 0; Break; Case 9: T -; IF (S [T]! = S [T 1]) s [T ] = 1; ELSE S [T] = 0; Break; Case 10: T -; IF (S [T] = s [t 1]) s [t] = 1; Else S [T ] = 0; Break; Case 12: T -; IF (s [t]> s [t 1]) s [t] = 1; ELSE S [t] = 0; Break; Case 13: T --- ; If (S [T] <= S [T 1]) s [t] = 1; ELSE S [T] = 0; Break; Case 14: Printf ("% D", S [T]); T -; Break; Case 15: Printf ("/ n"); Break; Case 16: T ; Printf ("?"); Sprintf ("% d", s [t]); print ("% d", S [T]) ; Break; default: exit (0);} if (if == LOD) {t ;

S [T] = S [Base (IL, B) IA];} if (if == STD) {S [Base (IL, B) IA] = S [T]; T -;} IF IF == CAL) {S [T 1] = Base (IL, B); S [T 2] = B; s [T 3] = P; B = T 1; P = IA;} (if == int) T = T IA; if (if == jmp) P = Ia; if (if == JPC) IF (s [t] == ​​0) P = IA;} while (p! = 0); Printf ("end pl / 0! / N");} / * ------------------------- End --- ----------------- * // * --------------------------- Result- --------------- * /

/ * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------- * / void main () {err = cc = ll = cx = ptext = 0; KK = 10; CH = '';

Getsym (); block (0,0, fsysa);

IF (Err == 0) interpret (); Else Printf ("Error In CaCl Program! / N");

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

New Post(0)