PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/avr-gdb-7.1/gdb-7.1/gdb/m2-exp.y

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