home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / compcomp / yacc / maze.y < prev    next >
Encoding:
Text File  |  1979-12-31  |  5.1 KB  |  147 lines

  1. /************************************************************************/
  2. /*                cpr.y                    */
  3. /*    Copyright (c) 1985  CurrenT Software.  All rights reserved.    */
  4. /* yacc test file.                            */
  5. /************************************************************************/
  6.  
  7. /************************************************************************/
  8. /*                              Contents                                */ 
  9. /*                                                                      */ 
  10. /*                                                                      */ 
  11. /*    token declarations for grammar                    */
  12. /*    rules for grammar                        */
  13. /*    skeletal runtimes.                        */
  14. /*                                    */
  15. /************************************************************************/ 
  16.          
  17. /************************************************************************/ 
  18. /*                              History                                 */  
  19. /*                                                                      */  
  20. /*                                              Last mod: 85Nov19       */ 
  21. /*                                                                      */ 
  22. /* 85Nov19 CrT  Created.                                                */ 
  23. /************************************************************************/ 
  24.  
  25. /************************************************************************/ 
  26. /*                Comments                */
  27. /*                                                                      */   
  28. /* This file implements a trivial maze game, providing a compact,    */
  29. /* tinkerable example of yacc as an interactive command parser.  One    */
  30. /* peculiar aspect of the program is that yacc wants to have both a    */
  31. /* "current" and "next" input token at all times, but we don't want    */
  32. /* to have the response messages always a move behind, so we pad an    */
  33. /* 'X' invisibly onto every character entered.                */
  34. /*                                    */
  35. /************************************************************************/
  36.  
  37. /************************************************************************/
  38. /*                Map                    */
  39. /*                                                                      */   
  40. /*                                    */
  41. /*                                    */
  42. /*                                    */
  43. /*            stream    pond   woods                */
  44. /*                                    */
  45. /*            swamp    glade  hollow                */
  46. /*                                    */
  47. /*            thicket knoll  exit                */
  48. /*                                    */
  49. /*                                    */
  50. /*                                    */
  51. /************************************************************************/
  52.  
  53.  
  54. %{ 
  55. int     yylval; 
  56. %} 
  57.  
  58. %start    exit
  59. %token    FIRST
  60.  
  61. %%
  62. exit    :    hollow    'S'    {printf("\nYou WIN!                      \n");}
  63.     |    knoll    'E'    {printf("\nYou WIN!                      \n");}
  64.     ;
  65. hollow    :    woods    'S'    {printf("\nSouth to a hollow.            \n");}
  66.     |    glade    'E'    {printf("\nEast to a hollow.             \n");}
  67.     |    hollow    'X'
  68.     |    hollow error
  69.              { yyerrok; yyclearin; printf("\nYou are at hollow.  \n");}
  70.     ;
  71. woods    :    hollow    'N'    {printf("\nNorth to a wood.              \n");}
  72.     |    pond    'E'    {printf("\nEast to a wood.               \n");}
  73.     |    woods    'X'
  74.     |    woods error
  75.              { yyerrok; yyclearin; printf("\nYou are at wood.    \n");}
  76.     ;
  77. knoll    :    glade    'S'    {printf("\nSouth to a knoll.             \n");}
  78.     |    thicket 'E'    {printf("\nEast to a knoll.              \n");}
  79.     |    knoll    'X'
  80.     |    knoll error
  81.              { yyerrok; yyclearin; printf("\nYou are at knoll.   \n");}
  82.     ;
  83. glade    :    FIRST        {printf("\nType N, S, E or W.            \n");}
  84.     |    knoll    'N'    {printf("\nNorth to a glade.             \n");}
  85.     |    hollow    'W'    {printf("\nWest to a glade.              \n");}
  86.     |    pond    'S'    {printf("\nSouth to a glade.             \n");}
  87.     |    swamp    'E'    {printf("\nEast to a glade.              \n");}
  88.     |    glade    'X'
  89.     |    glade error
  90.              { yyerrok; yyclearin; printf("\nYou are at glade.   \n");}
  91.     ;
  92. pond    :    glade    'N'    {printf("\nNorth to a pond.              \n");}
  93.     |    stream    'E'    {printf("\nEast to a pond.               \n");}
  94.     |    woods    'W'    {printf("\nWest to a pond.               \n");}
  95.     |    pond    'X'
  96.     |    pond error
  97.              { yyerrok; yyclearin; printf("\nYou are at pond.    \n");}
  98.     ;
  99. stream    :    pond    'W'    {printf("\nWest to a stream.             \n");}
  100.     |    swamp    'N'    {printf("\nNorth to a stream.            \n");}
  101.     |    stream    'X'
  102.     |    stream error
  103.              { yyerrok; yyclearin; printf("\nYou are at stream.  \n");}
  104.     ;
  105. swamp    :    stream    'S'    {printf("\nSouth to a swamp.             \n");}
  106.     |    glade    'W'    {printf("\nWest to a swamp.              \n");}
  107.     |    thicket 'N'    {printf("\nNorth to a swamp.             \n");}
  108.     |    swamp    'X'
  109.     |    swamp error
  110.              { yyerrok; yyclearin; printf("\nYou are at swamp.   \n");}
  111.     ;
  112. thicket :    swamp    'S'    {printf("\nSouth to a thicket.           \n");}
  113.     |    knoll    'W'    {printf("\nWest to a thicket.            \n");}
  114.     |    thicket 'X'
  115.     |    thicket error
  116.              { yyerrok; yyclearin; printf("\nYou are at thicket. \n");}
  117.     ;
  118. %%
  119. yylex() {
  120.     static firstTime = 1;
  121.     static echo = 0;
  122.     if (!firstTime)  {
  123.     if (echo) {
  124.         echo = 0;
  125.             return 'X';
  126.     } else {
  127.         echo = 1;
  128.         return toupper( getChar() );
  129.     }
  130.     } else {
  131.     firstTime = 0;
  132.         return FIRST;
  133.     }
  134. }
  135.  
  136. yyerror(s)
  137. char *s;
  138. {
  139. /* For this program we ignore yyerror() calls: */
  140. /*  printf("\n%s",s);   */
  141. }
  142.  
  143. main() {
  144.     return yyparse();
  145. }
  146.  
  147.