PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

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