parser.cup 26.3 KB
Newer Older
petter's avatar
petter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

/*================================================================*/ 
/* 
  JavaCup Specification for the JavaCup Specification Language
  by Scott Hudson, GVU Center, Georgia Tech, August 1995
  and Frank Flannery, Department of Computer Science, Princeton Univ,
  July 1996
  Bug Fixes: C. Scott Ananian, Dept of Electrical Engineering, Princeton
  University, October 1996. [later Massachusetts Institute of Technology]


  This JavaCup specification is used to implement JavaCup itself.
  It specifies the parser for the JavaCup specification language.
  (It also serves as a reasonable example of what a typical JavaCup
  spec looks like).

  The specification has the following parts:
    Package and import declarations
      These serve the same purpose as in a normal Java source file
      (and will appear in the generated code for the parser). In this 
      case we are part of the java_cup package and we import both the
      java_cup runtime system and Hashtable from the standard Java
      utilities package.

    Action code
      This section provides code that is included with the class encapsulating
      the various pieces of user code embedded in the grammar (i.e., the
      semantic actions).  This provides a series of helper routines and
      data structures that the semantic actions use.

    Parser code    
      This section provides code included in the parser class itself.  In
      this case we override the default error reporting routines.

    Init with and scan with 
      These sections provide small bits of code that initialize, then 
      indicate how to invoke the scanner.

    Symbols and grammar
      These sections declare all the terminal and non terminal symbols 
      and the types of objects that they will be represented by at runtime,
      then indicate the start symbol of the grammar (), and finally provide
      the grammar itself (with embedded actions).

    Operation of the parser
      The parser acts primarily by accumulating data structures representing
      various parts of the specification.  Various small parts (e.g., single
      code strings) are stored as static variables of the emit class and
      in a few cases as variables declared in the action code section.  
      Terminals, non terminals, and productions, are maintained as collection
      accessible via static methods of those classes.  In addition, two 
      symbol tables are kept:  
	symbols   maintains the name to object mapping for all symbols
	non_terms maintains a separate mapping containing only the non terms

      Several intermediate working structures are also declared in the action
      code section.  These include: rhs_parts, rhs_pos, and lhs_nt which
      build up parts of the current production while it is being parsed.
	
  Author(s)
    Scott Hudson, GVU Center, Georgia Tech.
    Frank Flannery, Department of Computer Science, Princeton Univ.
    C. Scott Ananian, Department of Electrical Engineering, Princeton Univ.

  Revisions
    v0.9a   First released version                     [SEH] 8/29/95
    v0.9b   Updated for beta language (throws clauses) [SEH] 11/25/95
    v0.10a  Made many improvements/changes. now offers:
              return value
              left/right positions and propagations
              cleaner label references
              precedence and associativity for terminals
              contextual precedence for productions
              [FF] 7/3/96
    v0.10b  Fixed %prec directive so it works like it's supposed to.
              [CSA] 10/10/96
    v0.10g   Added support for array types on symbols.
              [CSA] 03/23/98
    v0.10i  Broaden set of IDs allowed in multipart_id and label_id so
            that only java reserved words (and not CUP reserved words like
            'parser' and 'start') are prohibited.  Allow reordering of
	    action code, parser code, init code, and scan with sections,
	    and made closing semicolon optional for these sections.
	    Added 'nonterminal' as a terminal symbol, finally fixing a
	    spelling mistake that's been around since the beginning.
	    For backwards compatibility, you can still misspell the
	    word if you like.
*/
/*================================================================*/ 

package java_cup;
import java_cup.runtime.*;
import java.util.Hashtable;
94
import java.util.Stack;
petter's avatar
petter committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
/*----------------------------------------------------------------*/ 

