Смекни!
smekni.com

Лексический и синтаксический анализатор языка высокого уровня (стр. 6 из 7)

/*21*/{"<NEXT_VAR>", "=", "<EXPR>", "<NEXT_VAR>", "", "", "", "", "", "", "", "", "", "", ""},

/*22*/{"<NEXT_VAR>", "e", "", "", "", "", "", "", "", "", "", "", "", "", ""},

/*23*/{"<OPER_LIST>", "<OPERATOR>", "<NEXT_OPER>", "", "", "", "", "", "", "", "", "", "", "", ""},

/*24*/{"<NEXT_OPER>", ";", "<OPER_LIST>", "", "", "", "", "", "", "", "", "", "", "", ""},

/*25*/{"<NEXT_OPER>", "e", "", "", "", "", "", "", "", "", "", "", "", "", ""},

/*26*/{"<OPERATOR>", "FOR", "(", "ID", "=", "<EXPR>", ";", "<COND>", ";", "ID", "<LET>", ")", "{", "<OPER_LIST>", "}"},

/*27*/{"<OPERATOR>", "break", "", "", "", "", "", "", "", "", "", "", "", "", ""},

/*28*/{"<OPERATOR>", "continue", "", "", "", "", "", "", "", "", "", "", "", "", ""},

/*29*/{"<OPERATOR>", "WRITE", "(", "<VAR_LIST>", ")", "", "", "", "", "", "", "", "", "", ""},

/*30*/{"<OPERATOR>", "READ", "(", "<VAR_LIST>", ")", "", "", "", "", "", "", "", "", "", ""},

/*31*/{"<OPERATOR>", "ID", "<LET>", "", "", "", "", "", "", "", "", "", "", "", ""},

/*32*/{"<LET>", "=", "<EXPR>", "", "", "", "", "", "", "", "", "", "", "", ""},

/*33*/{"<LET>", "*", "=", "<EXPR>", "", "", "", "", "", "", "", "", "", "", ""},

/*34*/{"<LET>", "/", "=", "<EXPR>", "", "", "", "", "", "", "", "", "", "", ""},

/*35*/{"<EXPR>", "(", "<EXPR>", ")", "<OPERATION>", "", "", "", "", "", "", "", "", "", ""},

/*36*/{"<EXPR>", "ID", "<OPERATION>", "", "", "", "", "", "", "", "", "", "", "", ""},

/*37*/{"<EXPR>", "NUM", "<OPERATION>", "", "", "", "", "", "", "", "", "", "", "", ""},

/*38*/{"<OPERATION>", "+", "<EXPR>", "", "", "", "", "", "", "", "", "", "", "", ""},

/*39*/{"<OPERATION>", "-", "<EXPR>", "", "", "", "", "", "", "", "", "", "", "", ""},

/*40*/{"<OPERATION>", "e", "", "", "", "", "", "", "", "", "", "", "", "", ""},

/*41*/{"<COND>", "(", "<COND>", ")", "<RELATION>", "", "", "", "", "", "", "", "", "", ""},

/*42*/{"<COND>", "<EXPR>", "<RELATION>", "", "", "", "", "", "", "", "", "", "", "", ""},

/*43*/{"<RELATION>", ">", "<COND>", "", "", "", "", "", "", "", "", "", "", "", ""},

/*44*/{"<RELATION>", "<", "<COND>", "", "", "", "", "", "", "", "", "", "", "", ""},

/*45*/{"<RELATION>", "=", "=", "<COND>", "", "", "", "", "", "", "", "", "", "", ""},

/*46*/{"<RELATION>", "e", "", "", "", "", "", "", "", "", "", "", "", "", ""},

};

System.Collections.Stack SintaxStack = new System.Collections.Stack();

SintaxStack.Push("#");

SintaxStack.Push("<S>");

String sTopStack, sLexeme, sClass;

SinAnalizerState SintaxState = SinAnalizerState.InProcess;

lSinProgress.Visible = false;

lvSinTable.Items.Clear();

int nLexNum = 0;

while (SintaxState == SinAnalizerState.InProcess)

