PageRenderTime 956ms CodeModel.GetById 47ms RepoModel.GetById 16ms app.codeStats 0ms

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

#
Happy | 1121 lines | 944 code | 177 blank | 0 comment | 0 complexity | 617bf05c1cddcab7752b147ee000f4f8 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>m2-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">m2-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 grammar for Modula-2 expressions, for GDB.
  24. Copyright 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999,
  25. 2000
  26. Free Software Foundation, Inc.
  27. Generated from expread.y (now c-exp.y) and contributed by the Department
  28. of Computer Science at the State University of New York at Buffalo, 1991.
  29. This file is part of GDB.
  30. This program is free software; you can redistribute it and/or modify
  31. it under the terms of the GNU General Public License as published by
  32. the Free Software Foundation; either version 2 of the License, or
  33. (at your option) any later version.
  34. This program is distributed in the hope that it will be useful,
  35. but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. GNU General Public License for more details.
  38. You should have received a copy of the GNU General Public License
  39. along with this program; if not, write to the Free Software
  40. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  41. /* Parse a Modula-2 expression from text in a string,
  42. and return the result as a struct expression pointer.
  43. That structure contains arithmetic operations in reverse polish,
  44. with constants represented by operations that are followed by special data.
  45. See expression.h for the details of the format.
  46. What is important here is that it can be built up sequentially
  47. during the process of parsing; the lower levels of the tree always
  48. come first in the result.
  49. Note that malloc's and realloc's in this file are transformed to
  50. xmalloc and xrealloc respectively by the same sed command in the
  51. makefile that remaps any other malloc/realloc inserted by the parser
  52. generator. Doing this with #defines and trying to control the interaction
  53. with include files (&lt;malloc.h&gt; and &lt;stdlib.h&gt; for example) just became
  54. too messy, particularly when such includes can be inserted at random
  55. times by the parser generator. */
  56. %{
  57. #define NOTUI 1
  58. #include &quot;defs.h&quot;
  59. #include &quot;gdb_string.h&quot;
  60. #include &quot;expression.h&quot;
  61. #include &quot;language.h&quot;
  62. #include &quot;value.h&quot;
  63. #include &quot;parser-defs.h&quot;
  64. #include &quot;m2-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. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  69. as well as gratuitiously global symbol names, so we can have multiple
  70. yacc generated parsers in gdb. Note that these are only the variables
  71. produced by yacc. If other parser generators (bison, byacc, etc) produce
  72. additional global names that conflict at link time, then those parser
  73. generators need to be fixed instead of adding those names to this list. */
  74. #define yymaxdepth m2_maxdepth
  75. #define yyparse m2_parse
  76. #define yylex m2_lex
  77. #define yyerror m2_error
  78. #define yylval m2_lval
  79. #define yychar m2_char
  80. #define yydebug m2_debug
  81. #define yypact m2_pact
  82. #define yyr1 m2_r1
  83. #define yyr2 m2_r2
  84. #define yydef m2_def
  85. #define yychk m2_chk
  86. #define yypgo m2_pgo
  87. #define yyact m2_act
  88. #define yyexca m2_exca
  89. #define yyerrflag m2_errflag
  90. #define yynerrs m2_nerrs
  91. #define yyps m2_ps
  92. #define yypv m2_pv
  93. #define yys m2_s
  94. #define yy_yys m2_yys
  95. #define yystate m2_state
  96. #define yytmp m2_tmp
  97. #define yyv m2_v
  98. #define yy_yyv m2_yyv
  99. #define yyval m2_val
  100. #define yylloc m2_lloc
  101. #define yyreds m2_reds /* With YYDEBUG defined */
  102. #define yytoks m2_toks /* With YYDEBUG defined */
  103. #define yylhs m2_yylhs
  104. #define yylen m2_yylen
  105. #define yydefred m2_yydefred
  106. #define yydgoto m2_yydgoto
  107. #define yysindex m2_yysindex
  108. #define yyrindex m2_yyrindex
  109. #define yygindex m2_yygindex
  110. #define yytable m2_yytable
  111. #define yycheck m2_yycheck
  112. #ifndef YYDEBUG
  113. #define YYDEBUG 0 /* Default to no yydebug support */
  114. #endif
  115. int yyparse (void);
  116. static int yylex (void);
  117. void yyerror (char *);
  118. #if 0
  119. static char *make_qualname (char *, char *);
  120. #endif
  121. static int parse_number (int);
  122. /* The sign of the number being parsed. */
  123. static int number_sign = 1;
  124. /* The block that the module specified by the qualifer on an identifer is
  125. contained in, */
  126. #if 0
  127. static struct block *modblock=0;
  128. #endif
  129. %}
  130. /* Although the yacc &quot;value&quot; of an expression is not used,
  131. since the result is stored in the structure being created,
  132. other node types do have values. */
  133. %union
  134. {
  135. LONGEST lval;
  136. ULONGEST ulval;
  137. DOUBLEST dval;
  138. struct symbol *sym;
  139. struct type *tval;
  140. struct stoken sval;
  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. %type &lt;voidval&gt; exp type_exp start set
  149. %type &lt;voidval&gt; variable
  150. %type &lt;tval&gt; type
  151. %type &lt;bval&gt; block
  152. %type &lt;sym&gt; fblock
  153. %token &lt;lval&gt; INT HEX ERROR
  154. %token &lt;ulval&gt; UINT M2_TRUE M2_FALSE CHAR
  155. %token &lt;dval&gt; FLOAT
  156. /* Both NAME and TYPENAME tokens represent symbols in the input,
  157. and both convey their data as strings.
  158. But a TYPENAME is a string that happens to be defined as a typedef
  159. or builtin type name (such as int or char)
  160. and a NAME is any other symbol.
  161. Contexts where this distinction is not important can use the
  162. nonterminal &quot;name&quot;, which matches either NAME or TYPENAME. */
  163. %token &lt;sval&gt; STRING
  164. %token &lt;sval&gt; NAME BLOCKNAME IDENT VARNAME
  165. %token &lt;sval&gt; TYPENAME
  166. %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
  167. %token INC DEC INCL EXCL
  168. /* The GDB scope operator */
  169. %token COLONCOLON
  170. %token &lt;voidval&gt; INTERNAL_VAR
  171. /* M2 tokens */
  172. %left ','
  173. %left ABOVE_COMMA
  174. %nonassoc ASSIGN
  175. %left '&lt;' '&gt;' LEQ GEQ '=' NOTEQUAL '#' IN
  176. %left OROR
  177. %left LOGICAL_AND '&amp;'
  178. %left '@'
  179. %left '+' '-'
  180. %left '*' '/' DIV MOD
  181. %right UNARY
  182. %right '^' DOT '[' '('
  183. %right NOT '~'
  184. %left COLONCOLON QID
  185. /* This is not an actual token ; it is used for precedence.
  186. %right QID
  187. */
  188. %%
  189. start : exp
  190. | type_exp
  191. ;
  192. type_exp: type
  193. { write_exp_elt_opcode(OP_TYPE);
  194. write_exp_elt_type($1);
  195. write_exp_elt_opcode(OP_TYPE);
  196. }
  197. ;
  198. /* Expressions */
  199. exp : exp '^' %prec UNARY
  200. { write_exp_elt_opcode (UNOP_IND); }
  201. exp : '-'
  202. { number_sign = -1; }
  203. exp %prec UNARY
  204. { number_sign = 1;
  205. write_exp_elt_opcode (UNOP_NEG); }
  206. ;
  207. exp : '+' exp %prec UNARY
  208. { write_exp_elt_opcode(UNOP_PLUS); }
  209. ;
  210. exp : not_exp exp %prec UNARY
  211. { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
  212. ;
  213. not_exp : NOT
  214. | '~'
  215. ;
  216. exp : CAP '(' exp ')'
  217. { write_exp_elt_opcode (UNOP_CAP); }
  218. ;
  219. exp : ORD '(' exp ')'
  220. { write_exp_elt_opcode (UNOP_ORD); }
  221. ;
  222. exp : ABS '(' exp ')'
  223. { write_exp_elt_opcode (UNOP_ABS); }
  224. ;
  225. exp : HIGH '(' exp ')'
  226. { write_exp_elt_opcode (UNOP_HIGH); }
  227. ;
  228. exp : MIN_FUNC '(' type ')'
  229. { write_exp_elt_opcode (UNOP_MIN);
  230. write_exp_elt_type ($3);
  231. write_exp_elt_opcode (UNOP_MIN); }
  232. ;
  233. exp : MAX_FUNC '(' type ')'
  234. { write_exp_elt_opcode (UNOP_MAX);
  235. write_exp_elt_type ($3);
  236. write_exp_elt_opcode (UNOP_MIN); }
  237. ;
  238. exp : FLOAT_FUNC '(' exp ')'
  239. { write_exp_elt_opcode (UNOP_FLOAT); }
  240. ;
  241. exp : VAL '(' type ',' exp ')'
  242. { write_exp_elt_opcode (BINOP_VAL);
  243. write_exp_elt_type ($3);
  244. write_exp_elt_opcode (BINOP_VAL); }
  245. ;
  246. exp : CHR '(' exp ')'
  247. { write_exp_elt_opcode (UNOP_CHR); }
  248. ;
  249. exp : ODD '(' exp ')'
  250. { write_exp_elt_opcode (UNOP_ODD); }
  251. ;
  252. exp : TRUNC '(' exp ')'
  253. { write_exp_elt_opcode (UNOP_TRUNC); }
  254. ;
  255. exp : SIZE exp %prec UNARY
  256. { write_exp_elt_opcode (UNOP_SIZEOF); }
  257. ;
  258. exp : INC '(' exp ')'
  259. { write_exp_elt_opcode(UNOP_PREINCREMENT); }
  260. ;
  261. exp : INC '(' exp ',' exp ')'
  262. { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
  263. write_exp_elt_opcode(BINOP_ADD);
  264. write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
  265. ;
  266. exp : DEC '(' exp ')'
  267. { write_exp_elt_opcode(UNOP_PREDECREMENT);}
  268. ;
  269. exp : DEC '(' exp ',' exp ')'
  270. { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
  271. write_exp_elt_opcode(BINOP_SUB);
  272. write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
  273. ;
  274. exp : exp DOT NAME
  275. { write_exp_elt_opcode (STRUCTOP_STRUCT);
  276. write_exp_string ($3);
  277. write_exp_elt_opcode (STRUCTOP_STRUCT); }
  278. ;
  279. exp : set
  280. ;
  281. exp : exp IN set
  282. { error(&quot;Sets are not implemented.&quot;);}
  283. ;
  284. exp : INCL '(' exp ',' exp ')'
  285. { error(&quot;Sets are not implemented.&quot;);}
  286. ;
  287. exp : EXCL '(' exp ',' exp ')'
  288. { error(&quot;Sets are not implemented.&quot;);}
  289. set : '{' arglist '}'
  290. { error(&quot;Sets are not implemented.&quot;);}
  291. | type '{' arglist '}'
  292. { error(&quot;Sets are not implemented.&quot;);}
  293. ;
  294. /* Modula-2 array subscript notation [a,b,c...] */
  295. exp : exp '['
  296. /* This function just saves the number of arguments
  297. that follow in the list. It is *not* specific to
  298. function types */
  299. { start_arglist(); }
  300. non_empty_arglist ']' %prec DOT
  301. { write_exp_elt_opcode (MULTI_SUBSCRIPT);
  302. write_exp_elt_longcst ((LONGEST) end_arglist());
  303. write_exp_elt_opcode (MULTI_SUBSCRIPT); }
  304. ;
  305. exp : exp '('
  306. /* This is to save the value of arglist_len
  307. being accumulated by an outer function call. */
  308. { start_arglist (); }
  309. arglist ')' %prec DOT
  310. { write_exp_elt_opcode (OP_FUNCALL);
  311. write_exp_elt_longcst ((LONGEST) end_arglist ());
  312. write_exp_elt_opcode (OP_FUNCALL); }
  313. ;
  314. arglist :
  315. ;
  316. arglist : exp
  317. { arglist_len = 1; }
  318. ;
  319. arglist : arglist ',' exp %prec ABOVE_COMMA
  320. { arglist_len++; }
  321. ;
  322. non_empty_arglist
  323. : exp
  324. { arglist_len = 1; }
  325. ;
  326. non_empty_arglist
  327. : non_empty_arglist ',' exp %prec ABOVE_COMMA
  328. { arglist_len++; }
  329. ;
  330. /* GDB construct */
  331. exp : '{' type '}' exp %prec UNARY
  332. { write_exp_elt_opcode (UNOP_MEMVAL);
  333. write_exp_elt_type ($2);
  334. write_exp_elt_opcode (UNOP_MEMVAL); }
  335. ;
  336. exp : type '(' exp ')' %prec UNARY
  337. { write_exp_elt_opcode (UNOP_CAST);
  338. write_exp_elt_type ($1);
  339. write_exp_elt_opcode (UNOP_CAST); }
  340. ;
  341. exp : '(' exp ')'
  342. { }
  343. ;
  344. /* Binary operators in order of decreasing precedence. Note that some
  345. of these operators are overloaded! (ie. sets) */
  346. /* GDB construct */
  347. exp : exp '@' exp
  348. { write_exp_elt_opcode (BINOP_REPEAT); }
  349. ;
  350. exp : exp '*' exp
  351. { write_exp_elt_opcode (BINOP_MUL); }
  352. ;
  353. exp : exp '/' exp
  354. { write_exp_elt_opcode (BINOP_DIV); }
  355. ;
  356. exp : exp DIV exp
  357. { write_exp_elt_opcode (BINOP_INTDIV); }
  358. ;
  359. exp : exp MOD exp
  360. { write_exp_elt_opcode (BINOP_REM); }
  361. ;
  362. exp : exp '+' exp
  363. { write_exp_elt_opcode (BINOP_ADD); }
  364. ;
  365. exp : exp '-' exp
  366. { write_exp_elt_opcode (BINOP_SUB); }
  367. ;
  368. exp : exp '=' exp
  369. { write_exp_elt_opcode (BINOP_EQUAL); }
  370. ;
  371. exp : exp NOTEQUAL exp
  372. { write_exp_elt_opcode (BINOP_NOTEQUAL); }
  373. | exp '#' exp
  374. { write_exp_elt_opcode (BINOP_NOTEQUAL); }
  375. ;
  376. exp : exp LEQ exp
  377. { write_exp_elt_opcode (BINOP_LEQ); }
  378. ;
  379. exp : exp GEQ exp
  380. { write_exp_elt_opcode (BINOP_GEQ); }
  381. ;
  382. exp : exp '&lt;' exp
  383. { write_exp_elt_opcode (BINOP_LESS); }
  384. ;
  385. exp : exp '&gt;' exp
  386. { write_exp_elt_opcode (BINOP_GTR); }
  387. ;
  388. exp : exp LOGICAL_AND exp
  389. { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
  390. ;
  391. exp : exp OROR exp
  392. { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
  393. ;
  394. exp : exp ASSIGN exp
  395. { write_exp_elt_opcode (BINOP_ASSIGN); }
  396. ;
  397. /* Constants */
  398. exp : M2_TRUE
  399. { write_exp_elt_opcode (OP_BOOL);
  400. write_exp_elt_longcst ((LONGEST) $1);
  401. write_exp_elt_opcode (OP_BOOL); }
  402. ;
  403. exp : M2_FALSE
  404. { write_exp_elt_opcode (OP_BOOL);
  405. write_exp_elt_longcst ((LONGEST) $1);
  406. write_exp_elt_opcode (OP_BOOL); }
  407. ;
  408. exp : INT
  409. { write_exp_elt_opcode (OP_LONG);
  410. write_exp_elt_type (builtin_type_m2_int);
  411. write_exp_elt_longcst ((LONGEST) $1);
  412. write_exp_elt_opcode (OP_LONG); }
  413. ;
  414. exp : UINT
  415. {
  416. write_exp_elt_opcode (OP_LONG);
  417. write_exp_elt_type (builtin_type_m2_card);
  418. write_exp_elt_longcst ((LONGEST) $1);
  419. write_exp_elt_opcode (OP_LONG);
  420. }
  421. ;
  422. exp : CHAR
  423. { write_exp_elt_opcode (OP_LONG);
  424. write_exp_elt_type (builtin_type_m2_char);
  425. write_exp_elt_longcst ((LONGEST) $1);
  426. write_exp_elt_opcode (OP_LONG); }
  427. ;
  428. exp : FLOAT
  429. { write_exp_elt_opcode (OP_DOUBLE);
  430. write_exp_elt_type (builtin_type_m2_real);
  431. write_exp_elt_dblcst ($1);
  432. write_exp_elt_opcode (OP_DOUBLE); }
  433. ;
  434. exp : variable
  435. ;
  436. exp : SIZE '(' type ')' %prec UNARY
  437. { write_exp_elt_opcode (OP_LONG);
  438. write_exp_elt_type (builtin_type_int);
  439. write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
  440. write_exp_elt_opcode (OP_LONG); }
  441. ;
  442. exp : STRING
  443. { write_exp_elt_opcode (OP_M2_STRING);
  444. write_exp_string ($1);
  445. write_exp_elt_opcode (OP_M2_STRING); }
  446. ;
  447. /* This will be used for extensions later. Like adding modules. */
  448. block : fblock
  449. { $$ = SYMBOL_BLOCK_VALUE($1); }
  450. ;
  451. fblock : BLOCKNAME
  452. { struct symbol *sym
  453. = lookup_symbol (copy_name ($1), expression_context_block,
  454. VAR_NAMESPACE, 0, NULL);
  455. $$ = sym;}
  456. ;
  457. /* GDB scope operator */
  458. fblock : block COLONCOLON BLOCKNAME
  459. { struct symbol *tem
  460. = lookup_symbol (copy_name ($3), $1,
  461. VAR_NAMESPACE, 0, NULL);
  462. if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
  463. error (&quot;No function \&quot;%s\&quot; in specified context.&quot;,
  464. copy_name ($3));
  465. $$ = tem;
  466. }
  467. ;
  468. /* Useful for assigning to PROCEDURE variables */
  469. variable: fblock
  470. { write_exp_elt_opcode(OP_VAR_VALUE);
  471. write_exp_elt_block (NULL);
  472. write_exp_elt_sym ($1);
  473. write_exp_elt_opcode (OP_VAR_VALUE); }
  474. ;
  475. /* GDB internal ($foo) variable */
  476. variable: INTERNAL_VAR
  477. ;
  478. /* GDB scope operator */
  479. variable: block COLONCOLON NAME
  480. { struct symbol *sym;
  481. sym = lookup_symbol (copy_name ($3), $1,
  482. VAR_NAMESPACE, 0, NULL);
  483. if (sym == 0)
  484. error (&quot;No symbol \&quot;%s\&quot; in specified context.&quot;,
  485. copy_name ($3));
  486. write_exp_elt_opcode (OP_VAR_VALUE);
  487. /* block_found is set by lookup_symbol. */
  488. write_exp_elt_block (block_found);
  489. write_exp_elt_sym (sym);
  490. write_exp_elt_opcode (OP_VAR_VALUE); }
  491. ;
  492. /* Base case for variables. */
  493. variable: NAME
  494. { struct symbol *sym;
  495. int is_a_field_of_this;
  496. sym = lookup_symbol (copy_name ($1),
  497. expression_context_block,
  498. VAR_NAMESPACE,
  499. &amp;is_a_field_of_this,
  500. NULL);
  501. if (sym)
  502. {
  503. if (symbol_read_needs_frame (sym))
  504. {
  505. if (innermost_block == 0 ||
  506. contained_in (block_found,
  507. innermost_block))
  508. innermost_block = block_found;
  509. }
  510. write_exp_elt_opcode (OP_VAR_VALUE);
  511. /* We want to use the selected frame, not
  512. another more inner frame which happens to
  513. be in the same block. */
  514. write_exp_elt_block (NULL);
  515. write_exp_elt_sym (sym);
  516. write_exp_elt_opcode (OP_VAR_VALUE);
  517. }
  518. else
  519. {
  520. struct minimal_symbol *msymbol;
  521. register char *arg = copy_name ($1);
  522. msymbol =
  523. lookup_minimal_symbol (arg, NULL, NULL);
  524. if (msymbol != NULL)
  525. {
  526. write_exp_msymbol
  527. (msymbol,
  528. lookup_function_type (builtin_type_int),
  529. builtin_type_int);
  530. }
  531. else if (!have_full_symbols () &amp;&amp; !have_partial_symbols ())
  532. error (&quot;No symbol table is loaded. Use the \&quot;symbol-file\&quot; command.&quot;);
  533. else
  534. error (&quot;No symbol \&quot;%s\&quot; in current context.&quot;,
  535. copy_name ($1));
  536. }
  537. }
  538. ;
  539. type
  540. : TYPENAME
  541. { $$ = lookup_typename (copy_name ($1),
  542. expression_context_block, 0); }
  543. ;
  544. %%
  545. #if 0 /* FIXME! */
  546. int
  547. overflow(a,b)
  548. long a,b;
  549. {
  550. return (MAX_OF_TYPE(builtin_type_m2_int) - b) &lt; a;
  551. }
  552. int
  553. uoverflow(a,b)
  554. unsigned long a,b;
  555. {
  556. return (MAX_OF_TYPE(builtin_type_m2_card) - b) &lt; a;
  557. }
  558. #endif /* FIXME */
  559. /* Take care of parsing a number (anything that starts with a digit).
  560. Set yylval and return the token type; update lexptr.
  561. LEN is the number of characters in it. */
  562. /*** Needs some error checking for the float case ***/
  563. static int
  564. parse_number (olen)
  565. int olen;
  566. {
  567. register char *p = lexptr;
  568. register LONGEST n = 0;
  569. register LONGEST prevn = 0;
  570. register int c,i,ischar=0;
  571. register int base = input_radix;
  572. register int len = olen;
  573. int unsigned_p = number_sign == 1 ? 1 : 0;
  574. if(p[len-1] == 'H')
  575. {
  576. base = 16;
  577. len--;
  578. }
  579. else if(p[len-1] == 'C' || p[len-1] == 'B')
  580. {
  581. base = 8;
  582. ischar = p[len-1] == 'C';
  583. len--;
  584. }
  585. /* Scan the number */
  586. for (c = 0; c &lt; len; c++)
  587. {
  588. if (p[c] == '.' &amp;&amp; base == 10)
  589. {
  590. /* It's a float since it contains a point. */
  591. yylval.dval = atof (p);
  592. lexptr += len;
  593. return FLOAT;
  594. }
  595. if (p[c] == '.' &amp;&amp; base != 10)
  596. error(&quot;Floating point numbers must be base 10.&quot;);
  597. if (base == 10 &amp;&amp; (p[c] &lt; '0' || p[c] &gt; '9'))
  598. error(&quot;Invalid digit \'%c\' in number.&quot;,p[c]);
  599. }
  600. while (len-- &gt; 0)
  601. {
  602. c = *p++;
  603. n *= base;
  604. if( base == 8 &amp;&amp; (c == '8' || c == '9'))
  605. error(&quot;Invalid digit \'%c\' in octal number.&quot;,c);
  606. if (c &gt;= '0' &amp;&amp; c &lt;= '9')
  607. i = c - '0';
  608. else
  609. {
  610. if (base == 16 &amp;&amp; c &gt;= 'A' &amp;&amp; c &lt;= 'F')
  611. i = c - 'A' + 10;
  612. else
  613. return ERROR;
  614. }
  615. n+=i;
  616. if(i &gt;= base)
  617. return ERROR;
  618. if(!unsigned_p &amp;&amp; number_sign == 1 &amp;&amp; (prevn &gt;= n))
  619. unsigned_p=1; /* Try something unsigned */
  620. /* Don't do the range check if n==i and i==0, since that special
  621. case will give an overflow error. */
  622. if(RANGE_CHECK &amp;&amp; n!=i &amp;&amp; i)
  623. {
  624. if((unsigned_p &amp;&amp; (unsigned)prevn &gt;= (unsigned)n) ||
  625. ((!unsigned_p &amp;&amp; number_sign==-1) &amp;&amp; -prevn &lt;= -n))
  626. range_error(&quot;Overflow on numeric constant.&quot;);
  627. }
  628. prevn=n;
  629. }
  630. lexptr = p;
  631. if(*p == 'B' || *p == 'C' || *p == 'H')
  632. lexptr++; /* Advance past B,C or H */
  633. if (ischar)
  634. {
  635. yylval.ulval = n;
  636. return CHAR;
  637. }
  638. else if ( unsigned_p &amp;&amp; number_sign == 1)
  639. {
  640. yylval.ulval = n;
  641. return UINT;
  642. }
  643. else if((unsigned_p &amp;&amp; (n&lt;0))) {
  644. range_error(&quot;Overflow on numeric constant -- number too large.&quot;);
  645. /* But, this can return if range_check == range_warn. */
  646. }
  647. yylval.lval = n;
  648. return INT;
  649. }
  650. /* Some tokens */
  651. static struct
  652. {
  653. char name[2];
  654. int token;
  655. } tokentab2[] =
  656. {
  657. { {'&lt;', '&gt;'}, NOTEQUAL },
  658. { {':', '='}, ASSIGN },
  659. { {'&lt;', '='}, LEQ },
  660. { {'&gt;', '='}, GEQ },
  661. { {':', ':'}, COLONCOLON },
  662. };
  663. /* Some specific keywords */
  664. struct keyword {
  665. char keyw[10];
  666. int token;
  667. };
  668. static struct keyword keytab[] =
  669. {
  670. {&quot;OR&quot; , OROR },
  671. {&quot;IN&quot;, IN },/* Note space after IN */
  672. {&quot;AND&quot;, LOGICAL_AND},
  673. {&quot;ABS&quot;, ABS },
  674. {&quot;CHR&quot;, CHR },
  675. {&quot;DEC&quot;, DEC },
  676. {&quot;NOT&quot;, NOT },
  677. {&quot;DIV&quot;, DIV },
  678. {&quot;INC&quot;, INC },
  679. {&quot;MAX&quot;, MAX_FUNC },
  680. {&quot;MIN&quot;, MIN_FUNC },
  681. {&quot;MOD&quot;, MOD },
  682. {&quot;ODD&quot;, ODD },
  683. {&quot;CAP&quot;, CAP },
  684. {&quot;ORD&quot;, ORD },
  685. {&quot;VAL&quot;, VAL },
  686. {&quot;EXCL&quot;, EXCL },
  687. {&quot;HIGH&quot;, HIGH },
  688. {&quot;INCL&quot;, INCL },
  689. {&quot;SIZE&quot;, SIZE },
  690. {&quot;FLOAT&quot;, FLOAT_FUNC },
  691. {&quot;TRUNC&quot;, TRUNC },
  692. };
  693. /* Read one token, getting characters through lexptr. */
  694. /* This is where we will check to make sure that the language and the operators used are
  695. compatible */
  696. static int
  697. yylex ()
  698. {
  699. register int c;
  700. register int namelen;
  701. register int i;
  702. register char *tokstart;
  703. register char quote;
  704. retry:
  705. tokstart = lexptr;
  706. /* See if it is a special token of length 2 */
  707. for( i = 0 ; i &lt; (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
  708. if(STREQN(tokentab2[i].name, tokstart, 2))
  709. {
  710. lexptr += 2;
  711. return tokentab2[i].token;
  712. }
  713. switch (c = *tokstart)
  714. {
  715. case 0:
  716. return 0;
  717. case ' ':
  718. case '\t':
  719. case '\n':
  720. lexptr++;
  721. goto retry;
  722. case '(':
  723. paren_depth++;
  724. lexptr++;
  725. return c;
  726. case ')':
  727. if (paren_depth == 0)
  728. return 0;
  729. paren_depth--;
  730. lexptr++;
  731. return c;
  732. case ',':
  733. if (comma_terminates &amp;&amp; paren_depth == 0)
  734. return 0;
  735. lexptr++;
  736. return c;
  737. case '.':
  738. /* Might be a floating point number. */
  739. if (lexptr[1] &gt;= '0' &amp;&amp; lexptr[1] &lt;= '9')
  740. break; /* Falls into number code. */
  741. else
  742. {
  743. lexptr++;
  744. return DOT;
  745. }
  746. /* These are character tokens that appear as-is in the YACC grammar */
  747. case '+':
  748. case '-':
  749. case '*':
  750. case '/':
  751. case '^':
  752. case '&lt;':
  753. case '&gt;':
  754. case '[':
  755. case ']':
  756. case '=':
  757. case '{':
  758. case '}':
  759. case '#':
  760. case '@':
  761. case '~':
  762. case '&amp;':
  763. lexptr++;
  764. return c;
  765. case '\'' :
  766. case '&quot;':
  767. quote = c;
  768. for (namelen = 1; (c = tokstart[namelen]) != quote &amp;&amp; c != '\0'; namelen++)
  769. if (c == '\\')
  770. {
  771. c = tokstart[++namelen];
  772. if (c &gt;= '0' &amp;&amp; c &lt;= '9')
  773. {
  774. c = tokstart[++namelen];
  775. if (c &gt;= '0' &amp;&amp; c &lt;= '9')
  776. c = tokstart[++namelen];
  777. }
  778. }
  779. if(c != quote)
  780. error(&quot;Unterminated string or character constant.&quot;);
  781. yylval.sval.ptr = tokstart + 1;
  782. yylval.sval.length = namelen - 1;
  783. lexptr += namelen + 1;
  784. if(namelen == 2) /* Single character */
  785. {
  786. yylval.ulval = tokstart[1];
  787. return CHAR;
  788. }
  789. else
  790. return STRING;
  791. }
  792. /* Is it a number? */
  793. /* Note: We have already dealt with the case of the token '.'.
  794. See case '.' above. */
  795. if ((c &gt;= '0' &amp;&amp; c &lt;= '9'))
  796. {
  797. /* It's a number. */
  798. int got_dot = 0, got_e = 0;
  799. register char *p = tokstart;
  800. int toktype;
  801. for (++p ;; ++p)
  802. {
  803. if (!got_e &amp;&amp; (*p == 'e' || *p == 'E'))
  804. got_dot = got_e = 1;
  805. else if (!got_dot &amp;&amp; *p == '.')
  806. got_dot = 1;
  807. else if (got_e &amp;&amp; (p[-1] == 'e' || p[-1] == 'E')
  808. &amp;&amp; (*p == '-' || *p == '+'))
  809. /* This is the sign of the exponent, not the end of the
  810. number. */
  811. continue;
  812. else if ((*p &lt; '0' || *p &gt; '9') &amp;&amp;
  813. (*p &lt; 'A' || *p &gt; 'F') &amp;&amp;
  814. (*p != 'H')) /* Modula-2 hexadecimal number */
  815. break;
  816. }
  817. toktype = parse_number (p - tokstart);
  818. if (toktype == ERROR)
  819. {
  820. char *err_copy = (char *) alloca (p - tokstart + 1);
  821. memcpy (err_copy, tokstart, p - tokstart);
  822. err_copy[p - tokstart] = 0;
  823. error (&quot;Invalid number \&quot;%s\&quot;.&quot;, err_copy);
  824. }
  825. lexptr = p;
  826. return toktype;
  827. }
  828. if (!(c == '_' || c == '$'
  829. || (c &gt;= 'a' &amp;&amp; c &lt;= 'z') || (c &gt;= 'A' &amp;&amp; c &lt;= 'Z')))
  830. /* We must have come across a bad character (e.g. ';'). */
  831. error (&quot;Invalid character '%c' in expression.&quot;, c);
  832. /* It's a name. See how long it is. */
  833. namelen = 0;
  834. for (c = tokstart[namelen];
  835. (c == '_' || c == '$' || (c &gt;= '0' &amp;&amp; c &lt;= '9')
  836. || (c &gt;= 'a' &amp;&amp; c &lt;= 'z') || (c &gt;= 'A' &amp;&amp; c &lt;= 'Z'));
  837. c = tokstart[++namelen])
  838. ;
  839. /* The token &quot;if&quot; terminates the expression and is NOT
  840. removed from the input stream. */
  841. if (namelen == 2 &amp;&amp; tokstart[0] == 'i' &amp;&amp; tokstart[1] == 'f')
  842. {
  843. return 0;
  844. }
  845. lexptr += namelen;
  846. /* Lookup special keywords */
  847. for(i = 0 ; i &lt; (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
  848. if(namelen == strlen(keytab[i].keyw) &amp;&amp; STREQN(tokstart,keytab[i].keyw,namelen))
  849. return keytab[i].token;
  850. yylval.sval.ptr = tokstart;
  851. yylval.sval.length = namelen;
  852. if (*tokstart == '$')
  853. {
  854. write_dollar_variable (yylval.sval);
  855. return INTERNAL_VAR;
  856. }
  857. /* Use token-type BLOCKNAME for symbols that happen to be defined as
  858. functions. If this is not so, then ...
  859. Use token-type TYPENAME for symbols that happen to be defined
  860. currently as names of types; NAME for other symbols.
  861. The caller is not constrained to care about the distinction. */
  862. {
  863. char *tmp = copy_name (yylval.sval);
  864. struct symbol *sym;
  865. if (lookup_partial_symtab (tmp))
  866. return BLOCKNAME;
  867. sym = lookup_symbol (tmp, expression_context_block,
  868. VAR_NAMESPACE, 0, NULL);
  869. if (sym &amp;&amp; SYMBOL_CLASS (sym) == LOC_BLOCK)
  870. return BLOCKNAME;
  871. if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
  872. return TYPENAME;
  873. if(sym)
  874. {
  875. switch(sym-&gt;aclass)
  876. {
  877. case LOC_STATIC:
  878. case LOC_REGISTER:
  879. case LOC_ARG:
  880. case LOC_REF_ARG:
  881. case LOC_REGPARM:
  882. case LOC_REGPARM_ADDR:
  883. case LOC_LOCAL:
  884. case LOC_LOCAL_ARG:
  885. case LOC_BASEREG:
  886. case LOC_BASEREG_ARG:
  887. case LOC_CONST:
  888. case LOC_CONST_BYTES:
  889. case LOC_OPTIMIZED_OUT:
  890. return NAME;
  891. case LOC_TYPEDEF:
  892. return TYPENAME;
  893. case LOC_BLOCK:
  894. return BLOCKNAME;
  895. case LOC_UNDEF:
  896. error(&quot;internal: Undefined class in m2lex()&quot;);
  897. case LOC_LABEL:
  898. case LOC_UNRESOLVED:
  899. error(&quot;internal: Unforseen case in m2lex()&quot;);
  900. default:
  901. error (&quot;unhandled token in m2lex()&quot;);
  902. break;
  903. }
  904. }
  905. else
  906. {
  907. /* Built-in BOOLEAN type. This is sort of a hack. */
  908. if(STREQN(tokstart,&quot;TRUE&quot;,4))
  909. {
  910. yylval.ulval = 1;
  911. return M2_TRUE;
  912. }
  913. else if(STREQN(tokstart,&quot;FALSE&quot;,5))
  914. {
  915. yylval.ulval = 0;
  916. return M2_FALSE;
  917. }
  918. }
  919. /* Must be another type of name... */
  920. return NAME;
  921. }
  922. }
  923. #if 0 /* Unused */
  924. static char *
  925. make_qualname(mod,ident)
  926. char *mod, *ident;
  927. {
  928. char *new = malloc(strlen(mod)+strlen(ident)+2);
  929. strcpy(new,mod);
  930. strcat(new,&quot;.&quot;);
  931. strcat(new,ident);
  932. return new;
  933. }
  934. #endif /* 0 */
  935. void
  936. yyerror (msg)
  937. char *msg;
  938. {
  939. error (&quot;A %s in expression, near `%s'.&quot;, (msg ? msg : &quot;error&quot;), lexptr);
  940. }
  941. </pre>
  942. <hr />
  943. </body></html>