fortran.lex 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /******************************************************************************/
  2. /* */
  3. /* CONV (converter) for Agrif (Adaptive Grid Refinement In Fortran) */
  4. /* */
  5. /* Copyright or or Copr. Laurent Debreu (Laurent.Debreu@imag.fr) */
  6. /* Cyril Mazauric (Cyril_Mazauric@yahoo.fr) */
  7. /* This software is governed by the CeCILL-C license under French law and */
  8. /* abiding by the rules of distribution of free software. You can use, */
  9. /* modify and/ or redistribute the software under the terms of the CeCILL-C */
  10. /* license as circulated by CEA, CNRS and INRIA at the following URL */
  11. /* "http://www.cecill.info". */
  12. /* */
  13. /* As a counterpart to the access to the source code and rights to copy, */
  14. /* modify and redistribute granted by the license, users are provided only */
  15. /* with a limited warranty and the software's author, the holder of the */
  16. /* economic rights, and the successive licensors have only limited */
  17. /* liability. */
  18. /* */
  19. /* In this respect, the user's attention is drawn to the risks associated */
  20. /* with loading, using, modifying and/or developing or reproducing the */
  21. /* software by the user in light of its specific status of free software, */
  22. /* that may mean that it is complicated to manipulate, and that also */
  23. /* therefore means that it is reserved for developers and experienced */
  24. /* professionals having in-depth computer knowledge. Users are therefore */
  25. /* encouraged to load and test the software's suitability as regards their */
  26. /* requirements in conditions enabling the security of their systems and/or */
  27. /* data to be ensured and, more generally, to use and operate it in the */
  28. /* same conditions as regards security. */
  29. /* */
  30. /* The fact that you are presently reading this means that you have had */
  31. /* knowledge of the CeCILL-C license and that you accept its terms. */
  32. /******************************************************************************/
  33. /* version 1.7 */
  34. /******************************************************************************/
  35. %option warn
  36. %option noyywrap
  37. %x parameter
  38. %s character
  39. %x donottreat
  40. %s fortran77style
  41. %s fortran90style
  42. %{
  43. #include <math.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. extern FILE * yyin;
  47. #define MAX_INCLUDE_DEPTH 30
  48. YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
  49. int line_num_input = 1;
  50. int newlinef90 = 0;
  51. char tmpc;
  52. #define PRINT_LINE_NUM() // { fprintf(stderr,"== Parsing l.%4d...\n", line_num_input); }
  53. #define INCREMENT_LINE_NUM() { line_num_input++; PRINT_LINE_NUM(); }
  54. /******************************************************************************/
  55. /**************PETITS PB NON PREVUS *******************************************/
  56. /******************************************************************************/
  57. /* NEXTLINF77 un ligne fortran 77 peut commencer par - &a=b or on */
  58. /* a prevu seulement & a=b avec l'espace entre le symbole */
  59. /* de la 7eme et le debut de la ligne de commande */
  60. /* le ! est aussi interdit comme symbole de la 7 eme colonne */
  61. /* Normalement NEXTLINEF77 \n+[ ]{5}[^ ] */
  62. /******************************************************************************/
  63. #define YY_USER_ACTION if (firstpass == 0) ECHO;
  64. void out_of_donottreat(void);
  65. %}
  66. REAL8 "real*8"[ \t]*"(a-h,o-z)"
  67. SLASH "/"
  68. DSLASH "/"[ \t]*"/"
  69. HEXA Z\'[0-9a-fA-F]+\'
  70. NAME [a-zA-Z\_][a-zA-Z0-9\_]*
  71. INTEGER [0-9]+
  72. EXPONENT [edq][-+]?{INTEGER}
  73. BEG_DNT ^[C!]"$AGRIF_DO_NOT_TREAT"[ \t]*\n
  74. END_DNT ^[C!]"$AGRIF_END_DO_NOT_TREAT"[ \t]*\n
  75. BEG_INTERFACE ^[ \t]*interface
  76. END_INTERFACE ^[ \t]*end[ \t]*interface.*\n
  77. ASSIGNTYPE "assignment"[ \t]*"("[ \t]*[-+=]+[ \t]*")"
  78. COMM_F77 ^([Cc*](([ \t]*\n)|([^AaHhOo\n].*\n)))
  79. COMM_F90 ^[ \t]*!.*\n
  80. COMM_F90_2 !.*
  81. NEXTLINEF90 "&".*\n+
  82. NEXTLINEF77 [\n \t]*\n[ \t]{5}("&"|"+"|"$"|"*"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"|"."|"#")
  83. LABEL ^(((" "|[0-9]){1,5})|([ \t]{1,5}))[ &]+
  84. %%
  85. if (infixed) BEGIN(fortran77style) ;
  86. if (infree) BEGIN(fortran90style) ;
  87. {REAL8} { return TOK_REAL8; }
  88. subroutine { return TOK_SUBROUTINE; }
  89. program { return TOK_PROGRAM; }
  90. allocate { inallocate = 1; return TOK_ALLOCATE; }
  91. nullify { return TOK_NULLIFY; }
  92. null[ ]*\([ ]*\) { return TOK_NULL_PTR; }
  93. deallocate { inallocate = 1; return TOK_DEALLOCATE; }
  94. result { return TOK_RESULT; }
  95. function { return TOK_FUNCTION; }
  96. end[ \t]*program { strcpy(yylval.na,fortran_text); return TOK_ENDPROGRAM;}
  97. end[ \t]*module { strcpy(yylval.na,fortran_text); return TOK_ENDMODULE; }
  98. end[ \t]*subroutine { strcpy(yylval.na,fortran_text); return TOK_ENDSUBROUTINE;}
  99. end[ \t]*function { strcpy(yylval.na,fortran_text); return TOK_ENDFUNCTION;}
  100. end { strcpy(yylval.na,fortran_text); return TOK_ENDUNIT;}
  101. include { pos_curinclude = setposcur()-9; return TOK_INCLUDE;}
  102. ^[ \t]*use[ ]+ { strcpy(yylval.na,fortran_text);
  103. tmpc = (char) input(); unput(tmpc);
  104. if ( ( tmpc >= 'a' && tmpc <= 'z' ) ||
  105. ( tmpc >= 'A' && tmpc <= 'Z' ) ) return TOK_USE;
  106. else return TOK_NAME;
  107. }
  108. rewind { return TOK_REWIND; }
  109. implicit { return TOK_IMPLICIT; }
  110. none { return TOK_NONE; }
  111. call { return TOK_CALL; }
  112. .true. { return TOK_TRUE; }
  113. .false. { return TOK_FALSE; }
  114. \=\> { return TOK_POINT_TO; }
  115. {ASSIGNTYPE} { strcpy(yylval.na,fortran_text); return TOK_ASSIGNTYPE;}
  116. \*\* { strcpy(yylval.na,fortran_text); return TOK_DASTER; }
  117. \.[ \t]*eqv\. { strcpy(yylval.na,fortran_text); return TOK_EQV; }
  118. \.[ \t]*eq\. { strcpy(yylval.na,fortran_text); return TOK_EQ; }
  119. \.[ \t]*gt\. { strcpy(yylval.na,fortran_text); return TOK_GT; }
  120. \.[ \t]*ge\. { strcpy(yylval.na,fortran_text); return TOK_GE; }
  121. \.[ \t]*lt\. { strcpy(yylval.na,fortran_text); return TOK_LT; }
  122. \.[ \t]*le\. { strcpy(yylval.na,fortran_text); return TOK_LE; }
  123. \.[ \t]*neqv\. { strcpy(yylval.na,fortran_text); return TOK_NEQV;}
  124. \.[ \t]*ne\. { strcpy(yylval.na,fortran_text); return TOK_NE; }
  125. \.[ \t]*not\. { strcpy(yylval.na,fortran_text); return TOK_NOT; }
  126. \.[ \t]*or\. { strcpy(yylval.na,fortran_text); return TOK_OR; }
  127. \.[ \t]*xor\. { strcpy(yylval.na,fortran_text); return TOK_XOR; }
  128. \.[ \t]*and\. { strcpy(yylval.na,fortran_text); return TOK_AND; }
  129. module { return TOK_MODULE; }
  130. while { return TOK_WHILE; }
  131. concurrent { return TOK_CONCURRENT; }
  132. end[ \t]*do { return TOK_ENDDO; }
  133. do { return TOK_PLAINDO;}
  134. real { strcpy(yylval.na,fortran_text); return TOK_REAL; }
  135. integer { strcpy(yylval.na,fortran_text); return TOK_INTEGER; }
  136. logical { strcpy(yylval.na,fortran_text); return TOK_LOGICAL; }
  137. character { strcpy(yylval.na,fortran_text); return TOK_CHARACTER; }
  138. {HEXA} { strcpy(yylval.na,fortran_text); return TOK_HEXA;}
  139. double[ \t]*precision { strcpy(yylval.na,fortran_text); return TOK_DOUBLEPRECISION; }
  140. double[ \t]*complex { strcpy(yylval.na,fortran_text); return TOK_DOUBLECOMPLEX; }
  141. complex { return TOK_COMPLEX; }
  142. allocatable { return TOK_ALLOCATABLE; }
  143. close { return TOK_CLOSE; }
  144. inquire { return TOK_INQUIRE; }
  145. dimension { return TOK_DIMENSION; }
  146. pause { return TOK_PAUSE; }
  147. equivalence { return TOK_EQUIVALENCE; }
  148. stop { return TOK_STOP; }
  149. where { return TOK_WHERE; }
  150. end[ \t]*where { return TOK_ENDWHERE; }
  151. else[ \t]*where[ \t]*\( { return TOK_ELSEWHEREPAR; }
  152. else[ \t]*where { return TOK_ELSEWHERE; }
  153. ^[ \t]*contains { return TOK_CONTAINS; }
  154. only { return TOK_ONLY; }
  155. parameter { return TOK_PARAMETER; }
  156. recursive { return TOK_RECURSIVE; }
  157. common { return TOK_COMMON; }
  158. ^[ \t]*global[ \t]+ { return TOK_GLOBAL; }
  159. external { return TOK_EXTERNAL; }
  160. intent { return TOK_INTENT; }
  161. pointer { return TOK_POINTER; }
  162. optional { return TOK_OPTIONAL; }
  163. save { return TOK_SAVE; }
  164. ^[ \t]*type[ \t]*\( { pos_cur_decl = setposcur()-5; return TOK_TYPEPAR; }
  165. ^[ \t]*type[ \t\,]+ { return TOK_TYPE; }
  166. end[ \t]*type { return TOK_ENDTYPE; }
  167. stat { if (inallocate == 1) return TOK_STAT; else { strcpy(yylval.na,fortran_text); return TOK_NAME; } }
  168. open { return TOK_OPEN; }
  169. return { return TOK_RETURN; }
  170. exit[^(] { return TOK_EXIT; }
  171. print { return TOK_PRINT; }
  172. module[ \t]*procedure { return TOK_PROCEDURE; }
  173. read { return TOK_READ; }
  174. namelist { return TOK_NAMELIST; }
  175. write { return TOK_WRITE; }
  176. flush { return TOK_FLUSH; }
  177. target { return TOK_TARGET; }
  178. public { return TOK_PUBLIC; }
  179. private { return TOK_PRIVATE; }
  180. in { strcpy(yylval.na,fortran_text); return TOK_IN; }
  181. ^[ \t]*data[ \t]+ { pos_curdata = setposcur()-strlen(fortran_text); Init_List_Data_Var(); return TOK_DATA; }
  182. continue { return TOK_CONTINUE; }
  183. go[ \t]*to { return TOK_PLAINGOTO; }
  184. out { strcpy(yylval.na,fortran_text); return TOK_OUT; }
  185. inout { strcpy(yylval.na,fortran_text); return TOK_INOUT; }
  186. intrinsic { return TOK_INTRINSIC; }
  187. then { return TOK_THEN; }
  188. else[ \t]*if { return TOK_ELSEIF; }
  189. else { return TOK_ELSE; }
  190. end[ \t]*if { return TOK_ENDIF; }
  191. if { return TOK_LOGICALIF; }
  192. sum[ \t]*\( { return TOK_SUM; }
  193. max[ \t]*\( { return TOK_MAX; }
  194. tanh { return TOK_TANH; }
  195. maxval { return TOK_MAXVAL; }
  196. trim { return TOK_TRIM; }
  197. sqrt\( { return TOK_SQRT; }
  198. select[ \t]*case { return TOK_SELECTCASE; }
  199. ^[ \t]*case[ \t]* { return TOK_CASE; }
  200. default { return TOK_DEFAULT; }
  201. end[ \t]*select { return TOK_ENDSELECT; }
  202. file[ \t]*\= { return TOK_FILE; }
  203. unit[ \t]*\= { return TOK_UNIT; }
  204. fmt[ \t]*\= { return TOK_FMT; }
  205. nml[ \t]*\= { return TOK_NML; }
  206. end[ \t]*\= { return TOK_END; }
  207. eor[ \t]*\= { return TOK_EOR; }
  208. err[ \t]*\= { return TOK_ERR; }
  209. exist[ \t]*\= { return TOK_EXIST; }
  210. min[ \t]*\( { return TOK_MIN; }
  211. nint { return TOK_NINT; }
  212. float { return TOK_FLOAT; }
  213. exp { return TOK_EXP; }
  214. cos { return TOK_COS; }
  215. cosh { return TOK_COSH; }
  216. acos { return TOK_ACOS; }
  217. sin { return TOK_SIN; }
  218. sinh { return TOK_SINH; }
  219. asin { return TOK_ASIN; }
  220. log { return TOK_LOG; }
  221. tan { return TOK_TAN; }
  222. atan { return TOK_ATAN; }
  223. cycle { return TOK_CYCLE; }
  224. abs[ \t]*\( { return TOK_ABS; }
  225. mod { return TOK_MOD; }
  226. sign[ \t]*\( { return TOK_SIGN; }
  227. minloc { return TOK_MINLOC; }
  228. maxloc { return TOK_MAXLOC; }
  229. minval { return TOK_MINVAL; }
  230. backspace { return TOK_BACKSPACE; }
  231. :: { return TOK_FOURDOTS; }
  232. \({SLASH} { return TOK_LEFTAB; }
  233. {SLASH}\) { return TOK_RIGHTAB; }
  234. format[ \t]*\((.|{NEXTLINEF90}|{NEXTLINEF77})*\) {
  235. return TOK_FORMAT; }
  236. {SLASH} { strcpy(yylval.na,fortran_text); return TOK_SLASH; }
  237. DSLASH { strcpy(yylval.na,fortran_text); return TOK_DSLASH; }
  238. (\')[^']*&{0,1}\n[ \t]*&{0,1}[^']*(\') {
  239. strcpy(yylval.na,fortran_text); return TOK_CHAR_CUT; }
  240. (\')[^']*(\') { strcpy(yylval.na,fortran_text);return TOK_CHAR_CONSTANT; }
  241. (\")[^"]*(\") { strcpy(yylval.na,fortran_text);return TOK_CHAR_MESSAGE; }
  242. {BEG_INTERFACE} { BEGIN(donottreat); }
  243. <donottreat>{END_INTERFACE} { out_of_donottreat(); return '\n'; }
  244. {NAME} { strcpy(yylval.na,fortran_text); return TOK_NAME; }
  245. ({INTEGER}\.[0-9]*)/[^"and."|"false."|"true."|"eq."|"or."|"gt."|"ge."|"lt."|"le."|"not."|"ne."] { // REAL1
  246. strcpy(yylval.na,fortran_text); return TOK_CSTREAL; }
  247. (({INTEGER}\.[0-9]+|[0-9]*\.{INTEGER}){EXPONENT}?)|{INTEGER}(\.)?{EXPONENT} { // REAL2
  248. strcpy(yylval.na,fortran_text); return TOK_CSTREAL; }
  249. {INTEGER} { strcpy(yylval.na,fortran_text); return TOK_CSTINT; }
  250. \$ {}
  251. \. {}
  252. \(|\)|:|\[|\]|\+|\-|\* { strcpy(yylval.na,fortran_text); return (int) *fortran_text; }
  253. \% { strcpy(yylval.na,fortran_text); return (int) *fortran_text; }
  254. \; { return TOK_SEMICOLON; }
  255. \, { return (int) *fortran_text; }
  256. \= { return (int) *fortran_text; }
  257. \< { return (int) *fortran_text; }
  258. \> { return (int) *fortran_text; }
  259. \n { INCREMENT_LINE_NUM() ; return '\n'; }
  260. ^[ ]*$ {}
  261. [ \t]+ {}
  262. {LABEL} { if (newlinef90 == 0) return TOK_LABEL; else newlinef90 = 0; }
  263. {NEXTLINEF90} { INCREMENT_LINE_NUM() ; newlinef90=1; }
  264. {NEXTLINEF77} { INCREMENT_LINE_NUM() ; }
  265. {BEG_DNT} { INCREMENT_LINE_NUM() ; BEGIN(donottreat); }
  266. <donottreat>{END_DNT} { out_of_donottreat(); return '\n'; }
  267. <donottreat>.*\n { INCREMENT_LINE_NUM() ; }
  268. <fortran77style>{COMM_F77} { INCREMENT_LINE_NUM() ; }
  269. {COMM_F90} { INCREMENT_LINE_NUM() ; }
  270. {COMM_F90_2} {}
  271. %%
  272. void out_of_donottreat ( void )
  273. {
  274. BEGIN(INITIAL);
  275. if (infixed) BEGIN(fortran77style) ;
  276. if (infree) BEGIN(fortran90style) ;
  277. INCREMENT_LINE_NUM() ;
  278. }