Compilation Technology Implementation - Law Analysis

zhaozj2021-02-16  90

To do the internship of compilation technology, think of 9CBS to find a few related articles to see

Didn't find it, this may be very few in this area.

So put the source code of the lexical analysis written by myself.

I hope everyone criticizes the correct

#include "stdlib.h" #include "stdio.h" #include "iostream.h" #include "fstream.h" #include "string.h" #include "ctype.h" #include "malloc.h"

#DEFINE $ ID 0

#DEfine $ ID_INT 1 # define $ ID_CHAR 2 # define $ ID_FLOAT 3 # define $ ID_VOID 4 # define $ ID_STATIC 6

#define $ ID_IF 7 # define $ ID_ELSE 8 # define $ ID_DO 9 # define $ ID_WHILE 10 #define $ ID_SWITCH 11 # define $ ID_CASE 12 # define $ ID_DEFAULT 13 # define $ ID_BREAK 14 #define $ ID_CONTINUE 15 # define $ ID_RETURN 16 #define $ ID_DEFINE 17 # Define $ ID_INCLUDE 18 # define $ int 19

#define $ assign 20 # define $ add 21 # define $ sub 22 # define $ mul 23 # define $ div 24

#define $ lpar 25 # Define $ rpar 26 # define $ lpar_dim 27 # define $ rpar_dim 28 # define $ lpar_sen 29 # define $ rpar_sen 30

#define $ q 3 31 # Define $ not_equal 32 # define $ lt 33 # define $ gt 34 ​​# define $ lt_equal 35 # define $ gt_equal 36

#DEFINE $ COMMA 37 # Define $ dot 38 # define $ INTDIV 39 # define $ comment 40 # define $ comment_assign 41 # define $ send 42 # define $ Question 43 # Define $ not 44

Struct token {int ncode; char * NTOKEN;}; // array dimension table and its processing struct Dim {int uprange1; int uprange2;} DIM_TABLE [20];

INT DIMTABLE_INDEX;

Void Dimtable_initiate () {for (INT i = 0; I <20; i ) {DIM_TABLE [I] .uprange1 = 0; DIM_TABLE [I] .uprange2 = 0;}} void Dimtable_Print () {cout << "The Contents Of the DIM_TABLE: / N "; for (int i = 0; i <20; i ) {COUT <

<< "/ t" <

<

}

}

/// Key words and their processing

Struct Reserve

