Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace ConsoleApp3
  6. {
  7.     public enum TokenType // Typ znalezionego tokenu
  8.     {
  9.         Operator,
  10.         Bracket,
  11.         Integer,
  12.         Float,
  13.         Identifier,
  14.         Unknown
  15.     }
  16.  
  17.     public class Token // wartość(np zmienna1) i tego tokenu(np Identifier)
  18.     {
  19.         public Token(string value, TokenType tokenType)
  20.         {
  21.             Value = value;
  22.             TokenType = tokenType;
  23.         }
  24.         public string Value { get; }
  25.         public TokenType TokenType { get; }
  26.     }
  27.  
  28.     class Program2
  29.     {
  30.         private static Token GetToken(Match match, ref string line, TokenType tokenType)// wydobądź z tej lini token takiego typu
  31.         {
  32.             var tokenString = match.Groups[0].Value; //weź zerową grupe, czyli cały dopasowany tekst
  33.             //line = line.Substring(tokenString.Length);
  34.             var startIndex = line.IndexOf(tokenString);
  35.             line = $"{line.Substring(0, startIndex)}{line.Substring(startIndex + tokenString.Length)}";
  36.             return new Token(tokenString, tokenType);
  37.         }
  38.  
  39.         private static Token CheckRegex(ref string line, Regex regex, TokenType tokenType)
  40.         {
  41.             var match = regex.Match(line);
  42.             return match.Success ?
  43.                 GetToken(match, ref line, tokenType)
  44.                 : null;
  45.         }
  46.  
  47.         /// <summary>
  48.         /// Usuwanie spacji
  49.         /// </summary>
  50.         private static void RemoveSpace(
  51.             ref string line
  52.             )
  53.         {
  54.             var lineWithoutSpace = new List<char>();
  55.             foreach (var oneChar in line)
  56.             {
  57.                 if (oneChar != ' ')
  58.                     lineWithoutSpace.Add(oneChar);
  59.             }
  60.  
  61.             line = string.Join("", lineWithoutSpace);
  62.         }
  63.         static void Main(string[] args)
  64.         {
  65.             //zestaw danych
  66.             var data = new List<String>()
  67.             {
  68.                 "x12+7.5",
  69.                 "123x+2.5",
  70.                 "3-7",
  71.                 "10 / 3",
  72.                 "x * y - z",
  73.                 "zmienna1 + zmienna2",
  74.                 "w1a*w2b",
  75.                 "10.3-alfa",
  76.                 "0.685739*5",
  77.                 "(x+y)*z",
  78.                 "(a * a) + b",
  79.                 "(0.1 - beta) + 8",
  80.             };
  81.  
  82.             Regex ROperator = new Regex(@"([\+\-\\*\/]{1})");
  83.             Regex RBracket = new Regex(@"([\(\)]{1})");
  84.             Regex RInteger = new Regex(@"([\d]+)");
  85.             Regex RFloat = new Regex(@"[0-9]+\.[0-9]+");
  86.             Regex RIdentifier = new Regex(@"(([A-Za-z]+[0-9]+)|[A-Za-z]+)");
  87.  
  88.  
  89.             foreach (var line in data)
  90.             {
  91.                 var tokens = new List<Token>();
  92.                 var lineCopy = string.Copy(line);
  93.                 RemoveSpace(ref lineCopy);
  94.                 Console.WriteLine(lineCopy);
  95.                 while (lineCopy.Length > 0)
  96.                 {
  97.                     var token = CheckRegex(ref lineCopy, RIdentifier, TokenType.Identifier);
  98.  
  99.                     if (token != null)//Jeżeli wybrany typ tokena został wykryty dodaj go do listy
  100.                     {
  101.                         tokens.Add(token);
  102.                         continue;
  103.                     }
  104.                     else
  105.                         token = CheckRegex(ref lineCopy, RFloat, TokenType.Float);
  106.  
  107.                     if (token != null)
  108.                     {
  109.                         tokens.Add(token);
  110.                         continue;
  111.                     }
  112.                     else
  113.                         token = CheckRegex(ref lineCopy, RInteger, TokenType.Integer);
  114.  
  115.                     if (token != null)
  116.                     {
  117.                         tokens.Add(token);
  118.                         continue;
  119.                     }
  120.                     else
  121.                         token = CheckRegex(ref lineCopy, RBracket, TokenType.Bracket);
  122.  
  123.                     if (token != null)
  124.                     {
  125.                         tokens.Add(token);
  126.                         continue;
  127.                     }
  128.                     else
  129.                         token = CheckRegex(ref lineCopy, ROperator, TokenType.Operator);
  130.  
  131.                     if (token != null)
  132.                     {
  133.                         tokens.Add(token);
  134.                         continue;
  135.                     }
  136.                     else
  137.                     {
  138.                         token = new Token($"ERROR {lineCopy}", TokenType.Unknown);
  139.                         tokens.Add(token);
  140.                         break;
  141.                     }
  142.                 }
  143.  
  144.                 foreach (var token in tokens)
  145.                 {
  146.                     Console.WriteLine($"{token.Value} - {token.TokenType.ToString()}");
  147.                 }
  148.             }
  149.         }
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement