parser.cup 35.1 KB
Newer Older
Johannes Roith's avatar
Johannes Roith 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

/*================================================================*/ 
/* 
  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.
*/
/*================================================================*/ 

Johannes Roith's avatar
Johannes Roith committed
91
package de.in.tum.www2.cup.internal;
Johannes Roith's avatar
Johannes Roith committed
92
import java_cup.runtime.*;
Johannes Roith's avatar
Johannes Roith committed
93
import java_cup.runtime.ComplexSymbolFactory.ComplexSymbol;
Johannes Roith's avatar
Johannes Roith committed
94
95
import java.util.Hashtable;
import java.util.Stack;
Johannes Roith's avatar
Johannes Roith committed
96
97
98
99
100
101
import java.util.List;
import java.util.ArrayList;
import de.in.tum.www2.cup.*;
import de.in.tum.www2.cup.ast.*;


Johannes Roith's avatar
Johannes Roith committed
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
134
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
/*----------------------------------------------------------------*/ 

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();
  protected Stack multipart_names = new Stack();

  /** 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 {
Johannes Roith's avatar
Johannes Roith committed
175
	symbol sym = sp.the_symbol();
Johannes Roith's avatar
Johannes Roith committed
176
177
178
179
180
181
	if (sym instanceof terminal) 
	  ((terminal)sym).set_precedence(_cur_side, _cur_prec);
	else System.err.println("Precedence declaration: Can't find terminal " + term);
      }
    }
  }
Johannes Roith's avatar
Johannes Roith committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

  List<IProductionRightPart> rhs_accumulator = new ArrayList<IProductionRightPart>();
  
  protected void ast_add_rhs_part(IProductionRightPart part)
  {
	rhs_accumulator.add(part);
  }

  protected ArrayList<IProductionRightPart> ast_get_rhs_parts_copy ()
  {
  	return new ArrayList<IProductionRightPart>(rhs_accumulator);
  }
  
  protected void ast_new_rhs()
  {
  	rhs_accumulator.clear();
  }

200
  protected void ast_add_precedence(Object lst, Range range)
Johannes Roith's avatar
Johannes Roith committed
201
  {
202
	Precedence p = new Precedence(_cur_side, (List<Terminal>) lst,  range);
Johannes Roith's avatar
Johannes Roith committed
203
204
205
	parserResult.precedences.add(p);
  }
  
Johannes Roith's avatar
Johannes Roith committed
206
207
208
209
210
211
212
213
214
215
216
217
   private boolean WITH_DEBUGGING_SYMBOLS = true;
   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 (!WITH_DEBUGGING_SYMBOLS)
			return code;
		return "//@@CUPBDG" + id + "\n" + code;
	}
  
Johannes Roith's avatar
Johannes Roith committed
218
219
220
221
222
223
224
:};

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

parser code {:

  private java.io.InputStream input_stream;
Johannes Roith's avatar
Johannes Roith committed
225
226
227
228
229
230
231
232
233
234
235
  private ErrorManager errMan;
  private CupContext context;
  private terminal.terminal_shared terminal_shared;
  private non_terminal.non_terminal_shared non_terminal_shared;
  private ParserResult parserResult;

  public void init(ErrorManager errMan, CupContext context, java.io.InputStream is) {
  	this.errMan = errMan;
  	this.context = context;
  	this.terminal_shared = terminal.getShared(context);
  	this.non_terminal_shared = non_terminal.getShared(context);
Johannes Roith's avatar
Johannes Roith committed
236
237
238
  	this.input_stream = is;
  }
  
Johannes Roith's avatar
Johannes Roith committed
239
240
241
242
  public ParserResult getResult() {
  	return parserResult;
  }
  
Johannes Roith's avatar
Johannes Roith committed
243
244
245
246
247
248
249
 /* override error routines */
  protected Lexer lexer;
  public void report_fatal_error(
    String   message,
    Object   info)
    {
      done_parsing();
Johannes Roith's avatar
Johannes Roith committed
250
      if (info instanceof Symbol)
251
      	errMan.Fatal(ErrorSource.Parser, message + "\nCan't recover from previous error(s), giving up.",(ComplexSymbol)info);
Johannes Roith's avatar
Johannes Roith committed
252
      else
253
      	errMan.Fatal(ErrorSource.Parser, message + "\nCan't recover from previous error(s), giving up.",(ComplexSymbol)cur_token);
Johannes Roith's avatar
Johannes Roith committed
254
      throw new ParserAbortException();
Johannes Roith's avatar
Johannes Roith committed
255
256
257
258
259
    }

    public void report_error(String message, Object info)
    {
      if (info instanceof Symbol)
260
         errMan.Error(ErrorSource.Parser, message, (ComplexSymbol) info);
Johannes Roith's avatar
Johannes Roith committed
261
      else
262
         errMan.Error(ErrorSource.Parser, message, (ComplexSymbol) cur_token);
Johannes Roith's avatar
Johannes Roith committed
263
264
265
266
267
    }