{

sTopStack = (String) SintaxStack.Peek();

sLexeme = lvLexTable.Items[nLexNum].SubItems[0].Text;

sClass = lvLexTable.Items[nLexNum].SubItems[1].Text;

String[] subItems = {sTopStack, sLexeme, sClass};

lvSinTable.Items.Add(new ListViewItem(subItems));

if (sTopStack == "#")

{

SintaxState = (sLexeme == "#")?(SinAnalizerState.Accept):(SinAnalizerState.Error);

}

else if (sTopStack == "<S>")

{

if (sLexeme == "#")

{

SintaxStack.Pop();

}

else if (sLexeme == "PUBLIC")

{

Replace(SintaxStack, Grammatic, 1, 2);

nLexNum++;

}

else if (sLexeme == "USING")

{

Replace(SintaxStack, Grammatic, 0, 2);

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "LONG")

{

if (sLexeme == "LONG")

{

SintaxStack.Pop();

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "INT")

{

if (sLexeme == "INT")

{

SintaxStack.Pop();

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "(")

{

if (sLexeme == "(")

{

SintaxStack.Pop();

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == ")")

{

if (sLexeme == ")")

{

SintaxStack.Pop();

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == ";")

{

if (sLexeme == ";")

{

SintaxStack.Pop();

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "{")

{

if (sLexeme == "{")

{

SintaxStack.Pop();

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "}")

{

if (sLexeme == "}")

{

SintaxStack.Pop();

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<CLASS_BODY>")

{

if (sLexeme == "PUBLIC")

{

Replace(SintaxStack, Grammatic, 8, 2);

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<CLASS>")

{

if (sLexeme == "CLASS")

{

Replace(SintaxStack, Grammatic, 7, 2);

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<DEF_LIST>")

{

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

{

Replace(SintaxStack, Grammatic, 14, 2);

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<DEF>")

{

if (sLexeme == "UINT")

{

Replace(SintaxStack, Grammatic, 11, 2);

nLexNum++;

}

else if (sLexeme == "BOOL")

{

Replace(SintaxStack, Grammatic, 12, 2);

nLexNum++;

}

else if (sLexeme == "CONST")

{

Replace(SintaxStack, Grammatic, 13, 2);

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<EXPR>")

{

if (sLexeme == "(")

{

Replace(SintaxStack, Grammatic, 35, 2);

nLexNum++;

}

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

{

Replace(SintaxStack, Grammatic, 36, 2);

nLexNum++;

}

else if (sClass == "Число")

{

Replace(SintaxStack, Grammatic, 37, 2);

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<LET>")

{

if (sLexeme == "/")

{

Replace(SintaxStack, Grammatic, 34, 2);

nLexNum++;

}

else if (sLexeme == "*")

{

Replace(SintaxStack, Grammatic, 33, 2);

nLexNum++;

}

else if (sLexeme == "=")

{

Replace(SintaxStack, Grammatic, 32, 2);

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<NEXT_BODY>")

{

if (sLexeme == ";")

{

Replace(SintaxStack, Grammatic, 9, 2);

nLexNum++;

}

else if (sLexeme == "}")

{

SintaxStack.Pop();

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<NEXT_DEF>")

{

if (sLexeme == "(")

{

Replace(SintaxStack, Grammatic, 15, 2);

nLexNum++;

}

else if (sLexeme == ",")

{

Replace(SintaxStack, Grammatic, 16, 2);

nLexNum++;

}

else if (sLexeme == "=")

{

Replace(SintaxStack, Grammatic, 17, 2);

nLexNum++;

}

else if (sLexeme == ";")

{

SintaxStack.Pop();

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<NEXT_OPER>")

{

if (sLexeme == ";")

{

Replace(SintaxStack, Grammatic, 24, 2);

nLexNum++;

}

else if (sLexeme == "}")

{

SintaxStack.Pop();

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<NEXT_USING>")

{

if (sLexeme == ".")

{

Replace(SintaxStack, Grammatic, 5, 2);

nLexNum++;

}

else if (sLexeme == ";")

{

SintaxStack.Pop();

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<NEXT_VAR>")

{

if (sLexeme == ",")

{

Replace(SintaxStack, Grammatic, 20, 2);

nLexNum++;

}

else if (sLexeme == "=")

{

Replace(SintaxStack, Grammatic, 21, 2);

nLexNum++;

}

else if ((sLexeme == ")") || (sLexeme == ";"))

{

SintaxStack.Pop();

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<NEXT>")

{

if (sLexeme == ";")

{

Replace(SintaxStack, Grammatic, 2, 2);

nLexNum++;

}

else if (sLexeme == "#")

{

SintaxStack.Pop();

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<OPER_LIST>")

{

if ((sLexeme == "FOR") ||

(sLexeme == "CONTINUE") ||

(sLexeme == "BREAK") ||

(sLexeme == "READ") ||

(sLexeme == "WRITE") ||

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

{

Replace(SintaxStack, Grammatic, 23, 1);

}

else if (sLexeme == "}")

{

SintaxStack.Pop();

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<OPERATION>")

{

if (sLexeme == "+")

{

Replace(SintaxStack, Grammatic, 38, 2);

nLexNum++;

}

else if (sLexeme == "-")

{

Replace(SintaxStack, Grammatic, 39, 2);

nLexNum++;

}

else if ((sLexeme == ")") ||

(sLexeme == ";") ||

(sLexeme == ",") ||

(sLexeme == ">") ||

(sLexeme == "<") ||

(sLexeme == "="))

{

SintaxStack.Pop();

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<OPERATOR>")

{

if (sLexeme == "FOR")

{

Replace(SintaxStack, Grammatic, 26, 2);

nLexNum++;

}

else if (sLexeme == "BREAK")

{

SintaxStack.Pop();

nLexNum++;

}

else if (sLexeme == "CONTINUE")

{

SintaxStack.Pop();

nLexNum++;

}

else if (sLexeme == "READ")

{

Replace(SintaxStack, Grammatic, 30, 2);

nLexNum++;

}

else if (sLexeme == "WRITE")

{

Replace(SintaxStack, Grammatic, 29, 2);

nLexNum++;

}

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

{

Replace(SintaxStack, Grammatic, 31, 2);

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<USING_LIST>")

{

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

{

Replace(SintaxStack, Grammatic, 4, 2);

nLexNum++;

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<COND>")

{

if (sLexeme == "(")

{

Replace(SintaxStack, Grammatic, 41, 2);

nLexNum++;

}

else if ((sClass == "Идентификатор") ||

(sClass == "Число"))

{

Replace(SintaxStack, Grammatic, 42, 1);

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<RELATION>")

{

if (sLexeme == ">")

{

Replace(SintaxStack, Grammatic, 43, 2);

nLexNum++;

}

else if (sLexeme == "<")

{

Replace(SintaxStack, Grammatic, 44, 2);

nLexNum++;

}

else if (sLexeme == "=")

{

Replace(SintaxStack, Grammatic, 45, 2);

nLexNum++;

}

else if ((sLexeme == ")") ||

(sLexeme == ";"))

{

SintaxStack.Pop();

}

else SintaxState = SinAnalizerState.Error;

}

else if (sTopStack == "<VAR_LIST>")

{

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

{

Replace(SintaxStack, Grammatic, 19, 2);

nLexNum++;

}

else if (sLexeme == ";")

{

SintaxStack.Pop();