- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
50 lines (45 loc) · 1.98 KB
/
Copy pathProgram.cs
File metadata and controls
50 lines (45 loc) · 1.98 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
namespaceExpressionParser
{
internalclassProgram
{
staticvoidMain(string[]args)
{
Console.WriteLine("""
Sample implementation of operator-precedence expression parser. The following lexemes and operations are supported:
- Numbers One or more '0'-'9' characters.Limited to 32 bit integer values.
- Variables One or more 'a'-'z' and '_' characters.
- Grouping ((x + y) * z)
- Prefix operations -x, --x, ++x
- Postfix operations x++, x--
- Multiplicative x * y, x / y
- Additive x + y, x - y
- Relational x > y, x < y, x >= y, x <= y
- Equality x == y, x != y
- Conditional Ternary ?: operator. Expression that evaluates to a positive integer will be considered a true condition.
- Assignment x = y, x += y, x -= y, x *= y, x /= y
""");
Console.WriteLine("Please, enter the expression:");
string?expression=Console.ReadLine();
varvariables=newDictionary<string,int>();
while(!string.IsNullOrEmpty(expression))
{
Process(expression,variables);
expression=Console.ReadLine();
}
}
staticvoidProcess(stringexpression,Dictionary<string,int>variables)
{
try
{
varreducer=newExpressionEvaluator(variables);
varparser=newParser(reducer);
parser.Parse(expression);
Console.WriteLine($"Result: {reducer.GetResult()}");
}
catch(Exceptionex)
{
Console.WriteLine("Failed to evaluated with the following error: "+ex.Message);
}
}
}
}