:};

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

Johannes Roith's avatar
Johannes Roith committed
268
init with {:
Johannes Roith's avatar
Johannes Roith committed
269
270
    ComplexSymbolFactory f = new ComplexSymbolFactory();
    symbolFactory = f;
Johannes Roith's avatar
Johannes Roith committed
271
272
    parserResult = new ParserResult();
    lexer = new Lexer(errMan, f, this.input_stream);
Johannes Roith's avatar
Johannes Roith committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
:}
/*lexer.init(); :};*/
scan with {: 
    return lexer.next_token(); 
:};

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

terminal
  PACKAGE, IMPORT, CODE, ACTION, PARSER, TERMINAL, NON, INIT, SCAN, WITH, 
  START, SEMI, COMMA, STAR, DOT, COLON, COLON_COLON_EQUALS, BAR, PRECEDENCE,
  LEFT, RIGHT, NONASSOC, PERCENT_PREC, LBRACK, RBRACK, NONTERMINAL, GT, LT,
  QUESTION, SUPER, EXTENDS, CLASS;

Johannes Roith's avatar
Johannes Roith committed
287
terminal String ID, CODE_STRING;
Johannes Roith's avatar
Johannes Roith committed
288
289
290
291
292
293
294
295

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,
Johannes Roith's avatar
Johannes Roith committed
296
  precedence_list, preced, preced_type, terminal_list, precedence_l, declares_term, 
Johannes Roith's avatar
Johannes Roith committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
  declares_non_term, class_name;

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 */
Johannes Roith's avatar
Johannes Roith committed
311
          symbols.put("error", new symbol_part(terminal_shared.getError()));
Johannes Roith's avatar
Johannes Roith committed
312
313

          /* declare start non terminal */
Johannes Roith's avatar
Johannes Roith committed
314
          non_terms.put("$START", non_terminal_shared.getSTART_nt());
Johannes Roith's avatar
Johannes Roith committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
	:}
	package_spec
	import_list
	class_name
	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 ::= 
340
	PACKAGE : p
