parser.cup 34.8 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
200
201
202
203
204
205
206




  Stack<ComplexSymbol> head_tokens = new Stack<ComplexSymbol> ();
  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();
  }

  protected void ast_add_precedence(ComplexSymbol head, Object lst)
  {
	Precedence p = new Precedence(_cur_side, (List<Terminal>) lst, 
207
208
209
210
	  							new Range(Position.fromComplexSymbolLeft(head),
	  								Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()
	  							)
	  							));
Johannes Roith's avatar
Johannes Roith committed
211
212
213
	parserResult.precedences.add(p);
  }
  
Johannes Roith's avatar
Johannes Roith committed
214
215
216
217
218
219
220
:};

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

parser code {:

  private java.io.InputStream input_stream;
Johannes Roith's avatar
Johannes Roith committed
221
222
223
224
225
226
227
228
229
230
231
  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
232
233
234
  	this.input_stream = is;
  }
  
Johannes Roith's avatar
Johannes Roith committed
235
236
237
238
  public ParserResult getResult() {
  	return parserResult;
  }
  
Johannes Roith's avatar
Johannes Roith committed
239
240
241
242
243
244
245
 /* override error routines */
  protected Lexer lexer;
  public void report_fatal_error(
    String   message,
    Object   info)
    {
      done_parsing();
Johannes Roith's avatar
Johannes Roith committed
246
      if (info instanceof Symbol)
247
      	errMan.Fatal(ErrorSource.Parser, message + "\nCan't recover from previous error(s), giving up.",(ComplexSymbol)info);
Johannes Roith's avatar
Johannes Roith committed
248
      else
249
      	errMan.Fatal(ErrorSource.Parser, message + "\nCan't recover from previous error(s), giving up.",(ComplexSymbol)cur_token);
Johannes Roith's avatar
Johannes Roith committed
250
      throw new ParserAbortException();
Johannes Roith's avatar
Johannes Roith committed
251
252
253
254
255
    }

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

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

Johannes Roith's avatar
Johannes Roith committed
264
init with {:
Johannes Roith's avatar
Johannes Roith committed
265
266
    ComplexSymbolFactory f = new ComplexSymbolFactory();
    symbolFactory = f;
Johannes Roith's avatar
Johannes Roith committed
267
268
    parserResult = new ParserResult();
    lexer = new Lexer(errMan, f, this.input_stream);
Johannes Roith's avatar
Johannes Roith committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
:}
/*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
283
terminal String ID, CODE_STRING;
Johannes Roith's avatar
Johannes Roith committed
284
285
286
287
288
289
290
291

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
292
  precedence_list, preced, preced_type, terminal_list, precedence_l, declares_term, 
Johannes Roith's avatar
Johannes Roith committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  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
307
          symbols.put("error", new symbol_part(terminal_shared.getError()));
Johannes Roith's avatar
Johannes Roith committed
308
309

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

	  parserResult.pack = new de.in.tum.www2.cup.ast.Package(
343
344
345
	  					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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

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

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

import_list ::=
	import_list
	import_spec
	|
	empty
	;

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

import_spec ::=
Johannes Roith's avatar
Johannes Roith committed
367
368
369
370
371
	IMPORT {:
		head_tokens.push((ComplexSymbol) stack.peek());
	:}
	import_id
	{:
Johannes Roith's avatar
Johannes Roith committed
372
	  /* save this import on the imports list */
Johannes Roith's avatar
Johannes Roith committed
373
374
375
	  context.import_list.push(multipart_name);

      Import imprt = new Import(
376
377
378
      					new Name (multipart_name, null), // TODO: from, to 
	  					new Range(Position.fromComplexSymbolLeft(head_tokens.pop()),
      					Position.fromComplexSymbolRight((ComplexSymbol) cur_token)));
Johannes Roith's avatar
Johannes Roith committed
379
380
      parserResult.imports.add(imprt);
      
Johannes Roith's avatar
Johannes Roith committed
381
382
383
384
385
386
387
388
	  /* reset the accumulated multipart name */
	  multipart_name = new String();
	:}
	SEMI
	;

class_name ::=
    empty |
Johannes Roith's avatar
Johannes Roith committed
389
390
391
    CLASS {:
    	head_tokens.push((ComplexSymbol) stack.peek());
    :} ID:id SEMI
