home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / simplerd.lha / simplerad / FinalFTP / Conv / Qmodel / parser.y < prev    next >
Encoding:
Lex Description  |  1992-05-26  |  5.9 KB  |  244 lines

  1. %{
  2. /**********************************************************************/
  3. /* parser.y                                                           */
  4. /*                                                                    */
  5. /* Copyright (C) 1992, Bernard Kwok                                   */
  6. /* All rights reserved.                                               */
  7. /* Revision 1.0                                                       */
  8. /* May, 1992                                                          */
  9. /**********************************************************************/
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include "qm2patch.h"
  13.  
  14. float floatvalue;
  15. int readVector;        /* TRUE if parser is reading a vector */
  16. int readMatrix = FALSE;    /* TRUE if parser is reading a matrix */
  17. int elementVector;    /* current element of the vector read */
  18. int rowMatrix;        /* current row of a matrix read */
  19. int columnMatrix;    /* current column of a matrix read */
  20. int lineno;
  21.  
  22. /* Surface variables */
  23. int readSurfaceParm = FALSE; /* TRUE if parser is reading a surface parm */
  24. int readCurve = FALSE;  /* TRUE if parser is reading curves of surface */
  25. int Surfparm_read = 0;  /* Number of surface parameters read */
  26. int pts_on_curve_read = 0; /* Number of curve points read per curve */
  27. int curves_read = 0;    /* Number of curves read per surface */
  28. int pts_per_curve = 0;  /* Total number of pts per curve */
  29. int total_num_curves = 0; /* Total number of curves per surface */
  30.  
  31. /**********************************************************************/
  32. extern float SurfaceParm[];    /* Surface parameters */
  33.                                /* 0=pts/curve, 1=curves/surf*/
  34. extern surface  SurfaceRead;   /* Surface to read */
  35. extern vector    VectorRead;    /* Vector to read */
  36. extern matrix    MatrixRead;    /* Matrix to read */
  37. extern float    value;
  38.  
  39. extern void Surface();
  40. extern void Init_Matrix();
  41. extern void Scale();
  42. extern void Translate();
  43. extern void Rotate();
  44. extern void Cone();
  45. extern void Cube();
  46. extern void Cylinder();
  47. extern void Sphere();
  48. extern void Light();
  49. extern void SurfColour();
  50. extern void Diffusion();
  51. extern void Specularity();
  52. %}
  53. %token MODEL
  54. %token TM
  55. %token SCALE
  56. %token TRANSLATE
  57. %token ROTATE
  58. %token SURFACE
  59. %token CURVE
  60. %token CONE
  61. %token CUBE
  62. %token CYLINDER
  63. %token SPHERE
  64. %token LIGHT
  65. %token COLOR
  66. %token DIFFUSION
  67. %token SPECULARITY
  68. %token LEFT_CURLY
  69. %token RIGHT_CURLY
  70. %token LEFT_ROUND
  71. %token RIGHT_ROUND
  72. %token SEMI_COLON
  73. %token COMA
  74. %token FLOAT
  75. %%
  76.  
  77. scene: /* empty scene */ | object scene ;
  78. object: MODEL body ;
  79. body: LEFT_CURLY statements RIGHT_CURLY ;
  80. statements: /* empty statement */ | statement statements ;
  81. statement: transformation | primitive | property | surface ;
  82. transformation:    tm | scale | translate | rotate ;
  83.  
  84. surface: flag_surf surfparm LEFT_CURLY curves RIGHT_CURLY
  85. {
  86.   readSurfaceParm = FALSE;
  87.   readCurve = FALSE;
  88.   Surface();
  89. };
  90.  
  91. flag_surf: SURFACE {
  92.   /* Is a surface. Get ready to read surface parameters and curves. */
  93.   readSurfaceParm = TRUE;
  94.   readCurve = FALSE;
  95.   Surfparm_read = 0;
  96.   pts_on_curve_read = 0;
  97.   curves_read = 0;
  98.   pts_per_curve = 0;
  99.   total_num_curves = 0;
  100.   SurfaceParm[0] = 0;
  101.   SurfaceParm[1] = 0;
  102. }; 
  103.  
  104. surfparm: LEFT_ROUND float COMA float RIGHT_ROUND SEMI_COLON 
  105. {
  106.   readSurfaceParm = FALSE;     /* Finished reading surface parameters */
  107.   readCurve = TRUE;            /* reading surface now                 */
  108. };
  109.  
  110. curves: | curve curves ;
  111.  
  112. curve: flag_curve curveparm
  113.  
  114. flag_curve: CURVE {
  115.   elementVector = 0;
  116. }; 
  117.  
  118. curveparm : LEFT_ROUND float COMA float COMA float COMA float COMA float RIGHT_ROUND SEMI_COLON {
  119.   pts_on_curve_read = (pts_on_curve_read + 1) % pts_per_curve;
  120.   if (pts_on_curve_read == 0)
  121.     curves_read++;
  122.   if (curves_read == total_num_curves) {
  123.     readCurve = FALSE;
  124.   }
  125. };
  126.   
  127. primitive: cube | cone | cylinder | sphere | light ;
  128. property: color | diffusion | specularity ;
  129. tm: flag_tm matrix SEMI_COLON {
  130.   Init_Matrix ();
  131. };
  132. flag_tm: TM {
  133.   readMatrix = TRUE;
  134.   rowMatrix = 0;
  135.   columnMatrix = 0;
  136. };
  137.  
  138. scale: flag_scale vector SEMI_COLON {
  139.   Scale ();
  140. };
  141.  
  142. flag_scale: SCALE {
  143.   readVector = TRUE;
  144.   elementVector = 0;
  145. };
  146.  
  147. translate: flag_translate vector SEMI_COLON {
  148.   Translate ();
  149. };
  150.  
  151. flag_translate: TRANSLATE {
  152.   readVector = TRUE;
  153.   elementVector = 0;
  154. };
  155.  
  156. rotate: flag_rotate vector SEMI_COLON {
  157.   Rotate ();
  158. };
  159.  
  160. flag_rotate: ROTATE {
  161.   readVector = TRUE;
  162.   elementVector = 0;
  163. };
  164.  
  165. matrix:    LEFT_ROUND row COMA row COMA row COMA row RIGHT_ROUND {
  166.   readMatrix = FALSE;
  167. };
  168.  
  169. row: LEFT_ROUND float COMA float COMA float COMA float RIGHT_ROUND {
  170.   rowMatrix++;
  171.   columnMatrix = 0;
  172. };
  173.  
  174. vector: LEFT_ROUND float COMA float COMA float RIGHT_ROUND {
  175.   readVector = FALSE;
  176. };
  177.  
  178. float: FLOAT {
  179.   if (readSurfaceParm) { /* Reading surface parameters */
  180.     if (Surfparm_read == 0) 
  181.       pts_per_curve = floatvalue;
  182.     else
  183.       total_num_curves = floatvalue;
  184.     SurfaceParm[Surfparm_read] = floatvalue;
  185.     Surfparm_read++;
  186.   }
  187.  
  188.   else if (readCurve) { /* Reading curves */
  189.     SurfaceRead[pts_on_curve_read][curves_read][elementVector] = floatvalue;
  190.     elementVector = (elementVector + 1) % 5;
  191.   }
  192.  
  193.   else if (readVector)
  194.     VectorRead[elementVector++] = floatvalue;
  195.   else if (readMatrix)
  196.     MatrixRead[rowMatrix][columnMatrix++] = floatvalue;
  197.   else
  198.     value = floatvalue;
  199. };
  200.  
  201. cone: CONE LEFT_ROUND RIGHT_ROUND SEMI_COLON {
  202.   Cone ();
  203. }; 
  204.  
  205. cube: CUBE LEFT_ROUND RIGHT_ROUND SEMI_COLON {
  206.   Cube ();
  207. }; 
  208.  
  209. cylinder: CYLINDER LEFT_ROUND RIGHT_ROUND SEMI_COLON {
  210.   Cylinder ();
  211. }; 
  212.  
  213. sphere:    SPHERE LEFT_ROUND RIGHT_ROUND SEMI_COLON {
  214.   Sphere();
  215. }; 
  216.  
  217. light: LIGHT LEFT_ROUND RIGHT_ROUND SEMI_COLON {
  218.   Light();
  219. }; 
  220.  
  221. diffusion: DIFFUSION LEFT_ROUND float RIGHT_ROUND SEMI_COLON {
  222.   Diffusion();
  223. };
  224.  
  225. specularity: SPECULARITY LEFT_ROUND float RIGHT_ROUND SEMI_COLON {
  226.   Specularity();
  227. };
  228.  
  229. color: flag_color vector SEMI_COLON {
  230.   SurfColour();
  231. };
  232.  
  233. flag_color: COLOR {
  234.   readVector = TRUE;
  235.   elementVector = 0;
  236. };
  237.  
  238. %%
  239. yyerror(s)
  240.      char *s;
  241. {
  242.   fprintf(stderr,"error: '%s' in line %d\n", s, lineno);
  243. };
  244.