1、WHILE循环语句的翻译程序设计(LL(1)法、输出三地址表示)# include “iostream“Using namespace STD.# define MaxiProgramNumber 888 / / the number of maximum characters of the source program# define MaxiWordLength 18 / / the number of the longest character of the keyword# define MaxiTokenNumer 118 / / identifier can contain the
2、 maximum number of characters*. * /The # define MaxiWordLength1 18# define maxitokennumer1118*. * /Char Ecode MaxiTokenNumer1 MaxiWordLength1Int Top = 0;Int nID = 1; / / initialize theThe following definition stack structure, the element in the stack is a structure. * /Typedef structInt the add;Int
3、next;Char STR MaxiWordLength1; ADDL; / / ADDL is used to identify WHILE, DOADDL L MaxiTokenNumer1; / / L for the defined struct namesInt nL = 0;Int nadd bring them any closer = 1; / / initialize the/ *. The input stack, the elements in the stack * /Typedef structInt ID;Char MaxiWordLength b; the lin
4、k;The link Token MaxiTokenNumer;The link Token2 MaxiTokenNumer;Int nTokenNumer = 1; / / mark positionChar a, MaxiProgramNumber. / / arrays are used to load the source programInt nLength = 0; / / to point to the elements in the arrayFunction declarations (functions used in lexical analysis). * /Void
5、GetProgram (); / / load the source program into array aChar GetChr (); / / read the elements in array aInt Judge (char / / for the judgment 0Int IsLetter (char c); / / for the letterVoid Input (char * p, char c, int / / identifiers or keywords enter the array the nx + 1 element that points to the po
6、interVoid Save (char * word, int x); / / load the key or identifier or operator TokenVoid Getcode ();Function declarations (functions used in grammar analysis). * /Void the Pop (); / / out of the stackVoid InputS ();Void InputE1 ();Void InputE2 ();Void InputE3 ();Void InputA ();Int firstset ();Int p
7、anduanSEA (); / / identify non-terminal charactersInt EStrcmp ();*. The lexical analysis part. * /Int Wordanalyze ()Int jieshu = 0;Char CRH.Cout =“ :“ ; GetProgram (); / / load the source program into array aInt x;Char word MaxiWordLength; / / declare a temporary arrayWhile (CHR = GetChr () = 0)If (
8、CHR)Break; / / skip the space and return the elementX = 0;Word x = 0; / / to emptyIf (IsLetter (CRH)jieshu = 1;While (IsLetter (CRH)Input (word, CRH, x); / / a character loads it into an array wordCRH = GetChr ();If (CHR = = | | CHR = = = | | CHR = = ) / / put in TokenInput (word, CRH, x);Word x = 0
9、;Save (word, x);Else if (CHR = = “=“) / / load the TokenInput (word, CRH, x);Word x = 0;Save (word, x);Else if (CHR = = “ n “);For (int I = 1; I n “);(b) / / copy the input symbol in Token2For (int d = 1; d abcd.Void GetProgram () / / load the source program into array aChar ch;While (ch = getchar (
10、)! = #)If (nLength = MaxiProgramNumber - 2)Break;The elseA n = ch;A nLength = 0;NLength = 0;Char GetChr () / / read the elements in array aIf (nLength = MaxiProgramNumber - 1)Return 0;The elseReturn a n + +Int Judge (char If (CHR = = 0)Return 0;Return 1;Pop();strcpy(是(前+ +),“一个”);strcpy(是(前+ +),“做”)
11、;strcpy(是(前+ +),“E”);strcpy(是(前+ +),“虽然”);空白 InputE1()/ /压栈Pop();strcpy(是(前+ +),“id”);strcpy(是(前+ +),“”);strcpy(是(前+ +),“id”);空白 InputE2()/ /压栈Pop();strcpy(是(前+ +),“id”);strcpy(是(前+ +),“=”);strcpy(是(前+ +),“id”);空白 InputE3()/ /压栈Pop();strcpy(是(前+ +),“id”);strcpy(是(前+ +),“ “)= = 0)返回 1;如果(strcmp(Token23。b,“=”)= = 0)返回 2;如果(strcmp(Token23。b,“ 而做一个“ id1 id2 | id1 = id2 | id1 Cout If (Wordanalyze () / / lexical analysis succeedsif (expressionanalyze () / / syntax analysis is also successfulint I;L (nL). Add = nadd bring them any closer;For (I = 0; I “ “ Cout