action code {:
  /** helper routine to clone a new production part adding a given label */
  protected production_part add_lab(production_part part, String lab)
    throws internal_error
    {
      /* if there is no label, or this is an action, just return the original */
      if (lab == null || part.is_action()) return part;

      /* otherwise build a new one with the given label attached */
      return new symbol_part(((symbol_part)part).the_symbol(),lab);
    }

  /** max size of right hand side we will support */
  protected final int MAX_RHS = 200;

  /** array for accumulating right hand side parts */
  protected production_part[] rhs_parts = new production_part[MAX_RHS];

  /** where we are currently in building a right hand side */
  protected int rhs_pos = 0;

  /** start a new right hand side */
  protected void new_rhs() {rhs_pos = 0; }

  /** add a new right hand side part */
  protected void add_rhs_part(production_part part) throws java.lang.Exception
    {
      if (rhs_pos >= MAX_RHS)
	throw new Exception("Internal Error: Productions limited to " + 
			     MAX_RHS + " symbols and actions"); 

      rhs_parts[rhs_pos] = part;
      rhs_pos++;
    }

  /** string to build up multiple part names */
  protected String multipart_name = new String();
134
  protected Stack multipart_names = new Stack();
petter's avatar
petter committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  /** append a new name segment to the accumulated multipart name */

//  TUM CHANGES
//   protected void append_multipart(String name)
//     {
//       String dot = "";
// 
//       /* if we aren't just starting out, put on a dot */
//       if (multipart_name.length() != 0)  dot = ".";
// 
//       multipart_name = multipart_name.concat(dot + name);
//     }
// TUM CHANGES

  /** table of declared symbols -- contains production parts indexed by name */
  protected Hashtable symbols = new Hashtable();

  /** table of just non terminals -- contains non_terminals indexed by name */
  protected Hashtable non_terms = new Hashtable();

  /** declared start non_terminal */
  protected non_terminal start_nt = null;

  /** left hand side non terminal of the current production */
  protected non_terminal lhs_nt;

  /** Current precedence number */
  int _cur_prec = 0;

  /** Current precedence side */
  int _cur_side = assoc.no_prec;

  /** update the precedences we are declaring */
  protected void update_precedence(int p) {
    _cur_side = p;
    _cur_prec++;
  }
  /** add relevant data to terminals */ 
  protected void add_precedence(String term) {
    if (term == null) {
      System.err.println("Unable to add precedence to nonexistent terminal");
    } else {
      symbol_part sp = (symbol_part)symbols.get(term);
      if (sp == null) {
	System.err.println("Could find terminal " + term + " while declaring precedence");
      } else {
	java_cup.symbol sym = sp.the_symbol();
	if (sym instanceof terminal) 
	  ((terminal)sym).set_precedence(_cur_side, _cur_prec);
	else System.err.println("Precedence declaration: Can't find terminal " + term);
      }
    }
  }
188 189 190 191 192 193 194 195 196 197 198
  
  private int cur_debug_id = 0;
  public int get_new_debug_id() {
   	return cur_debug_id++;
  }
   
  public String attach_debug_symbol(int id, String code) {
 	if (!parser.debugSymbols)
 		return code;
 	return "//@@CUPDBG" + id + "\n" + code;
  }
petter's avatar
petter committed
199 200 201 202 203
:};

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

parser code {:
petter's avatar
petter committed
204
 /* override error routines */
petter's avatar
petter committed
205
  protected Lexer lexer;
206 207 208 209
  public boolean debugSymbols;
  public void setDebugSymbols(boolean debugSymbols){
  	this.debugSymbols = debugSymbols;
  }
petter's avatar
petter committed
210 211 212 213 214
  public void report_fatal_error(
    String   message,
    Object   info)
    {
      done_parsing();
petter's avatar
petter committed
215 216
      if (info instanceof Symbol) ErrorManager.getManager().emit_fatal(message+ "\nCan't recover from previous error(s), giving up.",(Symbol)info);
      else ErrorManager.getManager().emit_fatal(message + "\nCan't recover from previous error(s), giving up.",cur_token);
petter's avatar
petter committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230
      System.exit(1);
    }

    public void report_error(String message, Object info)
    {
      if (info instanceof Symbol)
         ErrorManager.getManager().emit_error(message,(Symbol)info);
      else
         ErrorManager.getManager().emit_error(message,cur_token);
    }
:};

