fork download
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4. using System.Collections.Generic;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. string StringRegex = "\"(?:[^\"\\\\]|\\\\.)*\"";
  11. string IntegerRegex = @"[0-9]+";
  12. string CommentRegex = @"//.*|/\*[\s\S]*\*/";
  13. string KeywordRegex = @"\b(?:astart|ainput|atake|aloop|batcommand|batshow|batprint|batmult|batadd|batsub|batdiv|batif|batelse|batgo|batend|till|and)\b";
  14. string DataTypeRegex = @"\b(?:int|string)\b";
  15. string IdentifierRegex = @"\b[a-zA-Z]\b";
  16. string ParenthesisRegex = @"\(|\)";
  17. string BracesRegex = @"\{|\}";
  18. string ArrayBracketRegex = @"\[|\]";
  19. string PuncuationRegex = @"\;|\:|\,|\.";
  20. string RelationalExpressionRegex = @"\>|\<|\==";
  21. string ArthimeticOperatorRegex = @"\+|\-|\*|\/";
  22. string WhitespaceRegex = @" ";
  23. Dictionary<string, string> Regexes = new Dictionary<string, string>()
  24. {
  25. {"String", StringRegex},
  26. {"Integer", IntegerRegex },
  27. {"Comment", CommentRegex},
  28. {"Keyword", KeywordRegex},
  29. {"Datatype", DataTypeRegex },
  30. {"Identifier", IdentifierRegex },
  31. {"Parenthesis", ParenthesisRegex },
  32. {"Brace", BracesRegex },
  33. {"Square Bracket", ArrayBracketRegex },
  34. {"Puncuation Mark", PuncuationRegex },
  35. {"Relational Expression", RelationalExpressionRegex },
  36. {"Arithmetic Operator", ArthimeticOperatorRegex },
  37. {"Whitespace", WhitespaceRegex }
  38. };
  39.  
  40. string input = "asdasdas"; // DEMO
  41. // input = Convert.ToString(Console.ReadLine());
  42.  
  43. var matches = Regexes.SelectMany(a => Regex.Matches(input, a.Value)
  44. .Cast<Match>()
  45. .Select(b =>
  46. new
  47. {
  48. Value = b.Value + "\n",
  49. Index = b.Index,
  50. Token= a.Key
  51. }))
  52. .OrderBy(a => a.Index).ToList();
  53. if (matches.Count == 0)
  54. Console.WriteLine("Not in grammar");
  55. else
  56. {
  57. for (int i = 0; i < matches.Count; i++)
  58. {
  59. if (i + 1 < matches.Count)
  60. {
  61. int firstEndPos = (matches[i].Index + matches[i].Value.Length);
  62. if (firstEndPos > matches[(i + 1)].Index)
  63. {
  64. matches.RemoveAt(i + 1);
  65. i--;
  66. }
  67. }
  68. }
  69. foreach (var match in matches)
  70. {
  71. Console.WriteLine(match);
  72. }
  73. }
  74. }
  75. }
Success #stdin #stdout 0.14s 24728KB
stdin
Standard input is empty
stdout
Not in grammar