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

/gdb-7.4.50.20120714/gdb/m2-exp.y

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