/*---------------------------------------------------------------- */

petter's avatar
petter committed
231 232 233 234 235
init with {: 
    ComplexSymbolFactory f = new ComplexSymbolFactory();
    symbolFactory = f;
    lexer = new Lexer(f); 
:}
petter's avatar
petter committed
236
/*lexer.init(); :};*/
petter's avatar
petter committed
237 238 239
scan with {: 
    return lexer.next_token(); 
:};
petter's avatar
petter committed
240 241 242 243

/*----------------------------------------------------------------*/ 

terminal
244
  PACKAGE, IMPORT, STATIC ,CODE, ACTION, PARSER, TERMINAL, NON, INIT, SCAN, WITH, 
petter's avatar
petter committed
245 246
  START, SEMI, COMMA, STAR, DOT, COLON, COLON_COLON_EQUALS, BAR, PRECEDENCE,
  LEFT, RIGHT, NONASSOC, PERCENT_PREC, LBRACK, RBRACK, NONTERMINAL, GT, LT,
247
  QUESTION, SUPER, EXTENDS, CLASS;
petter's avatar
petter committed
248 249 250 251 252 253 254 255 256 257 258

terminal String  ID, CODE_STRING;

non terminal
  spec, package_spec, import_list, action_code_part, 
  code_parts, code_part, opt_semi, non_terminal,
  parser_code_part, symbol_list, start_spec, production_list, 
  multipart_id, import_spec, import_id, init_code, scan_code, symbol, 
  type_id, term_name_list, non_term_name_list, production, prod_part_list, 
  prod_part, new_term_id, new_non_term_id, rhs_list, rhs, empty,
  precedence_list, preced, terminal_list, precedence_l, declares_term, 
259
  declares_non_term, class_name;
petter's avatar
petter committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

non terminal String  nt_id, symbol_id, label_id, opt_label, terminal_id,
                     term_id, robust_id, typearglist, typearguement, wildcard;

/*----------------------------------------------------------------*/ 

start with spec;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

spec ::= 
	{:
          /* declare "error" as a terminal */
          symbols.put("error", new symbol_part(terminal.error));

          /* declare start non terminal */
          non_terms.put("$START", non_terminal.START_nt);
	:}
	package_spec
	import_list
280
	class_name
petter's avatar
petter committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	code_parts
	symbol_list
	precedence_list
	start_spec
	production_list
	|
	/* error recovery assuming something went wrong before symbols 
	   and we have TERMINAL or NON TERMINAL to sync on.  if we get
	   an error after that, we recover inside symbol_list or 
	   production_list 
	*/
	error 
	symbol_list
	precedence_list
	start_spec
	production_list
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

package_spec ::= 
	PACKAGE
	multipart_id
	{:
	  /* save the package name */
	  emit.package_name = multipart_name;

	  /* reset the accumulated multipart name */
	  multipart_name = new String();
	:}
	SEMI
	|
	empty
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

import_list ::=
	import_list
	import_spec
	|
	empty
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

import_spec ::=
	IMPORT 
	import_id 
	{: 
	  /* save this import on the imports list */
	  emit.import_list.push(multipart_name);

	  /* reset the accumulated multipart name */
335 336 337 338 339 340 341 342 343 344 345
	  multipart_name = new String();
	:}
	SEMI
	|
	IMPORT STATIC
	import_id 
	{: 
	  /* save this import on the imports list */
	  emit.import_list.push(" static " + multipart_name);

	  /* reset the accumulated multipart name */
petter's avatar
petter committed
346 347 348 349 350
	  multipart_name = new String();
	:}
	SEMI
	;

351 352 353 354 355 356 357 358 359
class_name ::=
    empty |
    CLASS ID:id SEMI
    {:
     emit.parser_class_name = id;
     emit.symbol_const_class_name = id + "Sym";
    :}
    ;