Johannes Roith's avatar
Johannes Roith committed
341
342
343
	multipart_id
	{:
	  /* save the package name */
Johannes Roith's avatar
Johannes Roith committed
344
345
346
	  context.package_name = multipart_name;

	  parserResult.pack = new de.in.tum.www2.cup.ast.Package(
347
348
349
	  					new Name (multipart_name, null), // TODO: from, to 
	  					new Range(Position.fromLocation(pxleft),
	  					Position.fromComplexSymbolRight((ComplexSymbol) cur_token)));
Johannes Roith's avatar
Johannes Roith committed
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370

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

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

import_list ::=
	import_list
	import_spec
	|
	empty
	;

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

import_spec ::=
371
	IMPORT : import_token
Johannes Roith's avatar
Johannes Roith committed
372
	import_id
373
	SEMI : semi {:
Johannes Roith's avatar
Johannes Roith committed
374
	  /* save this import on the imports list */
Johannes Roith's avatar
Johannes Roith committed
375
376
377
	  context.import_list.push(multipart_name);

      Import imprt = new Import(
378
      					new Name (multipart_name, null), // TODO: from, to 
379
380
	  					new Range(Position.fromLocation(import_tokenxleft),
	      					Position.fromLocation(semixright)));
Johannes Roith's avatar
Johannes Roith committed
381
382
      parserResult.imports.add(imprt);
      
Johannes Roith's avatar
Johannes Roith committed
383
384
385
386
387
388
389
	  /* reset the accumulated multipart name */
	  multipart_name = new String();
	:}
	;

class_name ::=
    empty |
390
    CLASS : cls ID:id SEMI : semi
Johannes Roith's avatar
Johannes Roith committed
391
    {:
Johannes Roith's avatar
Johannes Roith committed
392
393
394
395
     context.parser_class_name = id;
     context.symbol_const_class_name = id + "Sym";
     
     parserResult.className = new ClassName (
396
397
398
399
     							new Name(id, new Range(Position.fromLocation(idxleft),
										Position.fromLocation(idxright))),
								new Range(Position.fromLocation(clsxleft),
										Position.fromLocation(semixright)));
Johannes Roith's avatar
Johannes Roith committed
400
     
Johannes Roith's avatar
Johannes Roith committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
    :}
    ;

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

// 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 ::= 
416
	ACTION CODE CODE_STRING : user_code opt_semi
Johannes Roith's avatar
Johannes Roith committed
417
	{:
Johannes Roith's avatar
Johannes Roith committed
418
	  if (context.action_code!=null)
419
420
421
	    errMan.Warning(ErrorSource.Parser,
	    		"Redundant action code (skipping)",
	    		Position.fromLocation(user_codexleft));
Johannes Roith's avatar
Johannes Roith committed
422
423
	  else { /* save the user included code string */
	    context.action_code = user_code;
424
425

		int debug_id = get_new_debug_id();
426
	    parserResult.actionCode = new SpecialCodeBlock(
427
								    debug_id,
428
429
	    							SpecialCodeBlock.BlockType.Action,
	    							user_code, 
430
431
	    							new Range(Position.fromLocation(user_codexleft),
	    								Position.fromLocation(user_codexright)));
Johannes Roith's avatar
Johannes Roith committed
432
	  }
Johannes Roith's avatar
Johannes Roith committed
433
434
435
436
437
438
	:}
	;

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

parser_code_part ::= 
439
	PARSER CODE CODE_STRING:user_code opt_semi
Johannes Roith's avatar
Johannes Roith committed
440
	{:
Johannes Roith's avatar
Johannes Roith committed
441
	  if (context.parser_code!=null)
442
443
444
	    errMan.Warning(ErrorSource.Parser,
	    				"Redundant parser code (skipping)",
	    		Position.fromLocation(user_codexleft));
Johannes Roith's avatar
Johannes Roith committed
445
446
	  else { /* save the user included code string */
	    context.parser_code = user_code;
447
		int debug_id = get_new_debug_id();
448
	    parserResult.parserCode = new SpecialCodeBlock(
449
	    							debug_id,
450
451
	    							SpecialCodeBlock.BlockType.Parser,
	    							user_code,
452
453
	    							new Range(Position.fromLocation(user_codexleft),
	    								Position.fromLocation(user_codexright)));
Johannes Roith's avatar
Johannes Roith committed
454
		}
Johannes Roith's avatar
Johannes Roith committed
455
456
457
458
459
460
	:}
	;

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

init_code ::= 
461
	INIT WITH CODE_STRING:user_code opt_semi
Johannes Roith's avatar
Johannes Roith committed
462
	{: 
Johannes Roith's avatar
Johannes Roith committed
463
	  if (context.init_code!=null)
464
465
	    errMan.Warning(ErrorSource.Parser, "Redundant init code (skipping)", 
	    		Position.fromLocation(user_codexleft));
Johannes Roith's avatar
Johannes Roith committed
466
467
	  else {/* save the user code */
	    context.init_code = user_code;
468
		int debug_id = get_new_debug_id();
469
	    parserResult.initCode = new SpecialCodeBlock(
470
	    							debug_id,
471
472
	    							SpecialCodeBlock.BlockType.Init,
	    							user_code, 
473
474
	    							new Range(Position.fromLocation(user_codexleft),
	    								Position.fromLocation(user_codexright)));
Johannes Roith's avatar
Johannes Roith committed
475
		}
Johannes Roith's avatar
Johannes Roith committed
476
477
478
479
480
481
	:}
	;

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

scan_code ::=
482
	SCAN WITH CODE_STRING:user_code opt_semi
Johannes Roith's avatar
Johannes Roith committed
483
	{: 
Johannes Roith's avatar
Johannes Roith committed
484
	  if (context.scan_code!=null)
485
486
	    errMan.Warning(ErrorSource.Parser, "Redundant scan code (skipping)", 
	    		Position.fromLocation(user_codexleft));
Johannes Roith's avatar
Johannes Roith committed
487
488
489
	  else {
	  	 /* save the user code */
	    context.scan_code = user_code;
490
		int debug_id = get_new_debug_id();
491
	    parserResult.scanCode = new SpecialCodeBlock(
492
	    							debug_id,
493
494
	    							SpecialCodeBlock.BlockType.Scan,
	    							user_code, 
495
496
	    							new Range(Position.fromLocation(user_codexleft),
	    								Position.fromLocation(user_codexright)));
Johannes Roith's avatar
Johannes Roith committed
497
498
	    
	  }
Johannes Roith's avatar
Johannes Roith committed
499
500
501
502
503
504
505
506
507
508
509
	:}
	;

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

symbol_list ::= symbol_list symbol | symbol;

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

symbol ::= 
	TERMINAL
Johannes Roith's avatar
Johannes Roith committed
510
511
512
513
514
515
516
517
518
519
	type_id : tid
	declares_term : lst {:
	
		//jrTODO: BUG? this is strange!!! -> there must be a better way 
		//        to refer to these terminals.
		ComplexSymbol terminal = (ComplexSymbol) stack.elementAt(stack.size()-2);		

		parserResult.symbols.add(new TypedTerminalDeclaration(
							(String) tid,
							(List<Terminal>) lst,
520
521
							new Range(Position.fromComplexSymbolLeft(terminal),
							Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
Johannes Roith's avatar
Johannes Roith committed
522
	:}
Johannes Roith's avatar
Johannes Roith committed
523
524
	|
	TERMINAL
Johannes Roith's avatar
Johannes Roith committed
525
526
527
528
529
530
531
	declares_term : lst {:
		//jrTODO: BUG? this is strange!!! -> there must be a better way 
		//        to refer to these terminals.
		ComplexSymbol terminal = (ComplexSymbol) stack.elementAt(stack.size()-2);		

		parserResult.symbols.add(new TerminalDeclaration(
							(List<Terminal>) lst,
532
533
							new Range(Position.fromComplexSymbolLeft(terminal),
							Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
Johannes Roith's avatar
Johannes Roith committed
534
535
		;
	:}
Johannes Roith's avatar
Johannes Roith committed
536
537
	|
	non_terminal
Johannes Roith's avatar
Johannes Roith committed
538
539
540
541
542
543
544
545
546
	type_id : tid
	declares_non_term : lst {:
	
		//jrTODO: BUG? this is strange!!! -> there must be a better way 
		//        to refer to these terminals.
		ComplexSymbol nonTerminal = (ComplexSymbol) stack.elementAt(stack.size()-3);		
		parserResult.symbols.add(new TypedNonTerminalDeclaration(
							(String) tid,
							(List<NonTerminal>) lst,
547
548
							new Range(Position.fromComplexSymbolLeft(nonTerminal),
							Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
Johannes Roith's avatar
Johannes Roith committed
549
	:}
Johannes Roith's avatar
Johannes Roith committed
550
551
	|
	non_terminal
Johannes Roith's avatar
Johannes Roith committed
552
553
554
555
556
557
558
	declares_non_term:lst {:
	
		//jrTODO: BUG? this is strange!!! -> there must be a better way 
		//        to refer to these terminals.
		ComplexSymbol nonTerminal = (ComplexSymbol) stack.elementAt(stack.size()-2);		
		parserResult.symbols.add(new NonTerminalDeclaration(
							(List<NonTerminal>) lst,
559
560
							new Range(Position.fromComplexSymbolLeft(nonTerminal),
							Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
Johannes Roith's avatar
Johannes Roith committed
561
562
	:}
	
Johannes Roith's avatar
Johannes Roith committed
563
	|
Johannes Roith's avatar
Johannes Roith committed
564
	
Johannes Roith's avatar
Johannes Roith committed
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
	/* 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 ::=
Johannes Roith's avatar
Johannes Roith committed
587
	term_name_list : lst
Johannes Roith's avatar
Johannes Roith committed
588
589
590
	{: 
	  /* reset the accumulated multipart name */
	  multipart_name = new String();
Johannes Roith's avatar
Johannes Roith committed
591
	  RESULT = lst;
Johannes Roith's avatar
Johannes Roith committed
592
593
594
595
596
597
	:}
	SEMI
	;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

declares_non_term ::=
Johannes Roith's avatar
Johannes Roith committed
598
	non_term_name_list : lst
Johannes Roith's avatar
Johannes Roith committed
599
600
601
	{: 
	  /* reset the accumulated multipart name */
	  multipart_name = new String();
Johannes Roith's avatar
Johannes Roith committed
602
	  RESULT = lst;
Johannes Roith's avatar
Johannes Roith committed
603
604
605
606
607
	:}
	SEMI
	;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

Johannes Roith's avatar
Johannes Roith committed
608
609
610
611
612
613
614
615
616
617
term_name_list ::= 
	term_name_list : lst COMMA new_term_id : item {:
		((List<Terminal>) lst).add((Terminal) item);
		RESULT = lst;
	:} | new_term_id:item {:
		List<Terminal> lst = new ArrayList<Terminal>();
		lst.add((Terminal) item);
		RESULT = lst;
	:}
	;
Johannes Roith's avatar
Johannes Roith committed
618
619
620
621

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

non_term_name_list ::=
Johannes Roith's avatar
Johannes Roith committed
622
623
624
625
	non_term_name_list : lst COMMA new_non_term_id : item {:
		((List<NonTerminal>) lst).add((NonTerminal) item);
		RESULT = lst;
	:}
Johannes Roith's avatar
Johannes Roith committed
626
	|
Johannes Roith's avatar
Johannes Roith committed
627
628
629
630
631
	new_non_term_id : item {:
		List<NonTerminal> lst = new ArrayList<NonTerminal>();
		lst.add((NonTerminal) item);
		RESULT = lst;
	:}
Johannes Roith's avatar
Johannes Roith committed
632
633
634
635
636
637
638
639
640
641
642
643
644
645
	;

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


precedence_list ::= precedence_l | empty; 

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

precedence_l ::= precedence_l preced | preced;

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

preced ::= 
646
	PRECEDENCE : prec
Johannes Roith's avatar
Johannes Roith committed
647
	preced_type
648
    terminal_list:lst SEMI : semi
Johannes Roith's avatar
Johannes Roith committed
649
    {:
650
651
652
	  ast_add_precedence(lst, new Range(
	  			Position.fromLocation(precxleft), 
	  			Position.fromLocation(semixright)));
Johannes Roith's avatar
Johannes Roith committed
653
654
655
656
657
658
659
    :}
	;

preced_type ::= 
	  LEFT     {: update_precedence(assoc.left);     :}
	| RIGHT    {: update_precedence(assoc.right);    :}
	| NONASSOC {: update_precedence(assoc.nonassoc); :}
Johannes Roith's avatar
Johannes Roith committed
660
661
662
663
	;

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

Johannes Roith's avatar
Johannes Roith committed
664
665
terminal_list ::= terminal_list:lst COMMA terminal_id:trm
	{:
666
667
668
		Range range = new Range(Position.fromLocation(trmxleft),
	      						Position.fromLocation(trmxright));
		((List<Terminal>) lst).add(new Terminal(new Name (trm, range), range));
Johannes Roith's avatar
Johannes Roith committed
669
670
		RESULT = lst;
	:}
Johannes Roith's avatar
Johannes Roith committed
671
	|
Johannes Roith's avatar
Johannes Roith committed
672
673
	terminal_id:trm
	{:
674
675
		Range range = new Range(Position.fromLocation(trmxleft),
	      						Position.fromLocation(trmxright));
Johannes Roith's avatar
Johannes Roith committed
676
		List<Terminal> lst = new ArrayList<Terminal>();
677
		lst.add(new Terminal(new Name (trm, range), range));
Johannes Roith's avatar
Johannes Roith committed
678
679
		RESULT = lst;
	:}
Johannes Roith's avatar
Johannes Roith committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
	;

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

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 */
698
699
	      errMan.Error(ErrorSource.Parser, "Terminal \"" + sym + 
			   "\" has not been declared", Position.fromLocation(symxleft));
Johannes Roith's avatar
Johannes Roith committed
700
701
702
703
704
705
706
	    }
          RESULT = sym;
         :};

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

start_spec ::= 
707
708
	START : start WITH nt_id : start_name 
	SEMI : semi	{: 
Johannes Roith's avatar
Johannes Roith committed
709
710
711
712
	  /* verify that the name has been declared as a non terminal */
	  non_terminal nt = (non_terminal)non_terms.get(start_name);
	  if (nt == null)
	    {
713
714
715
	    errMan.Error(ErrorSource.Parser, 
	    			"Start non terminal \"" + start_name + "\" has not been declared", 
	  		         Position.fromLocation(start_namexleft));
Johannes Roith's avatar
Johannes Roith committed
716
717
718
719
720
721
722
723
724
	    }
          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"));
Johannes Roith's avatar
Johannes Roith committed
725
726
	      add_rhs_part(new symbol_part(terminal_shared.getEOF()));
	      if (!context.getXmlActions()) add_rhs_part(new action_part("RESULT = start_val;"));
Johannes Roith's avatar
Johannes Roith committed
727
728
729
730

		  production p = new production(context, non_terminal_shared.getSTART_nt(), rhs_parts, rhs_pos);
		  //p.index();
		  context.start_production = p;
Johannes Roith's avatar
Johannes Roith committed
731
	      new_rhs();
Johannes Roith's avatar
Johannes Roith committed
732
	      
733
734
	      Range range = new Range(Position.fromLocation(startxleft), 
	      					Position.fromLocation(semixright));
735
736
	      Range innerRange = new Range(Position.fromLocation(start_namexleft),
	      							Position.fromLocation(start_namexright));
737
738
	      parserResult.startWith = new StartWith(new NonTerminal (
	      							new Name (start_name, innerRange), innerRange), range);
Johannes Roith's avatar
Johannes Roith committed
739
740
741
742
743
744
745
746
747
748
749
750
751
	    }
	:}
	|
	empty
	;

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

production_list ::= production_list production | production;

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

production ::=
Johannes Roith's avatar
Johannes Roith committed
752
	nt_id : lhs_id
Johannes Roith's avatar
Johannes Roith committed
753
754
755
756
757
758
759
	{:
	  /* 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)
	    {
Johannes Roith's avatar
Johannes Roith committed
760
	      if (errMan.getErrorCount() == 0)
761
762
763
	      errMan.Warning(ErrorSource.Parser, "LHS non terminal \"" + lhs_id + 
			       "\" has not been declared", 
	    		Position.fromLocation(lhs_idxleft));
Johannes Roith's avatar
Johannes Roith committed
764
765
766
767
768
769
770
	    }

	  /* reset the rhs accumulation */
	  new_rhs();
	:}
	COLON_COLON_EQUALS
/*	{: :}*/
Johannes Roith's avatar
Johannes Roith committed
771
	rhs_list : rhs
772
773
774
775
776
777
778
779
780
781
782
783
	SEMI : s
	{:
		parserResult.productions.add(
				new Production(
					new Name(
						(String) lhs_id,
						Range.fromLocations(lhs_idxleft, lhs_idxright)
					),
					(List<ProductionRight>) rhs,
					Range.fromLocations(lhs_idxleft, sxright)
				)
		);
Johannes Roith's avatar
Johannes Roith committed
784
	:}
Johannes Roith's avatar
Johannes Roith committed
785
	|
786
787
788
	error  : e {:
		errMan.Error(ErrorSource.Parser, "Production Syntax Error", Position.fromLocation(exleft));
	:}
Johannes Roith's avatar
Johannes Roith committed
789
790
791
792
793
	SEMI
	;

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

Johannes Roith's avatar
Johannes Roith committed
794
795
796
797
798
799
800
801
802
803
rhs_list ::= rhs_list : lst BAR rhs : item {:
		if (item != null)
			((List<ProductionRight>) lst).add((ProductionRight) item);
		RESULT = lst;
	:} | rhs : item {:
		List<ProductionRight> lst = new ArrayList<ProductionRight>();
		if (item != null)
			lst.add((ProductionRight) item);
		RESULT = lst;
	:};
Johannes Roith's avatar
Johannes Roith committed
804
805
806
807
808
809

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

rhs ::= 
	prod_part_list PERCENT_PREC term_id:term_name 
	{:
Johannes Roith's avatar
Johannes Roith committed
810
	  symbol sym = null;
Johannes Roith's avatar
Johannes Roith committed
811
812
813
814
	  if (lhs_nt != null) 
	    {
	      /* Find the precedence symbol */
	      if (term_name == null) {
815
816
	      
	      // jrTODO: report proper error!
Johannes Roith's avatar
Johannes Roith committed
817
818
819
		System.err.println("No terminal for contextual precedence");
		sym = null;
	      } else {
820
821
822
823
824
825
826
827
828
829
830
	      symbol_part sp = null;
	      try{
		      sp = (symbol_part) symbols.get(term_name);
		  } catch (NullPointerException e) {
		  	// fix crash.
			errMan.Error(ErrorSource.Parser,
					"could not resolve symbol!",
					Position.fromLocation(term_namexleft));
		  }
	      if (sp != null)
			  sym = sp.the_symbol();
Johannes Roith's avatar
Johannes Roith committed
831
832
833
834
	      }
	      /* build the production */
	      production p;
	      if ((sym!=null) && (sym instanceof terminal)) {
Johannes Roith's avatar
Johannes Roith committed
835
		p = new production(context, lhs_nt, rhs_parts, rhs_pos,
Johannes Roith's avatar
Johannes Roith committed
836
837
838
839
840
841
				   ((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");
Johannes Roith's avatar
Johannes Roith committed
842
		p = new production(context, lhs_nt, rhs_parts, rhs_pos);
Johannes Roith's avatar
Johannes Roith committed
843
844
	      }

Johannes Roith's avatar
Johannes Roith committed
845
846
	// p.index();

Johannes Roith's avatar
Johannes Roith committed
847
848
849
850
851
852
853
854
855
	      /* 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();
Johannes Roith's avatar
Johannes Roith committed
856
857
	          ast_new_rhs();
	          
Johannes Roith's avatar
Johannes Roith committed
858
	          add_rhs_part(add_lab(new symbol_part(start_nt),"start_val"));
Johannes Roith's avatar
Johannes Roith committed
859
860
	          add_rhs_part(new symbol_part(terminal_shared.getEOF()));
		  if (!context.getXmlActions()) add_rhs_part(new action_part("RESULT = start_val;"));
Johannes Roith's avatar
Johannes Roith committed
861
		  if ((sym!=null) && (sym instanceof terminal)) {
Johannes Roith's avatar
Johannes Roith committed
862
863
		    context.start_production = 
		      new production(context, non_terminal_shared.getSTART_nt(), rhs_parts, 
Johannes Roith's avatar
Johannes Roith committed
864
865
866
				     rhs_pos, ((terminal)sym).precedence_num(),
				     ((terminal)sym).precedence_side());
		  } else {
Johannes Roith's avatar
Johannes Roith committed
867
868
		    context.start_production = 
		      new production(context, non_terminal_shared.getSTART_nt(), rhs_parts, rhs_pos);
Johannes Roith's avatar
Johannes Roith committed
869
870
		  }
	          new_rhs();
Johannes Roith's avatar
Johannes Roith committed
871
	          ast_new_rhs();
Johannes Roith's avatar
Johannes Roith committed
872
873
		}
	    }
Johannes Roith's avatar
Johannes Roith committed
874
875
876
	  
	  // jrTODO
	  // RESULT = "blub!!!";
Johannes Roith's avatar
Johannes Roith committed
877
878
879

	  /* reset the rhs accumulation in any case */
	  new_rhs();
Johannes Roith's avatar
Johannes Roith committed
880
	  ast_new_rhs();
Johannes Roith's avatar
Johannes Roith committed
881
882
	:}
	|
883
	prod_part_list : ppl
Johannes Roith's avatar
Johannes Roith committed
884
885
886
	{:
	
		
Johannes Roith's avatar
Johannes Roith committed
887
888
889
	  if (lhs_nt != null) 
	    {
	      /* build the production */
Johannes Roith's avatar
Johannes Roith committed
890
891
	      production p = new production(context, lhs_nt, rhs_parts, rhs_pos);

Johannes Roith's avatar
Johannes Roith committed
892
893
894
895
		RESULT = new ProductionRight(p.index(),
                            p.get_embedded_actions(), 
                            ast_get_rhs_parts_copy(), 
					        Range.fromLocations(pplxleft, pplxright));
Johannes Roith's avatar
Johannes Roith committed
896

Johannes Roith's avatar
Johannes Roith committed
897
898
899
900

	      /* 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. */
Johannes Roith's avatar
Johannes Roith committed
901
        if (start_nt == null)
Johannes Roith's avatar
Johannes Roith committed
902
903
904
905
906
		{
		  start_nt = lhs_nt;

	          /* build a special start production */
	          new_rhs();
Johannes Roith's avatar
Johannes Roith committed
907
908
	          ast_new_rhs();
	          
Johannes Roith's avatar
Johannes Roith committed
909
	          add_rhs_part(add_lab(new symbol_part(start_nt),"start_val"));
Johannes Roith's avatar
Johannes Roith committed
910
911
912
913
	          add_rhs_part(new symbol_part(terminal_shared.getEOF()));
		  if (!context.getXmlActions()) add_rhs_part(new action_part("RESULT = start_val;"));
	          context.start_production = 
		     new production(context, non_terminal_shared.getSTART_nt(), rhs_parts, rhs_pos);
Johannes Roith's avatar
Johannes Roith committed
914
915

	          new_rhs();
Johannes Roith's avatar
Johannes Roith committed
916
	          ast_new_rhs();
Johannes Roith's avatar
Johannes Roith committed
917
918
919
		}
	    }
	  
Johannes Roith's avatar
Johannes Roith committed
920
921
	  
	  
Johannes Roith's avatar
Johannes Roith committed
922
923
	  /* reset the rhs accumulation in any case */
	  new_rhs();
Johannes Roith's avatar
Johannes Roith committed
924
	  ast_new_rhs();
Johannes Roith's avatar
Johannes Roith committed
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
	:} 
	;

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

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 */
Johannes Roith's avatar
Johannes Roith committed
941
942
	  if (symb == null) {
	      if (errMan.getErrorCount() == 0)
943
944
	        errMan.Error(ErrorSource.Parser, "Symbol \"" + symid + 
			       "\" has not been declared", Position.fromLocation(symidxleft));
Johannes Roith's avatar
Johannes Roith committed
945
946
	  }
	  else {
Johannes Roith's avatar
Johannes Roith committed
947
948
	      /* add a labeled production part */
	      add_rhs_part(add_lab(symb, labid));
949

950
		  ProductionSymbolRef ref = null;
951
952
953
		  Range range = new Range (Position.fromLocation(symidxleft), 
	      							Position.fromLocation(symidxright));
		  if (labid == null)
954
			  ref = new ProductionSymbolRef(new Name(symid, range), range);
Johannes Roith's avatar
Johannes Roith committed
955
956
		  else
			  ref = new LabeledProductionSymbolRef(new Name(symid, range), labid, range);
957
	      ast_add_rhs_part(ref);
Johannes Roith's avatar
Johannes Roith committed
958
959
960
	    }
	:}
	|
Johannes Roith's avatar
Johannes Roith committed
961
962
	CODE_STRING : code_str
	{:
Johannes Roith's avatar
Johannes Roith committed
963
	  /* add a new production part */
Johannes Roith's avatar
Johannes Roith committed
964
965
966
967

	  int debug_id = get_new_debug_id();
	  add_rhs_part(new action_part(attach_debug_symbol(debug_id, code_str)));
	  ast_add_rhs_part(new ActionCodeBlock(debug_id, code_str, 
968
969
	  		Range.fromLocations(code_strxleft, code_strxright)
	  )); // jrTODO: positions
Johannes Roith's avatar
Johannes Roith committed
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
	:}
	;

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

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 
        {: multipart_names.push(multipart_name); multipart_name="";:}
        LT typearglist:types GT
         {: multipart_name =
         ((String)multipart_names.pop()).concat("<"+types+">"); :}
        |
	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
	;

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

1039
1040
1041
type_id ::= multipart_id {:
		RESULT = multipart_name;
	:}
Johannes Roith's avatar
Johannes Roith committed
1042
	| type_id LBRACK RBRACK
1043
1044
1045
1046
	{:
		multipart_name = multipart_name.concat("[]");
		RESULT = multipart_name;
	:}
Johannes Roith's avatar
Johannes Roith committed
1047
1048
1049
1050
1051
       	;

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

new_term_id ::= 
1052
	ID : term_id 
Johannes Roith's avatar
Johannes Roith committed
1053
1054
1055
1056
1057
	{: 
	  /* see if this terminal has been declared before */
	  if (symbols.get(term_id) != null)
	    {
	      /* issue a message */
1058
1059
	      errMan.Error(ErrorSource.Parser, "Symbol \"" + term_id + 
			   "\" has already been declared", Position.fromLocation(term_idxleft));
Johannes Roith's avatar
Johannes Roith committed
1060
1061
1062
1063
1064
	    }
	  else
	    {
	      /* if no type declared, declare one */
	      if (multipart_name.equals("")) {
Johannes Roith's avatar
Johannes Roith committed
1065
			multipart_name = "Object";
Johannes Roith's avatar
Johannes Roith committed
1066
	      }
Johannes Roith's avatar
Johannes Roith committed
1067
	      
Johannes Roith's avatar
Johannes Roith committed
1068
1069
	      /* build a production_part and put it in the table */ 
	      symbols.put(term_id, 
Johannes Roith's avatar
Johannes Roith committed
1070
1071
	        new symbol_part(new terminal(context, term_id, multipart_name)));
	        
1072
1073
1074
	     Range range = new Range(Position.fromLocation(term_idxleft), 
	     					Position.fromLocation(term_idxright));
	     RESULT = new Terminal(new Name(term_id, range), range);
Johannes Roith's avatar
Johannes Roith committed
1075
	        
Johannes Roith's avatar
Johannes Roith committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
	    }
	:}
	;

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

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 */
1089
1090
	      errMan.Error(ErrorSource.Parser, "Symbol \"" + non_term_id + 
			          "\" has already been declared", Position.fromLocation(non_term_idxleft));
Johannes Roith's avatar
Johannes Roith committed
1091
1092
1093
1094
	    }
	  else
	    {
	      if (multipart_name.equals("")) {
1095
			multipart_name ="Object";
Johannes Roith's avatar
Johannes Roith committed
1096
1097
1098
	      }
	      /* build the non terminal object */
              non_terminal this_nt = 
Johannes Roith's avatar
Johannes Roith committed
1099
		new non_terminal(context, non_term_id, multipart_name);
Johannes Roith's avatar
Johannes Roith committed
1100
1101
1102
1103
1104
1105

	      /* 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));
Johannes Roith's avatar
Johannes Roith committed
1106
1107
1108

	      // jrTODO: positions!
	      // jrTODO: non_term_id vs. multipart_name
1109
1110
1111
	      Range range = new Range(Position.fromLocation(non_term_idxleft),
	      						Position.fromLocation(non_term_idxright));
	      RESULT = new NonTerminal(new Name(non_term_id, range), range);
Johannes Roith's avatar
Johannes Roith committed
1112
1113
1114
1115
1116
1117
1118
1119
1120
	    }
	:}
	;

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

nt_id ::=
	ID:the_id
	{: RESULT = the_id; :}
1121
	| error : e
Johannes Roith's avatar
Johannes Roith committed
1122
	{:
1123
1124
1125
		errMan.Error(ErrorSource.Parser, 
						"Illegal use of reserved word", 
						Position.fromLocation(exleft));
Johannes Roith's avatar
Johannes Roith committed
1126
1127
1128
1129
1130
1131
1132
1133
1134
		RESULT="ILLEGAL";
	:}
	;

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

symbol_id ::=
	ID:the_id
	{: RESULT = the_id; :}
1135
	| error : e
Johannes Roith's avatar
Johannes Roith committed
1136
	{:
1137
1138
1139
		errMan.Error(ErrorSource.Parser, 
					"Illegal use of reserved word",
					Position.fromLocation(exleft));
Johannes Roith's avatar
Johannes Roith committed
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
		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"; :}
1171
	| error : e
Johannes Roith's avatar
Johannes Roith committed
1172
	{:
1173
1174
1175
		errMan.Error(ErrorSource.Parser, 
					"Illegal use of reserved word",
					Position.fromLocation(exleft));
Johannes Roith's avatar
Johannes Roith committed
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
		RESULT="ILLEGAL";
	:}
	;

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

non_terminal ::= NON TERMINAL | NONTERMINAL;

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

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

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

empty ::= /* nothing */;

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