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

/src/netbsd/src/gnu/dist/gdb6/gdb/m2-exp.y

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