petter's avatar
petter committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

// allow any order; all parts are optional. [CSA, 23-Jul-1999]
// (we check in the part action to make sure we don't have 2 of any part)
code_part ::=
	action_code_part | parser_code_part | init_code | scan_code ;
code_parts ::= 
	| code_parts code_part;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

action_code_part ::= 
	ACTION CODE CODE_STRING:user_code opt_semi
	{:
	  if (emit.action_code!=null)
petter's avatar
petter committed
375
	    ErrorManager.getManager().emit_warning("Redundant action code (skipping)");
petter's avatar
petter committed
376
	  else /* save the user included code string */
377
	    emit.action_code = attach_debug_symbol(get_new_debug_id(),user_code);
petter's avatar
petter committed
378 379 380 381 382 383 384 385 386
	:}
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

parser_code_part ::= 
	PARSER CODE CODE_STRING:user_code opt_semi
	{:
	  if (emit.parser_code!=null)
petter's avatar
petter committed
387
	    ErrorManager.getManager().emit_warning("Redundant parser code (skipping)");
petter's avatar
petter committed
388
	  else /* save the user included code string */
389
	    emit.parser_code = attach_debug_symbol(get_new_debug_id(),user_code);
petter's avatar
petter committed
390 391 392 393 394 395 396 397 398
	:}
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

init_code ::= 
	INIT WITH CODE_STRING:user_code opt_semi
	{: 
	  if (emit.init_code!=null)
petter's avatar
petter committed
399
	    ErrorManager.getManager().emit_warning("Redundant init code (skipping)");
petter's avatar
petter committed
400
	  else /* save the user code */
401
	    emit.init_code = attach_debug_symbol(get_new_debug_id(),user_code);
petter's avatar
petter committed
402 403 404 405 406 407 408 409 410
	:}
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

scan_code ::=
	SCAN WITH CODE_STRING:user_code opt_semi
	{: 
	  if (emit.scan_code!=null)
petter's avatar
petter committed
411
	    ErrorManager.getManager().emit_warning("Redundant scan code (skipping)");
petter's avatar
petter committed
412
	  else /* save the user code */
413
	    emit.scan_code = attach_debug_symbol(get_new_debug_id(),user_code);
petter's avatar
petter committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
	:}
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

symbol_list ::= symbol_list symbol | symbol;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

symbol ::= 
	TERMINAL
	type_id	
	declares_term
	|
	TERMINAL
	declares_term
	|
	non_terminal
	type_id
	declares_non_term
	|
	non_terminal
	declares_non_term
	|
	/* error recovery productions -- sync on semicolon */

	TERMINAL
	error 
	{:
	  /* reset the accumulated multipart name */
	  multipart_name = new String();
	:}
	SEMI
	|
	non_terminal
	error 
	{:
	  /* reset the accumulated multipart name */
	  multipart_name = new String();
	:}
	SEMI
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

declares_term ::=
	term_name_list
	{: 
	  /* reset the accumulated multipart name */
	  multipart_name = new String();
	:}
	SEMI
	;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

declares_non_term ::=
	non_term_name_list
	{: 
	  /* reset the accumulated multipart name */
	  multipart_name = new String();
	:}
	SEMI
	;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

term_name_list ::= term_name_list COMMA new_term_id | new_term_id;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

non_term_name_list ::=
	non_term_name_list
	COMMA
	new_non_term_id
	|
	new_non_term_id
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 


precedence_list ::= precedence_l | empty; 

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

precedence_l ::= precedence_l preced | preced;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

preced ::= 
	PRECEDENCE LEFT 
	{:
	  update_precedence(assoc.left);
	:}	
        terminal_list SEMI
	|
	PRECEDENCE RIGHT 
	{:
	  update_precedence(assoc.right);
	:}	
        terminal_list SEMI
	|
	PRECEDENCE NONASSOC 
	{:
	  update_precedence(assoc.nonassoc);
	:}	
        terminal_list SEMI
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

