Листинг лексического анализатора

private void btnLex_Click(object sender, EventArgs e)

{

char[] Letters={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'G',

 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',

'U', 'V', 'W', 'X', 'Y', 'Z', '_'};

char[] Digits={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

char[] Symbols={',', ';', '(', ')', '=', '<', '>', '{', '}', '.', '+', '-', '*', '/'};

char[] Spaces={' ', char.Parse(Char.ConvertFromUtf32(9))};

 

LexAnalizerState CommentState = LexAnalizerState.Start;

 

lvLexTable.Items.Clear();

lvIdTable.Items.Clear();

lLexProgress.Text = "Лексический анализ завершен успешно!";

lLexProgress.ForeColor = Color.Blue;

lLexProgress.Visible = false;

 

for (int nNumStr = 0; nNumStr < rSrcFile.Lines.Length; nNumStr++)

{

String sSrcLine = rSrcFile.Lines[nNumStr].ToUpper();

if (String.IsNullOrEmpty(sSrcLine)) continue;

 

int nSrcSymbol = 0;

String sLexeme = "", sClass = "";

LexAnalizerState LexState = CommentState;

while(LexState!= LexAnalizerState.Stop)

{

switch(LexState)

{

case LexAnalizerState.Start:

{

if (sSrcLine[nSrcSymbol] == '/')

{

sLexeme += sSrcLine[nSrcSymbol];

nSrcSymbol++;

LexState = LexAnalizerState.Comment;

}

else if (Array.IndexOf(Spaces, sSrcLine[nSrcSymbol])!= -1)

{

nSrcSymbol++;

LexState = (nSrcSymbol == sSrcLine.Length)?

(LexAnalizerState.Stop):(LexAnalizerState.Start);

}

else if (Array.IndexOf(Letters, sSrcLine[nSrcSymbol])!= -1)

{

LexState = LexAnalizerState.Identify;

}

else if (Array.IndexOf(Symbols, sSrcLine[nSrcSymbol])!= -1)

{

sLexeme += sSrcLine[nSrcSymbol];

sClass = "Символ";

nSrcSymbol++;

LexState = LexAnalizerState.Ready;

}

else if (Array.IndexOf(Digits, sSrcLine[nSrcSymbol])!= -1)

{

sLexeme += sSrcLine[nSrcSymbol];

nSrcSymbol++;

LexState = LexAnalizerState.Num;

}

else LexState = LexAnalizerState.Error;

}

break;

case LexAnalizerState.Comment:

{

if (sSrcLine[nSrcSymbol] == '*')

{

sLexeme = "";

sClass = "";

nSrcSymbol++;

LexState = (nSrcSymbol == sSrcLine.Length)?

 (LexAnalizerState.Stop):(LexAnalizerState.CommentStart);

CommentState = LexAnalizerState.CommentStart;

}

else

{

LexState = LexAnalizerState.Ready;

CommentState = LexAnalizerState.Start;

sClass = "Символ";

}

}

break;

case LexAnalizerState.CommentStart:

{

if (sSrcLine[nSrcSymbol] == '*')

{

nSrcSymbol++;

LexState = (nSrcSymbol == sSrcLine.Length)?

(LexAnalizerState.Stop):(LexAnalizerState.CommentEnd);

CommentState = LexAnalizerState.CommentEnd;

}

else

{

nSrcSymbol++;

LexState = (nSrcSymbol == sSrcLine.Length)?

 (LexAnalizerState.Stop):(LexAnalizerState.CommentStart);

}

}

break;

case LexAnalizerState.CommentEnd:

{

if (sSrcLine[nSrcSymbol] == '/')

{

nSrcSymbol++;

LexState = (nSrcSymbol == sSrcLine.Length)?

(LexAnalizerState.Stop):(LexAnalizerState.Start);

CommentState = LexAnalizerState.Start;

}

else

{

nSrcSymbol++;

LexState = (nSrcSymbol == sSrcLine.Length)?

(LexAnalizerState.Stop):(LexAnalizerState.CommentStart);

CommentState = LexAnalizerState.CommentStart;

}

}

break;

case LexAnalizerState.Identify:

{

if ((Array.IndexOf(Digits, sSrcLine[nSrcSymbol])!= -1)

 || (Array.IndexOf(Letters, sSrcLine[nSrcSymbol])!= -1))

{

sLexeme += sSrcLine[nSrcSymbol];

nSrcSymbol++;

LexState = (nSrcSymbol == sSrcLine.Length)?

(LexAnalizerState.Ready):(LexAnalizerState.Identify);

}

else if ((Array.IndexOf(Spaces, sSrcLine[nSrcSymbol])!= -1)

|| (Array.IndexOf(Symbols, sSrcLine[nSrcSymbol])!= -1))

{

LexState = LexAnalizerState.Ready;

}

else LexState = LexAnalizerState.Error;

sClass = "Идентификатор";

}

break;

case LexAnalizerState.Num:

{

if (Array.IndexOf(Digits, sSrcLine[nSrcSymbol])!= -1)

{

sLexeme += sSrcLine[nSrcSymbol];

nSrcSymbol++;

LexState = (nSrcSymbol == sSrcLine.Length)?

(LexAnalizerState.Ready):(LexAnalizerState.Num);

}

else if (sSrcLine[nSrcSymbol] == 'L')

{

sLexeme += sSrcLine[nSrcSymbol];

nSrcSymbol++;

LexState = LexAnalizerState.Ready;

}

else if ((Array.IndexOf(Spaces, sSrcLine[nSrcSymbol])!= -1)

|| (Array.IndexOf(Symbols, sSrcLine[nSrcSymbol])!= -1))

{

LexState = LexAnalizerState.Ready;

}

else LexState = LexAnalizerState.Error;

sClass = "Число";

}

break;

case LexAnalizerState.Ready:

{

sClass = (IsKeyword(sLexeme) == true)?("Ключевое слово"):(sClass);

 

if (sClass == "Идентификатор") AddId(sLexeme);

AddLexeme(sLexeme, sClass);

 

sLexeme = "";

LexState = (nSrcSymbol == sSrcLine.Length)?

(LexAnalizerState.Stop):(LexAnalizerState.Start);

}

break;

case LexAnalizerState.Error:

{

lLexProgress.Text = "ОШИБКА! Неизвестный символ (" +

(nNumStr + 1).ToString() + ", " + (nSrcSymbol + 1).ToString() +

 "): \"" + sSrcLine[nSrcSymbol].ToString() + "\"";

lLexProgress.ForeColor = Color.Red;

LexState = LexAnalizerState.Stop;

}

break;

}

}

}

AddLexeme("#", "Конец");

lLexProgress.Visible = true;

btnSintax.Enabled = true;

синтаксическийАнализToolStripMenuItem.Enabled = true;

}
Приложение Б.



Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: