This is a small grammar analysis program made when compiling course design, and the program has some redundancy. The implementation of the implementation is not complete, and the analysis process can only be given by inputting the analysis table. The first set and the FOLLOW set of asking for the meaning of the mean is still not implemented.
/ * * File Name: Syntax Analysis. H * Summary: Analysis Table and String of Any Enter LL (1) Grammar, this program can automatically determine whether the string is * No is the sentence of this gram, and can give Analysis process. * / # include
#include
#include
#include "ctype.h"
Class string;
Class Stack
{
CHAR STCK [100];
Int top;
PUBLIC:
void init ()
{TOP = 0;}
Void Push (Char CH);
CHAR POP ();
Void showstck (); // Display elements in the stack
}
Class string {
Int length;
Char * PTR;
PUBLIC:
String ();
~ String ()
{}
String (char * s);
INT getLength ();
INT getLength (char * s);
Char & Operator [] (int);
String & Operator = (const string & ob);
Bool Operator == (Const String & PW);
Friend Ostream & Operator << (Ostream &, Const String &);
Friend Istream & Operator >> (ISTREAM &, STRING &);
}
Void Stack :: Push (Char CH) //
{
STCK [TOP] = CH;
TOP ;
}
Char Stack :: POP ()
{
IF (TOP == 0)
{
COUT << "stack is empty!";
Return 0;
}
Top -;
Return STCK [TOP];
}
Void Stack :: Showstck ()
{
INT I;
For (i = 0; i
Cout <
}
String :: string ()
{Length = 1;
PTR = New char [1];
* PTR = '/ 0';
}
String :: string (char * s)
{PTR = New Char [getLength (s) 1];
STRCPY (PTR, S);
Length = getLength (s) 1;
}
Int string :: getLength ()
{
Int Len;
Len = getLength (PTR);
Return Len;
}
Int string :: getLength (char * s)
{INT i = 0;
While (s [i]! = '/ 0')
{i ;
}
Return I;
}
Char & string :: Operator [] (int J)
{
Return PTR [J];
}
String & string :: Operator = (const string & ob) // Reserved '=' operator
{IF (this == & ob)
Return * this; delete PTR;
Ptr = new char [getLength (b.ptr) 1];
Length = getLength (OB.PTR) 1;
STRCPY (PTR, OB.PTR);
RETURN * THIS;
}
Bool string :: Operator == (const string & pw)
{
IF (* ptr == * pw.ptr)
Return True;
Else
Return False;
}
Ostream & Operator << (Ostream & S, Const String & OB)
{
s
Return S;
}
iStream & Operator >> (ISTREAM & IN, STRING & OB)
{
IN >> ob.ptr;
Return IN;
}
Bool Find (Char C, Char Array []) / / Find functions, return to Boolean
{
INT I;
Bool flag = false;
For (i = 0; i <100; i )
{
IF (c == array [i])
Flag = true;
}
Return flag;
}
INT Location (Char C, Char Array [] // Positioning function, pointing out the location of the character
{
INT I;
For (i = 0; i <100; i )
{
IF (c == array [i])
Return I;
}
}
void error ()
{
COUT << "Error!" <
}
Void Analyz (Char VN [], Char VT [], String M [100] [100])
{
INT I, J, M, P, Q, Length, T, H;
CHAR W, X;
String Str;
OPT0:
CIN >> STR;
For (i = 0; i
{
IF (! Find (STR [i], vt))
{COUT << "Enter a string error! Please re-enter!" <
Goto OPT0;
Break;
}
STACK ST;
St.init (); // Initialization
St.push ('#');
St.push (vn [0]); // # with the identifier symbol
J = 0;
H = 1;
W = str [0];
COUT << "step" << "Analysis Stack" << "The Residual Input String" << "The generated" <
OPT1:
Cout <
<< "??????????????? display step
J ;
W = STR [J];
Goto OPT1;
}
Else
Error ();
}
Else
{
IF (x == '#')
{
IF (x == w)
{
Cout << "" << "Accepted!" <
<
}
Else
Error ();
}
Else
{
P = Location (X, VN);
Q = Location (W, VT); String S ("NULL");
IF (M [P] [q] == s) / / Find the generated in the two-dimensional array
Error ();
Else
{
String str0 = m [p] [q];
Cout << "<
<< "->" <
<
IF (STR0 == "$")
Goto OPT1;
Else
{Length = str0.getlength (); // Reverse in the stack
For (m = length-1; m> = 0; M -)
{
St.push (STR0 [M]);
}
Goto OPT1;
}
}
}
}
}
Main ()
{
INT I, K, N, R;
Char VN [100], VT [100], SELECT
String M [100] [100];
COUT << "* file name: syntax analysis" <
Cout << "* Summary: Analysis table and string of any input LL (1) text method, this program can automatically determine that the string is" <
Cout << "* No to the sentence of this grammat, and give the analysis process." <
COUT << "* --------------------------------------------- ----------------------- * <
Cout <
<
OPT2:
Cout << "Please enter the number of non-end:";
CIN >> N;
COUT << "Please enter the end of the end of the end.
For (i = 0; i <100; i )
{
CIN >> VT [I];
IF (vt [i] == '#')
{
R = i;
Break;
}
}
For (i = 0; i
{
Cout << "Please enter non-final VN [" <
<< ""] VN: "; <<"] VN: ";
CIN >> VN [I];
Cout << "Please enter this non-ending corresponding to the generated right part of each end of the end, $ represents an empty string:";
For (k = 0; k <= r; k )
{
CIN >> M [I] [K];
}
}
OPT3:
Cout << "Please enter the string to be analyzed and end:";
Analyze (VN, VT, M);
COUT << "*********************************************** *** "<
COUT << "1: Input string" <
COUT << "2: Enter new analysis table" <
COUT << "0: Reversed" <
OPT4:
CIN >> SELECT; SWITCH (SELECT)
{
Case '1': {Goto Opt3; Break;}
Case '2': {goto OPT2;
Case '0': {Break;}
Default: {cout << "Enter an error! Please reselect:" <
Goto OPT4;
Break;
}
Return 0;
}