Johannes Roith's avatar
Johannes Roith committed
392
    {:
Johannes Roith's avatar
Johannes Roith committed
393
394
395
396
     context.parser_class_name = id;
     context.symbol_const_class_name = id + "Sym";
     
     parserResult.className = new ClassName (
397
398
399
     							new Name(id, null), //jrTODO: positions
								new Range(Position.fromComplexSymbolLeft(head_tokens.pop()),
								Position.fromComplexSymbolRight((ComplexSymbol) stack.peek())));
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 ::= 
Johannes Roith's avatar
Johannes Roith committed
416
417
418
	ACTION {:
		head_tokens.push((ComplexSymbol) stack.peek());
	:} CODE CODE_STRING : user_code opt_semi
Johannes Roith's avatar
Johannes Roith committed
419
	{:
Johannes Roith's avatar
Johannes Roith committed
420
	  if (context.action_code!=null)
421
422
423
	    errMan.Warning(ErrorSource.Parser,
	    		"Redundant action code (skipping)",
	    		Position.fromLocation(user_codexleft));
Johannes Roith's avatar
Johannes Roith committed
424
425
	  else { /* save the user included code string */
	    context.action_code = user_code;
426
427
428
429
430
	    parserResult.actionCode = new SpecialCodeBlock(
	    							SpecialCodeBlock.BlockType.Action,
	    							user_code, 
	    							new Range(Position.fromComplexSymbolLeft(head_tokens.pop()),
	    							Position.fromComplexSymbolRight((ComplexSymbol) cur_token))); // jrTODO!
Johannes Roith's avatar
Johannes Roith committed
431
	  }
Johannes Roith's avatar
Johannes Roith committed
432
433
434
435
436
437
	:}
	;

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

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

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

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

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

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

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

symbol_list ::= symbol_list symbol | symbol;

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

symbol ::= 
	TERMINAL
Johannes Roith's avatar
Johannes Roith committed
509
510
511
512
513
514
515
516
517
518
	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,
519
520
							new Range(Position.fromComplexSymbolLeft(terminal),
							Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
Johannes Roith's avatar
Johannes Roith committed
521
	:}
Johannes Roith's avatar
Johannes Roith committed
522
523
	|
	TERMINAL
Johannes Roith's avatar
Johannes Roith committed
524
525
526
527
528
529
530
	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,
531
532
							new Range(Position.fromComplexSymbolLeft(terminal),
							Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
Johannes Roith's avatar
Johannes Roith committed
533
534
		;
	:}
Johannes Roith's avatar
Johannes Roith committed
535
536
	|
	non_terminal
Johannes Roith's avatar
Johannes Roith committed
537
538
539
540
541
542
543
544
545
	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,
546
547
							new Range(Position.fromComplexSymbolLeft(nonTerminal),
							Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
Johannes Roith's avatar
Johannes Roith committed
548
	:}
Johannes Roith's avatar
Johannes Roith committed
549
550
	|
	non_terminal
Johannes Roith's avatar
Johannes Roith committed
551
552
553
554
555
556
557
	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,
558
559
							new Range(Position.fromComplexSymbolLeft(nonTerminal),
							Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
Johannes Roith's avatar
Johannes Roith committed
560
561
	:}
	
Johannes Roith's avatar
Johannes Roith committed
562
	|
Johannes Roith's avatar
Johannes Roith committed
563
	
Johannes Roith's avatar
Johannes Roith committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
	/* 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
586
	term_name_list : lst
Johannes Roith's avatar
Johannes Roith committed
587
588
589
	{: 
	  /* reset the accumulated multipart name */
	  multipart_name = new String();
Johannes Roith's avatar
Johannes Roith committed
590
	  RESULT = lst;
Johannes Roith's avatar
Johannes Roith committed
591
592
593
594
595
596
	:}
	SEMI
	;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ 

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

Johannes Roith's avatar
Johannes Roith committed
607
608
609
610
611
612
613
614
615
616
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
617
618
619
620

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

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

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


precedence_list ::= precedence_l | empty; 

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

precedence_l ::= precedence_l preced | preced;

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

preced ::= 
Johannes Roith's avatar
Johannes Roith committed
645
	PRECEDENCE
Johannes Roith's avatar
Johannes Roith committed
646
	{:
Johannes Roith's avatar
Johannes Roith committed
647
648
649
650
651
652
653
654
655
656
657
658
659
660
		head_tokens.push((ComplexSymbol) stack.peek());
	:}
	preced_type
    terminal_list:lst SEMI
    {:
	  ComplexSymbol head = head_tokens.pop();
	  ast_add_precedence(head, lst);
    :}
	;

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

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

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

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

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

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

start_spec ::= 
Johannes Roith's avatar
Johannes Roith committed
708
709
710
	START{:
		head_tokens.push((ComplexSymbol) stack.peek());
	:} WITH nt_id:start_name 
