PageRenderTime 88ms CodeModel.GetById 48ms RepoModel.GetById 2ms app.codeStats 0ms

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

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