Lexical analysis in a c ++ Language

Source: Internet
Author: User

I wrote it in college, but it was a small assignment in the assembly language course. I tried it myself, but it was a pity that it was not complete.
Send the code of this class. If you are interested, you can check it out.
// Complie. h
// Lexical analysis result information
Struct scanresult
{
CString code; // code value
Int nType; // type
CString remark; // type description
};


Class CComplie
{
Public:
// Separate Characters Based on the keyword exp
Void SplitStr (CString str, CString exp, CStringArray & strnew );
// A custom Find character
Int FindStr (CString str, CString findstr );
// Key functions, lexical Scanning
CString Scan (CString str );
// Load the file
CString LoadFile ();
Public:
CComplie ();
Virtual ~ CComplie ();
Public:
// Further separates the delimiters
CString IsSeparate (CString str );
CString CharType (CString str );

// Save the lexical result and output 1
CString m_strOut;
CString m_result;
// Open the file name
CString m_FileName;
};
// Complie. cpp
CComplie: CComplie ()
{
M_FileName = "";
M_strOut = "";
M_result = "";
}

CComplie ::~ CComplie ()
{

}

CString CComplie: LoadFile ()
{
CString strTemp, strOut;
// Create an object to open the dialog box
CFileDialog dlg (TRUE, _ T ("CPP"), _ T ("*. cpp "), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _ T (" C ++ file (*. cpp) | *. cpp | "));
If (IDOK = dlg. DoModal ())
{
// Obtain the file name
M_FileName.Format ("% s", dlg. GetPathName ());
// Create a file read/write object
CStdioFile sf;
// Open the file
If (sf. Open (LPCTSTR) m_FileName, CFile: modeRead ))
{
// Read by row
While (sf. ReadString (strTemp ))
{
StrOut + = strTemp;
StrOut + = "";
}
}
}
Return strOut;
}

CString CComplie: Scan (CString str)
{
CString strleft;
CString strTmp;
// End symbol of the string
Str + = "#";
Int I = 0;
Int j = 0;
Int k = 0;
// Find the newline character of the carriage return.
While (I = FindStr (str ,""))
{
// Obtain all characters on the left of the first carriage return.
Strleft = str. Left (I );
// Remove leading and trailing Spaces
Strleft. TrimLeft ();
Strleft. TrimRight ();
While (j = FindStr (strleft ,""))
{
StrTmp = strleft. Left (j );
StrTmp. TrimLeft ();
StrTmp. TrimRight ();
M_strOut + = strTmp;
M_strOut + = "| ";
Strleft = strleft. Right (strleft. GetLength ()-J-1 );
Strleft. TrimLeft ();
Strleft. TrimRight ();
// MessageBox (strleft );
}

If (! Strleft. IsEmpty ())
{
Strleft. TrimLeft ();
Strleft. TrimRight ();
M_strOut + = strleft;
M_strOut + = "| ";
}
Str = str. Right (str. GetLength ()-i-1 );
Str. TrimRight ();
Str. TrimLeft ();
// MessageBox (str );
}
Return m_strOut;
}

Int CComplie: FindStr (CString str, CString findstr)
{
Int I = 0;
I = str. Find (findstr );
If (I =-1)
{
Return 0;
}
If (I = 0)
{
Return 10000;
}
Else
{
Return I;
}

}

Void CComplie: SplitStr (CString str, CString exp, CStringArray & strnew)
{
Int nPos = 0;
While (nPos = FindStr (str, exp ))
{
CString strleft;
Strleft = str. Left (nPos );
Strnew. Add (strleft );
Str = str. Right (str. GetLength ()-nPos-1 );
}
}

CString CComplie: CharType (CString str)
{
Return str;
}

CString CComplie: IsSeparate (CString str)
{
Int k = 0;
M_result = "";
CString strleft;
CStringArray strTmp;
// Separate words separated by the scan function. The separator is |
SplitStr (str, "|", strTmp );
// List of delimiters separated #
CString separate = "{#}# (#) # [#] #=#| #&&#! #,###:#<## "####>#- >#%#= #! ##++ # -- # ^ # + #-#*#/#";
CStringArray strseparte;
// Separate the keyword and save it to the CString Array
SplitStr (separate, "#", strseparte );
/*
For (int x = 0; x {
AfxMessageBox (strseparte [x]);
}
*/
For (int I = 0; I {
For (int j = 0; j {
While (k = FindStr (strTmp [I], strseparte [j])
{
If (k = 10000)
{
K = 1;
Strleft = strTmp [I]. Left (k + 1 );
}
Strleft = strTmp [I]. Left (k );
AfxMessageBox (strleft );
M_result + = strleft;
M_result + = "| ";
// AfxMessageBox (m_result );
StrTmp [I] = strTmp [I]. Right (strTmp [I]. GetLength ()-k );
}

}
If (! StrTmp [I]. IsEmpty ())
{
M_result + = strTmp [I];
M_result + = "| ";
// AfxMessageBox (m_result );
}
}
Return m_result;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.