terminal_list ::= terminal_list COMMA terminal_id
	|
	terminal_id
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

terminal_id ::= term_id:sym
	{:	
	  add_precedence(sym);
	  RESULT = sym;
	:};
	  
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

term_id ::= symbol_id:sym
        {:
	  /* check that the symbol_id is a terminal */
	  if (symbols.get(sym) == null)
	    {
	      /* issue a message */
	      ErrorManager.getManager().emit_error("Terminal \"" + sym + 
			   "\" has not been declared");
	    }
          RESULT = sym;
         :};

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

start_spec ::= 
	START WITH nt_id:start_name 
	{: 
	  /* verify that the name has been declared as a non terminal */
	  non_terminal nt = (non_terminal)non_terms.get(start_name);
	  if (nt == null)
	    {
	    ErrorManager.getManager().emit_error( "Start non terminal \"" + start_name + 
	  		         "\" has not been declared");
	    }
          else
	    {
	      /* remember the non-terminal for later */
	      start_nt = nt;

	      /* build a special start production */
	      new_rhs();
	      add_rhs_part(add_lab(new symbol_part(start_nt), "start_val"));
	      add_rhs_part(new symbol_part(terminal.EOF));
petter's avatar
petter committed
572
	      if (!emit._xmlactions) add_rhs_part(new action_part("RESULT = start_val;"));
petter's avatar
petter committed
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	      emit.start_production = 
		     new production(non_terminal.START_nt, rhs_parts, rhs_pos);
	      new_rhs();
	    }
	:}
	SEMI
	|
	empty
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

production_list ::= production_list production | production;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

production ::=
	nt_id:lhs_id
	{:
	  /* lookup the lhs nt */
	  lhs_nt = (non_terminal)non_terms.get(lhs_id);

          /* if it wasn't declared, emit a message */
	  if (lhs_nt == null)
	    {
	      if (ErrorManager.getManager().getErrorCount() == 0)
petter's avatar
petter committed
599
	      ErrorManager.getManager().emit_warning("LHS non terminal \"" + lhs_id + 
petter's avatar
petter committed
600 601 602 603 604 605 606
			       "\" has not been declared");
	    }

	  /* reset the rhs accumulation */
	  new_rhs();
	:}
	COLON_COLON_EQUALS
607
/*	{: :}*/
petter's avatar
petter committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
	rhs_list
	SEMI
	|
	error
	{: ErrorManager.getManager().emit_error("Syntax Error"); :}
	SEMI
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

rhs_list ::= rhs_list BAR rhs | rhs;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