Johannes Roith's avatar
Johannes Roith committed
711
712
713
714
715
	{: 
	  /* verify that the name has been declared as a non terminal */
	  non_terminal nt = (non_terminal)non_terms.get(start_name);
	  if (nt == null)
	    {
716
717
718
	    errMan.Error(ErrorSource.Parser, 
	    			"Start non terminal \"" + start_name + "\" has not been declared", 
	  		         Position.fromLocation(start_namexleft));
Johannes Roith's avatar
Johannes Roith committed
719
720
721
722
723
724
725
726
727
	    }
          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
728
729
730
731
	      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
732
	      new_rhs();
Johannes Roith's avatar
Johannes Roith committed
733
734
735
	      
	      //jrTODO: position, start symbol?, side effects?
	      parserResult.startWith = new StartWith(
736
737
738
	      							new NonTerminal (start_name, null),
	      							new Range(Position.fromComplexSymbolLeft(head_tokens.pop()),
	      							Position.fromComplexSymbolRight((ComplexSymbol) stack.peek())));
Johannes Roith's avatar
Johannes Roith committed
739
	      
Johannes Roith's avatar
Johannes Roith committed
740
741
742
743
744
745
746
747
748
749
750
751
752
753
	    }
	:}
	SEMI
	|
	empty
	;

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

production_list ::= production_list production | production;

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

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

	  /* reset the rhs accumulation */
	  new_rhs();
	:}
	COLON_COLON_EQUALS
/*	{: :}*/
Johannes Roith's avatar
Johannes Roith committed
773
	rhs_list : rhs
774
775
776
777
778
779
780
781
782
783
784
785
	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
786
	:}
Johannes Roith's avatar
Johannes Roith committed
787
	|
788
789
790
	error  : e {:
		errMan.Error(ErrorSource.Parser, "Production Syntax Error", Position.fromLocation(exleft));
	:}
Johannes Roith's avatar
Johannes Roith committed
791
792
793
794
795
	SEMI
	;

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

Johannes Roith's avatar
Johannes Roith committed
796
797
798
799
800
801
802
803
804
805
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
806
807
808
809
810
811

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

rhs ::= 
	prod_part_list PERCENT_PREC term_id:term_name 
	{:
Johannes Roith's avatar
Johannes Roith committed
812
	  symbol sym = null;
Johannes Roith's avatar
Johannes Roith committed
813
814
815
816
	  if (lhs_nt != null) 
	    {
	      /* Find the precedence symbol */
	      if (term_name == null) {
817
818
	      
	      // jrTODO: report proper error!
Johannes Roith's avatar
Johannes Roith committed
819
820
821
		System.err.println("No terminal for contextual precedence");
		sym = null;
	      } else {
822
823
824
825
826
827
828
829
830
831
832
	      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
833
834
835
836
	      }
	      /* build the production */
	      production p;
	      if ((sym!=null) && (sym instanceof terminal)) {
Johannes Roith's avatar
Johannes Roith committed
837
		p = new production(context, lhs_nt, rhs_parts, rhs_pos,
Johannes Roith's avatar
Johannes Roith committed
838
839
840
841
842
843
				   ((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
844
		p = new production(context, lhs_nt, rhs_parts, rhs_pos);
Johannes Roith's avatar
Johannes Roith committed
845
846
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);

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

Johannes Roith's avatar
Johannes Roith committed
895
896
897
898

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

	          /* build a special start production */
	          new_rhs();
Johannes Roith's avatar
Johannes Roith committed
905
906
	          ast_new_rhs();
	          
Johannes Roith's avatar
Johannes Roith committed
907
	          add_rhs_part(add_lab(new symbol_part(start_nt),"start_val"));
Johannes Roith's avatar
Johannes Roith committed
908
909
910
911
	          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
912
913

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

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

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

948
949
950
951
952
953
954
955
		  ProductionRef ref = null;
		  Range range = new Range (Position.fromLocation(symidxleft), 
	      							Position.fromLocation(symidxright));
		  if (labid == null)
			  ref = new LabeledProductionRef(new Name(symid, range), labid, range);
		  else
			  ref = new ProductionRef(new Name(symid, range), range);
	      ast_add_rhs_part(ref);
Johannes Roith's avatar
Johannes Roith committed
956
957
958
	    }
	:}
	|
Johannes Roith's avatar
Johannes Roith committed
959
960
	CODE_STRING : code_str
	{:
Johannes Roith's avatar
Johannes Roith committed
961
962
	  /* add a new production part */
	  add_rhs_part(new action_part(code_str));
963
964
965
	  ast_add_rhs_part(new ActionCodeBlock(code_str, 
	  		Range.fromLocations(code_strxleft, code_strxright)
	  )); // jrTODO: positions
Johannes Roith's avatar
Johannes Roith committed
966
967
968
969
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
	:}
	;

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

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; :}