byDlg.cpp
上传用户:qdzhkyjx
上传日期:2013-04-04
资源大小:43k
文件大小:10k
源码类别:

词法分析

开发平台:

Visual C++

  1. // byDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "by.h"
  5. #include "byDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. char  ch =' '; // 存放读入当前的输入字符
  14. int lineno;
  15. struct reserve  //关键字
  16. {
  17. char lexptr[MAXBUF];
  18. int token;
  19. };
  20. struct reserve symtable[MAX];
  21. char * str[]={"program","input","output","begin","end","var","integer","real","for","to","if","then","else","do","while","write","array","proceure" };
  22. class CAboutDlg : public CDialog
  23. {
  24. public:
  25. CAboutDlg();
  26. // Dialog Data
  27. //{{AFX_DATA(CAboutDlg)
  28. enum { IDD = IDD_ABOUTBOX };
  29. //}}AFX_DATA
  30. // ClassWizard generated virtual function overrides
  31. //{{AFX_VIRTUAL(CAboutDlg)
  32. protected:
  33. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  34. //}}AFX_VIRTUAL
  35. // Implementation
  36. protected:
  37. //{{AFX_MSG(CAboutDlg)
  38. //}}AFX_MSG
  39. DECLARE_MESSAGE_MAP()
  40. };
  41. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  42. {
  43. //{{AFX_DATA_INIT(CAboutDlg)
  44. //}}AFX_DATA_INIT
  45. }
  46. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  47. {
  48. CDialog::DoDataExchange(pDX);
  49. //{{AFX_DATA_MAP(CAboutDlg)
  50. //}}AFX_DATA_MAP
  51. }
  52. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  53. //{{AFX_MSG_MAP(CAboutDlg)
  54. // No message handlers
  55. //}}AFX_MSG_MAP
  56. END_MESSAGE_MAP()
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CByDlg dialog
  59. CByDlg::CByDlg(CWnd* pParent /*=NULL*/)
  60. : CDialog(CByDlg::IDD, pParent)
  61. {
  62. //{{AFX_DATA_INIT(CByDlg)
  63. // NOTE: the ClassWizard will add member initialization here
  64. //}}AFX_DATA_INIT
  65. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  66. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  67. }
  68. void CByDlg::DoDataExchange(CDataExchange* pDX)
  69. {
  70. CDialog::DoDataExchange(pDX);
  71. //{{AFX_DATA_MAP(CByDlg)
  72. DDX_Control(pDX, IDC_STATIC1, m_me);
  73. //}}AFX_DATA_MAP
  74. }
  75. BEGIN_MESSAGE_MAP(CByDlg, CDialog)
  76. //{{AFX_MSG_MAP(CByDlg)
  77. ON_WM_SYSCOMMAND()
  78. ON_WM_PAINT()
  79. ON_WM_QUERYDRAGICON()
  80. ON_BN_CLICKED(IDC_OPEN, OnOpen)
  81. ON_BN_CLICKED(IDC_FENXI, OnFenxi)
  82. ON_BN_CLICKED(IDC_STATIC1, OnStatic1)
  83. ON_BN_CLICKED(IDC_SAVE, OnSave)
  84. //}}AFX_MSG_MAP
  85. END_MESSAGE_MAP()
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CByDlg message handlers
  88. BOOL CByDlg::OnInitDialog()
  89. {
  90. CDialog::OnInitDialog();
  91. // Add "About..." menu item to system menu.
  92. // IDM_ABOUTBOX must be in the system command range.
  93. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  94. ASSERT(IDM_ABOUTBOX < 0xF000);
  95. CMenu* pSysMenu = GetSystemMenu(FALSE);
  96. if (pSysMenu != NULL)
  97. {
  98. CString strAboutMenu;
  99. strAboutMenu.LoadString(IDS_ABOUTBOX);
  100. if (!strAboutMenu.IsEmpty())
  101. {
  102. pSysMenu->AppendMenu(MF_SEPARATOR);
  103. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  104. }
  105. }
  106. // Set the icon for this dialog.  The framework does this automatically
  107. //  when the application's main window is not a dialog
  108. SetIcon(m_hIcon, TRUE); // Set big icon
  109. SetIcon(m_hIcon, FALSE); // Set small icon
  110. m_font.CreateFont(12, 0,0,0,FW_NORMAL, 0,0,0,
  111. DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  112. DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Fixedsys");
  113. GetDlgItem(IDC_EDIT1)->SetFont(&m_font);
  114. GetDlgItem(IDC_EDIT2)->SetFont(&m_font);
  115. m_me.SetToolTipText("访问我的网站: http://www.59hao.com/chx");
  116. return TRUE;  // return TRUE  unless you set the focus to a control
  117. }
  118. void CByDlg::OnSysCommand(UINT nID, LPARAM lParam)
  119. {
  120. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  121. {
  122. CAboutDlg dlgAbout;
  123. dlgAbout.DoModal();
  124. }
  125. else
  126. {
  127. CDialog::OnSysCommand(nID, lParam);
  128. }
  129. }
  130. // If you add a minimize button to your dialog, you will need the code below
  131. //  to draw the icon.  For MFC applications using the document/view model,
  132. //  this is automatically done for you by the framework.
  133. void CByDlg::OnPaint() 
  134. {
  135. if (IsIconic())
  136. {
  137. CPaintDC dc(this); // device context for painting
  138. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  139. // Center icon in client rectangle
  140. int cxIcon = GetSystemMetrics(SM_CXICON);
  141. int cyIcon = GetSystemMetrics(SM_CYICON);
  142. CRect rect;
  143. GetClientRect(&rect);
  144. int x = (rect.Width() - cxIcon + 1) / 2;
  145. int y = (rect.Height() - cyIcon + 1) / 2;
  146. // Draw the icon
  147. dc.DrawIcon(x, y, m_hIcon);
  148. }
  149. else
  150. {
  151. CDialog::OnPaint();
  152. }
  153. }
  154. // The system calls this to obtain the cursor to display while the user drags
  155. //  the minimized window.
  156. HCURSOR CByDlg::OnQueryDragIcon()
  157. {
  158. return (HCURSOR) m_hIcon;
  159. }
  160. void CByDlg::init()
  161. {
  162. for( int j=0; j<18; j++)
  163. strcpy(symtable[j].lexptr,str[j]);
  164. symtable[j].token=j+3;
  165. }
  166. }
  167. int CByDlg::search(char *temp)
  168. {
  169. for(int i=0; i<sizeof(symtable)/sizeof(symtable[0]); i++)
  170. {
  171. if(!strcmp(symtable[i].lexptr ,temp))
  172. {
  173. return  symtable[i].token;
  174. }
  175. }
  176. return 0;
  177. }
  178. void CByDlg::analyse(FILE *fpin, FILE *fpout)
  179. {
  180. char arr[MAXBUF];
  181. int i=0;
  182. int j=0;
  183. while((ch=fgetc(fpin))!=EOF)   //读入字符判断,空格、字母、数字、界符
  184. {
  185. if(ch==' '||ch=='t')
  186. {
  187. }
  188. else if(ch=='n') //如果是换行符,则行号加1
  189. {  
  190. lineno++;
  191. }
  192. else if(isdigit(ch)) //如果是数字
  193. while(isdigit(ch)) //判断和读取数字
  194. arr[j]=ch;
  195. j++;
  196. ch=fgetc(fpin);
  197. arr[j]='';
  198. j=0;
  199. fseek(fpin,-1L,SEEK_CUR);
  200. fprintf(fpout,"%st%dn",arr,2) ;
  201. }
  202. else if (isalpha(ch)) //如果是字母
  203. {
  204. while(isalpha(ch)||isdigit(ch))
  205. {
  206. arr[j]=ch;
  207. j++;
  208. ch=fgetc(fpin);
  209. }
  210. fseek(fpin,-1L,SEEK_CUR);
  211. arr[j]='';
  212. j=0;
  213. if (search(arr)) //如果是关键字
  214. {
  215. fprintf(fpout,"%st%dn",arr,search(arr));
  216. }
  217. else  
  218. fprintf(fpout,"%st%dn",arr,1); //普通标志符
  219. }
  220. else if(ch==':')  
  221. {
  222. ch=fgetc(fpin);
  223. if(ch=='=') 
  224. {
  225. fprintf(fpout,"%st%dn",":=",29);    //如果是 :=
  226. }
  227. else
  228. {
  229. fprintf(fpout,"%st%dn",":",30);   //如果是 :
  230. fseek(fpin,-1L,SEEK_CUR);
  231. }
  232. }
  233. else if (ch=='>')
  234. {
  235. ch=fgetc(fpin);
  236. if(ch=='=')    //如果是 >=
  237. {
  238. fprintf(fpout,"%st%dn",">=",32);
  239. }
  240. else
  241. {
  242. fprintf(fpout,"%st%dn",">",31);  //如果是 >
  243. fseek(fpin,-1L,SEEK_CUR);
  244. }
  245. }
  246. else if(ch=='<')
  247. ch=fgetc(fpin);
  248. if(ch=='>')
  249. {
  250. fprintf(fpout,"%st%dn","<>",35);  // 如果是 <>
  251. }
  252. else if(ch=='=')
  253. {
  254. fprintf(fpout,"%st%dn","<=",34);   //如果是 <=
  255. }
  256. else 
  257. {
  258. fprintf(fpout,"%st%dn","<",33);   //如果是 <
  259. fseek(fpin,-1L,SEEK_CUR);
  260. }
  261. }
  262. else if(ch=='/')
  263. ch=fgetc(fpin);
  264. if(ch=='*')
  265. {
  266. ch=fgetc(fpin);
  267. s:
  268. while(ch!='*')
  269. {
  270. ch=fgetc(fpin);
  271. }
  272. while(ch=='*')
  273. {
  274. ch=fgetc(fpin);
  275. while(ch!='/')
  276. {
  277. goto s; //如果是注释 /*  */
  278. }
  279. }
  280. }
  281. else if(ch=='/')
  282. {
  283. ch=fgetc(fpin);
  284. while(ch!='n')
  285. {
  286. ch=fgetc(fpin);   //如果是注释 //
  287. }
  288. }
  289. else 
  290. {  
  291. fprintf(fpout,"%st%dn","/",24);
  292. fseek(fpin,-1L,SEEK_CUR);
  293. }
  294. }
  295. else if(ch=='+')
  296. {
  297. fprintf(fpout,"%st%dn","+",21);
  298. }
  299. else if(ch=='-')
  300. {
  301. fprintf(fpout,"%st%dn","-",22);
  302. }
  303. else if(ch=='*')
  304. {
  305. fprintf(fpout,"%st%dn","*",23);
  306. }
  307. else if(ch=='(')
  308. {
  309. fprintf(fpout,"%st%dn","(",25);
  310. }
  311. else if(ch==')')
  312. {
  313. fprintf(fpout,"%st%dn",")",26);
  314. }
  315. else if(ch=='[')
  316. {
  317. fprintf(fpout,"%st%dn","[",27);
  318. }
  319. else if(ch==']')
  320. {
  321. fprintf(fpout,"%st%dn","]",28);
  322. }
  323. else if(ch=='.')
  324. {
  325. fprintf(fpout,"%st%dn",".",39);
  326. }
  327. else if(ch==';')
  328. {
  329. fprintf(fpout,"%st%dn",";",36);
  330. }
  331. else if(ch=='=')
  332. {
  333. fprintf(fpout,"%st%dn","=",38);
  334. }
  335. else if(ch==',')
  336. {
  337. fprintf(fpout,"%st%dn",",",40);
  338. }
  339. else fprintf(fpout,"无法识别的字符 %cn",ch)  ;
  340.    }
  341. }
  342. void CByDlg::OnOpen() 
  343. {
  344. CFileDialog fd(true);
  345. fd.m_ofn.lpstrTitle="请选择你要打开的文件";  //标题
  346. fd.m_ofn.lpstrInitialDir="d:\";            //初始目录
  347. if(fd.DoModal())
  348. {
  349. m_strFileName=fd.GetPathName();
  350. CStdioFile sf;
  351. CString str;
  352. if(sf.Open(m_strFileName.GetBuffer(0),CFile::modeRead))
  353. {
  354. CString  strTemp;
  355. while(sf.ReadString(strTemp))
  356. {
  357. str+=strTemp;
  358. str+="rn";
  359. }
  360. SetDlgItemText(IDC_EDIT1,str);
  361. sf.Close();
  362. }
  363. }
  364. }
  365. void CByDlg::OnFenxi() 
  366. {
  367. if(this->m_strFileName.IsEmpty())
  368. {
  369. MessageBox("请先选择一个源文件或者保存你编辑的代码后再进行词法分析","词法分析器",MB_ICONEXCLAMATION);
  370. return;
  371. }
  372. FILE* fpin=fopen(m_strFileName.GetBuffer(0),"r");
  373. FILE* fpout=fopen("output.txt","w");
  374. init();
  375. analyse(fpin,fpout);
  376. fclose(fpin);
  377. fclose(fpout);
  378. CStdioFile sf;
  379. CString str;
  380. if(sf.Open("output.txt",CFile::modeRead))
  381. {
  382. CString  strTemp;
  383. while(sf.ReadString(strTemp))
  384. {
  385. str+=strTemp;
  386. str+="rn";
  387. }
  388. SetDlgItemText(IDC_EDIT2,str);
  389. sf.Close();
  390. }
  391. }
  392. void CByDlg::OnStatic1() 
  393. {
  394. ::ShellExecute(NULL,"open","http://www.59hao.com/chx",NULL,NULL,SW_SHOWNORMAL);
  395. }
  396. BOOL CByDlg::PreTranslateMessage(MSG* pMsg) 
  397. {
  398.     if(pMsg -> message == WM_KEYDOWN)
  399. {
  400.         if(pMsg -> wParam == VK_ESCAPE)
  401. return TRUE;
  402. }return CDialog::PreTranslateMessage(pMsg);
  403. }
  404. void CByDlg::OnSave() 
  405. {
  406. if(this->m_strFileName.IsEmpty())
  407. {
  408. CFileDialog fd(false);
  409. fd.m_ofn.lpstrTitle="请选择你要打开的文件";  //标题
  410. fd.m_ofn.lpstrInitialDir="d:\";            //初始目录
  411. if(fd.DoModal())
  412. {
  413. m_strFileName=fd.GetPathName();
  414. }
  415. }
  416. CFile sf;
  417. if(sf.Open(this->m_strFileName,CFile::modeWrite|CFile::modeCreate))
  418. {
  419. UpdateData();
  420. char buf[200]={0};
  421. GetDlgItem(IDC_EDIT1)->GetWindowText(buf,200);
  422. sf.Write(buf,strlen(buf));
  423. sf.Close();
  424. MessageBox("保存成功","词法分析器",MB_ICONINFORMATION);
  425. }
  426. }