rhs ::= 
	prod_part_list PERCENT_PREC term_id:term_name 
	{:
	  java_cup.symbol sym = null;
	  if (lhs_nt != null) 
	    {
	      /* Find the precedence symbol */
	      if (term_name == null) {
		System.err.println("No terminal for contextual precedence");
		sym = null;
	      } else {
		sym = ((symbol_part)symbols.get(term_name)).the_symbol();
	      }
	      /* build the production */
	      production p;
	      if ((sym!=null) && (sym instanceof terminal)) {
		p = new production(lhs_nt, rhs_parts, rhs_pos,
				   ((terminal)sym).precedence_num(),
				   ((terminal)sym).precedence_side());
		((symbol_part)symbols.get(term_name)).the_symbol().note_use();
	      } else {
		System.err.println("Invalid terminal " + term_name + 
				   " for contextual precedence assignment");
		p = new production(lhs_nt, rhs_parts, rhs_pos);
	      }

	      /* if we have no start non-terminal declared and this is 
		 the first production, make its lhs nt the start_nt 
		 and build a special start production for it. */
              if (start_nt == null)
		{
		  start_nt = lhs_nt;

	          /* build a special start production */
	          new_rhs();
	          add_rhs_part(add_lab(new symbol_part(start_nt),"start_val"));
	          add_rhs_part(new symbol_part(terminal.EOF));
petter's avatar
petter committed
659
		  if (!emit._xmlactions) add_rhs_part(new action_part("RESULT = start_val;"));
petter's avatar
petter committed
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		  if ((sym!=null) && (sym instanceof terminal)) {
		    emit.start_production = 
		      new production(non_terminal.START_nt, rhs_parts, 
				     rhs_pos, ((terminal)sym).precedence_num(),
				     ((terminal)sym).precedence_side());
		  } else {
		    emit.start_production = 
		      new production(non_terminal.START_nt, rhs_parts, rhs_pos);
		  }
	          new_rhs();
		}
	    }

	  /* reset the rhs accumulation in any case */
	  new_rhs();
	:}
	|
	prod_part_list 
	{: 
	  if (lhs_nt != null) 
	    {
	      /* build the production */
	      production p = new production(lhs_nt, rhs_parts, rhs_pos);

	      /* if we have no start non-terminal declared and this is 
		 the first production, make its lhs nt the start_nt 
		 and build a special start production for it. */
              if (start_nt == null)
		{
		  start_nt = lhs_nt;

	          /* build a special start production */
	          new_rhs();
	          add_rhs_part(add_lab(new symbol_part(start_nt),"start_val"));
	          add_rhs_part(new symbol_part(terminal.EOF));
petter's avatar
petter committed
695
		  if (!emit._xmlactions) add_rhs_part(new action_part("RESULT = start_val;"));
petter's avatar
petter committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
	          emit.start_production = 
		     new production(non_terminal.START_nt, rhs_parts, rhs_pos);

	          new_rhs();
		}
	    }
	  
	  /* reset the rhs accumulation in any case */
	  new_rhs();
	:} 
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

prod_part_list ::= prod_part_list prod_part | empty;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

prod_part ::=
	symbol_id:symid opt_label:labid
	{: 
	  /* try to look up the id */
	  production_part symb = (production_part)symbols.get(symid);

	  /* if that fails, symbol is undeclared */
	  if (symb == null)
	    {
	      if (ErrorManager.getManager().getErrorCount() == 0)
	        ErrorManager.getManager().emit_error("java_cup.runtime.Symbol \"" + symid + 
			       "\" has not been declared");
	    }
	  else
	    {
	      /* add a labeled production part */
	      add_rhs_part(add_lab(symb, labid));
	    }
	:}
	|
	CODE_STRING:code_str
	{: 
	  /* add a new production part */
737
	  add_rhs_part(new action_part(attach_debug_symbol(get_new_debug_id(),code_str)));
petter's avatar
petter committed
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
	:}
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

opt_label ::=
	COLON label_id:labid
	{: RESULT = labid; :}
	|
	empty
	{: RESULT = null; :}
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

multipart_id ::=
	multipart_id DOT robust_id:another_id
	{: multipart_name = multipart_name.concat("."+another_id); :}
	|multipart_id 
757
        {: multipart_names.push(multipart_name); multipart_name="";:}
petter's avatar
petter committed
758 759
        LT typearglist:types GT
         {: multipart_name =
760
         ((String)multipart_names.pop()).concat("<"+types+">"); :}
petter's avatar
petter committed
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
        |
	robust_id:an_id
	{: multipart_name = multipart_name.concat(an_id); :}
	;
/*. . . . . . . . . . . .TUM CHANGES. . . . . . . . . . . . . . . */


typearglist ::= 
            typearguement:arg
            {: RESULT = arg; :}
            | typearglist:list COMMA typearguement:arg
            {: RESULT = list + "," + arg; :}
            ;

typearguement ::=
              type_id
              {: RESULT = multipart_name; multipart_name = new String(); :}
              | wildcard:w
              {: RESULT = w; :}
              ;

