1、WHILE循环语句的翻译程序设计(LL(1)法、输出三地址表示)_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 t
2、he 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;In
3、t 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 l
4、ink;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). * /Voi
5、d 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
6、pointerVoid 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
7、 panduanSEA (); / / 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 =
9、 0;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;Int IsLetter (char c) / / is used to determi
11、ne whether a letter or a number is usedIf (c = a Else if (Token I.b, “= 0“) = = 0)Token I. ID = 4;Else if (Token I.b, “ while E do A“InputS ();Else if (f1 = = 2)int f3 = 0;F3 = firstset ();If (f3 = = 1)Cout InputE1 ();Else if (f3 = = 2)The cout InputE2 ();The elseThe cout InputE3 ();Else if (f1 = =
12、3)The cout ”);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