using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace complier
{
public partial class Form1 : Form
{
OpenFileDialog file = new OpenFileDialog();
public Form1()
{
InitializeComponent();
txtOutput.SelectAll();
}
private string[] token; //标识符表
private int tokenIndex = 0; //标识符表指针
private long[] constant; //整型常数表
private int constantIndex = 0; //整型常数表指针
private double[] dblConstant; //浮点常数表
private int dblConstantIndex = 0; //浮点常数表指针
private string[] strConst; //字符串表
private int strConstIndex; //字符串表指针
private char[] charConst; //字符表
private int charConstIndex = 0; //字符表指针
private string input; //存放待识别的源程序字符串
private int inputIndex = 0; //input字符串指针
private char ch; //存放最新读进的源程序字符
private string strToken; //存放构成单词符号的字符串
private bool scanned = false; //是否扫描过至少一次
private static string[] tokenReserve =
{ "If",
"else",
"while",
"read",
"write",
"int",
"real",
}; //C#保留关键字表(7个)
//将下一输入字符读到ch中,搜索指示器前移一字符位置
private void getChar()
{
ch = input[inputIndex];
inputIndex++;
}
//将ch中的字符连接到strToken之后
private void concat()
{
strToken += ch;
}
//判断ch中的字符是否为字母
private bool isLetter()
{
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) return true;
else return false;
}
//判断ch中的字符是否为数字
private bool isDigit()
{
if (ch >= '0' && ch <= '9') return true;
else return false;
}
//对strToken中的字符串查找保留字表,若它是一个保留字则返回其编码,否则返回0
private int reserve()
{
int i;
bool notFound = true;
string s = strToken;
for (i = 0; notFound && i < tokenReserve.Length; i++)
{
if (tokenReserve[i] == s) notFound = false;
}
if (notFound) return 0;
else return i;
}
//将搜索指示器回调一个字符位置,将ch置为空白字符
private void retract()
{
inputIndex--;
}
//将strToken中的标识符插入符号表(表中不存在该元素时才插入),返回符号表指针
private int insertId()
{
//先在符号表里找该元素
int i;
bool notFound = true;
string s = strToken;
for (i = 0; notFound && i < token.Length; i++)
{
if (token[i] == s) notFound = false;
}
//没找到
if (notFound)
{
token[tokenIndex] = strToken;
tokenIndex++;
return (tokenIndex - 1);
}
//找到了
else return i - 1;
}
//将strToken中的整型常数插入整型常数表(表中不存在该元素时才插入),返回整型常数表指针
private int insertConst()
{
int i;
bool notFound = true;
long s = int.Parse(strToken);
for (i = 0; notFound && i < constant.Length; i++)
{
if (constant[i] == s) notFound = false;
}
//没找到
if (notFound)
{
constant[constantIndex] = int.Parse(strToken);
constantIndex++;
return (constantIndex - 1);
}
//找到了
else return i - 1;
}
//将strToken中的浮点型常数插入浮点常数表(表中不存在该元素时才插入),返回浮点常数表指针
private int insertDblConst()
{
int i;
bool notFound = true;
double s = double.Parse(strToken);
for (i = 0; notFound && i < dblConstant.Length; i++)
{
if (dblConstant[i] == s) notFound = false;
}
//没找到
if (notFound)
{
dblConstant[dblConstantIndex] = double.Parse(strToken);
dblConstantIndex++;
return (dblConstantIndex - 1);
}
//找到了
else return i - 1;
}
//将strToken中的字符串插入字符串表(表中不存在该元素时才插入),返回字符串表指针
private int insertString()
{
int i;
bool notFound = true;
string s = strToken;
for (i = 0; notFound && i < strConst.Length; i++)
{
if (strConst[i] == s) notFound = false;
}
//没找到
if (notFound)
{
strConst[strConstIndex] = strToken;
strConstIndex++;
return (strConstIndex - 1);
}
//找到了
else return i - 1;
}
//将strToken中的字符插入字符表(表中不存在该元素时才插入),返回字符表指针
private int insertChar()
{
int i;
bool notFound = true;
char c = ch;
for (i = 0; notFound && i < charConst.Length; i++)
{
if (charConst[i] == c) notFound = false;
}
//没找到
if (notFound)
{
charConst[charConstIndex] = ch;
charConstIndex++;
return (charConstIndex - 1);
}
//找到了
else return i - 1;
}
//Scan方法
private string scan()
{
strToken = "";
int code, value;
getChar();
//标识符及保留字
if (isLetter() || ch == '_')
{
if (input.Length > 1)
{
while ((isLetter() || isDigit() || (ch == '_')) && inputIndex < input.Length)
{
concat();
getChar();
}
if (inputIndex < input.Length) retract();
else if (inputIndex == input.Length)
{
if (isLetter() || isDigit() || (ch == '_')) concat();
else retract();
}
code = reserve();
if (code == 0)
{
value = insertId();
return (strToken + " < @ID" + ", " + value.ToString() + " >");
}
else return (strToken + " < @RESERVED_WORD_" + code.ToString() + ", - >");
}
else
{
return (ch + " < @ID" + ", 0 >");
}
}
//整型常数和浮点常数
else if (isDigit())
{
if (input.Length > 1)
{
while (isDigit() && inputIndex < input.Length)
{