wildcard ::=
         QUESTION
         {: RESULT = " ? "; :}
         | QUESTION EXTENDS type_id
         {: RESULT = " ? extends "+multipart_name; multipart_name = new
         String(); :}
         | QUESTION SUPER type_id
         {: RESULT = " ? super "+multipart_name; multipart_name = new
         String(); :}
         ;

/*. . . . . . . . . . .END TUM CHANGES. . . . . . . . . . . . . . */ 


/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

import_id ::= 
	multipart_id DOT STAR
	{: multipart_name = multipart_name.concat(".*"); :}
	|
	multipart_id
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

type_id ::= multipart_id
	| type_id LBRACK RBRACK
	{: multipart_name = multipart_name.concat("[]"); :}
       	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

new_term_id ::= 
	ID:term_id 
	{: 
	  /* see if this terminal has been declared before */
	  if (symbols.get(term_id) != null)
	    {
	      /* issue a message */
	      ErrorManager.getManager().emit_error("java_cup.runtime.Symbol \"" + term_id + 
			   "\" has already been declared");
	    }
	  else
	    {
	      /* if no type declared, declare one */
	      if (multipart_name.equals("")) {
		multipart_name = "Object";
	      }
	      /* build a production_part and put it in the table */ 
	      symbols.put(term_id, 
	        new symbol_part(new terminal(term_id, multipart_name)));
	    }
	:}
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

new_non_term_id ::=
	ID:non_term_id
	{: 
	  /* see if this non terminal has been declared before */
	  if (symbols.get(non_term_id) != null)
	    {
	      /* issue a message */
	      ErrorManager.getManager().emit_error( "java_cup.runtime.Symbol \"" + non_term_id + 
			                      "\" has already been declared");
	    }
	  else
	    {
	      if (multipart_name.equals("")) {
                   multipart_name ="Object";
	      }
	      /* build the non terminal object */
              non_terminal this_nt = 
		new non_terminal(non_term_id, multipart_name);

	      /* put it in the non_terms table */
	      non_terms.put(non_term_id, this_nt);

	      /* build a production_part and put it in the symbols table */ 
	      symbols.put(non_term_id, new symbol_part(this_nt));
	    }
	:}
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

nt_id ::=
	ID:the_id
	{: RESULT = the_id; :}
	| error
	{:
		ErrorManager.getManager().emit_error("Illegal use of reserved word");
		RESULT="ILLEGAL";
	:}
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

symbol_id ::=
	ID:the_id
	{: RESULT = the_id; :}
	| error
	{:
		ErrorManager.getManager().emit_error("Illegal use of reserved word");
		RESULT="ILLEGAL";
	:}
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

label_id ::= 
	robust_id:the_id
	{: RESULT = the_id; :}
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

robust_id ::= /* all ids that aren't reserved words in Java */
	ID:the_id	{: RESULT = the_id; :}
	/* package is reserved. */
	/* import is reserved. */
	| CODE		{: RESULT = "code"; :}
	| ACTION	{: RESULT = "action"; :}
	| PARSER	{: RESULT = "parser"; :}
	| TERMINAL	{: RESULT = "terminal"; :}
	| NON		{: RESULT = "non"; :}
	| NONTERMINAL   {: RESULT = "nonterminal"; :}
	| INIT		{: RESULT = "init"; :}
	| SCAN		{: RESULT = "scan"; :}
	| WITH		{: RESULT = "with"; :}
	| START		{: RESULT = "start"; :}
	| PRECEDENCE	{: RESULT = "precedence"; :}
	| LEFT		{: RESULT = "left"; :}
	| RIGHT		{: RESULT = "right"; :}
	| NONASSOC	{: RESULT = "nonassoc"; :}
	| error
	{:
		ErrorManager.getManager().emit_error("Illegal use of reserved word");
		RESULT="ILLEGAL";
	:}
	;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

non_terminal ::= NON TERMINAL | NONTERMINAL;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

opt_semi ::= /* nothing */
	| SEMI;

/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

empty ::= /* nothing */;

/*----------------------------------------------------------------*/