-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
187 lines (145 loc) · 4.38 KB
/
Copy pathProgram.cs
File metadata and controls
187 lines (145 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
namespace Lexer
{
class Program
{
static void Main(string[] args)
{
Lexer lexer = new Lexer("123+ 45*(2 -6)");
var alltokens = lexer.GetAllTokens();
var tokens = lexer.GetAllValidTokens();
}
}
enum TokenType
{
Number,
LeftParenthesis,
RightParenthesis,
WhiteSpace,
Plus,
Minus,
Star,
Splash,
Hat,
EndOfLine,
Unknown
}
class Token
{
public TokenType Type { get; }
public int Position { get; }
public int Length { get; }
public string Text { get; }
public Token(TokenType tokenType, int position, int length, string text)
{
Type = tokenType;
Position = position;
Length = length;
Text = text;
}
public override string ToString()
{
return $"Type: {Type}. Text = {Text}";
}
}
class Lexer
{
private readonly string _text;
private int _pos;
private readonly char _DECIMAL_SEPARATOR = '.';
public char Current
{
get
{
if (_pos >= _text.Length)
return '\0';
return _text[_pos];
}
}
public Lexer(string text)
{
_text = text;
_pos = 0;
}
public List<Token> GetAllTokens()
{
ResetPosition();
List<Token> tokens = new List<Token>();
Token token;
do
{
token = GetNextToken();
tokens.Add(token);
} while (token.Type != TokenType.EndOfLine);
return tokens;
}
public List<Token> GetAllValidTokens()
{
ResetPosition();
List<Token> tokens = new List<Token>();
Token token;
do
{
token = GetNextToken();
if((token.Type != TokenType.WhiteSpace) && (token.Type != TokenType.Unknown) && (token.Type != TokenType.EndOfLine))
tokens.Add(token);
} while (token.Type != TokenType.EndOfLine);
return tokens;
}
private void ResetPosition()
{
_pos = 0;
}
public Token GetNextToken()
{
char c = Current;
if (c == '\0')
return new Token(TokenType.EndOfLine, _pos++, 1, c.ToString());
// Open parenthesis
if (c == '(')
return new Token(TokenType.LeftParenthesis, _pos++, 1, c.ToString());
// Close parenthesis
if (c == ')')
return new Token(TokenType.RightParenthesis, _pos++, 1, c.ToString());
// Operator
if (c == '+')
return new Token(TokenType.Plus, _pos++, 1, c.ToString());
else if (c == '-')
return new Token(TokenType.Minus, _pos++, 1, c.ToString());
else if (c == '*')
return new Token(TokenType.Star, _pos++, 1, c.ToString());
else if (c == '/')
return new Token(TokenType.Splash, _pos++, 1, c.ToString());
else if (c == '^')
return new Token(TokenType.Hat, _pos++, 1, c.ToString());
// Number (integer)
if (Char.IsDigit(c))
{
int start = _pos;
while (Char.IsDigit(Current))
_pos++;
int length = _pos - start;
return new Token(TokenType.Number, start, length, _text.Substring(start, length));
}
// Space(s)
if (Char.IsWhiteSpace(c))
{
int start = _pos;
while (Char.IsWhiteSpace(Current))
_pos++;
int length = _pos - start;
return new Token(TokenType.WhiteSpace, start, length, _text.Substring(start, length));
}
return new Token(TokenType.Unknown, _pos++, 1, c.ToString());
}
}
class Parser
{
public Parser(string text)
{
var lexer = new Lexer(text);
var tokens = lexer.GetAllValidTokens();
}
}
}