📄 编译原理dlg.cpp
字号:
// 编译原理Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "编译原理.h"
#include "编译原理Dlg.h"
#include<fstream>
#include<string>
#include<stack>
#include<vector>
#include "point.h"
#include "E.h"
#include "siyuanshi.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
using namespace std;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Control(pDX, IDC_EDIT4, m_result);
DDX_Control(pDX, Edit_input, m_Input);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(Menu_save, Onsave)
ON_COMMAND(Menu_Analyse, OnAnalyse)
ON_BN_CLICKED(IDC_Analyse, OnAnalyse_Button)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::OnInitDialog()
{
word2=new CString[50];
word2_len=0;
CDialog::OnInitDialog();
isTrue=true;
content=new char[10000];
nextline=new char[3];
nextline[0]='\r'; nextline[1]='\n'; nextline[2]='\0';
sentence=new CString[50];
CheckRadioButton(IDC_RADIO1,IDC_RADIO3,IDC_RADIO2);//单选按钮的初始化
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyDlg::Onsave() //单击保存在默认的文件里面
{
content[0]='\0';
m_Input.GetWindowText(STR);
count=STR.GetLength();
for(int i=0;i<count;i++)
content[i]=STR.GetAt(i);
content[count]='\0';
if(content=="")
MessageBox("编辑框中的内容为空!");
else
{
fstream fout;
def_filepath="save.txt";
fout.open(def_filepath,ios::out);
fout<<content;
isSaved=true;
fout.close();
}
}
void CMyDlg::OnAnalyse()
{
int type;
isTrue=true;
out_type=2;
//-----------------
CifaFenxi();//词法分析
if(isTrue)
YufaFenxi();
if(isTrue)
{
Printsiyushi();
PrintInWindow();
MessageBox("分析成功!");
}
else
{
if(isSaved==true)
MessageBox("语法有错误!");
}
ClearVar();
}
int CMyDlg::Search(char *Buffer)
{
char *key[]={"auto","break","case","char","const","continue","default","do","double",
"else","enum","extern","float","for","goto","if","int","long","register",
"return","short","signed","sizeof","static","struct","switch","typedef",
"union","unsigned","void","volatile","while"}; //C语言所有关键字 32个
char *limit[]={";","(",")","[","]",".","!","&","~",",","{","}","#","_","'"};//限界符
char *opt[]={"+","-","*","/","%","<","<=",">",
">=","==","!=","&&","||","<<",">>","->"};//操作运算符
char *evaluate[]={"=","+=","-=","*=","/=","++","--"};//赋值符
int isSearched=0;//是否被匹配
int r=0;
//-----------------开始搜索
for(int i=0;i<32;i++){//先搜索关键字
if(strcmp(Buffer,key[i])==0){
isSearched=1;
location=i+1;
r=1;
break;
}
}
if(isSearched==0){
for(int i=0;i<15;i++)
if(strcmp(Buffer,limit[i])==0){
isSearched=1;
location=i+1;
r=2;
break;
} }
if(isSearched==0){
for(int i=0;i<16;i++)
if(strcmp(Buffer,opt[i])==0){
isSearched=1;
location=i+1;
r=3;
break;
}
}
if(isSearched==0){
for(int i=0;i<7;i++)
if(strcmp(Buffer,evaluate[i])==0){
isSearched=1;
location=i+1;
r=4;
break;
}
}
if(isSearched==0){
if((Buffer[0]>='0')&&(Buffer[0]<='9')){
isSearched=1;
r=5;
}
}
if(isSearched==0){
if(Buffer[0]=='\"'){
isSearched=1;
r=6;
}
}
if(isSearched==0){
if(Buffer[0]=='\''){
isSearched=1;
r=7;
}
}
if(isSearched==0) {
if(((Buffer[0]>='A')&&(Buffer[0]<='Z'))||((Buffer[0]>='a')&&(Buffer[0]<='z'))||(Buffer[0]=='_'))
{
isSearched=1;
r=8;
}
}
return r;
}
void CMyDlg::CifaFenxi()//词法分析
{
char temp;
word2_len=0;
m_Input.GetWindowText(str);
if(str.Compare(STR)!=0)//0表示两字符串相等
isSaved=false;//当修改后需要保存文件
else{
count=str.GetLength();
for(int i=0;i<count;i++)
content[i]=str.GetAt(i);
content[count]='\0';
}
char *array=new char[100];//暂时存放一个单词
int j=0;
int Style;
fstream out_file;
if(isSaved==true)
{
out_file.open("cifafenxi.txt",ios::out);
for(int i=0;i<count;i++)
{ j=0;
array[0]='\0';
temp=content[i];
if(((temp>='A')&&(temp<='Z'))||((temp>='a')&&(temp<='z'))||(temp=='_'))
//以字母或下划线开头
{
while(((temp>='A')&&(temp<='Z'))||((temp>='a')&&(temp<='z'))||(temp=='_')||
((temp>='0')&&(temp<='9')))
{ array[j]=temp;
i++;
j++;
temp=content[i];
} i--;
array[j]='\0';
}
else if((temp>='0')&&(temp<='9')){//数字开头
while((temp>='0')&&(temp<='9')){
array[j]=temp;
i++;
j++;
temp=content[i];
} i--;
array[j]='\0';
}
else if((temp=='{')||(temp=='(')||(temp==')')||(temp=='[')||(temp==']')
||(temp=='~')||(temp==',')||(temp==';')
||(temp=='#')||(temp=='_')||(temp=='.')||(temp=='}')||(temp=='%')){
array[0]=temp;
array[1]='\0';
}
else if((temp=='!')||(temp=='=')||(temp=='<')||(temp=='>')||(temp=='-')||(temp=='+')||
(temp=='*'))
{
i++;
array[0]=temp;
temp=content[i];
if((temp!='=')&&(array[0]=='!')){
array[1]='\0';
i--;
}
else if((temp==array[0])||(temp=='=')||((array[0]=='-')&&(temp=='>'))){
array[1]=temp;
array[2]='\0';
}
else{
array[1]='\0';
i--;
}
}
else if((temp=='&')||(temp=='|')){//&&、||
i++;
array[0]=temp;
temp=content[i];
if(temp==array[0])
{
array[1]=temp;
array[2]='\0';
}
else{
array[1]='\0';
i--;
}
}
else if(temp=='/'){
i++;
temp=content[i];
if(temp=='=')//如果是符号'/='
{
i++;
out_file<<"/="<<" "<<" "<<"---->运算符"<<endl;
continue;
}
else if(temp=='/')//是双斜线的注释,删除一行的内容
{
while(temp!='\n'){
i++;
temp=content[i];
}
}
else if(temp!='*'){
i--;
array[0]=content[i];
array[1]='\0';
}
else if(temp=='*'){
while(temp!='/'){
i++;
temp=content[i];
}
}
}
else if((temp=='\"')||(temp=='\''))
//单引号或者双引号
{ i++;
array[0]=temp;
array[1]=content[i];
array[2]='\0';
i++;
}
else if((temp==' ')||(temp='\t')||temp=='\n'||temp=='\r')//空格|水平制表符|回车
continue;
//---------------输出内容---------------------------------
if(array[0]!='\0')
{
str.Format("%s",array);
// words.push(str);//存入新单词
Style=Search(array);
word2[word2_len]=str;
word2_len++;
}
if(Style>0)
switch(Style)
{
case 1:
out_file<<array<<" "<<"关键字"<<'\r'<<endl;
break;
case 2:
out_file<<array<<" "<<"限定符"<<'\r'<<endl;
break;
case 3:
out_file<<array<<" "<<"操作符"<<'\r'<<endl;
break;
case 4:
out_file<<array<<" "<<"赋值符"<<'\r'<<endl;
break;
case 5:
out_file<<array<<" "<<"数字"<<'\r'<<endl;
break;
case 6:
out_file<<array[1]<<" "<<"字符串常量"<<'\r'<<endl;
break;
case 7:
out_file<<array[1]<<" "<<"字符常量"<<'\r'<<endl;
break;
case 8:
out_file<<array<<" "<<"标识符"<<'\r'<<endl;
break;
}
}
out_file.close();
}
else
{
MessageBox("请先保存文件内容");
}
}
//方法2
void CMyDlg::YufaFenxi()
{
bool flag=false;//id,i标志
int m=0,len_sb=0;//符号栈的长度
int prior=3;
CString row;
word2[word2_len]="#";
int i=0;
fstream OUT_file;
char *process=new char[200];
OUT_file.open("Yufa.txt",ios::out);
CString current,Q;
CString product_r[27]={"#G#","doAwhileN;","A1}","{K","B1)","(B","K1K","E1;","i=E",
"E+E","E-E","E*E","E/E","(E)","i","B&&B","B||B","idRid","true","false",
"N","<","<=",">",">=","==","!="}; //k-->ε另外讨论
CString product_l[27]={"G1","G","A","A1","N","B1","K","K1","E1","E","E","E","E","E","E",
"B","B","B","B","B","B","R","R","R","R","R","R"};
stack<CString>stk;
stack<CString>wd;
//translate
stack<point>oprend;//操作数
point pt,x,y;
int rt=0;
CString opt,RT;
char *log=new char[20];
stack<E>bol;
//-----
CString *stk_sb=new CString[50];
stk_sb[len_sb]="#";
len_sb++;
stk.push("#");
char *Temp=new char[10];
int product_p=0;
int time=0;
nextstart=0;
while(i<=word2_len)
{
Temp[0]='\0';
current=word2[i];
if(Compare2(current)==1)
{
if(flag==false)
current="i";
else
current="id";
}
if(current=="while")
flag=true;
//打印语法分析过程
process[0]='\0';
int p=0;
strcpy(process,"#");
for(p=1;p<len_sb;p++)
strcat(process,stk_sb[p]);
strcat(process," ");
strcat(process,word2[i]);
strcat(process," ");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -