⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 whiledlg.cpp

📁 编译原理程序设计 写的很好哦 很有参考价值
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// whileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "while.h"
#include "whileDlg.h"



#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stack>

/************************************************************************************
                       (1)      S --> while(A) B                                    
                       (2)      A --> N rop N 
                       (3)      B --> N = N op N                                                           
                       (4)      N --> id
*************************************************************************************/

const  buf=20;
const  MAXBUF=255;
const  Act=8;
const  Goto=4;
const  n_lab=16;
const  Total=12;

typedef struct Token
{
	int num;
	char name[buf];
}Token;

typedef struct Ana_lable
{
	int Action[Act];
	int Goto[Goto];
	int lab;                       //移进,规约标志(0移进,1规约)
	int status;                       
	int num;                       //规约使用的产生式编号
	char chr[Total];
}Ana_lable;

char sym,ch,S[MAXBUF];
int num;
int t;
int i;
int Check;
char *wh="while",*rop="rop",*op="op";
Token token[MAXBUF];
Ana_lable lable[n_lab];


std::stack <int> status;
std::stack <char> symbol;

ifstream input;
ofstream output;


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class OTTF
{
public:
	static void one()
	{
		int over=0;
		char One[5];
		One[0]='w';
		One[1]='(';
		One[2]='A';
		One[3]=')';
		One[4]='B';
		int k=0;
		for (k=0;k<5;k++)
		{
			if(!symbol.empty())
			{
				if(One[4-k]==symbol.top()) 
				{
					over=0;
					symbol.pop();
				}
				else over=-1;
			}
			else 
			{
				over=-1;
				break;
			}
		}
		if(over==0) 
		{
			symbol.push('S');
			for(k=0;k<5;k++) status.pop();
		}
		else Check=-1;
	}
	
	static void two()
	{
		int over=0;
		char Two[3];
		Two[0]='N';
		Two[1]='r';
		Two[2]='N';
		int k=0;
		for (k=0;k<3;k++)
		{
			if(!symbol.empty())
			{
				if(Two[2-k]==symbol.top()) 
				{
					over=0;
					symbol.pop();
				}
				else over=-1;
			}
			else 
			{
				over=-1;
				break;
			}
		}
		if(over==0) 
		{
			symbol.push('A');
			for(k=0;k<3;k++) status.pop();
		}
		else Check=-1;
	}
	
	static void three()
	{
		int over=0;
		char Three[5];
		Three[0]='N';
		Three[1]='=';
		Three[2]='N';
		Three[3]='o';
		Three[4]='N';
		int k=0;
		for (k=0;k<5;k++)
		{
			if(!symbol.empty())
			{
				if(Three[4-k]==symbol.top()) 
				{
					over=0;
					symbol.pop();
				}
				else over=-1;
			}
			else 
			{
				over=-1;
				break;
			}
		}
		if(over==0) 
		{
			symbol.push('B');
			for(k=0;k<5;k++) status.pop();
		}
		else Check=-1;
	}
	
	static void four()
	{
		int over=0;                    //
		char Four[1];
		Four[0]='i';
		int k=0;                     //
		for (k=0;k<1;k++)
		{
			if(!symbol.empty())
			{
				if(symbol.top()==Four[0-k])         //
				{
					over=0;
					symbol.pop();
				}
				else over=-1;
			}
			else 
			{
				over=-1;
				break;
			}
		}     
		if(over==0)                        
		{
			symbol.push('N');
			for(k=0;k<1;k++) 
			{
				status.pop();
			}
		}
		else Check=-1;
	}
};
class AG
{
public:
	static void ag(int c)
	{
		switch(c)
		{
		case 1:
			OTTF::one();
			break;
		case 2:
			OTTF::two();
			break;
		case 3:
			OTTF::three();
			break;
		case 4:
			OTTF::four();
			break;
		default:
			break;
		}
	}
};

class Mt
{
public:
	friend class AG;
	static int meet(int c,char s);
};


Mt::meet(int c,char s)
{
	if(s=='X') return -1;
	int m,k=0;
	if(lable[c].lab==1)
	{
		AG::ag(lable[c].num); 
		//	cout<<"规约成功"<<endl;
		return 1;                         //规约成功
	}
	else
	{
		for(k=0;k<Total;k++)
		{
			if(lable[c].chr[k]==s) 
			{
				m=k;
				break;
			}
			if(k>11) return -1;
		}
		if(lable[c].chr[m]=='#'&&lable[c].Action[m]==-2) return 2;                 //acc
		else 
			if((m<8&&lable[c].Action[m]==-1)||m>=8&&lable[c].Goto[m-8]==-1) return -1;   
			else 
				if(m<8)
				{
					symbol.push(S[i]);
					status.push(lable[c].Action[m]);
					i++;
					return 0;
				}
				else 
					if(m>=8)
					{
						status.push(lable[c].Goto[m-8]);
						return 0;
					}
					else return -1;
	}  
}
class NN
{
public:
	static void N(int c)
	{
		int a[buf];
		for(i=0;i<buf;i++) a[i]=0;
		int i=0,k=0,j=0;
		for(i=0;i<num;i++)
		{
			if(S[i]=='i') a[k++]=i;
		}
		j=a[c-1];
		for(k=0;k<token[j].num;k++)
		{
			cout<<token[j].name[k];
			output<<token[j].name[k];
	}
	}
};

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()

/////////////////////////////////////////////////////////////////////////////
// CWhileDlg dialog

CWhileDlg::CWhileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWhileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWhileDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWhileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWhileDlg)
	DDX_Control(pDX, IDC_DSP, m_Edit);
	DDX_Control(pDX, IDC_EDIT_OUTPUT, m_Output);
	DDX_Control(pDX, IDC_EDIT_INPUT, m_Input);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWhileDlg, CDialog)
	//{{AFX_MSG_MAP(CWhileDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnTr)
	ON_BN_CLICKED(IDC_BUTTON3, OnExit)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWhileDlg message handlers

BOOL CWhileDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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 CWhileDlg::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 CWhileDlg::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 CWhileDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWhileDlg::OnOK() 
{
	// TODO: Add extra validation here
	CDialog::OnOK();
}

void CWhileDlg::SaveTr()
{
	char c_c;
	ofstream tfile("output.txt",ios::out);
	ifstream f_c;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -