C
C#2y ago
bialasik__

❔ Lexer and parser, cannot get expressions to formulate as they should, have a look at the output...

static void Lexer(string input)
{
char[] delimiters = "!@#$%^&()=_{}|\\][:\"';?><,.` ".ToCharArray();
string[] words;
Dictionary<string, int> tokens = new Dictionary<string, int>();

words = input.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

Console.WriteLine(">>> Tokens:");
foreach (string word in words)
{
if (tokens.ContainsKey(word))
{
tokens[word] += 1;
}
else
{
tokens.Add(word, 1);
}
}
foreach (KeyValuePair<string, int> token in tokens)
{
Console.WriteLine($">>> {token.Key} : {token.Value}");
}
Parser(tokens);
}

static void Parser(Dictionary<string, int> tokens)
{
string expression = String.Empty;
string[] operators =
{
"+", "-", "*", "/"
};
string[] nums =
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
};

foreach (KeyValuePair<string, int> token in tokens)
{
if (operators.Contains(token.Key))
{
Console.WriteLine($">>> Operator:\t{token.Key}");
}
else if (nums.Contains(token.Key))
{
Console.WriteLine($">>> Number:\t{token.Key}");
}
else
{
Console.WriteLine($">>> Identifier:\t{token.Key}");
}
expression += token.Key;
}
Console.WriteLine($">>> Expression: {expression}");
}
}
}
static void Lexer(string input)
{
char[] delimiters = "!@#$%^&()=_{}|\\][:\"';?><,.` ".ToCharArray();
string[] words;
Dictionary<string, int> tokens = new Dictionary<string, int>();

words = input.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

Console.WriteLine(">>> Tokens:");
foreach (string word in words)
{
if (tokens.ContainsKey(word))
{
tokens[word] += 1;
}
else
{
tokens.Add(word, 1);
}
}
foreach (KeyValuePair<string, int> token in tokens)
{
Console.WriteLine($">>> {token.Key} : {token.Value}");
}
Parser(tokens);
}

static void Parser(Dictionary<string, int> tokens)
{
string expression = String.Empty;
string[] operators =
{
"+", "-", "*", "/"
};
string[] nums =
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
};

foreach (KeyValuePair<string, int> token in tokens)
{
if (operators.Contains(token.Key))
{
Console.WriteLine($">>> Operator:\t{token.Key}");
}
else if (nums.Contains(token.Key))
{
Console.WriteLine($">>> Number:\t{token.Key}");
}
else
{
Console.WriteLine($">>> Identifier:\t{token.Key}");
}
expression += token.Key;
}
Console.WriteLine($">>> Expression: {expression}");
}
}
}
The lexer works fine, however when i want to make an expression out of the tokens it just wont work out...
5 Replies
phaseshift
phaseshift2y ago
Yes... Your dictionary only has two KVP in it. You're not going to be able to rebuild an expression if you put the tokens into a normal dictionary Have you heard of an (abstract) syntax tree?
bialasik__
bialasik__OP2y ago
Yes Should I use a Stack<>
Anton
Anton2y ago
make a function that outputs tokens lazily with yield return, that would be the most flexible way
bialasik__
bialasik__OP2y ago
I'll see
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server