fork(1) download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. enum lex_t {
  7. INVALID,
  8. THE,
  9. NOUN,
  10. VERB,
  11. CONJ,
  12. DOT,
  13. };
  14.  
  15. string word;
  16. lex_t token;
  17.  
  18. lex_t word_token( const string& w )
  19. {
  20. if( w == "the" ) {
  21. return THE;
  22. }
  23.  
  24. if( w == "birds" ||
  25. w == "fish" ||
  26. w == "C++"
  27. ) {
  28. return NOUN;
  29. }
  30.  
  31. if( w == "fly" ||
  32. w == "rules" ||
  33. w == "swim"
  34. ) {
  35. return VERB;
  36. }
  37.  
  38. if( w == "and" ||
  39. w == "or" ||
  40. w == "but"
  41. ) {
  42. return CONJ;
  43. }
  44.  
  45. if( w == "." ) {
  46. return DOT;
  47. }
  48.  
  49. return INVALID;
  50. }
  51.  
  52. void next_word()
  53. {
  54. cin >> word;
  55. token = word_token( word );
  56. }
  57.  
  58. bool maybe( lex_t t )
  59. {
  60. if( token == t ) {
  61. next_word();
  62. }
  63. return true;
  64. }
  65.  
  66. bool match( lex_t t )
  67. {
  68. if( token == t ) {
  69. next_word();
  70. return true;
  71. }
  72. return false;
  73. }
  74.  
  75. void skip( lex_t t )
  76. {
  77. while( !match( t ) ) {
  78. next_word();
  79. }
  80. }
  81.  
  82. bool sentence()
  83. {
  84. if( maybe( THE ) && match( NOUN ) && match( VERB ) ) {
  85. if( match( CONJ ) ) {
  86. return sentence();
  87. }
  88. if( match( DOT ) ) {
  89. return true;
  90. }
  91. return false;
  92. }
  93. skip( DOT );
  94. return false;
  95. }
  96.  
  97. int main() {
  98. next_word();
  99. while( cin ) {
  100. cout << boolalpha << sentence() << endl;
  101. }
  102. }
Success #stdin #stdout 0s 15240KB
stdin
birds fly but fish swim .
the fish fly .
the the C++ rules .
birds fly
stdout
true
true
false
false