{

Int ncode;

Char NToken [20];

Word [20] = {{0, ""}, {1, "int"}, {2, "char"}, {3, "float"}, {4, "void"}, {5, " Const "}, {6," static "}, {7," if "}, {8," else "}, {9," do "}, {10," while "}, {11," switch " }, {12, "CASE"}, {13, "default"},

{14, "Break"}, {15, "continue"}, {16, "return"}, {17, "deflud"}, {18, "incrude"}, {19, "}};

Int Reserve_Search (Char * Search)

{

For (int i = 0; i <20; i )

{

// cout << "i =" <

<

IF (STRCMP (Search, Word [i] .ntoken == 0) Return i;

}

Return 0;

}

void reserve_print ()

{

For (int i = 0; i <20; i )

{

Cout <

<< ": =" <

<

<< ": =" <

<

ID_TABLE [I] .kind = 0;

ID_TABLE [I] .type = 0;

ID_TABLE [I] .idform = 0;

ID_TABLE [I] .da = 0;

ID_TABLE [I] .Offset = 0;

ID_TABLE [I] .arrp = -1;

}}} void idtable_print () {cout << "The contents of the id_table: / n"; cout << "i: =" << "i" << "/ t" << "name" << "/ t "<<" "" "" "" << "

For (int i = 0; i <100; i ) {cout << "i: =" <<< "/ t" <

<< "/ t" <

<< "/ t" <

<< "/ t" <

<< "/ t" <

<< "/ t" <

<< "/ t"

<

<

}

}

Static FStream Infile; CHAR BUFFER [80]; int buffer = 0; cHar token [20]; char ch; int line_number = 0; int rt error_number = 0; int isletter (char CH) {IF ((((((((((((((((( CH> = 'a') && (ch <= 'z')) || ((CH> = 'a') && (ch <= 'z'))) Return 1; Else Return 0;} int isdigit CHAR CH) {IF (CH> = '0' && Ch <= '9') Return 1; Else Return 0;} int String_to_int (char * source) {INT I, Result; Result = 0; intlene = strlen (Source ); For (i = 0; i {

Result = result * 10 (* (Source i)) - '0');

}

Return Result;

}

// readline () reads a row in the file into buffer () {if (! infile.eof ()) {infile.getLine (buffer, sizeof (buffer); // cout <

<

BufferLength = Strlen (buffer);

}

Return bufferlength;

}

// getch () reads a character from the buffer to CH

Void getch ()

{

IF (bufferLength == 0) readline ();

// cout <

CH = Buffer [PBUFFER ];

// cout <

IF (PBuffer> BufferLength)

{

Readline ();

PBuffer = 0;

}

}

Void getnbc ()

{

While (CH == '')

{

Getch ();

Getnbc ();

}

}

Void Concat ()

{

// cout <

INT I = Strlen (token); // Cout <

Token [i ] = CH;

Token [i] = '/ 0';

}

Void retract () {pbuffer ---; void error (char * string) {cout << "line (" <

<< ")?

}

Struct token nextToken ()

{

Struct token temp;

STRCPY (Token, ");

Getch ();

Getnbc ();

IF (Isletter (CH)) {While (Isletter (CH) | ISDITIGIT (CH)) {Concat (); getch ();} retract (); int result = reserve_search ((char *) token; // cout << "Result =" <

<

IF (Result! = 0)

{

TEMP.NCODE = Result;

TEMP.NTOKEN = token;

Return Temp;

}

Else {TEMP.NCODE = $ ID; TEMP.NTOKEN = Tokeen;

Return Temp;

}

ELSE IF (Isdigit (CH))

{

While (Isdigit (CH))

{

CONCAT ();

Getch ();

}

Retract ();

TEMP.NCODE = $ INT;

TEMP.NTOKEN = token;

Return Temp;

}

Else

{

Switch (CH)

{

// 20 ~ 30

CASE '=':

Getch ();

IF (ch! = '=') {TEMP.NCODE = $ assign; retract ();

Else {TEMP.NCODE = $ Equal;

TEMP.NTOKEN = token;

Return Temp;

Case ' ':

Temp.ncode = $ add; temp.ntoken = token;

Return Temp;

Case '-':

Temp.ncode = $ Sub; Temp.ntoken = token;

Return Temp;

Case '*':

Temp.ncode = $ mul; temp.ntoken = token;

Return Temp;

Case '/':

Temp.ncode = $ div; Temp.ntoken = token;

Return Temp;

Case '(')

Temp.ncode = $ lpar; temp.ntoken = token;

Return Temp;

Case ')':

Temp.ncode = $ rpar; temp.ntoken = token;

Return Temp;

Case '[':

Temp.ncode = $ lpar_dim; temp.ntoken = token;

Return Temp;

Case ']':

Temp.ncode = $ rpar_dim; temp.ntoken = token;

Return Temp;

Case '{':

Temp.ncode = $ lpar_sen; temp.ntoken = token;

Return Temp;

Case '}':

Temp.ncode = $ rpar_sen; temp.ntoken = token;

Return Temp;

Case '!':

Getch ();

IF (ch! = '=') {TEMP.NCODE = $ not; retract ();

Else {TEMP.NCODE = $ not_equal;}

TEMP.NTOKEN = token;

Return Temp;

Case '<':

Getch ();

IF (ch! = '=') {TEMP.NCODE = $ LT; RETRACT ();

Else {TEMP.NCODE = $ LT_EQUAL;

TEMP.NTOKEN = token;

Return Temp;

Case '>':

Getch ();

IF (ch! = '=') {TEMP.NCODE = $ gt; retract ();

Else {TEMP.NCODE = $ gt_equal;

TEMP.NTOKEN = token;

Return Temp;

Case ',':

Temp.ncode = $ comma; Temp.ntoken = token; return temp;

Case '.':

Temp.ncode = $ dot; temp.ntoken = token;

Return Temp;

Case '%':

Temp.ncode = $ INTDIV; TEMP.NTOKEN = Token;

Return Temp;

Case ';';

Temp.ncode = $ send; temp.ntoken = token;

Return Temp;

Case '?':

Temp.ncode = $ question; TEMP.NTOKEN = Token;

Return Temp;

Case ':': getch (); if (ch! = '=') {TEMP.NCODE = $ comment; retract ();} else {temp.ncode = $ comment_assign;} temp.ntoken = token; return; Default: line_number ; cout << "get line:" <

<

Getnbc ();

Temp.ncode = 100; Temp.ntoken = token;

Return Temp;

}

}} void main () {idtable_initiate (); dimtable_initiate (); struct token tokenreturn; infile.open ("d: //vc//compileproject//data.txt", ios :: in); if (! infile) {COUT << "Conn't open the source file, please check it! / N"; Abort ();} while (! Infile.eof ()) {// Variable Description Statement Processing Tokenreturn = NextToken (); COUT <

Cout <

<

IF (tokenreturn.ncode == $ ID_INT) // Integer

{//2

DO

{// 3

Tokenreturn = NextToken ();

IF (tokenreturn.ncode == $ ID)

{// 4

IDTABLE_INSERT_INT (TokenReturn.ntoken);

Tokenreturn = nextToken (); // is not an array description

IF (tokenreturn.ncode == $ lpar_dim)

{/ 5

Tokenreturn = NextToken ();

IF (tokenreturn.ncode == $ int)

{//6

// Insert an item in DIM_TABLE

DIM_TABLE [DIMTABLE_INDEX] .uprange1 = string_to_int (tokenreturn.ntoken);

IDTABLE_ADD_UPRANGE ();

Tokenreturn = NextToken ();

IF (tokenreturn.ncode == $ rpar_dim)

{//7

// Is it a two-dimensional array description

Tokenreturn = NextToken ();

IF (tokenreturn.ncode == $ lpar_dim)

{//8

Tokenreturn = NextToken ();

IF (tokenreturn.ncode == $ int)

{//9

/ / Insert a DIM_TABLE in DIM_TABLE [DIMTABLE_INDEX] .uprange2 = string_to_int (tokenreturn.ntoken);

IDTABLE_ADD_UPRANGE ();

DIMTABLE_INDEX ;

Tokenreturn = NextToken ();

IF (tokenreturn.ncode == $ rpar_dim)

{// 10

Tokenreturn = NextToken ();

}

Else

{

Error ("Lack of 2D Right Brand"); Break;

}

}

Else

{

Error ("Lack of two-dimensional dimension"); BREAK;

}

}//8

Else

{// only one dimension

DIMTABLE_INDEX ;

}

}

Else

{

Error ("lack of one-dimensional parentheses");

}

} // 6

Else

{

LINE_NUMBER ;

Error ("lack of one-dimensional dimension");

Break;

}

} //

Else if (tokenreturn.ncode == $ lpar) / / function description

{

} // Variable name legal} else {Error ("Variable Name is not legal");} // 4} while (tokenreturn.ncode == $ comma); if (tokenreturn.ncode! = $ Send) {Error ("missing ';' ");} // 3} // 2 else if (tokenreturn.ncode == $ ID_FLOAT) {// float chuli} else if (tokenreturn.ncode == $ ID_VOID) {// void chuli} Else IF (TokenReturn.ncode == $ ID) // Expression handle {cout << "process: / n";}} idtable_print (); DIMTABLE_PRINT ();} Lastking Contact: LeefiLiCITY@hotmail.com

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

New Post(0)