bianyiDlg.cpp
上传用户:imnightsky
上传日期:2013-04-01
资源大小:37k
文件大小:12k
源码类别:

词法分析

开发平台:

Visual C++

  1. // bianyiDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "bianyi.h"
  5. #include "bianyiDlg.h"
  6. #include<ctype.h>
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CBianyiDlg dialog
  51. CBianyiDlg::CBianyiDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CBianyiDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CBianyiDlg)
  55. m_show = _T("");
  56. //}}AFX_DATA_INIT
  57. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }
  60. void CBianyiDlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62. CDialog::DoDataExchange(pDX);
  63. //{{AFX_DATA_MAP(CBianyiDlg)
  64. DDX_Control(pDX, IDC_EDIT_SHOW, m_com);
  65. DDX_Control(pDX, IDC_EDIT_GET, m_get);
  66. DDX_Text(pDX, IDC_EDIT_SHOW, m_show);
  67. //}}AFX_DATA_MAP
  68. }
  69. BEGIN_MESSAGE_MAP(CBianyiDlg, CDialog)
  70. //{{AFX_MSG_MAP(CBianyiDlg)
  71. ON_WM_SYSCOMMAND()
  72. ON_WM_PAINT()
  73. ON_WM_QUERYDRAGICON()
  74. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  75. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  76. ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  77. ON_EN_CHANGE(IDC_EDIT_GET, OnChangeEditGet)
  78. //}}AFX_MSG_MAP
  79. END_MESSAGE_MAP()
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CBianyiDlg message handlers
  82. BOOL CBianyiDlg::OnInitDialog()
  83. {
  84. CDialog::OnInitDialog();
  85. // Add "About..." menu item to system menu.
  86. // IDM_ABOUTBOX must be in the system command range.
  87. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  88. ASSERT(IDM_ABOUTBOX < 0xF000);
  89. CMenu* pSysMenu = GetSystemMenu(FALSE);
  90. if (pSysMenu != NULL)
  91. {
  92. CString strAboutMenu;
  93. strAboutMenu.LoadString(IDS_ABOUTBOX);
  94. if (!strAboutMenu.IsEmpty())
  95. {
  96. pSysMenu->AppendMenu(MF_SEPARATOR);
  97. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  98. }
  99. }
  100. // Set the icon for this dialog.  The framework does this automatically
  101. //  when the application's main window is not a dialog
  102. SetIcon(m_hIcon, TRUE); // Set big icon
  103. SetIcon(m_hIcon, FALSE); // Set small icon
  104. // TODO: Add extra initialization here
  105. return TRUE;  // return TRUE  unless you set the focus to a control
  106. }
  107. void CBianyiDlg::OnSysCommand(UINT nID, LPARAM lParam)
  108. {
  109. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  110. {
  111. CAboutDlg dlgAbout;
  112. dlgAbout.DoModal();
  113. }
  114. else
  115. {
  116. CDialog::OnSysCommand(nID, lParam);
  117. }
  118. }
  119. // If you add a minimize button to your dialog, you will need the code below
  120. //  to draw the icon.  For MFC applications using the document/view model,
  121. //  this is automatically done for you by the framework.
  122. void CBianyiDlg::OnPaint() 
  123. {
  124. if (IsIconic())
  125. {
  126. CPaintDC dc(this); // device context for painting
  127. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  128. // Center icon in client rectangle
  129. int cxIcon = GetSystemMetrics(SM_CXICON);
  130. int cyIcon = GetSystemMetrics(SM_CYICON);
  131. CRect rect;
  132. GetClientRect(&rect);
  133. int x = (rect.Width() - cxIcon + 1) / 2;
  134. int y = (rect.Height() - cyIcon + 1) / 2;
  135. // Draw the icon
  136. dc.DrawIcon(x, y, m_hIcon);
  137. }
  138. else
  139. {
  140. CDialog::OnPaint();
  141. }
  142. }
  143. // The system calls this to obtain the cursor to display while the user drags
  144. //  the minimized window.
  145. HCURSOR CBianyiDlg::OnQueryDragIcon()
  146. {
  147. return (HCURSOR) m_hIcon;
  148. }
  149. void CBianyiDlg::OnButton1() 
  150. {
  151. CString GetString;
  152. m_get.GetWindowText(GetString);
  153. GetString=GetString+'';
  154. Saomiao(GetString);
  155. UpdateData(0);
  156. }
  157. void CBianyiDlg::OnButton2() 
  158. {
  159. m_show=_T("");
  160. m_get.SetWindowText("");
  161. UpdateData(0);
  162. // TODO: Add your control notification handler code here
  163. }
  164. void CBianyiDlg::Saomiao(CString GetString)
  165. {
  166. int i=0,Temp,Sum=117,START=2000,SZTemp=0;
  167. char Ch=' ';
  168. CString MidString,Ms,s=_T(""),ID,CS;
  169. CString KEYWORDS[117]={"else","main","struct","enum","switch","auto","template","explicit",
  170. "this","bool","extern","mutable","break","false","throw","case","namespace","true",
  171. "catch","new","try","float","noreturn","char","for","operator","typedef",
  172. "class","friend","private","typeid","const","goto","protected","typename","const_cast",
  173. "if","public","union","continue","inline","register","unsigned","reinterpret_cast",
  174. "using","default","int","return","uuid","delete","short","signed","virtual","sizeof",
  175. "void","do","static","volatile","double","static_cast","dynamic_cast","long","while",
  176.     "<<=","<","<=","<<",">>",">>=",">",">=","->*","-","--","-=","->",
  177. "+=","+","++","^=","^","&=","&&","&","|","|=","||","*=","*","/=","/","//","/*","*/","%=","%",
  178. "=","==","!=","!",".*",".","::",":","{","}","(",")","[","]","~",",","?","#",";","'","""};
  179. while(Ch!='')
  180. {
  181. Ch=GetString[i];
  182. while(Empty(Ch))Ch=GetString[++i];
  183. s.Format(_T("%c"),Ch);
  184. Ms=s;
  185. if(ZOrX(Ch))
  186. {
  187. SZTemp=0;
  188. while(ZOrXOrS(GetString[i+1]))
  189. {
  190. Ch=GetString[++i];
  191. s.Format(_T("%c"),Ch);
  192. Ms=Ms+s;
  193. }
  194. // Temp=MyCompare(KEYWORDS,Ms,Sum);
  195. // if(Temp==-1)ID="BIANLIANG";
  196. // else ID.Format(_T("%d"),Temp+START);
  197. // CS=CS+Ms+"     "+ID+"rn";
  198. // ID=_T("");
  199. }
  200. else if(Shuzi(Ch))
  201. {
  202. while(Shuzi(GetString[i+1]))
  203. {
  204. Ch=GetString[++i];
  205. s.Format(_T("%c"),Ch);
  206. Ms=Ms+s;
  207. }
  208. if(GetString[i+1]=='.')
  209. {
  210. Ch=GetString[++i];
  211. s.Format(_T("%c"),Ch);
  212. Ms=Ms+s;
  213. while(Shuzi(GetString[i+1]))
  214. {
  215. Ch=GetString[++i];
  216. s.Format(_T("%c"),Ch);
  217. Ms=Ms+s;
  218. }
  219. }
  220. SZTemp=1;
  221. // ID="CHANGLIANG";
  222. // CS=CS+Ms+"     "+ID+"rn";
  223. // ID=_T("");
  224. }
  225. else switch(Ch)  //////// 字符
  226. {
  227. case '<': ////////   <<=  <=   <<   <
  228. if(GetString[i+1]=='=')
  229. {
  230. Ms="<=";i++;
  231. }
  232. else if(GetString[i+1]=='<')
  233. {
  234. if(GetString[i+2]=='=')
  235. {
  236. Ms="<<=";i+=2;
  237. }
  238. else Ms="<<";
  239. i++;
  240. }
  241. else Ms="<";break;
  242. case '>': ////////    >=   >    >>=  >>
  243. if(GetString[i+1]=='=')
  244. {
  245. Ms=">=";i++;
  246. }
  247. else if(GetString[i+1]=='>')
  248. {
  249. if(GetString[i+2]=='=')
  250. {
  251. Ms=">>=";i+=2;
  252. }
  253. else Ms=">>";
  254. i++;
  255. }
  256. else Ms=">";break;
  257. case '-':   ///   ->*  -=   --   ->   -
  258. if(GetString[i+1]=='>')
  259. {
  260. if(GetString[i+2]=='*')
  261. {
  262. Ms="->*";i+=2;
  263. }
  264. else Ms="->";i++;
  265. }
  266. else if(GetString[i+1]=='=')
  267. {
  268. Ms="-=";i++;
  269. }
  270. else if(GetString[i+1]=='-')
  271. {
  272. Ms="--";i++;
  273. }
  274. /////////////////////
  275. else if(Shuzi(GetString[i+1]))
  276. {
  277. while(Shuzi(GetString[i+1]))
  278. {
  279. Ch=GetString[++i];
  280. s.Format(_T("%c"),Ch);
  281. Ms=Ms+s;
  282. }
  283. if(GetString[i+1]=='.')
  284. {
  285. Ch=GetString[++i];
  286. s.Format(_T("%c"),Ch);
  287. Ms=Ms+s;
  288. while(Shuzi(GetString[i+1]))
  289. {
  290. Ch=GetString[++i];
  291. s.Format(_T("%c"),Ch);
  292. Ms=Ms+s;
  293. }
  294. }
  295. SZTemp=1;
  296. }
  297. ////////////
  298. else Ms="-";break;
  299. case '+':  ////// +=   ++  +
  300. if(GetString[i+1]=='+')
  301. {
  302. Ms="++";i++;
  303. }
  304. else if(GetString[i+1]=='=')
  305. {
  306. Ms="+=";i++;
  307. }
  308. else Ms="+";break;
  309. case '&':  /////////  &&  &=   &
  310. if(GetString[i+1]=='&')
  311. Ms="&&";i++;
  312. }
  313. else if (GetString[i+1]=='=')
  314. {
  315. Ms="&=";i++;
  316. }
  317. else Ms="&";break;
  318. case '^':   ///////   ^=   ^
  319. if(GetString[i+1]=='=')
  320. {
  321. Ms="^=";i++;
  322. }
  323. else Ms="^";break;
  324. case '!':  ////  !=   !
  325. if(GetString[i+1]=='=')
  326. {
  327. Ms="!=";i++;
  328. }
  329. else Ms="!";break;
  330. case '|':   ////////  ||    |   |=
  331. if(GetString[i+1]=='|')
  332. {
  333. Ms="||";i++;
  334. }
  335. else if(GetString[i+1]=='=')
  336. {
  337. Ms="|=";i++;
  338. }
  339. else Ms="|";break;
  340. case '*':  //  *  *=
  341. if(GetString[i+1]=='=')
  342. {
  343. Ms="*=";i++;
  344. }
  345. else if(GetString[i+1]=='/')
  346. {
  347. Ms="*/";i++;
  348. }
  349. else Ms="*";break;
  350. case '/':  /////   /=
  351. if(GetString[i+1]=='=')
  352. {
  353. Ms="/=";i++;
  354. }
  355. else if(GetString[i+1]=='*')
  356. i++;
  357. while(GetString[i+1]!=''&&!(GetString[i+1]=='*'&&GetString[i+2]=='/'))i++;
  358. Ms="/*";
  359. }
  360. else if(GetString[i+1]=='/')
  361. {
  362. while(GetString[1+i]!='n'&&GetString[i+1]!='')++i;
  363. Ms="//";
  364. }
  365. else Ms="/";break;
  366. case '=':  ///  ==  =
  367. if(GetString[i+1]=='=')
  368. {
  369. Ms="==";i++;
  370. }
  371. else Ms="=";break;
  372. case '%':
  373. if(GetString[i+1]=='=')
  374. {
  375. Ms="%=";i++;
  376. }
  377. else Ms="%";break;
  378. case '.':
  379. if(GetString[i+1]=='*')
  380. {
  381. Ms=".*";i++;
  382. }
  383. else Ms=".";break;
  384. case '(':
  385. Ms="(";break;
  386. case ')':
  387. Ms=")";break;
  388. case ':':
  389. if(GetString[i+1]==':')
  390. {
  391. Ms="::";i++;
  392. }
  393. else Ms=":";break;
  394. case '{':
  395. Ms="{";break;
  396. case '}':
  397. Ms="}";break;
  398. case '[':
  399. Ms="[";break;
  400. case ']':
  401. Ms="]";break;
  402. case '~':
  403. Ms="~";break;
  404.         }
  405. if(SZTemp==1)ID="数字常量";
  406. else if(Ms=="")ID="rrrn---编译结束-----";
  407. else
  408. {
  409. Temp=MyCompare(KEYWORDS,Ms,Sum);
  410. if(GetString[i+1]=='')ID=_T("");
  411. else if(Temp==-1)ID="变量";
  412. else ID.Format(_T("%d"),Temp+START);
  413. }
  414. CS=CS+Ms+"           "+ID+"rn";
  415. ID=_T("");SZTemp=0;
  416. i++;
  417.     }
  418. m_show=CS;
  419. UpdateData(0);
  420. }
  421. int CBianyiDlg::MyCompare(CString KEYWORDS[],CString Ms,int Sum)
  422. {
  423. int loop,result=1;
  424. for(loop=0;loop<Sum;loop++)
  425. if(!strcmp(KEYWORDS[loop],Ms) )return loop;
  426. return -1;
  427. }
  428. BOOL CBianyiDlg::Empty(char Ch)
  429. {
  430. if(Ch=='r'||Ch=='n'||Ch==' '||Ch=='b'||Ch=='t')return TRUE;
  431. else return FALSE;
  432. }
  433. BOOL CBianyiDlg::Shuzi(char Ch)
  434. {
  435. if(Ch>='0'&&Ch<='9')return TRUE;
  436. else return FALSE;
  437. }
  438. BOOL CBianyiDlg::ZOrX(char Ch)
  439. {
  440. if(Ch>='a'&&Ch<='z' || Ch>='A'&&Ch<='Z' || Ch=='_')return TRUE;
  441. else return FALSE;
  442. }
  443. BOOL CBianyiDlg::ZOrXOrS(char Ch)
  444. {
  445. if(Ch>='a'&&Ch<='z' || Ch>='A'&&Ch<='Z' || Ch=='_'||Ch>='0'&&Ch<='9')return TRUE;
  446. else return FALSE;
  447. }
  448. void CBianyiDlg::OnButton3() 
  449. {
  450. // TODO: Add your control notification handler code here
  451. CFileDialog m_File(true);
  452. ofstream myFile;
  453. CString Get,Ms=_T(""),s;
  454. if (m_File.DoModal() == IDOK)
  455. {
  456. Get= m_File.GetFileName();
  457. ifstream OpenFile(Get);
  458. char ch;
  459. while(!OpenFile.eof())
  460. {
  461. OpenFile.get(ch);
  462. if(ch=='n')Ms=Ms+'r'+'n';
  463. else Ms=Ms+ch;
  464. }
  465. int length=Ms.GetLength();
  466. m_get.SetWindowText(Ms);
  467. UpdateData(0);
  468. m_str=Ms;
  469. OpenFile.close();
  470. }
  471. // Get=file->;
  472. // myFile.open(Get,ios::out,filebuf::openprot);
  473. //
  474. }
  475. void CBianyiDlg::OnChangeEditGet() 
  476. {
  477. // TODO: If this is a RICHEDIT control, the control will not
  478. // send this notification unless you override the CDialog::OnInitDialog()
  479. // function and call CRichEditCtrl().SetEventMask()
  480. // with the ENM_CHANGE flag ORed into the mask.
  481. // TODO: Add your control notification handler code here
  482. m_get.ModifyStyle(0, ES_WANTRETURN);
  483. // m_Edit.ModifyStyle(0, ES_WANTRETURN);
  484. }