?? mylexdlg.cpp
字號:
// MyLEXDlg.cpp : 實現文件
//
#include "stdafx.h"
#include "MyLEX.h"
#include "MyLEXDlg.h"
#include ".\mylexdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
List<TreeNode*> P;
CString content1;
// 用于應用程序“關于”菜單項的 CAboutDlg 對話框
CString def[8]={"if","then","else","end","repeat","until","read","write"};
int isDefined(CString &str)
{
for(int i=0;i<8;i++)
{
if(str==def[i])
return i+1;
}
return 0;
}
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 對話框數據
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 實現
protected:
DECLARE_MESSAGE_MAP()
public:
// virtual BOOL DestroyWindow();
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CMyLEXDlg 對話框
CMyLEXDlg::CMyLEXDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyLEXDlg::IDD, pParent)
, m_p(_T(""))
, m_a(_T(""))
, num1(0)
, m_all(_T(""))
, m_s(_T(""))
, p(1)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyLEXDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT1, m_program);
DDX_Text(pDX, IDC_EDIT1, m_p);
DDX_Control(pDX, IDC_EDIT2, m_answer);
DDX_Text(pDX, IDC_EDIT2, m_a);
//DDX_Control(pDX, IDC_CHECK, m_bCtrl);
}
BEGIN_MESSAGE_MAP(CMyLEXDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_FENXI, OnBnClickedFenxi)
ON_BN_CLICKED(IDC_CHECK, OnBnClickedCheck)
END_MESSAGE_MAP()
// CMyLEXDlg 消息處理程序
BOOL CMyLEXDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 將\“關于...\”菜單項添加到系統菜單中。
// IDM_ABOUTBOX 必須在系統命令范圍內。
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);
}
}
// 設置此對話框的圖標。當應用程序主窗口不是對話框時,框架將自動
// 執行此操作
SetIcon(m_hIcon, TRUE); // 設置大圖標
SetIcon(m_hIcon, FALSE); // 設置小圖標
// TODO: 在此添加額外的初始化代碼
//m_bCtrl.EnableWindow(1);
return TRUE; // 除非設置了控件的焦點,否則返回 TRUE
}
void CMyLEXDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向對話框添加最小化按鈕,則需要下面的代碼
// 來繪制該圖標。對于使用文檔/視圖模型的 MFC 應用程序,
// 這將由框架自動完成。
void CMyLEXDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于繪制的設備上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使圖標在工作矩形中居中
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;
// 繪制圖標
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//當用戶拖動最小化窗口時系統調用此函數取得光標顯示。
HCURSOR CMyLEXDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
int CMyLEXDlg::GetToken(CString & name)
{
name="";
char ch=m_p.GetAt(num1);
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
name+=ch;
ch=m_p.GetAt(++num1);
}
//num1--;
int ii=0;
if(ii=isDefined(name))
return ii;
else return id;
}
else if(ch>='0'&&ch<='9')
{
while(ch>='0'&&ch<='9')
{
name+=ch;
ch=m_p.GetAt(++num1);
}
//num1--;
return num;
}
switch(ch)
{
case '+':name+=ch;num1++;return plus;break;
case '-':name+=ch;num1++;return dec;break;
case '*':name+=ch;num1++;return mul;break;
case '/':name+=ch;num1++;return div;break;
case '=':name+=ch;num1++;return equ;break;
case '<':name+=ch;num1++;return plus;break;
case '{':name+=ch;num1++;return lef;break;
case '}':name+=ch;num1++;return rig;break;
case ';':name+=ch;num1++;return fen;break;
case ':':name+=ch;ch=m_p.GetAt(++num1);if(ch=='='){name+=ch;num1++;}return fuz;break;
case' ':
case'\t':num1++;return 1000;
case'\r':num1++;return 1001;
case'\n':num1++;return 1000;
default:return 0;
}
return 0;
}
void CMyLEXDlg::OnBnClickedFenxi()
{
p=1;
P.empty();
content1="";
m_answer.SetWindowText("");
UpdateData();
content1=m_p;
num1=0;
int j=0;
CString idName,temp;
m_a="line 1:";
int line=1;
while(num1<m_p.GetLength())
{
if(j=GetToken(idName))
{
if(j!=1000&&j!=1001)
{
VEC * v=new VEC();
v->i=j;
v->name=idName;
vl.Insert(v);
temp.Format("<%d,",j);
idName+=">";
m_a+=temp+idName;
idName="";
}
else if (j==1001)
{
program=prase();
if(!program)
{
CString str;
str.Format("第%d行,第%d個字符串 \"%s\" 附近有語法錯誤!!",line,p,vl.Find(p)->name);
AfxMessageBox(str);
str="";
vl.empty();
p=1;
return ;
}
P.Insert(program);
vl.empty();
p=1;
line++;
idName.Format("line %d:",line);
m_a+="\r\n"+idName;
}
}
else
{
idName.Format("%s處有錯誤",idName);
m_a+=idName;
break;
}
}
if(vl.length)
{
program=prase();
if(!program)
{
CString str;
str.Format("第%d行,第%d個字符串 \"%s\" 附近有語法錯誤!!",line,p-1,vl.Find(p-1)->name);
AfxMessageBox(str);
str="";
vl.empty();
p=1;
return ;
}
P.Insert(program);
vl.empty();
p=1;
}
m_answer.SetWindowText(m_a);
}
TreeNode* CMyLEXDlg::prase()
{
TreeNode* root=new TreeNode("stmt_seqence");
if(stmt_seqence(root))
return root;
return NULL;
}
TreeNode* CMyLEXDlg::stmt_seqence(TreeNode* root)
{
if(p>vl.length)
return root;
if((vl.Find(p)->i)==If||(vl.Find(p)->i)==Repeat||(vl.Find(p)->i)==Read||(vl.Find(p)->i)==Write||(vl.Find(p)->i)==id)
{
TreeNode* statement1=new TreeNode("statement");
if(statement(statement1))root->list.Insert(statement1);
else return NULL;
TreeNode* stmt_sequence11=new TreeNode("stmt_sequence\'");
if(stmt_seqence1(stmt_sequence11))root->list.Insert(stmt_sequence11);
else return NULL;
return root;
}
return NULL;
}
TreeNode* CMyLEXDlg::stmt_seqence1(TreeNode* root)
{
if((vl.Find(p)->i)==fen)
{
TreeNode* fen1=new TreeNode(";");
match(fen);
root->list.Insert(fen1);
if(p>vl.length)
return root;
TreeNode* statementx=new TreeNode("statement");
if(statement(statementx))root->list.Insert(statementx);
else return NULL;
TreeNode* stmt_sequence1x=new TreeNode("stmt_sequence\'");
if(stmt_seqence1(stmt_sequence1x))root->list.Insert(stmt_sequence1x);
else return NULL;
return root;
}
else
{
TreeNode* N=new TreeNode(" ");
root->list.Insert(N);
return N;
}
}
TreeNode* CMyLEXDlg::statement(TreeNode* root)
{
if(p>vl.length)
return root;
else if((vl.Find(p)->i)==If)
{
TreeNode* if_stmtx=new TreeNode("if_stmt");
if(if_stmt(if_stmtx)){root->list.Insert(if_stmtx);return if_stmtx;}
else return NULL;
}
else if((vl.Find(p)->i)==Repeat)
{
TreeNode* repeat_stmtx=new TreeNode("repeat_stmt");
if(repeat_stmt(repeat_stmtx)){root->list.Insert(repeat_stmtx);return repeat_stmtx;}
else return NULL;
}
else if((vl.Find(p)->i)==Read)
{
TreeNode* read_stmtx=new TreeNode("read_stmt");
if(read_stmt(read_stmtx)){root->list.Insert(read_stmtx);return read_stmtx;}
else return NULL;
}
else if((vl.Find(p)->i)==Write)
{
TreeNode* write_stmtx=new TreeNode("write_stmt");
if(write_stmt(write_stmtx)){root->list.Insert(write_stmtx);return write_stmtx;}
else return NULL;
}
else if((vl.Find(p)->i)==id)
{
TreeNode* assign_stmtx=new TreeNode("assign_stmt");
if(assign_stmt(assign_stmtx)){root->list.Insert(assign_stmtx);return assign_stmtx;}
else return NULL;
}
return NULL;
}
TreeNode* CMyLEXDlg::if_stmt(TreeNode* root)
{
if(p>vl.length)
return root;
TreeNode* if1=new TreeNode("if");
match(If);
root->list.Insert(if1);
if(p>vl.length)
return NULL;
TreeNode* expx=new TreeNode("exp");
if(exp(expx))root->list.Insert(expx);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -