Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- namespace ConsoleApp3
- {
- public enum TokenType // Typ znalezionego tokenu
- {
- Operator,
- Bracket,
- Integer,
- Float,
- Identifier,
- Unknown
- }
- public class Token // wartość(np zmienna1) i tego tokenu(np Identifier)
- {
- public Token(string value, TokenType tokenType)
- {
- Value = value;
- TokenType = tokenType;
- }
- public string Value { get; }
- public TokenType TokenType { get; }
- }
- class Program2
- {
- private static Token GetToken(Match match, ref string line, TokenType tokenType)// wydobądź z tej lini token takiego typu
- {
- var tokenString = match.Groups[0].Value; //weź zerową grupe, czyli cały dopasowany tekst
- //line = line.Substring(tokenString.Length);
- var startIndex = line.IndexOf(tokenString);
- line = $"{line.Substring(0, startIndex)}{line.Substring(startIndex + tokenString.Length)}";
- return new Token(tokenString, tokenType);
- }
- private static Token CheckRegex(ref string line, Regex regex, TokenType tokenType)
- {
- var match = regex.Match(line);
- return match.Success ?
- GetToken(match, ref line, tokenType)
- : null;
- }
- /// <summary>
- /// Usuwanie spacji
- /// </summary>
- private static void RemoveSpace(
- ref string line
- )
- {
- var lineWithoutSpace = new List<char>();
- foreach (var oneChar in line)
- {
- if (oneChar != ' ')
- lineWithoutSpace.Add(oneChar);
- }
- line = string.Join("", lineWithoutSpace);
- }
- static void Main(string[] args)
- {
- //zestaw danych
- var data = new List<String>()
- {
- "x12+7.5",
- "123x+2.5",
- "3-7",
- "10 / 3",
- "x * y - z",
- "zmienna1 + zmienna2",
- "w1a*w2b",
- "10.3-alfa",
- "0.685739*5",
- "(x+y)*z",
- "(a * a) + b",
- "(0.1 - beta) + 8",
- };
- Regex ROperator = new Regex(@"([\+\-\\*\/]{1})");
- Regex RBracket = new Regex(@"([\(\)]{1})");
- Regex RInteger = new Regex(@"([\d]+)");
- Regex RFloat = new Regex(@"[0-9]+\.[0-9]+");
- Regex RIdentifier = new Regex(@"(([A-Za-z]+[0-9]+)|[A-Za-z]+)");
- foreach (var line in data)
- {
- var tokens = new List<Token>();
- var lineCopy = string.Copy(line);
- RemoveSpace(ref lineCopy);
- Console.WriteLine(lineCopy);
- while (lineCopy.Length > 0)
- {
- var token = CheckRegex(ref lineCopy, RIdentifier, TokenType.Identifier);
- if (token != null)//Jeżeli wybrany typ tokena został wykryty dodaj go do listy
- {
- tokens.Add(token);
- continue;
- }
- else
- token = CheckRegex(ref lineCopy, RFloat, TokenType.Float);
- if (token != null)
- {
- tokens.Add(token);
- continue;
- }
- else
- token = CheckRegex(ref lineCopy, RInteger, TokenType.Integer);
- if (token != null)
- {
- tokens.Add(token);
- continue;
- }
- else
- token = CheckRegex(ref lineCopy, RBracket, TokenType.Bracket);
- if (token != null)
- {
- tokens.Add(token);
- continue;
- }
- else
- token = CheckRegex(ref lineCopy, ROperator, TokenType.Operator);
- if (token != null)
- {
- tokens.Add(token);
- continue;
- }
- else
- {
- token = new Token($"ERROR {lineCopy}", TokenType.Unknown);
- tokens.Add(token);
- break;
- }
- }
- foreach (var token in tokens)
- {
- Console.WriteLine($"{token.Value} - {token.TokenType.ToString()}");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement