资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
精选优质文档-倾情为你奉上/* * 写在最前面 * 词法分析前先将源码预处理,去掉多余的空格和换行以及注释 * 单词与单词之间最好用空格隔开,否则将被识别为未知符号 * */using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Collections;using System.Text;using System.Windows.Forms;using System.IO;namespace 词法分析 public partial class Form1 : Form private static string 保留字 =bool,break,case,char,do,double,else, false,float,for,if,int,long,main,new,null,private,protected,public, static,true,try,typeof,unit,void,while,return,short,sizeof,switch ; private static string 运算符 =*,/,%,+,-,=, =,!=,&,|,&,|,?:,+=,-=,*=,/=,%=,&=,|=,=, =,=; private static string 界符 = , ;, ., , (, ), :, , , , , / ; private static string 标识符; private static string 常数; private static string 未知符号; private ArrayList temp = new ArrayList(); public Form1() InitializeComponent(); public int Search_Location(string str, string a) /查找字符编码 for (int i = 0; i a.Length; i+) if (str = ai) return i + 1; return -1; public int InsertId(string strr, ref string a)/标识符插入标识符表返回位置 temp.Clear(); if (a = null) temp.Add(strr); else for (int i = 0; i a.Length; i+) if (strr = ai) return i; for (int j = 0; j a.Length; j+) temp.Add(aj); temp.Add(strr); a = (string)temp.ToArray(typeof(string); return a.Length - 1; private string Pretreatment() /预处理 string s1 = textBox1.Text; string str1 = null; /去除换行和/注释 string str = textBox1.Lines; for (int i = 0; i str.Length; i+) if (stri.Contains(/) string strr = stri.Split(/); stri = strr0; for (int i = 0; i str.Length; i+) str1 += stri + ; /去除跳格,回车 if (str1.Contains(/v) str1 = str1.Replace(/v, ); if (str1.Contains(/r) str1 = str1.Replace(/r, ); if (str1.Contains(/n) str1 = str1.Replace(/n, ); if (str1.Contains(/t) str1 = str1.Replace(/t, ); /去除注释 while (str1.Contains(/*) & str1.Contains(*/) str1 = str1.Remove(str1.IndexOf(/*), str1.IndexOf(*/) - str1.IndexOf(/*) + 2); /去除多余空格 while (str1.Contains( ) str1 = str1.Replace( , ); return str1; private bool IsOperator(string str) for (int i = 0; i 运算符.Length; i+) if (str = 运算符i) return true; return false; private bool IsCommunity_character(string str) for (int i = 0; i 界符.Length; i+) if (str = 界符i) return true; return false; void analyze(string string1, ref string string2) string str = string1.Split(new char ); for (int i = 0; i str.Length - 1; i+) switch (liexingpanduan(stri) case 0: string2 += ( + stri + , + 保留字, + Search_Location(stri, 保留字) + ) + r + n; break; case 1: string2 += ( + stri + , + 标识符, + InsertId(stri, ref 标识符) + ) + r + n; break; case 2: string2 += ( + stri + , + 常数, + InsertId(stri, ref 常数) + ) + r + n;
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号