-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69013b8
commit 1bef877
Showing
8 changed files
with
543 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Compiler; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Lab7; | ||
|
||
public enum TokenType | ||
{ | ||
While, | ||
Or, | ||
And, | ||
Rel, | ||
Do, | ||
Var, | ||
Const, | ||
Assignment, | ||
End, | ||
Semicolon, | ||
ArithOp, | ||
Error | ||
} | ||
|
||
public class Token | ||
{ | ||
public TokenType Type { get; set; } | ||
public string Value { get; set; } | ||
public int StartIndex { get; set; } | ||
public int EndIndex { get; set; } | ||
|
||
public Token(TokenType type, string value, int startIndex, int endIndex) | ||
{ | ||
Type = type; | ||
Value = value; | ||
StartIndex = startIndex; | ||
EndIndex = endIndex; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using Compiler; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Lab7; | ||
|
||
public class WhileLexer | ||
{ | ||
private List<Token> Tokens; | ||
|
||
public List<Token> Analyze(string input) | ||
{ | ||
int i; | ||
string value; | ||
|
||
Tokens.Clear(); | ||
|
||
for (i = 0; i < input.Length; i++) | ||
{ | ||
value = string.Empty + input[i]; | ||
|
||
if (char.IsLetter(input[i])) | ||
{ | ||
int startIndex = i; | ||
|
||
while ((i + 1) < input.Length && char.IsLetter(input[i + 1])) | ||
{ | ||
i++; | ||
value += input[i]; | ||
} | ||
|
||
switch (value) | ||
{ | ||
case "while": | ||
Tokens.Add(new Token(TokenType.While, value, startIndex + 1, i + 1)); | ||
break; | ||
case "do": | ||
Tokens.Add(new Token(TokenType.Do, value, startIndex + 1, i + 1)); | ||
break; | ||
case "end": | ||
Tokens.Add(new Token(TokenType.End, value, startIndex + 1, i + 1)); | ||
break; | ||
case "and": | ||
Tokens.Add(new Token(TokenType.And, value, startIndex + 1, i + 1)); | ||
break; | ||
case "or": | ||
Tokens.Add(new Token(TokenType.Or, value, startIndex + 1, i + 1)); | ||
break; | ||
default: | ||
Tokens.Add(new Token(TokenType.Var, value, startIndex + 1, i + 1)); | ||
break; | ||
} | ||
} | ||
else | ||
{ | ||
if (char.IsDigit(input[i])) | ||
{ | ||
int startIndex = i; | ||
|
||
while ((i + 1) < input.Length && char.IsDigit(input[i + 1])) | ||
{ | ||
i++; | ||
value += input[i]; | ||
} | ||
|
||
Tokens.Add(new Token(TokenType.Const, value, startIndex + 1, i + 1)); | ||
} | ||
else | ||
{ | ||
switch (input[i]) | ||
{ | ||
case '\t': | ||
case ' ': | ||
break; | ||
case (char)13: | ||
if ((i + 1) < input.Length && input[i + 1] == (char)10) | ||
{ | ||
i++; | ||
value = "\\n"; | ||
} | ||
break; | ||
case '>': | ||
case '<': | ||
case '!': | ||
if ((i + 1) < input.Length && input[i + 1] == '=') | ||
{ | ||
i++; | ||
value += input[i]; | ||
Tokens.Add(new Token(TokenType.Rel, value, i, i + 1)); | ||
} | ||
else | ||
{ | ||
if (input[i] == '!') | ||
{ | ||
Tokens.Add(new Token(TokenType.Error, value, i + 1, i + 1)); | ||
} | ||
else | ||
{ | ||
Tokens.Add(new Token(TokenType.Rel, value, i + 1, i + 1)); | ||
} | ||
} | ||
break; | ||
case '=': | ||
if ((i + 1) < input.Length && input[i + 1] == '=') | ||
{ | ||
i++; | ||
value += input[i]; | ||
Tokens.Add(new Token(TokenType.Rel, value, i, i + 1)); | ||
} | ||
else | ||
{ | ||
Tokens.Add(new Token(TokenType.Assignment, value, i, i + 1)); | ||
} | ||
break; | ||
case '+': | ||
case '-': | ||
Tokens.Add(new Token(TokenType.ArithOp, value, i + 1, i + 1)); | ||
break; | ||
case ';': | ||
Tokens.Add(new Token(TokenType.Semicolon, value, i + 1, i + 1)); | ||
break; | ||
default: | ||
Tokens.Add(new Token(TokenType.Error, value, i + 1, i + 1)); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return Tokens; | ||
} | ||
|
||
public WhileLexer() | ||
{ | ||
Tokens = new List<Token>(); | ||
} | ||
} |
Oops, something went wrong.