词法分析器设计实验报告 联系客服

发布时间 : 星期二 文章词法分析器设计实验报告更新完毕开始阅读

ch = text_is[p++]; }

type_code = -1; } else {

number = 0;

while ((ch >= '0' && ch <= '9')) {

number = number * 10 + ch - '0'; ch = text_is[p++]; } p--;

type_code = 41; if (number > 32767) number = -1; } }

else switch (ch) //其他字符 {

case '<':text=text+ch; ch = text_is[p++]; if (ch == '>') {

type_code = 48; text = text + ch; }

else if (ch == '=') {

type_code = 49; text = text + ch; } else {

type_code = 47; p--; } break;

case '>': text = text + ch; ch = text_is[p++]; if (ch == '=')

{

type_code = 50; text = text + ch; } else {

type_code= 46; p--; } break;

case '+': text = text + ch; ch = text_is[p++]; if (ch == '+') {

text = text + ch; ch = text_is[p++]; if(ch=='+'||ch=='-'){ text = text + ch; type_code = -1; } else

type_code = 57; p--; } else {

type_code = 44; p--; } break;

case '-': text = text + ch; ch = text_is[p++]; if (ch == '-') {

text = text + ch; ch = text_is[p++]; if (ch == '+' || ch == '-') {

text = text + ch; type_code = -1; } else

type_code = 58;

p--; } else {

type_code = 45; p--; } break;

case '*': type_code = 42; text = \ case '/': type_code = 43; text = \ case '=': type_code = 51; text = \ case ';': type_code = 52; text = \ case '(': type_code = 53; text = \ case ')': type_code = 54; text = \ case '{': type_code = 55; text = \ case '}': type_code = 56; text = \ case '#': type_code = 0; text = \ case '\\n': type_code = -2; break; case '\\r': type_code = -3; break; default: type_code = -1; break; } }

private void button1_Click(object sender, EventArgs e) {

String path = \

OpenFileDialog op = new OpenFileDialog(); if (op.ShowDialog() == DialogResult.OK) {

path = op.FileName;

richTextBox1.Text = System.IO.File.ReadAllText(path, Encoding.Default); } }

private String getTextIo(){

return richTextBox1.Text+'#'; }

private void addData(String data1,String data2,String data3,String data4){ try {

DataGridViewRow dgr = new DataGridViewRow();

DataGridViewTextBoxCell dt1 = new DataGridViewTextBoxCell(); DataGridViewTextBoxCell dt2 = new DataGridViewTextBoxCell(); DataGridViewTextBoxCell dt3 = new DataGridViewTextBoxCell(); DataGridViewTextBoxCell dt4 = new DataGridViewTextBoxCell(); dt1.Value = data1;

dt2.Value = data2; dt3.Value = data3; dt4.Value = data4; dgr.Cells.Add(dt1); dgr.Cells.Add(dt2); dgr.Cells.Add(dt3); dgr.Cells.Add(dt4);

dataGridView1.Rows.Add(dgr); }

catch (Exception) {

; } }

private void button2_Click(object sender, EventArgs e) {

type_code = 10; p = 0; line = 1; column = 1; try {

dataGridView1.Rows.Clear(); }

catch (Exception) { ; }

text_is = getTextIo().ToCharArray(); do {

readIo();

switch (type_code) {

case 41:

addData(number+\\type_code+ \+ number + \常量\

column++; break; case -1:

addData(text + \有错误\+ line + \