PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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