fork(1) download
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class Grammar;
  9.  
  10. class CristallParser{
  11.  
  12. std::vector <Grammar *> Operations;
  13. std::string &T;
  14. public:
  15. CristallParser(std::string &Text);
  16. void parse();
  17. };
  18.  
  19. class Grammar{
  20. public:
  21. std::string Label;
  22. virtual bool isRule(std::string& Text) = 0;
  23. virtual void parse(std::string& Text) = 0;
  24. };
  25.  
  26.  
  27.  
  28. class NumbersGrammar: public Grammar{
  29. int Limit = 0;
  30. public:
  31. NumbersGrammar(std::string Label, int Limit);
  32. bool isRule(std::string &Text);
  33. void parse(std::string &Text);
  34. };
  35.  
  36. class SingleGrammar : public Grammar
  37. {
  38. std::string Text;
  39. public:
  40. SingleGrammar(std::string Label, std::string Char);
  41. bool isRule(std::string &Text);
  42. void parse(std::string &Text);
  43. };
  44.  
  45. CristallParser::CristallParser(std::string &Text): T(Text){
  46. Operations.push_back(new NumbersGrammar("Number",0));
  47. Operations.push_back(new SingleGrammar("Text","test"));
  48. T = Text;
  49. }
  50.  
  51. void CristallParser::parse(){
  52. while (T.size()>0){
  53. for (auto El: Operations){
  54. if(El->isRule(T))
  55. El->parse(T);
  56. }
  57. T.erase(0,1);
  58. }
  59. }
  60.  
  61. NumbersGrammar::NumbersGrammar(std::string Label, int Limit = 0)
  62. {
  63. this->Label = Label;
  64. if (Limit != 0)
  65. this->Limit = 0;
  66. }
  67.  
  68. bool NumbersGrammar::isRule(std::string& Text){
  69.  
  70. return (isdigit(Text[0]) == true || Text[0] == '-' );
  71. }
  72.  
  73. void NumbersGrammar::parse(std::string& Text){
  74. std::cout<<Text<<std::endl;
  75. int Element = 0;
  76. for (char Char:Text){
  77. if (isdigit(Char) || Char == '-')
  78. Element++;
  79. else
  80. break;
  81. }
  82. if (Limit == Element || (Limit == 0)){
  83. std::cout<<Text.substr(0,Element);
  84. Text = Text.erase(0,Element);
  85. }
  86. }
  87.  
  88. SingleGrammar::SingleGrammar(std::string Label, std::string Char)
  89. {
  90. this->Label = Label;
  91. Text = Char;
  92. }
  93.  
  94. bool SingleGrammar::isRule(std::string &Text)
  95. {
  96. return (Text.substr(0, this-> Text.length()) == Text );
  97. }
  98.  
  99. void SingleGrammar::parse(std::string &Text)
  100. {
  101. std::cout<<Text<<std::endl;
  102. std::cout<<"Wykryto teskt";
  103. Text.erase(0, this->Text.length());
  104. }
  105.  
  106. #define DBG(x) { cout << left << setw(30) << #x << (x) << endl; }
  107.  
  108. int main()
  109. {
  110. string s = "12345dupatestdupa223";
  111.  
  112. DBG(s);
  113.  
  114. CristallParser c(s);
  115.  
  116. c.parse();
  117.  
  118. cout << endl << endl;
  119.  
  120. DBG(s);
  121.  
  122. }
  123.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
s                             12345dupatestdupa223
12345dupatestdupa223
12345a223
Wykryto teskt

s