close

Вход

Забыли?

вход по аккаунту

?

Kursovaya rabota(23)

код для вставкиСкачать
???????????? ??????????? ? ????? ??
??????????? ??????????????? ?????????
??????????????? ?????????? ??????? ????????????????? ??????????? ?????
??????? ???????????? ???????????
???????? ??????
?? ?????????? ?????? ????? ????????????????
????: "?????????? ??????????? ? ????????????? ???????????? ????? ???????? ??????"
?????????:
??????? 4 ?????
????????????? ????-0906
??????????? ?.?.
????????:
??????? ?.?.
????? 2012
??????????.
????????............................................................................................................3
1. ????????????? ?????......................................................................................4
1.1 ??????????............................................................................................4
1.2 ??????????? ??????????.........................................................................4
1.3 ?????????????? ??????????.....................................................................5
1.4 ????????? ????......................................................................................5
2. ???????????? ?????.......................................................................................7
2.1 ????????? ????? ? ???. ?????????, ???????????, ????????? ?????? ? ???????.................................................................................................7
2.2 ?????? ???????????................................................................................9
2.3 ??????????? ??????????........................................................................11
2.4 ?????????????? ??????????...................................................................15
2.5 ????????? ????.....................................................................................18
3. ???????????? ??????????............................................................................22
??????????.......................................................................................................26
????????.
??? ????????, ????? ?????????? ???????? ?????????????? ????????? ?????? ????????? ? ?????, ??????? ????? ??????? ????????. ? ???????? ???????? ????? ????????? ??? ???????????, ??? ? ??????????? ????????. ?????????????, ? ????????? ?????????????? ?????? ?????????? ????????????? ??????????? ???????? ?????????? ?????. ????????, ??? ?????????? ????? ??? ?????????????:
* ?????????? ????????? ?????????(????????) ??????.
* ?????????? ????????? ???????????? ?????????????? ????????.
* ???????????????.
* ???????????????? ??????.
* ??????? ?? ?????? ??????????? ?????????? ????????? ????????? ??????.
???? ????????????? ?????? ??????? ??? ??????????, ?????? ??? ??????????, ??????? ????? ??????????? ???????????? ??????? ??????, ??? ????? ?? ?????? ?????? ?????????, ?????? ?? ?????. ??? ?? ????? ????? ???????? ????????? ??????? ??????, ??????? ??????????? ?????? ?? ????? ????.
????? ?????? ???????? ?????? ???????? ?????????? ???????????. ??? ?????????? ???????????? ???? ?????????? ?????? ????????? ??????:
* ??????????? ????????? ????? ? ???. ?????????? ?????????, ???????????, ????????? ?????? ? ????? ?????? ??? ??????? ?????.
* ??????? ?????? ???????????.
* ????????? ??????????? ??????????. ??????????? ?????? ??????????? ?????? ???? ??????? ??????.
* ????????? ?????????????? ??????????. ?????????? ????????? ? ???????? ???????? ??????.
* ????????? ????????? ????.
* ?????????????? ??????????.
1. ????????????? ?????.
1.1 ??????????.
?????????? - ????????? ??? ??????????? ????????, ??????????? ?????????? ?????????. ???? ?????????? - ????????????? ????? ? ?????? ????? ?? ??????, ??????? ??????? ???????? ??????. ? ?????? ????????-????????????, ????????? ???????? ??????????? ?????????? (?????????) ??? ?????????-?????????????.
???? ??????????? (???????? ???) ?????? ???????? ??????????????. ?????????? ?????????, ???????????? ? ???????? ????????? ???? ???????? ?????? , ?? ??? ???????? ??????????? ?? ??????? ? ???? ????????? ? ???????????. ??????????, ??????? ??????????? ????????? ? ???????? ????, ??????????? ? ??????????? ??????????????? ???????????, ?????????? ????????????.
??????? ?????????? ??? ??????? ??????? ?? ?????????? ??????: ????????????, ??????????????? ? ?????????????? ????????, ????????? ?????????????? ????, ??????????? ? ????????? ??????????????? ????????? ????. ?????? ?????, ????????? ??? ??????? ??????? ?? ????????, ??????????????? ???????????? ???????? ? ?????????? ???????????? (????????, ???????? ????-????? ??? ??????????? ?????????), ? ???????? ??? ????????? ?????????? ??????? ? ????? ?????????. ?????????? ?? ???????????? ???????????? ??????????? ?????????? ?????? ??? ????????????? (??????? ????? ???????????? ????? ????????? ????????? ??? ???? ?????? ???????????), ? ? ???????????? ???????? ? ????????????? ???????????? ?????????? ??????????? ??? ?????? ?????????? ????????? ???????????.
??????????? ???????????: ????????? ????????????? ???? ??? ? ??? ?????? ?????????? ?? ????????? ?????????????? ??????????????. ??????????????, ?? ????????? ??????? ??????????? ?? ??????? ??????, ??? ??????? ????????????? ?????????. ??????????: ????????? ???? ?????????? ????????? ????????? ? ??????? ? ?????????? ?????????? ?????????, ????????? ??? ??????? ????????.
1.2 ??????????? ??????????.
? ??????????? ??????????? ?????? - ??????? ?????????????? ??????? ??????? ?????????????????? ???????? (????????, ????? ??? ???????? ??? ?? ????? ?? ?????? ????????????????) ? ????? ????????? ?? ?????? ?????????????????? ????????, ?????????? "????????" (??????? ??????????? ???? ? ?????). ?????? ???????? ??????? ??????????????????, ???????????????? ?? ?????? ???????? ??? ?????, ?????????? ????????. ? ???????? ???????????? ??????? ???????????? ????????????? ? ????????? ?????? ?? ??????? ?????????????????? ????????.
??? ???????, ??????????? ?????? ???????????? ? ????? ?????? ????????????? ??????????? ????? ??? ?????? ??????. ????, ? ?????? ??? ??????????, ?????? ???????????? ????? ??????, ??????? ????? ??????????? ?? ????? ????????.
??????????? ??????? ?????????????? ??????? ???????????? ???????, ???????????? ??????? ?????????????????? ???????? ??? ????? ????????. ??? ????? ??????????? ??????? ?????????????? ????????? ???????? ????????? ???????? ?? ???????? ??????. ????????????? ?????? ? ????????? ?????????? ?????? ???????????? ????? ?? ????????????? (??? ?????????????) ???????? ??????????????? (??? ???????) ???????, ???????????? ??????????? ?????. ??? ???? ????? ?????????????????? ???????? ???????? ?????? (???????), ??????? ???????? ?????????? ?? ????? ???? ???????????????? ??? ????? ?????, ?????? ??????????????? ??? ??????????? ?????-??????.
?????? ????? ????? ??????????? ? ???? ?????????, ?????????? ????????????? ?????? (??? ????????????? ?????? ??????) ?, ???? ?????, ?????????????????? ???????? ???????, ?????????? ?? ???????? ??????.
???? ????? ??????????? ?????? ??????? ? ???, ????? ??????????? ??????? ?????????????????? ??? ?????? ?????????, ???????? ??? ??????????????? ???????????, ? ???????? ??? ?? ??????????? ??????????? ???????????? ? ??????????-????????? ?????????? (??? ??????? ?? ? ?????????? ??????????).
1.3 ?????????????? ??????????.
? ???????????, ????????�?????? ???�??? - ??? ??????? ????????????? ???????? ?????????????????? ?????? (????, ???????) ????? ? ??? ?????????? ???????????. ??????????? ?????? ???????? ?????? ??????? (?????????????? ??????). ?????? ??????????? ????????? ? ??????????? ????????. ?????????????? ?????????? (??????) - ??? ????????? ??? ????? ?????????, ??????????? ?????????????? ??????.
??? ???????? ???????? ????? ????????????? ? ????????? ??????, ?????? - ? ??????, ??????? ???????? ?????????????? ????????? ??????? ?????????????????? ? ?????? ???????? ??? ?????????? ?????????.
??? ???????, ??????????? ??????????????? ??????? ???????? ?????????????? ????????? ???????????, ?????????????? ???? ? ???? ?????? ????????????, ???? ? ???? ?????? ????????????, ???? ? ???? ????????? ?????????? ??????? ? ??????? ???????? ?????????????.
1.4 ????????? ????.
????????? ????? ?????????? ???????? ????????? ????. ??????????? ?????????? ???? ???? ?????? ???????? ????????? ? ??????????? ????? ??? ???, ?? ??????? ??? ?????? ???????????. ?????? ? ???? ??????? ? ???????? ????????? ????? ??????????? ?????????? ?????????. ? ?????? ?????? ?? ????? ???????????? ????????? ?? ????? ??????????. ????? ????????? ????????? ?????????? ???? ? ?????????? ??? ?? ??????????? ???????????, ????????? ? ??????????? ????????????? ?????-???? ???, ????? ???????????? ?????????????? ????????? (??????????? ??????). ???? ????????? ?? ?????????? ?? ????? ???? (? ?????????? ????). ??? ?????? ??? ??????????? ??????????? ???????????? ???????? ?, ? ?? ?? ?????, ??????????? ????? ????????? ?? ??? ????????? ?? ??????, ??????????? ? ???????? ?????????? ???????????? ????? ? ????????? ??????????????. ???????????? ??????????? ???????? ??, ??? ??? ???????? ??????????? ?????? ??? ?????????? ? ??????? ?????, ?????????? ???????? ????? ?????????? ? ??????? ????? . ??????? ? ?????????????? ? ?????????? ????????? ??? ????????????? ? ???????? ?????? ?????????. ???? ???????? ????? 2 ????????, ?? ?? ?????????? ????????????? ??????? ???????? ?? ??????? ????? ? ???????-??????????? ? "?????????" ?? ???? ??????? ???? ????????? ?????. ?????? ???????, ??????????? ? ??????? ?????, ???????? ??????????????? ? ???. ????????? ???????? ?????????? ? ??????? ????? ?????? ????.
2. ???????????? ?????.
2.1 ????????? ????? ? ???. ?????????, ???????????, ????????? ?????? ? ???????.
??????? ???????:
<?????????> ::= <?????????? ??????????> <???????? ??????????> <???????? ??????????> ::= Begin <?????? ????????????> End <?????????? ??????????> ::= Int <?????? ??????????> | Int <?????? ??????????> <?????????? ??????????> <?????? ??????????> ::= <?????>; | <?????> , <?????? ??????????> <?????? ????????????>::= <????????????> | <????????????> <?????? ????????????> <????????????> ::= <?????> := <?????????> ; <?????????> ::= <??.??.> <????????????> | <????????????> <????????????> :: = ( <?????????> ) | <???????> |
< ???????????? > <???.??.> <????????????> <??.??.> ::= "-" <???.??.> ::= "-" | "+" | "*" | "/" <???????> ::= <?????> | <Const> <?????> ::= <?????> <?????> | <?????>
<Const> ::= <?????> <Const> | <?????> ????? ?????? - ????? - ????? ??????, ???????????????? ??????????? ??????? ????? ????????? ????? ???????????.
?????????? ????????????, ??? ????????? ???????? Vt - ???????, ??????? ???????? ?????????? ??????????? ?? ??? ???????? ??????? ??????????? ???????????; Vn - ???????, ??????? ???????? ?????????? ??????????????? (?? ???????????), ???????????? ??? ?????????? ???????. P - ????? ??????, ?? ??????? ???????? ??????????; S - ????????? ???????. ???????????:
N= {
S=<?????????>
D=<?????????? ??????????>
F=<???????? ??????????>
P=<???????? ??????>
V=<?????? ??????????>
I=<?????????????>
G=<?????? ????????????>
A=<????????????>
E=<?????????>
H=<????????????>
O=<???????>
C=<Const>
N=<?????>
}
?????????:
???????? ????? ?????:
T={begin, end, integer, print}
???????????:
R={:=, .();}
???????:
A={a|...|z}
???????? ????????:
B={+|-|/|*}
??????? ????????:
U={-}
?????:
C={0|...|9}
???????:
1. S=DF
2. F=begin G end
3. G=A
4. G=AG
5. A=I:=E
6. I=LI
7. I=L
8. E=UH
9. E=H
10. H=(E)
11. H=O
12. H= HBH
13. O=I
14. O=C
15. C=NC
16. C=N
17. D=integer V
18. V=I;
19. V=I,V
20. P=print I
2.2 ?????? ???????????.
??? ???????, ?????????? ??????? ?? ???????? ?????????????? ??????, ? ??????? ????????? ?????? ?????? ????????? ?????, ??????? ???????????????, ??????????? ??????????, ?????????????? ?????????? ? ????????? ????.
???????? ?????? ? ???????????? ??????????? ? ?????????? ??? ?????????? ???? ?????? ???????????:
/// <summary>
/// ??????
/// </summary>
public static class Reader
/// <summary>
/// ??????????? ??????????
/// </summary>
public static class LexicalAnalyzer
/// <summary>
/// ???????
/// </summary>
public enum Lexems
{
None, Name, Number, Begin, End, Multiplication, Division,
Plus, Minus, Equal, NotEqual, Less, LessOrEqual, More, MoreOrEqual, Int,
Print, LeftBracket, RightBracket, Semi, Comma, EOF, Determine,
LongInt, Until, Do, EndUntil }
/// <summary>
/// ???????? ?????
/// </summary>
internal struct KeyWord
{
public string word;
public Lexems lex;
}
/// <summary>
/// ?????????
/// </summary>
public enum tCat
{
Const, Var, Type
}
/// <summary>
/// ???
/// </summary>
public enum tType
{
None, Int, LInt, Bool
}
/// <summary>
/// ?????????????
/// </summary>
public struct Identificator
{
public string name;
public tCat category;
public tType type;
}
/// <summary>
/// ??????? ????
/// </summary>
public static class NameTable
/// <summary>
/// ?????????????? ??????????
/// </summary>
public static class SyntaxAnalyzer
/// <summary>
/// ????????? ??????????? ??????
/// </summary>
public static class PostFix
/// <summary>
/// ????????? ????
/// </summary>
public static class CodeGenerator
?????? ??????? ??????????? ??? ??????? ?????? ????????? (???.1), ? ??????? ????:
???.1 ????????? ??????????
2.3 ??????????? ??????????.
??? ??????? ???? ??????? ?????? ???????????? ????????????, ?????????? ? ???? ??? ????????? ? ???????? ????? ????: ???????? ?????, ?????????, ?????????????? ? ?????.
public enum Lexems
{
None, Name, Number, Begin, End, Multiplication, Division,
Plus, Minus, Equal, NotEqual, Less, LessOrEqual, More, MoreOrEqual, Int,
Print, LeftBracket, RightBracket, Semi, Comma, EOF, Determine,
LongInt, Until, Do, EndUntil }
????? ??????? ????????? ?????????, ??????????? ?????? ??? ???????:
/// <summary>
/// ???????? ?????
/// </summary>
internal struct KeyWord
{
public string word;
public Lexems lex;
}
public static void Initialize()
{
keywords = new KeyWord[10];
keywordPointer = 0;
currentLexem = 0;
currentName = null;
AddKeyword("Begin", Lexems.Begin);
AddKeyword("End", Lexems.End);
AddKeyword("Integer", Lexems.Int);
AddKeyword("Long Integer", Lexems.LongInt);
AddKeyword("Print", Lexems.Print);
AddKeyword("UNTIL", Lexems.Until);
AddKeyword("DO", Lexems.Do);
AddKeyword("ENDUNTIL", Lexems.EndUntil);
}
? ?????? ?????? LexicalAnalyzer ????? ????????????? ?????? ???????????? ???????: ???????? ???????? ???? ????? ? ?? ?????????????, ?????? ??????? ??????? ? ??????????? ?? ?? ????.
/// <summary>
/// ??????????? ??????????
/// </summary>
public static class LexicalAnalyzer
{
private static KeyWord[] keywords;
private static int keywordPointer;
private static string currentName;
private static Lexems currentLexem;
/// <summary>
/// ????????????????
/// </summary>
public static void Initialize()
{
keywords = new KeyWord[10];
keywordPointer = 0;
currentLexem = 0;
currentName = null;
AddKeyword("Begin", Lexems.Begin);
AddKeyword("End", Lexems.End);
AddKeyword("Integer", Lexems.Int);
AddKeyword("Long Integer", Lexems.LongInt);
AddKeyword("Print", Lexems.Print);
AddKeyword("UNTIL", Lexems.Until);
AddKeyword("DO", Lexems.Do);
AddKeyword("ENDUNTIL", Lexems.EndUntil);
}
/// <summary>
/// ??????? ???
/// </summary>
public static string CurrentName
{
get
{
return currentName;
}
}
/// <summary>
/// ??????? ???????
/// </summary>
public static Lexems CurrentLexem
{
get
{
return currentLexem;
}
}
/// <summary>
/// ???????? ???????? ?????
/// </summary>
/// <param name="keyword">?????</param>
/// <param name="lex">???????</param>
/// <returns></returns>
public static bool AddKeyword(string keyword, Lexems lex)
{
if (keywordPointer < keywords.Length)
{
KeyWord kw = new KeyWord();
kw.word = keyword;
kw.lex = lex;
keywords[keywordPointer++] = kw;
return true;
}
return false;
}
/// <summary>
/// ???????? ???????? ?????
/// </summary>
/// <param name="keyword">?????</param>
/// <returns></returns>
public static Lexems GetKeyword(string keyword)
{
for (int i = keywordPointer - 1; i >= 0; i--)
{
if (keywords[i].word == keyword)
{
return keywords[i].lex;
}
}
return Lexems.Name;
}
/// <summary>
/// ????????? ????????? ???????
/// </summary>
public static void ParseNextLexem()
{
while (char.IsWhiteSpace(Reader.CurrentChar))
Reader.ReadNextChar();
if (char.IsLetter(Reader.CurrentChar))
ParseIdentificator();
else if (char.IsDigit(Reader.CurrentChar))
ParseNumber();
else if (Reader.CurrentChar == '\n')
{
Reader.ReadNextChar();
currentLexem = Lexems.None;
}
else if (Reader.CurrentChar == '<')
{
Reader.ReadNextChar();
if (Reader.CurrentChar == '=')
{
Reader.ReadNextChar();
currentName = "<=";
currentLexem = Lexems.LessOrEqual;
}
else
{
currentName = "<";
currentLexem = Lexems.Less;
}
}
else if (Reader.CurrentChar == '>')
{
Reader.ReadNextChar();
if (Reader.CurrentChar == '=')
{
Reader.ReadNextChar();
currentName = ">=";
currentLexem = Lexems.MoreOrEqual;
}
else
{
currentName = ">";
currentLexem = Lexems.More;
}
}
else if (Reader.CurrentChar == '=')
{
Reader.ReadNextChar();
if (Reader.CurrentChar == '=')
{
Reader.ReadNextChar();
currentName = "==";
currentLexem = Lexems.Equal;
}
else
{
currentName = "=";
currentLexem = Lexems.Determine;
}
}
else if (Reader.CurrentChar == '!')
{
Reader.ReadNextChar();
if (Reader.CurrentChar == '=')
{
Reader.ReadNextChar();
currentName = "!=";
currentLexem = Lexems.NotEqual;
}
else
Errors.Error("??????????? ??????");
}
else if (Reader.CurrentChar == '+')
{
Reader.ReadNextChar();
currentName = "+";
currentLexem = Lexems.Plus;
}
else if (Reader.CurrentChar == '*')
{
Reader.ReadNextChar();
currentName = "*";
currentLexem = Lexems.Multiplication;
}
else if (Reader.CurrentChar == '/')
{
Reader.ReadNextChar();
currentName = "/";
currentLexem = Lexems.Division;
}
else if (Reader.CurrentChar == ',')
{
Reader.ReadNextChar();
currentName = ",";
currentLexem = Lexems.Comma;
}
else if (Reader.CurrentChar == ';')
{
Reader.ReadNextChar();
currentName = ";";
currentLexem = Lexems.Semi;
}
else if (Reader.CurrentChar == '(')
{
Reader.ReadNextChar();
currentName = "(";
currentLexem = Lexems.LeftBracket;
}
else if (Reader.CurrentChar == ')')
{
Reader.ReadNextChar();
currentName = ")";
currentLexem = Lexems.RightBracket;
}
else if (Reader.CurrentChar == char.MinValue)
{
currentName = "EOF";
currentLexem = Lexems.EOF;
}
else
Errors.Error("???????????? ??????!");
}
/// <summary>
/// ????????? ?????????????
/// </summary>
private static void ParseIdentificator()
{
string identificator = string.Empty;
int count = 0;
do
{
identificator += Reader.CurrentChar;
if (identificator == "Long")
{
Reader.ReadNextChar();
identificator += Reader.CurrentChar;
count++;
}
Reader.ReadNextChar();
if (++count > 20)
Errors.Error("Chars overflow");
}
while (char.IsLetter(Reader.CurrentChar));
currentName = identificator;
currentLexem = GetKeyword(identificator);
}
/// <summary>
/// ????????? ?????
/// </summary>
private static void ParseNumber()
{
string number = string.Empty;
do
{
number += Reader.CurrentChar;
Reader.ReadNextChar();
}
while (char.IsDigit(Reader.CurrentChar));
try
{
Int32.Parse(number);
}
catch (OverflowException)
{
Errors.Error("?? ???????? ??????");
}
currentName = number;
currentLexem = Lexems.Number;
}
}
2.3 ?????????????? ??????????.
????? ???? ??? ??? ?????? ????? class Reader, ????? ?????? ????????? ??? ????????????.
??????? ????? ? ?????????, ??????? ????? ???????? ?? ??????? ????????? ? ???????? ???????? ??????:
public static class PostFix
{
private static List<string> res;
private static Stack<Lexems> stack;
/// <summary>
/// ????????????????
/// </summary>
public static void Initialize()
{
res = new List<string>();
stack = new Stack<Lexems>();
}
/// <summary>
/// ????????
/// </summary>
/// <param name="lexem"></param>
public static void Add(Lexems lexem)
{
switch (lexem)
{
case Lexems.Name:
{
string temp = LexicalAnalyzer.CurrentName;
res.Add(temp);
}
break;
case Lexems.Number:
{
string temp = LexicalAnalyzer.CurrentName;
res.Add(temp);
}
break;
case Lexems.LeftBracket:
{
stack.Push(Lexems.LeftBracket);
}
break;
case Lexems.RightBracket:
{
while (stack.Peek() != Lexems.LeftBracket)
{
res.Add(ConvertToString(stack.Pop()));
}
stack.Pop();
}
break;
case Lexems.Minus:
case Lexems.Plus:
{
Lexems lex = new Lexems();
if (stack.Count > 0)
lex = stack.Peek();
while (lex == Lexems.Plus
|| lex == Lexems.Minus
|| lex == Lexems.Multiplication
|| lex == Lexems.Division)
{
res.Add(ConvertToString(stack.Pop()));
if (stack.Count > 0)
lex = stack.Peek();
else
break;
}
stack.Push(lexem);
}
break;
case Lexems.Multiplication:
case Lexems.Division:
{
Lexems lex = new Lexems();
if (stack.Count > 0)
lex = stack.Peek();
while (lex == Lexems.Multiplication
|| lex == Lexems.Division)
{
res.Add(ConvertToString(stack.Pop()));
if (stack.Count > 0)
lex = stack.Peek();
else
break;
}
stack.Push(lexem);
}
break;
case Lexems.Determine:
{
stack.Push(lexem);
}
break;
}
}
????? ??????? ????????? ??????, ??? ??????????????? ??????? ????????? ?????? ????????? ????: ???? ?????????? ??????????, ???? ??????????????? ?????????? ????? ???? ????????, ???? ?????? ?? ??????, ???? ????? UNTIL.
public static void Compille()
{
PostFix.Initialize();
CodeGenerator.DeclairDataSegment();
LexicalAnalyzer.Initialize();
NameTable.Initialize();
ParseVariables();
CodeGenerator.DeclairVariables();
CodeGenerator.DeclairSegmentOfStackAndCode();
CheckLexem(Lexems.Semi);
if (LexicalAnalyzer.CurrentLexem == Lexems.Begin)
LexicalAnalyzer.ParseNextLexem();
ParseInstructionSequence();
CheckLexem(Lexems.End);
CodeGenerator.DeclairEndMainProc();
CodeGenerator.DeclairPrint();
CodeGenerator.DeclairCodeEnd();
if (Errors.ErrorsCount == 0)
Errors.Error("Compille success");
}
public static void ParseCycle()
{
CheckLexem(Lexems.Until);
CodeGenerator.AddLabel();
string upperLabel = CodeGenerator.GetCurrentLabel();
CodeGenerator.AddLabel();
string lowerLabel = CodeGenerator.GetCurrentLabel();
CodeGenerator.AddInstruction(upperLabel + ":");
ParseExpression();
CheckLexem(Lexems.Do);
ParseInstructionSequence();
CheckLexem(Lexems.EndUntil);
CodeGenerator.AddInstruction("jmp " + upperLabel);
CodeGenerator.AddInstruction(lowerLabel + ":");
}
static void ParseVariables()
{
CheckLexem(Lexems.None);
CheckLexem(Lexems.Int);
if (LexicalAnalyzer.CurrentLexem != Lexems.Name)
Errors.Error("?? ???????? ???????????????");
else
{
NameTable.AddIdentificator(LexicalAnalyzer.CurrentName, tCat.Var);
LexicalAnalyzer.ParseNextLexem();
}
while (LexicalAnalyzer.CurrentLexem == Lexems.Comma)
{
LexicalAnalyzer.ParseNextLexem();
if (LexicalAnalyzer.CurrentLexem != Lexems.Name)
Errors.Error("?? ???????? ???????????????");
else
{
NameTable.AddIdentificator(LexicalAnalyzer.CurrentName, tCat.Var);
LexicalAnalyzer.ParseNextLexem();
}
}
2.4 ????????? ????.
LIT const - ????????? ????????? ? ??????? ?????. LOAD n - ????????? ??????????, ??????????? ?? ?????? n ? ???????
?????. STO n - ?????? ???????? ?? ??????? ????? ?? ?????? n (????????????).
JMP k - ??????????? ??????? ? ???????, ????????????? ?? ?????? k. JEQ k - ??????? ? ???????, ????????????? ?? ?????? k ? ?????? ????????? ???? ??????? ????????? ?????. JLT k - ??????? ? ???????, ????????????? ?? ?????? k, ???? ????? ? ??????? ????? ?????? ?????????? ?? ??? ????? ?????. JLE k - ??????? ? ???????, ????????????? ?? ?????? k, ???? ????? ? ??????? ????? ?????? ??? ????? ?????????? ?? ??? ????? ?????. JGT k - ??????? ? ???????, ????????????? ?? ?????? k, ???? ????? ? ??????? ????? ?????? ?????????? ?? ??? ????? ?????. JGE k - ??????? ? ???????, ????????????? ?? ?????? k, ???? ????? ? ??????? ????? ?????? ??? ????? ?????????? ?? ??? ????? ?????. JNE k - ??????? ? ???????, ????????????? ?? ?????? k ? ?????? ??????????? ???? ??????? ????????? ?????. ADR - ?????????? ???????? ?????? ?????? ?????????? ? ??????? ?????. STAD - ?????????? ??????? ????? ?????????? ? ??????? ?????? ??????. ADD - ???????? ???? ??????? ????????? ?????, ????????? ?????????? ? ??????? ?????. MUL - ????????? ???? ??????? ????????? ?????, ????????? ?????????? ? ??????? ?????. SUB - ????????? ???????? ? ??????? ????? ?? ?????????? ?? ??? ???????? ?????, ????????? ?????????? ? ??????? ?????. DIV - ??????? ?? ??????? ? ??????? ????? ?????????? ?? ??? ???????? ?????, ????????? ?????????? ? ??????? ?????. AND - ?????????? "?" (?????????? ?????????) ???? ??????? ????????? ?????, ????????? ?????????? ? ??????? ?????. OR - ?????????? "???" (?????????? ????????) ???? ??????? ????????? ?????, ????????? ?????????? ? ??????? ?????. DIV - ??????? ?? ??????? ? ??????? ????? ?????????? ?? ??? XOR - ???????? ?? ?????? 2 ???? ??????? ????????? ?????, ????????? ?????????? ? ??????? ?????. NOT - ???????? ???????? ???????? ? ??????? ????? NOL - ??????????? ?????????? ???????? ???????? ? ??????? ?????. NOP - ?????? ???????? . ??? ??? ??? ?????????? ???????? ?????????????, ?????? ?????????? ???? ????? ?????????? ?? ??????? ??????????????? ???????????. ?? ???? ?????????? ??????????????? ??????? ????? ?????????????? ? ???????????? ???. ??? ???????? ????? ??????? ??? ? ??????? ?????.
/// <summary>
/// ????????? ????
/// </summary>
public static class CodeGenerator
{
private const int MAX_NUMBER_STRINGS = 1000;
private static string[] code = new string[MAX_NUMBER_STRINGS];
private static int codePointer = 0;
private static int countLabels = 0;
/// <summary>
/// ???????? ??????????
/// </summary>
/// <param name="instraction"></param>
public static void AddInstruction(string instraction)
{
code[codePointer++] = instraction;
}
/// <summary>
/// ???????? ?????
/// </summary>
public static void AddLabel()
{
countLabels++;
}
/// <summary>
/// ??????? ??????? ?????
/// </summary>
/// <returns></returns>
public static string GetCurrentLabel()
{
return "label" + countLabels.ToString();
}
/// <summary>
/// ??????? ??????? ??????
/// </summary>
public static void DeclairDataSegment()
{
AddInstruction("data segment para public \"data\"");
}
/// <summary>
/// ??????? ??????? ????? ? ????
/// </summary>
public static void DeclairSegmentOfStackAndCode()
{
AddInstruction("PRINT_BUF DB ' ' DUP(10)");
AddInstruction("BUFEND DB '$'");
AddInstruction("data ends");
AddInstruction("stk segment stack");
AddInstruction("db 256 dup (\"?\")");
AddInstruction("stk ends");
AddInstruction("code segment para public \"code\"");
AddInstruction("main proc");
AddInstruction("assume cs:code,ds:data,ss:stk");
AddInstruction("mov ax,data");
AddInstruction("mov ds,ax");
}
/// <summary>
/// ??????? ????? ??????? ?????????
/// </summary>
public static void DeclairEndMainProc()
{
AddInstruction("mov ax,4c00h");
AddInstruction("int 21h");
AddInstruction("main endp");
}
/// <summary>
/// ??????? ????? ???????? ????
/// </summary>
public static void DeclairCodeEnd()
{
AddInstruction("code ends");
AddInstruction("end main"); }
/// <summary>
/// ??????? ??????????
/// </summary>
public static void DeclairVariables()
{
LinkedListNode<Identificator> node = NameTable.GetIdentificators.First;
while (node != null)
{
if (SyntaxAnalyzer.Type == tType.Int)
AddInstruction(node.Value.name + " dw 1");
if (SyntaxAnalyzer.Type == tType.LInt)
AddInstruction(node.Value.name + " dl 1");
node = node.Next;
}
}
/// <summary>
/// ??????? ????????? ?????? ?? ??????
/// </summary>
public static void DeclairPrint()
{
AddInstruction("PRINT PROC NEAR");
AddInstruction("MOV CX, 10");
AddInstruction("MOV DI, BUFEND - PRINT_BUF");
AddInstruction("PRINT_LOOP:");
AddInstruction("MOV DX, 0");
AddInstruction("DIV CX");
AddInstruction("ADD DL, '0'");
AddInstruction("MOV [PRINT_BUF + DI - 1], DL");
AddInstruction("DEC DI");
AddInstruction("CMP AL, 0");
AddInstruction("JNE PRINT_LOOP");
AddInstruction("LEA DX, PRINT_BUF");
AddInstruction("ADD DX, DI");
AddInstruction("MOV AH, 09H");
AddInstruction("INT 21H");
AddInstruction("RET");
AddInstruction("PRINT ENDP");
}
??? ?????????? ?????????????? (? ?? ??????) ???????? ? ????? ????????? ???????????? ?????? ? ??????????? ???????. ???, ????? ????????? ???????? (???????? ????????? ??? ??????????) ? ???????, ???????????? ?????????? mov. ??????? ??? ?????????????? ???????? ????????? ? ??????? ????.
?????????????????? ??????????+add-sub*mul/div ????? ???????, ??? ??? ????????? a + b ????? ????????? ????????? ???????:
mov ax, a
mov bx b
add ax, bx
????????? ???? ?????????????????? ?????? ????? ???????? ? ???????? ax. ?? ? ???????? ?? ???????? ??????????? ?????????? ???-?? ????????? ????????????? ??????????. ??? ???? ????? ?? ????? ???????????? ???????????? ????: ?????????? push - ????????? ???????? ???????? ?? ???????? ?????, pop - ?????????? ?? ????? ? ???????. ????? ?????? ????????? ???????? ?? ????? ????????? ????????? ? ????, ????? ?? ???????? ???.
3. ???????????? ??????????.
??? ??? ???? ?????????? ?????? ??????????? ?????? ?? ????? ?? ???? ?????? ??????????, ??? ???????? ???????????? ??????, ?????????? ?????????????? ?????????. ????? ???????? ?? ?? ???? ????????? ???????? ????????? ????, ??? ???????????, ??? ? ???????? ?????????????, ??? ????, ????? ????????? ????? ??????????.
?????? 1:
Int a,s,r;
Begin
a=c+b;
t=s-r;
Print a;
End
????????? ?????????? ?????????:
?????? 2:
Int a,s,r;
Begin
a=s+r;
Do a=a+1;
Until a<8;
Print a;
End
?????????:
??????????.
? ?????? ???????? ?????? ???? ??????????? ?????????? ???????????, ? ????? Visual Studio 2008, ?? ????? C#. ???????????? ???? ???? ?????????? ????? ??????? ????????? ?????:
* ???????? ??????????? ?????????? ? ????????????? ?????? ?? ?????? ????? ??????????.
* ???????? ?????????????? ?????????? ? ????????????? ?????? ?? ?????? ????? ??????????.
* ???????? ????????? ???? ???????? ?????? ???????? ?????????, ??????????????? ????????? ?????, ? ????? ?????????????? ?????? SWITCH ? WHILE
* ????????? ???????????? ??????????, ??? ???????? ???????????? ??????.
1
Документ
Категория
Без категории
Просмотров
6
Размер файла
186 Кб
Теги
kursovaya_rabota
1/--страниц
Пожаловаться на содержимое документа