PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/opensource.apple.com/source/gdb/gdb-213/src/gdb/f-exp.y

#
Happy | 1200 lines | 1030 code | 170 blank | 0 comment | 0 complexity | 2f2e394592b0d8894bf59a4eda6d5054 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0, GPL-2.0, ISC, LGPL-2.1, Apache-2.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause, WTFPL, MIT, AGPL-1.0, AGPL-3.0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <title>f-exp.y</title>
  6. <style type="text/css">
  7. .enscript-comment { font-style: italic; color: rgb(178,34,34); }
  8. .enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
  9. .enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
  10. .enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
  11. .enscript-reference { font-weight: bold; color: rgb(95,158,160); }
  12. .enscript-string { font-weight: bold; color: rgb(188,143,143); }
  13. .enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
  14. .enscript-type { font-weight: bold; color: rgb(34,139,34); }
  15. .enscript-highlight { text-decoration: underline; color: 0; }
  16. </style>
  17. </head>
  18. <body id="top">
  19. <h1 style="margin:8px;" id="f1">f-exp.y&nbsp;&nbsp;&nbsp;<span style="font-weight: normal; font-size: 0.5em;">[<a href="?txt">plain text</a>]</span></h1>
  20. <hr/>
  21. <div></div>
  22. <pre>
  23. /* YACC parser for Fortran expressions, for GDB.
  24. Copyright 1986, 1989, 1990, 1991, 1993, 1994, 1995, 1996, 2000, 2001
  25. Free Software Foundation, Inc.
  26. Contributed by Motorola. Adapted from the C parser by Farooq Butt
  27. (<a href="mailto:fmbutt@engage.sps.mot.com">fmbutt@engage.sps.mot.com</a>).
  28. This file is part of GDB.
  29. This program is free software; you can redistribute it and/or modify
  30. it under the terms of the GNU General Public License as published by
  31. the Free Software Foundation; either version 2 of the License, or
  32. (at your option) any later version.
  33. This program is distributed in the hope that it will be useful,
  34. but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. GNU General Public License for more details.
  37. You should have received a copy of the GNU General Public License
  38. along with this program; if not, write to the Free Software
  39. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  40. /* This was blantantly ripped off the C expression parser, please
  41. be aware of that as you look at its basic structure -FMB */
  42. /* Parse a F77 expression from text in a string,
  43. and return the result as a struct expression pointer.
  44. That structure contains arithmetic operations in reverse polish,
  45. with constants represented by operations that are followed by special data.
  46. See expression.h for the details of the format.
  47. What is important here is that it can be built up sequentially
  48. during the process of parsing; the lower levels of the tree always
  49. come first in the result.
  50. Note that malloc's and realloc's in this file are transformed to
  51. xmalloc and xrealloc respectively by the same sed command in the
  52. makefile that remaps any other malloc/realloc inserted by the parser
  53. generator. Doing this with #defines and trying to control the interaction
  54. with include files (&lt;malloc.h&gt; and &lt;stdlib.h&gt; for example) just became
  55. too messy, particularly when such includes can be inserted at random
  56. times by the parser generator. */
  57. %{
  58. #include &quot;defs.h&quot;
  59. #include &quot;gdb_string.h&quot;
  60. #include &quot;expression.h&quot;
  61. #include &quot;value.h&quot;
  62. #include &quot;parser-defs.h&quot;
  63. #include &quot;language.h&quot;
  64. #include &quot;f-lang.h&quot;
  65. #include &quot;bfd.h&quot; /* Required by objfiles.h. */
  66. #include &quot;symfile.h&quot; /* Required by objfiles.h. */
  67. #include &quot;objfiles.h&quot; /* For have_full_symbols and have_partial_symbols */
  68. #include &lt;ctype.h&gt;
  69. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  70. as well as gratuitiously global symbol names, so we can have multiple
  71. yacc generated parsers in gdb. Note that these are only the variables
  72. produced by yacc. If other parser generators (bison, byacc, etc) produce
  73. additional global names that conflict at link time, then those parser
  74. generators need to be fixed instead of adding those names to this list. */
  75. #define yymaxdepth f_maxdepth
  76. #define yyparse f_parse
  77. #define yylex f_lex
  78. #define yyerror f_error
  79. #define yylval f_lval
  80. #define yychar f_char
  81. #define yydebug f_debug
  82. #define yypact f_pact
  83. #define yyr1 f_r1
  84. #define yyr2 f_r2
  85. #define yydef f_def
  86. #define yychk f_chk
  87. #define yypgo f_pgo
  88. #define yyact f_act
  89. #define yyexca f_exca
  90. #define yyerrflag f_errflag
  91. #define yynerrs f_nerrs
  92. #define yyps f_ps
  93. #define yypv f_pv
  94. #define yys f_s
  95. #define yy_yys f_yys
  96. #define yystate f_state
  97. #define yytmp f_tmp
  98. #define yyv f_v
  99. #define yy_yyv f_yyv
  100. #define yyval f_val
  101. #define yylloc f_lloc
  102. #define yyreds f_reds /* With YYDEBUG defined */
  103. #define yytoks f_toks /* With YYDEBUG defined */
  104. #define yylhs f_yylhs
  105. #define yylen f_yylen
  106. #define yydefred f_yydefred
  107. #define yydgoto f_yydgoto
  108. #define yysindex f_yysindex
  109. #define yyrindex f_yyrindex
  110. #define yygindex f_yygindex
  111. #define yytable f_yytable
  112. #define yycheck f_yycheck
  113. #ifndef YYDEBUG
  114. #define YYDEBUG 1 /* Default to no yydebug support */
  115. #endif
  116. int yyparse (void);
  117. static int yylex (void);
  118. void yyerror (char *);
  119. static void growbuf_by_size (int);
  120. static int match_string_literal (void);
  121. %}
  122. /* Although the yacc &quot;value&quot; of an expression is not used,
  123. since the result is stored in the structure being created,
  124. other node types do have values. */
  125. %union
  126. {
  127. LONGEST lval;
  128. struct {
  129. LONGEST val;
  130. struct type *type;
  131. } typed_val;
  132. DOUBLEST dval;
  133. struct symbol *sym;
  134. struct type *tval;
  135. struct stoken sval;
  136. struct ttype tsym;
  137. struct symtoken ssym;
  138. int voidval;
  139. struct block *bval;
  140. enum exp_opcode opcode;
  141. struct internalvar *ivar;
  142. struct type **tvec;
  143. int *ivec;
  144. }
  145. %{
  146. /* YYSTYPE gets defined by %union */
  147. static int parse_number (char *, int, int, YYSTYPE *);
  148. %}
  149. %type &lt;voidval&gt; exp type_exp start variable
  150. %type &lt;tval&gt; type typebase
  151. %type &lt;tvec&gt; nonempty_typelist
  152. /* %type &lt;bval&gt; block */
  153. /* Fancy type parsing. */
  154. %type &lt;voidval&gt; func_mod direct_abs_decl abs_decl
  155. %type &lt;tval&gt; ptype
  156. %token &lt;typed_val&gt; INT
  157. %token &lt;dval&gt; FLOAT
  158. /* Both NAME and TYPENAME tokens represent symbols in the input,
  159. and both convey their data as strings.
  160. But a TYPENAME is a string that happens to be defined as a typedef
  161. or builtin type name (such as int or char)
  162. and a NAME is any other symbol.
  163. Contexts where this distinction is not important can use the
  164. nonterminal &quot;name&quot;, which matches either NAME or TYPENAME. */
  165. %token &lt;sval&gt; STRING_LITERAL
  166. %token &lt;lval&gt; BOOLEAN_LITERAL
  167. %token &lt;ssym&gt; NAME
  168. %token &lt;tsym&gt; TYPENAME
  169. %type &lt;sval&gt; name
  170. %type &lt;ssym&gt; name_not_typename
  171. %type &lt;tsym&gt; typename
  172. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  173. but which would parse as a valid number in the current input radix.
  174. E.g. &quot;c&quot; when input_radix==16. Depending on the parse, it will be
  175. turned into a name or into a number. */
  176. %token &lt;ssym&gt; NAME_OR_INT
  177. %token SIZEOF
  178. %token ERROR
  179. /* Special type cases, put in to allow the parser to distinguish different
  180. legal basetypes. */
  181. %token INT_KEYWORD INT_S2_KEYWORD LOGICAL_S1_KEYWORD LOGICAL_S2_KEYWORD
  182. %token LOGICAL_KEYWORD REAL_KEYWORD REAL_S8_KEYWORD REAL_S16_KEYWORD
  183. %token COMPLEX_S8_KEYWORD COMPLEX_S16_KEYWORD COMPLEX_S32_KEYWORD
  184. %token BOOL_AND BOOL_OR BOOL_NOT
  185. %token &lt;lval&gt; CHARACTER
  186. %token &lt;voidval&gt; VARIABLE
  187. %token &lt;opcode&gt; ASSIGN_MODIFY
  188. %left ','
  189. %left ABOVE_COMMA
  190. %right '=' ASSIGN_MODIFY
  191. %right '?'
  192. %left BOOL_OR
  193. %right BOOL_NOT
  194. %left BOOL_AND
  195. %left '|'
  196. %left '^'
  197. %left '&amp;'
  198. %left EQUAL NOTEQUAL
  199. %left LESSTHAN GREATERTHAN LEQ GEQ
  200. %left LSH RSH
  201. %left '@'
  202. %left '+' '-'
  203. %left '*' '/' '%'
  204. %right UNARY
  205. %right '('
  206. %%
  207. start : exp
  208. | type_exp
  209. ;
  210. type_exp: type
  211. { write_exp_elt_opcode(OP_TYPE);
  212. write_exp_elt_type($1);
  213. write_exp_elt_opcode(OP_TYPE); }
  214. ;
  215. exp : '(' exp ')'
  216. { }
  217. ;
  218. /* Expressions, not including the comma operator. */
  219. exp : '*' exp %prec UNARY
  220. { write_exp_elt_opcode (UNOP_IND); }
  221. exp : '&amp;' exp %prec UNARY
  222. { write_exp_elt_opcode (UNOP_ADDR); }
  223. exp : '-' exp %prec UNARY
  224. { write_exp_elt_opcode (UNOP_NEG); }
  225. ;
  226. exp : BOOL_NOT exp %prec UNARY
  227. { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
  228. ;
  229. exp : '~' exp %prec UNARY
  230. { write_exp_elt_opcode (UNOP_COMPLEMENT); }
  231. ;
  232. exp : SIZEOF exp %prec UNARY
  233. { write_exp_elt_opcode (UNOP_SIZEOF); }
  234. ;
  235. /* No more explicit array operators, we treat everything in F77 as
  236. a function call. The disambiguation as to whether we are
  237. doing a subscript operation or a function call is done
  238. later in eval.c. */
  239. exp : exp '('
  240. { start_arglist (); }
  241. arglist ')'
  242. { write_exp_elt_opcode (OP_F77_UNDETERMINED_ARGLIST);
  243. write_exp_elt_longcst ((LONGEST) end_arglist ());
  244. write_exp_elt_opcode (OP_F77_UNDETERMINED_ARGLIST); }
  245. ;
  246. arglist :
  247. ;
  248. arglist : exp
  249. { arglist_len = 1; }
  250. ;
  251. arglist : substring
  252. { arglist_len = 2;}
  253. arglist : arglist ',' exp %prec ABOVE_COMMA
  254. { arglist_len++; }
  255. ;
  256. substring: exp ':' exp %prec ABOVE_COMMA
  257. { }
  258. ;
  259. complexnum: exp ',' exp
  260. { }
  261. ;
  262. exp : '(' complexnum ')'
  263. { write_exp_elt_opcode(OP_COMPLEX); }
  264. ;
  265. exp : '(' type ')' exp %prec UNARY
  266. { write_exp_elt_opcode (UNOP_CAST);
  267. write_exp_elt_type ($2);
  268. write_exp_elt_opcode (UNOP_CAST); }
  269. ;
  270. /* Binary operators in order of decreasing precedence. */
  271. exp : exp '@' exp
  272. { write_exp_elt_opcode (BINOP_REPEAT); }
  273. ;
  274. exp : exp '*' exp
  275. { write_exp_elt_opcode (BINOP_MUL); }
  276. ;
  277. exp : exp '/' exp
  278. { write_exp_elt_opcode (BINOP_DIV); }
  279. ;
  280. exp : exp '%' exp
  281. { write_exp_elt_opcode (BINOP_REM); }
  282. ;
  283. exp : exp '+' exp
  284. { write_exp_elt_opcode (BINOP_ADD); }
  285. ;
  286. exp : exp '-' exp
  287. { write_exp_elt_opcode (BINOP_SUB); }
  288. ;
  289. exp : exp LSH exp
  290. { write_exp_elt_opcode (BINOP_LSH); }
  291. ;
  292. exp : exp RSH exp
  293. { write_exp_elt_opcode (BINOP_RSH); }
  294. ;
  295. exp : exp EQUAL exp
  296. { write_exp_elt_opcode (BINOP_EQUAL); }
  297. ;
  298. exp : exp NOTEQUAL exp
  299. { write_exp_elt_opcode (BINOP_NOTEQUAL); }
  300. ;
  301. exp : exp LEQ exp
  302. { write_exp_elt_opcode (BINOP_LEQ); }
  303. ;
  304. exp : exp GEQ exp
  305. { write_exp_elt_opcode (BINOP_GEQ); }
  306. ;
  307. exp : exp LESSTHAN exp
  308. { write_exp_elt_opcode (BINOP_LESS); }
  309. ;
  310. exp : exp GREATERTHAN exp
  311. { write_exp_elt_opcode (BINOP_GTR); }
  312. ;
  313. exp : exp '&amp;' exp
  314. { write_exp_elt_opcode (BINOP_BITWISE_AND); }
  315. ;
  316. exp : exp '^' exp
  317. { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
  318. ;
  319. exp : exp '|' exp
  320. { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
  321. ;
  322. exp : exp BOOL_AND exp
  323. { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
  324. ;
  325. exp : exp BOOL_OR exp
  326. { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
  327. ;
  328. exp : exp '=' exp
  329. { write_exp_elt_opcode (BINOP_ASSIGN); }
  330. ;
  331. exp : exp ASSIGN_MODIFY exp
  332. { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
  333. write_exp_elt_opcode ($2);
  334. write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
  335. ;
  336. exp : INT
  337. { write_exp_elt_opcode (OP_LONG);
  338. write_exp_elt_type ($1.type);
  339. write_exp_elt_longcst ((LONGEST)($1.val));
  340. write_exp_elt_opcode (OP_LONG); }
  341. ;
  342. exp : NAME_OR_INT
  343. { YYSTYPE val;
  344. parse_number ($1.stoken.ptr, $1.stoken.length, 0, &amp;val);
  345. write_exp_elt_opcode (OP_LONG);
  346. write_exp_elt_type (val.typed_val.type);
  347. write_exp_elt_longcst ((LONGEST)val.typed_val.val);
  348. write_exp_elt_opcode (OP_LONG); }
  349. ;
  350. exp : FLOAT
  351. { write_exp_elt_opcode (OP_DOUBLE);
  352. write_exp_elt_type (builtin_type_f_real_s8);
  353. write_exp_elt_dblcst ($1);
  354. write_exp_elt_opcode (OP_DOUBLE); }
  355. ;
  356. exp : variable
  357. ;
  358. exp : VARIABLE
  359. ;
  360. exp : SIZEOF '(' type ')' %prec UNARY
  361. { write_exp_elt_opcode (OP_LONG);
  362. write_exp_elt_type (builtin_type_f_integer);
  363. CHECK_TYPEDEF ($3);
  364. write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
  365. write_exp_elt_opcode (OP_LONG); }
  366. ;
  367. exp : BOOLEAN_LITERAL
  368. { write_exp_elt_opcode (OP_BOOL);
  369. write_exp_elt_longcst ((LONGEST) $1);
  370. write_exp_elt_opcode (OP_BOOL);
  371. }
  372. ;
  373. exp : STRING_LITERAL
  374. {
  375. write_exp_elt_opcode (OP_STRING);
  376. write_exp_string ($1);
  377. write_exp_elt_opcode (OP_STRING);
  378. }
  379. ;
  380. variable: name_not_typename
  381. { struct symbol *sym = $1.sym;
  382. if (sym)
  383. {
  384. if (symbol_read_needs_frame (sym))
  385. {
  386. if (innermost_block == 0 ||
  387. contained_in (block_found,
  388. innermost_block))
  389. innermost_block = block_found;
  390. }
  391. write_exp_elt_opcode (OP_VAR_VALUE);
  392. /* We want to use the selected frame, not
  393. another more inner frame which happens to
  394. be in the same block. */
  395. write_exp_elt_block (NULL);
  396. write_exp_elt_sym (sym);
  397. write_exp_elt_opcode (OP_VAR_VALUE);
  398. break;
  399. }
  400. else
  401. {
  402. struct minimal_symbol *msymbol;
  403. register char *arg = copy_name ($1.stoken);
  404. msymbol =
  405. lookup_minimal_symbol (arg, NULL, NULL);
  406. if (msymbol != NULL)
  407. {
  408. write_exp_msymbol (msymbol,
  409. lookup_function_type (builtin_type_int),
  410. builtin_type_int);
  411. }
  412. else if (!have_full_symbols () &amp;&amp; !have_partial_symbols ())
  413. error (&quot;No symbol table is loaded. Use the \&quot;file\&quot; command.&quot;);
  414. else
  415. error (&quot;No symbol \&quot;%s\&quot; in current context.&quot;,
  416. copy_name ($1.stoken));
  417. }
  418. }
  419. ;
  420. type : ptype
  421. ;
  422. ptype : typebase
  423. | typebase abs_decl
  424. {
  425. /* This is where the interesting stuff happens. */
  426. int done = 0;
  427. int array_size;
  428. struct type *follow_type = $1;
  429. struct type *range_type;
  430. while (!done)
  431. switch (pop_type ())
  432. {
  433. case tp_end:
  434. done = 1;
  435. break;
  436. case tp_pointer:
  437. follow_type = lookup_pointer_type (follow_type);
  438. break;
  439. case tp_reference:
  440. follow_type = lookup_reference_type (follow_type);
  441. break;
  442. case tp_array:
  443. array_size = pop_type_int ();
  444. if (array_size != -1)
  445. {
  446. range_type =
  447. create_range_type ((struct type *) NULL,
  448. builtin_type_f_integer, 0,
  449. array_size - 1);
  450. follow_type =
  451. create_array_type ((struct type *) NULL,
  452. follow_type, range_type);
  453. }
  454. else
  455. follow_type = lookup_pointer_type (follow_type);
  456. break;
  457. case tp_function:
  458. follow_type = lookup_function_type (follow_type);
  459. break;
  460. }
  461. $$ = follow_type;
  462. }
  463. ;
  464. abs_decl: '*'
  465. { push_type (tp_pointer); $$ = 0; }
  466. | '*' abs_decl
  467. { push_type (tp_pointer); $$ = $2; }
  468. | '&amp;'
  469. { push_type (tp_reference); $$ = 0; }
  470. | '&amp;' abs_decl
  471. { push_type (tp_reference); $$ = $2; }
  472. | direct_abs_decl
  473. ;
  474. direct_abs_decl: '(' abs_decl ')'
  475. { $$ = $2; }
  476. | direct_abs_decl func_mod
  477. { push_type (tp_function); }
  478. | func_mod
  479. { push_type (tp_function); }
  480. ;
  481. func_mod: '(' ')'
  482. { $$ = 0; }
  483. | '(' nonempty_typelist ')'
  484. { free ((PTR)$2); $$ = 0; }
  485. ;
  486. typebase /* Implements (approximately): (type-qualifier)* type-specifier */
  487. : TYPENAME
  488. { $$ = $1.type; }
  489. | INT_KEYWORD
  490. { $$ = builtin_type_f_integer; }
  491. | INT_S2_KEYWORD
  492. { $$ = builtin_type_f_integer_s2; }
  493. | CHARACTER
  494. { $$ = builtin_type_f_character; }
  495. | LOGICAL_KEYWORD
  496. { $$ = builtin_type_f_logical;}
  497. | LOGICAL_S2_KEYWORD
  498. { $$ = builtin_type_f_logical_s2;}
  499. | LOGICAL_S1_KEYWORD
  500. { $$ = builtin_type_f_logical_s1;}
  501. | REAL_KEYWORD
  502. { $$ = builtin_type_f_real;}
  503. | REAL_S8_KEYWORD
  504. { $$ = builtin_type_f_real_s8;}
  505. | REAL_S16_KEYWORD
  506. { $$ = builtin_type_f_real_s16;}
  507. | COMPLEX_S8_KEYWORD
  508. { $$ = builtin_type_f_complex_s8;}
  509. | COMPLEX_S16_KEYWORD
  510. { $$ = builtin_type_f_complex_s16;}
  511. | COMPLEX_S32_KEYWORD
  512. { $$ = builtin_type_f_complex_s32;}
  513. ;
  514. typename: TYPENAME
  515. ;
  516. nonempty_typelist
  517. : type
  518. { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
  519. $&lt;ivec&gt;$[0] = 1; /* Number of types in vector */
  520. $$[1] = $1;
  521. }
  522. | nonempty_typelist ',' type
  523. { int len = sizeof (struct type *) * (++($&lt;ivec&gt;1[0]) + 1);
  524. $$ = (struct type **) realloc ((char *) $1, len);
  525. $$[$&lt;ivec&gt;$[0]] = $3;
  526. }
  527. ;
  528. name : NAME
  529. { $$ = $1.stoken; }
  530. | TYPENAME
  531. { $$ = $1.stoken; }
  532. | NAME_OR_INT
  533. { $$ = $1.stoken; }
  534. ;
  535. name_not_typename : NAME
  536. /* These would be useful if name_not_typename was useful, but it is just
  537. a fake for &quot;variable&quot;, so these cause reduce/reduce conflicts because
  538. the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  539. =exp) or just an exp. If name_not_typename was ever used in an lvalue
  540. context where only a name could occur, this might be useful.
  541. | NAME_OR_INT
  542. */
  543. ;
  544. %%
  545. /* Take care of parsing a number (anything that starts with a digit).
  546. Set yylval and return the token type; update lexptr.
  547. LEN is the number of characters in it. */
  548. /*** Needs some error checking for the float case ***/
  549. static int
  550. parse_number (p, len, parsed_float, putithere)
  551. register char *p;
  552. register int len;
  553. int parsed_float;
  554. YYSTYPE *putithere;
  555. {
  556. register LONGEST n = 0;
  557. register LONGEST prevn = 0;
  558. register int c;
  559. register int base = input_radix;
  560. int unsigned_p = 0;
  561. int long_p = 0;
  562. ULONGEST high_bit;
  563. struct type *signed_type;
  564. struct type *unsigned_type;
  565. if (parsed_float)
  566. {
  567. /* It's a float since it contains a point or an exponent. */
  568. /* [dD] is not understood as an exponent by atof, change it to 'e'. */
  569. char *tmp, *tmp2;
  570. tmp = xstrdup (p);
  571. for (tmp2 = tmp; *tmp2; ++tmp2)
  572. if (*tmp2 == 'd' || *tmp2 == 'D')
  573. *tmp2 = 'e';
  574. putithere-&gt;dval = atof (tmp);
  575. free (tmp);
  576. return FLOAT;
  577. }
  578. /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
  579. if (p[0] == '0')
  580. switch (p[1])
  581. {
  582. case 'x':
  583. case 'X':
  584. if (len &gt;= 3)
  585. {
  586. p += 2;
  587. base = 16;
  588. len -= 2;
  589. }
  590. break;
  591. case 't':
  592. case 'T':
  593. case 'd':
  594. case 'D':
  595. if (len &gt;= 3)
  596. {
  597. p += 2;
  598. base = 10;
  599. len -= 2;
  600. }
  601. break;
  602. default:
  603. base = 8;
  604. break;
  605. }
  606. while (len-- &gt; 0)
  607. {
  608. c = *p++;
  609. if (isupper (c))
  610. c = tolower (c);
  611. if (len == 0 &amp;&amp; c == 'l')
  612. long_p = 1;
  613. else if (len == 0 &amp;&amp; c == 'u')
  614. unsigned_p = 1;
  615. else
  616. {
  617. int i;
  618. if (c &gt;= '0' &amp;&amp; c &lt;= '9')
  619. i = c - '0';
  620. else if (c &gt;= 'a' &amp;&amp; c &lt;= 'f')
  621. i = c - 'a' + 10;
  622. else
  623. return ERROR; /* Char not a digit */
  624. if (i &gt;= base)
  625. return ERROR; /* Invalid digit in this base */
  626. n *= base;
  627. n += i;
  628. }
  629. /* Portably test for overflow (only works for nonzero values, so make
  630. a second check for zero). */
  631. if ((prevn &gt;= n) &amp;&amp; n != 0)
  632. unsigned_p=1; /* Try something unsigned */
  633. /* If range checking enabled, portably test for unsigned overflow. */
  634. if (RANGE_CHECK &amp;&amp; n != 0)
  635. {
  636. if ((unsigned_p &amp;&amp; (unsigned)prevn &gt;= (unsigned)n))
  637. range_error(&quot;Overflow on numeric constant.&quot;);
  638. }
  639. prevn = n;
  640. }
  641. /* If the number is too big to be an int, or it's got an l suffix
  642. then it's a long. Work out if this has to be a long by
  643. shifting right and and seeing if anything remains, and the
  644. target int size is different to the target long size.
  645. In the expression below, we could have tested
  646. (n &gt;&gt; TARGET_INT_BIT)
  647. to see if it was zero,
  648. but too many compilers warn about that, when ints and longs
  649. are the same size. So we shift it twice, with fewer bits
  650. each time, for the same result. */
  651. if ((TARGET_INT_BIT != TARGET_LONG_BIT
  652. &amp;&amp; ((n &gt;&gt; 2) &gt;&gt; (TARGET_INT_BIT-2))) /* Avoid shift warning */
  653. || long_p)
  654. {
  655. high_bit = ((ULONGEST)1) &lt;&lt; (TARGET_LONG_BIT-1);
  656. unsigned_type = builtin_type_unsigned_long;
  657. signed_type = builtin_type_long;
  658. }
  659. else
  660. {
  661. high_bit = ((ULONGEST)1) &lt;&lt; (TARGET_INT_BIT-1);
  662. unsigned_type = builtin_type_unsigned_int;
  663. signed_type = builtin_type_int;
  664. }
  665. putithere-&gt;typed_val.val = n;
  666. /* If the high bit of the worked out type is set then this number
  667. has to be unsigned. */
  668. if (unsigned_p || (n &amp; high_bit))
  669. putithere-&gt;typed_val.type = unsigned_type;
  670. else
  671. putithere-&gt;typed_val.type = signed_type;
  672. return INT;
  673. }
  674. struct token
  675. {
  676. char *operator;
  677. int token;
  678. enum exp_opcode opcode;
  679. };
  680. static const struct token dot_ops[] =
  681. {
  682. { &quot;.and.&quot;, BOOL_AND, BINOP_END },
  683. { &quot;.AND.&quot;, BOOL_AND, BINOP_END },
  684. { &quot;.or.&quot;, BOOL_OR, BINOP_END },
  685. { &quot;.OR.&quot;, BOOL_OR, BINOP_END },
  686. { &quot;.not.&quot;, BOOL_NOT, BINOP_END },
  687. { &quot;.NOT.&quot;, BOOL_NOT, BINOP_END },
  688. { &quot;.eq.&quot;, EQUAL, BINOP_END },
  689. { &quot;.EQ.&quot;, EQUAL, BINOP_END },
  690. { &quot;.eqv.&quot;, EQUAL, BINOP_END },
  691. { &quot;.NEQV.&quot;, NOTEQUAL, BINOP_END },
  692. { &quot;.neqv.&quot;, NOTEQUAL, BINOP_END },
  693. { &quot;.EQV.&quot;, EQUAL, BINOP_END },
  694. { &quot;.ne.&quot;, NOTEQUAL, BINOP_END },
  695. { &quot;.NE.&quot;, NOTEQUAL, BINOP_END },
  696. { &quot;.le.&quot;, LEQ, BINOP_END },
  697. { &quot;.LE.&quot;, LEQ, BINOP_END },
  698. { &quot;.ge.&quot;, GEQ, BINOP_END },
  699. { &quot;.GE.&quot;, GEQ, BINOP_END },
  700. { &quot;.gt.&quot;, GREATERTHAN, BINOP_END },
  701. { &quot;.GT.&quot;, GREATERTHAN, BINOP_END },
  702. { &quot;.lt.&quot;, LESSTHAN, BINOP_END },
  703. { &quot;.LT.&quot;, LESSTHAN, BINOP_END },
  704. { NULL, 0, 0 }
  705. };
  706. struct f77_boolean_val
  707. {
  708. char *name;
  709. int value;
  710. };
  711. static const struct f77_boolean_val boolean_values[] =
  712. {
  713. { &quot;.true.&quot;, 1 },
  714. { &quot;.TRUE.&quot;, 1 },
  715. { &quot;.false.&quot;, 0 },
  716. { &quot;.FALSE.&quot;, 0 },
  717. { NULL, 0 }
  718. };
  719. static const struct token f77_keywords[] =
  720. {
  721. { &quot;complex_16&quot;, COMPLEX_S16_KEYWORD, BINOP_END },
  722. { &quot;complex_32&quot;, COMPLEX_S32_KEYWORD, BINOP_END },
  723. { &quot;character&quot;, CHARACTER, BINOP_END },
  724. { &quot;integer_2&quot;, INT_S2_KEYWORD, BINOP_END },
  725. { &quot;logical_1&quot;, LOGICAL_S1_KEYWORD, BINOP_END },
  726. { &quot;logical_2&quot;, LOGICAL_S2_KEYWORD, BINOP_END },
  727. { &quot;complex_8&quot;, COMPLEX_S8_KEYWORD, BINOP_END },
  728. { &quot;integer&quot;, INT_KEYWORD, BINOP_END },
  729. { &quot;logical&quot;, LOGICAL_KEYWORD, BINOP_END },
  730. { &quot;real_16&quot;, REAL_S16_KEYWORD, BINOP_END },
  731. { &quot;complex&quot;, COMPLEX_S8_KEYWORD, BINOP_END },
  732. { &quot;sizeof&quot;, SIZEOF, BINOP_END },
  733. { &quot;real_8&quot;, REAL_S8_KEYWORD, BINOP_END },
  734. { &quot;real&quot;, REAL_KEYWORD, BINOP_END },
  735. { NULL, 0, 0 }
  736. };
  737. /* Implementation of a dynamically expandable buffer for processing input
  738. characters acquired through lexptr and building a value to return in
  739. yylval. Ripped off from ch-exp.y */
  740. static char *tempbuf; /* Current buffer contents */
  741. static int tempbufsize; /* Size of allocated buffer */
  742. static int tempbufindex; /* Current index into buffer */
  743. #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
  744. #define CHECKBUF(size) \
  745. do { \
  746. if (tempbufindex + (size) &gt;= tempbufsize) \
  747. { \
  748. growbuf_by_size (size); \
  749. } \
  750. } while (0);
  751. /* Grow the static temp buffer if necessary, including allocating the first one
  752. on demand. */
  753. static void
  754. growbuf_by_size (count)
  755. int count;
  756. {
  757. int growby;
  758. growby = max (count, GROWBY_MIN_SIZE);
  759. tempbufsize += growby;
  760. if (tempbuf == NULL)
  761. tempbuf = (char *) malloc (tempbufsize);
  762. else
  763. tempbuf = (char *) realloc (tempbuf, tempbufsize);
  764. }
  765. /* Blatantly ripped off from ch-exp.y. This routine recognizes F77
  766. string-literals.
  767. Recognize a string literal. A string literal is a nonzero sequence
  768. of characters enclosed in matching single quotes, except that
  769. a single character inside single quotes is a character literal, which
  770. we reject as a string literal. To embed the terminator character inside
  771. a string, it is simply doubled (I.E. 'this''is''one''string') */
  772. static int
  773. match_string_literal ()
  774. {
  775. char *tokptr = lexptr;
  776. for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
  777. {
  778. CHECKBUF (1);
  779. if (*tokptr == *lexptr)
  780. {
  781. if (*(tokptr + 1) == *lexptr)
  782. tokptr++;
  783. else
  784. break;
  785. }
  786. tempbuf[tempbufindex++] = *tokptr;
  787. }
  788. if (*tokptr == '\0' /* no terminator */
  789. || tempbufindex == 0) /* no string */
  790. return 0;
  791. else
  792. {
  793. tempbuf[tempbufindex] = '\0';
  794. yylval.sval.ptr = tempbuf;
  795. yylval.sval.length = tempbufindex;
  796. lexptr = ++tokptr;
  797. return STRING_LITERAL;
  798. }
  799. }
  800. /* Read one token, getting characters through lexptr. */
  801. static int
  802. yylex ()
  803. {
  804. int c;
  805. int namelen;
  806. unsigned int i,token;
  807. char *tokstart;
  808. retry:
  809. tokstart = lexptr;
  810. /* First of all, let us make sure we are not dealing with the
  811. special tokens .true. and .false. which evaluate to 1 and 0. */
  812. if (*lexptr == '.')
  813. {
  814. for (i = 0; boolean_values[i].name != NULL; i++)
  815. {
  816. if STREQN (tokstart, boolean_values[i].name,
  817. strlen (boolean_values[i].name))
  818. {
  819. lexptr += strlen (boolean_values[i].name);
  820. yylval.lval = boolean_values[i].value;
  821. return BOOLEAN_LITERAL;
  822. }
  823. }
  824. }
  825. /* See if it is a special .foo. operator */
  826. for (i = 0; dot_ops[i].operator != NULL; i++)
  827. if (STREQN (tokstart, dot_ops[i].operator, strlen (dot_ops[i].operator)))
  828. {
  829. lexptr += strlen (dot_ops[i].operator);
  830. yylval.opcode = dot_ops[i].opcode;
  831. return dot_ops[i].token;
  832. }
  833. switch (c = *tokstart)
  834. {
  835. case 0:
  836. return 0;
  837. case ' ':
  838. case '\t':
  839. case '\n':
  840. lexptr++;
  841. goto retry;
  842. case '\'':
  843. token = match_string_literal ();
  844. if (token != 0)
  845. return (token);
  846. break;
  847. case '(':
  848. paren_depth++;
  849. lexptr++;
  850. return c;
  851. case ')':
  852. if (paren_depth == 0)
  853. return 0;
  854. paren_depth--;
  855. lexptr++;
  856. return c;
  857. case ',':
  858. if (comma_terminates &amp;&amp; paren_depth == 0)
  859. return 0;
  860. lexptr++;
  861. return c;
  862. case '.':
  863. /* Might be a floating point number. */
  864. if (lexptr[1] &lt; '0' || lexptr[1] &gt; '9')
  865. goto symbol; /* Nope, must be a symbol. */
  866. /* FALL THRU into number case. */
  867. case '0':
  868. case '1':
  869. case '2':
  870. case '3':
  871. case '4':
  872. case '5':
  873. case '6':
  874. case '7':
  875. case '8':
  876. case '9':
  877. {
  878. /* It's a number. */
  879. int got_dot = 0, got_e = 0, got_d = 0, toktype;
  880. register char *p = tokstart;
  881. int hex = input_radix &gt; 10;
  882. if (c == '0' &amp;&amp; (p[1] == 'x' || p[1] == 'X'))
  883. {
  884. p += 2;
  885. hex = 1;
  886. }
  887. else if (c == '0' &amp;&amp; (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
  888. {
  889. p += 2;
  890. hex = 0;
  891. }
  892. for (;; ++p)
  893. {
  894. if (!hex &amp;&amp; !got_e &amp;&amp; (*p == 'e' || *p == 'E'))
  895. got_dot = got_e = 1;
  896. else if (!hex &amp;&amp; !got_d &amp;&amp; (*p == 'd' || *p == 'D'))
  897. got_dot = got_d = 1;
  898. else if (!hex &amp;&amp; !got_dot &amp;&amp; *p == '.')
  899. got_dot = 1;
  900. else if (((got_e &amp;&amp; (p[-1] == 'e' || p[-1] == 'E'))
  901. || (got_d &amp;&amp; (p[-1] == 'd' || p[-1] == 'D')))
  902. &amp;&amp; (*p == '-' || *p == '+'))
  903. /* This is the sign of the exponent, not the end of the
  904. number. */
  905. continue;
  906. /* We will take any letters or digits. parse_number will
  907. complain if past the radix, or if L or U are not final. */
  908. else if ((*p &lt; '0' || *p &gt; '9')
  909. &amp;&amp; ((*p &lt; 'a' || *p &gt; 'z')
  910. &amp;&amp; (*p &lt; 'A' || *p &gt; 'Z')))
  911. break;
  912. }
  913. toktype = parse_number (tokstart, p - tokstart, got_dot|got_e|got_d,
  914. &amp;yylval);
  915. if (toktype == ERROR)
  916. {
  917. char *err_copy = (char *) alloca (p - tokstart + 1);
  918. memcpy (err_copy, tokstart, p - tokstart);
  919. err_copy[p - tokstart] = 0;
  920. error (&quot;Invalid number \&quot;%s\&quot;.&quot;, err_copy);
  921. }
  922. lexptr = p;
  923. return toktype;
  924. }
  925. case '+':
  926. case '-':
  927. case '*':
  928. case '/':
  929. case '%':
  930. case '|':
  931. case '&amp;':
  932. case '^':
  933. case '~':
  934. case '!':
  935. case '@':
  936. case '&lt;':
  937. case '&gt;':
  938. case '[':
  939. case ']':
  940. case '?':
  941. case ':':
  942. case '=':
  943. case '{':
  944. case '}':
  945. symbol:
  946. lexptr++;
  947. return c;
  948. }
  949. if (!(c == '_' || c == '$'
  950. || (c &gt;= 'a' &amp;&amp; c &lt;= 'z') || (c &gt;= 'A' &amp;&amp; c &lt;= 'Z')))
  951. /* We must have come across a bad character (e.g. ';'). */
  952. error (&quot;Invalid character '%c' in expression.&quot;, c);
  953. namelen = 0;
  954. for (c = tokstart[namelen];
  955. (c == '_' || c == '$' || (c &gt;= '0' &amp;&amp; c &lt;= '9')
  956. || (c &gt;= 'a' &amp;&amp; c &lt;= 'z') || (c &gt;= 'A' &amp;&amp; c &lt;= 'Z'));
  957. c = tokstart[++namelen]);
  958. /* The token &quot;if&quot; terminates the expression and is NOT
  959. removed from the input stream. */
  960. if (namelen == 2 &amp;&amp; tokstart[0] == 'i' &amp;&amp; tokstart[1] == 'f')
  961. return 0;
  962. lexptr += namelen;
  963. /* Catch specific keywords. */
  964. for (i = 0; f77_keywords[i].operator != NULL; i++)
  965. if (STREQN(tokstart, f77_keywords[i].operator,
  966. strlen(f77_keywords[i].operator)))
  967. {
  968. /* lexptr += strlen(f77_keywords[i].operator); */
  969. yylval.opcode = f77_keywords[i].opcode;
  970. return f77_keywords[i].token;
  971. }
  972. yylval.sval.ptr = tokstart;
  973. yylval.sval.length = namelen;
  974. if (*tokstart == '$')
  975. {
  976. write_dollar_variable (yylval.sval);
  977. return VARIABLE;
  978. }
  979. /* Use token-type TYPENAME for symbols that happen to be defined
  980. currently as names of types; NAME for other symbols.
  981. The caller is not constrained to care about the distinction. */
  982. {
  983. char *tmp = copy_name (yylval.sval);
  984. struct symbol *sym;
  985. int is_a_field_of_this = 0;
  986. int hextype;
  987. sym = lookup_symbol (tmp, expression_context_block,
  988. VAR_NAMESPACE,
  989. current_language-&gt;la_language == language_cplus
  990. ? &amp;is_a_field_of_this : NULL,
  991. NULL);
  992. if (sym &amp;&amp; SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  993. {
  994. yylval.tsym.type = SYMBOL_TYPE (sym);
  995. return TYPENAME;
  996. }
  997. if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
  998. return TYPENAME;
  999. /* Input names that aren't symbols but ARE valid hex numbers,
  1000. when the input radix permits them, can be names or numbers
  1001. depending on the parse. Note we support radixes &gt; 16 here. */
  1002. if (!sym
  1003. &amp;&amp; ((tokstart[0] &gt;= 'a' &amp;&amp; tokstart[0] &lt; 'a' + input_radix - 10)
  1004. || (tokstart[0] &gt;= 'A' &amp;&amp; tokstart[0] &lt; 'A' + input_radix - 10)))
  1005. {
  1006. YYSTYPE newlval; /* Its value is ignored. */
  1007. hextype = parse_number (tokstart, namelen, 0, &amp;newlval);
  1008. if (hextype == INT)
  1009. {
  1010. yylval.ssym.sym = sym;
  1011. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1012. return NAME_OR_INT;
  1013. }
  1014. }
  1015. /* Any other kind of symbol */
  1016. yylval.ssym.sym = sym;
  1017. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1018. return NAME;
  1019. }
  1020. }
  1021. void
  1022. yyerror (msg)
  1023. char *msg;
  1024. {
  1025. error (&quot;A %s in expression, near `%s'.&quot;, (msg ? msg : &quot;error&quot;), lexptr);
  1026. }
  1027. </pre>
  1028. <hr />
  1029. </body></html>