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

NumberText                    12345dupatestdupa223
12345
NumberText                    dupatestdupa223

SingleText                    testdupa223
testdupa223
Wykryto teskt
SingleText                    dupa223
223

NumberText                    223
223
NumberText                    



s