-
Notifications
You must be signed in to change notification settings - Fork 1
/
production.cs
824 lines (686 loc) · 24.5 KB
/
production.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
175
176
177
178
179
180
181
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
namespace CUP
{
using System;
/// <summary>This class represents a production in the grammar. It contains
/// a LHS non terminal, and an array of RHS symbols. As various
/// transformations are done on the RHS of the production, it may shrink.
/// As a result a separate length is always maintained to indicate how much
/// of the RHS array is still valid.<p>
///
/// I addition to construction and manipulation operations, productions provide
/// methods for factoring out actions (see remove_embedded_actions()), for
/// computing the nullability of the production (i.e., can it derive the empty
/// string, see check_nullable()), and operations for computing its first
/// set (i.e., the set of terminals that could appear at the beginning of some
/// string derived from the production, see check_first_set()).
///
/// </summary>
/// <seealso cref=" CUP.production_part
/// "/>
/// <seealso cref=" CUP.symbol_part
/// "/>
/// <seealso cref=" CUP.action_part
/// "/>
/// <version> last updated: 7/3/96
/// </version>
/// <author> Frank Flannery
///
/// </author>
public class production
{
private void InitBlock()
{
_first_set = new terminal_set();
}
/*-----------------------------------------------------------*/
/*--- Constructor(s) ----------------------------------------*/
/*-----------------------------------------------------------*/
/// <summary>Full constructor. This constructor accepts a LHS non terminal,
/// an array of RHS parts (including terminals, non terminals, and
/// actions), and a string for a final reduce action. It does several
/// manipulations in the process of creating a production object.
/// After some validity checking it translates labels that appear in
/// actions into code for accessing objects on the runtime parse stack.
/// It them merges adjacent actions if they appear and moves any trailing
/// action into the final reduce actions string. Next it removes any
/// embedded actions by factoring them out with new action productions.
/// Finally it assigns a unique index to the production.<p>
/// *
/// Factoring out of actions is accomplished by creating new "hidden"
/// non terminals. For example if the production was originally: <pre>
/// A ::= B {action} C D
/// </pre>
/// then it is factored into two productions:<pre>
/// A ::= B X C D
/// X ::= {action}
/// </pre>
/// (where X is a unique new non terminal). This has the effect of placing
/// all actions at the end where they can be handled as part of a reduce by
/// the parser.
/// </summary>
public production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, string action_str)
{
InitBlock();
int i;
action_part tail_action;
System.String declare_str;
int rightlen = rhs_l;
/* remember the length */
if (rhs_l >= 0)
_rhs_length = rhs_l;
else if (rhs_parts != null)
_rhs_length = rhs_parts.Length;
else
_rhs_length = 0;
/* make sure we have a valid left-hand-side */
if (lhs_sym == null)
throw new internal_error("Attempt to construct a production with a null LHS");
/* I'm not translating labels anymore, I'm adding code to declare
labels as valid variables. This way, the users code string is
untouched
6/96 frankf */
/* check if the last part of the right hand side is an action. If
it is, it won't be on the stack, so we don't want to count it
in the rightlen. Then when we search down the stack for a
Symbol, we don't try to search past action */
if (rhs_l > 0)
{
if (rhs_parts[rhs_l - 1].is_action())
{
rightlen = rhs_l - 1;
}
else
{
rightlen = rhs_l;
}
}
/* get the generated declaration code for the necessary labels. */
declare_str = declare_labels(rhs_parts, rightlen, action_str);
if (action_str == null)
action_str = declare_str;
else
action_str = declare_str + action_str;
/* count use of lhs */
lhs_sym.note_use();
/* create the part for left-hand-side */
_lhs = new symbol_part(lhs_sym);
/* merge adjacent actions (if any) */
_rhs_length = merge_adjacent_actions(rhs_parts, _rhs_length);
/* strip off any trailing action */
tail_action = strip_trailing_action(rhs_parts, _rhs_length);
if (tail_action != null)
_rhs_length--;
/* Why does this run through the right hand side happen
over and over? here a quick combination of two
prior runs plus one I wanted of my own
frankf 6/25/96 */
/* allocate and copy over the right-hand-side */
/* count use of each rhs symbol */
_rhs = new production_part[_rhs_length];
for (i = 0; i < _rhs_length; i++)
{
_rhs[i] = rhs_parts[i];
if (!_rhs[i].is_action())
{
((symbol_part) _rhs[i]).the_symbol().note_use();
if (((symbol_part) _rhs[i]).the_symbol() is terminal)
{
_rhs_prec = ((terminal) ((symbol_part) _rhs[i]).the_symbol()).precedence_num();
_rhs_assoc = ((terminal) ((symbol_part) _rhs[i]).the_symbol()).precedence_side();
}
}
}
/*now action string is really declaration string, so put it in front!
6/14/96 frankf */
if (action_str == null)
action_str = "";
if (tail_action != null && tail_action.code_string() != null)
action_str = action_str + "\t\t" + tail_action.code_string();
/* stash the action */
_action = new action_part(action_str);
/* rewrite production to remove any embedded actions */
remove_embedded_actions();
/* assign an index */
_index = next_index++;
/* put us in the global collection of productions */
SupportClass.PutElement(_all, _index, this);
/* put us in the production list of the lhs non terminal */
lhs_sym.add_production(this);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Constructor with no action string.
/// </summary>
public production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l):this(lhs_sym, rhs_parts, rhs_l, null)
{
InitBlock();
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/* Constructor with precedence and associativity of production
contextually define */
public production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, string action_str, int prec_num, int prec_side):this(lhs_sym, rhs_parts, rhs_l, action_str)
{
InitBlock();
/* set the precedence */
set_precedence_num(prec_num);
set_precedence_side(prec_side);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/* Constructor w/ no action string and contextual precedence
defined */
public production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, int prec_num, int prec_side):this(lhs_sym, rhs_parts, rhs_l, null)
{
InitBlock();
/* set the precedence */
set_precedence_num(prec_num);
set_precedence_side(prec_side);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/*-----------------------------------------------------------*/
/*--- (Access to) Static (Class) Variables ------------------*/
/*-----------------------------------------------------------*/
/// <summary>Table of all productions. Elements are stored using their index as
/// the key.
/// </summary>
protected internal static System.Collections.Hashtable _all = new System.Collections.Hashtable();
/// <summary>Access to all productions.
/// </summary>
public static System.Collections.IEnumerator all()
{
return _all.Values.GetEnumerator();
}
/// <summary>Lookup a production by index.
/// </summary>
public static production find(int indx)
{
return (production) _all[indx];
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Total number of productions.
/// </summary>
public static int number()
{
return _all.Count;
}
/// <summary>Static counter for assigning unique index numbers.
/// </summary>
protected internal static int next_index;
/*-----------------------------------------------------------*/
/*--- (Access to) Instance Variables ------------------------*/
/*-----------------------------------------------------------*/
/// <summary>The left hand side non-terminal.
/// </summary>
protected internal symbol_part _lhs;
/// <summary>The left hand side non-terminal.
/// </summary>
public virtual symbol_part lhs()
{
return _lhs;
}
/// <summary>The precedence of the rule
/// </summary>
protected internal int _rhs_prec = - 1;
protected internal int _rhs_assoc = - 1;
/// <summary>Access to the precedence of the rule
/// </summary>
public virtual int precedence_num()
{
return _rhs_prec;
}
public virtual int precedence_side()
{
return _rhs_assoc;
}
/// <summary>Setting the precedence of a rule
/// </summary>
public virtual void set_precedence_num(int prec_num)
{
_rhs_prec = prec_num;
}
public virtual void set_precedence_side(int prec_side)
{
_rhs_assoc = prec_side;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>A collection of parts for the right hand side.
/// </summary>
protected internal production_part[] _rhs;
/// <summary>Access to the collection of parts for the right hand side.
/// </summary>
public virtual production_part rhs(int indx)
{
if (indx >= 0 && indx < _rhs_length)
return _rhs[indx];
else
throw new internal_error("Index out of range for right hand side of production");
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>How much of the right hand side array we are presently using.
/// </summary>
protected internal int _rhs_length;
/// <summary>How much of the right hand side array we are presently using.
/// </summary>
public virtual int rhs_length()
{
return _rhs_length;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>An action_part containing code for the action to be performed when we
/// reduce with this production.
/// </summary>
protected internal action_part _action;
/// <summary>An action_part containing code for the action to be performed when we
/// reduce with this production.
/// </summary>
public virtual action_part action()
{
return _action;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Index number of the production.
/// </summary>
protected internal int _index;
/// <summary>Index number of the production.
/// </summary>
public virtual int index()
{
return _index;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Count of number of reductions using this production.
/// </summary>
protected internal int _num_reductions = 0;
/// <summary>Count of number of reductions using this production.
/// </summary>
public virtual int num_reductions()
{
return _num_reductions;
}
/// <summary>Increment the count of reductions with this non-terminal
/// </summary>
public virtual void note_reduction_use()
{
_num_reductions++;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Is the nullability of the production known or unknown?
/// </summary>
protected internal bool _nullable_known = false;
/// <summary>Is the nullability of the production known or unknown?
/// </summary>
public virtual bool nullable_known()
{
return _nullable_known;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Nullability of the production (can it derive the empty string).
/// </summary>
protected internal bool _nullable = false;
/// <summary>Nullability of the production (can it derive the empty string).
/// </summary>
public virtual bool nullable()
{
return _nullable;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>First set of the production. This is the set of terminals that
/// could appear at the front of some string derived from this production.
/// </summary>
//UPGRADE_NOTE: The initialization of '_first_set' was moved to method 'InitBlock'. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1005"'
protected internal terminal_set _first_set;
/// <summary>First set of the production. This is the set of terminals that
/// could appear at the front of some string derived from this production.
/// </summary>
public virtual terminal_set first_set()
{
return _first_set;
}
/*-----------------------------------------------------------*/
/*--- Static Methods ----------------------------------------*/
/*-----------------------------------------------------------*/
/// <summary>Determine if a given character can be a label id starter.
/// </summary>
/// <param name="c">the character in question.
///
/// </param>
protected internal static bool is_id_start(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == '_');
//later need to handle non-8-bit chars here
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Determine if a character can be in a label id.
/// </summary>
/// <param name="c">the character in question.
///
/// </param>
protected internal static bool is_id_char(char c)
{
return is_id_start(c) || (c >= '0' && c <= '9');
}
/*-----------------------------------------------------------*/
/*--- General Methods ---------------------------------------*/
/*-----------------------------------------------------------*/
/// <summary>Return label declaration code
/// </summary>
/// <param name="labelname"> the label name
/// </param>
/// <param name="stack_type"> the stack type of label?
/// </param>
/// <author> frankf
///
/// </author>
protected internal virtual string make_declaration(string labelname, string stack_type, int offset)
{
System.String ret;
/* Put in the left/right value labels */
if (emit.lr_values())
ret = "\t\tint " + labelname + "left = ((CUP.runtime.Symbol)" + emit.pre("stack") + ".Peek(" + emit.pre("top") + "-" + offset + ")).left;\n" + "\t\tint " + labelname + "right = ((CUP.runtime.Symbol)" + emit.pre("stack") + ".Peek(" + emit.pre("top") + "-" + offset + ")).right;\n";
else
ret = "";
/* otherwise, just declare label. */
return ret + "\t\t" + stack_type + " " + labelname + " = (" + stack_type + ")((" + "CUP.runtime.Symbol) " + emit.pre("stack") + ".Peek(" + emit.pre("top") + "-" + offset + ")).Value;\n";
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Declare label names as valid variables within the action string
/// </summary>
/// <param name="rhs"> array of RHS parts.
/// </param>
/// <param name="rhs_len"> how much of rhs to consider valid.
/// </param>
/// <param name="final_action">the final action string of the production.
/// </param>
/// <param name="lhs_type"> the object type associated with the LHS symbol.
///
/// </param>
protected internal virtual string declare_labels(production_part[] rhs, int rhs_len, string final_action)
{
System.String declaration = "";
symbol_part part;
// action_part act_part;
int pos;
/* walk down the parts and extract the labels */
for (pos = 0; pos < rhs_len; pos++)
{
if (!rhs[pos].is_action())
{
part = (symbol_part) rhs[pos];
/* if it has a label, make declaration! */
if (part.label() != null)
{
declaration = declaration + make_declaration(part.label(), part.the_symbol().stack_type(), rhs_len - pos - 1);
}
}
}
return declaration;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Helper routine to merge adjacent actions in a set of RHS parts
/// </summary>
/// <param name="rhs_parts">array of RHS parts.
/// </param>
/// <param name="len"> amount of that array that is valid.
/// </param>
/// <returns> remaining valid length.
///
/// </returns>
protected internal virtual int merge_adjacent_actions(production_part[] rhs_parts, int len)
{
int from_loc, to_loc, merge_cnt;
/* bail out early if we have no work to do */
if (rhs_parts == null || len == 0)
return 0;
merge_cnt = 0;
to_loc = - 1;
for (from_loc = 0; from_loc < len; from_loc++)
{
/* do we go in the current position or one further */
if (to_loc < 0 || !rhs_parts[to_loc].is_action() || !rhs_parts[from_loc].is_action())
{
/* next one */
to_loc++;
/* clear the way for it */
if (to_loc != from_loc)
rhs_parts[to_loc] = null;
}
/* if this is not trivial? */
if (to_loc != from_loc)
{
/* do we merge or copy? */
if (rhs_parts[to_loc] != null && rhs_parts[to_loc].is_action() && rhs_parts[from_loc].is_action())
{
/* merge */
rhs_parts[to_loc] = new action_part(((action_part) rhs_parts[to_loc]).code_string() + ((action_part) rhs_parts[from_loc]).code_string());
merge_cnt++;
}
else
{
/* copy */
rhs_parts[to_loc] = rhs_parts[from_loc];
}
}
}
/* return the used length */
return len - merge_cnt;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Helper routine to strip a trailing action off rhs and return it
/// </summary>
/// <param name="rhs_parts">array of RHS parts.
/// </param>
/// <param name="len"> how many of those are valid.
/// </param>
/// <returns> the removed action part.
///
/// </returns>
protected internal virtual action_part strip_trailing_action(production_part[] rhs_parts, int len)
{
action_part result;
/* bail out early if we have nothing to do */
if (rhs_parts == null || len == 0)
return null;
/* see if we have a trailing action */
if (rhs_parts[len - 1].is_action())
{
/* snip it out and return it */
result = (action_part) rhs_parts[len - 1];
rhs_parts[len - 1] = null;
return result;
}
else
return null;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Remove all embedded actions from a production by factoring them
/// out into individual action production using new non terminals.
/// if the original production was: <pre>
/// A ::= B {action1} C {action2} D
/// </pre>
/// then it will be factored into: <pre>
/// A ::= B NT$1 C NT$2 D
/// NT$1 ::= {action1}
/// NT$2 ::= {action2}
/// </pre>
/// where NT$1 and NT$2 are new system created non terminals.
/// </summary>
/* the declarations added to the parent production are also passed along,
as they should be perfectly valid in this code string, since it
was originally a code string in the parent, not on its own.
frank 6/20/96 */
protected internal virtual void remove_embedded_actions()
{
non_terminal new_nt;
production new_prod;
System.String declare_str;
/* walk over the production and process each action */
for (int act_loc = 0; act_loc < rhs_length(); act_loc++)
if (rhs(act_loc).is_action())
{
declare_str = declare_labels(_rhs, act_loc, "");
/* create a new non terminal for the action production */
new_nt = non_terminal.create_new();
new_nt.is_embedded_action = true; /* 24-Mar-1998, CSA */
/* create a new production with just the action */
new_prod = new action_production(this, new_nt, null, 0, declare_str + ((action_part) rhs(act_loc)).code_string());
/* replace the action with the generated non terminal */
_rhs[act_loc] = new symbol_part(new_nt);
}
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Check to see if the production (now) appears to be nullable.
/// A production is nullable if its RHS could derive the empty string.
/// This results when the RHS is empty or contains only non terminals
/// which themselves are nullable.
/// </summary>
public virtual bool check_nullable()
{
production_part part;
symbol sym;
int pos;
/* if we already know bail out early */
if (nullable_known())
return nullable();
/* if we have a zero size RHS we are directly nullable */
if (rhs_length() == 0)
{
/* stash and return the result */
return set_nullable(true);
}
/* otherwise we need to test all of our parts */
for (pos = 0; pos < rhs_length(); pos++)
{
part = rhs(pos);
/* only look at non-actions */
if (!part.is_action())
{
sym = ((symbol_part) part).the_symbol();
/* if its a terminal we are definitely not nullable */
if (!sym.is_non_term())
return set_nullable(false);
else if (!((non_terminal) sym).nullable())
return false;
}
}
/* if we make it here all parts are nullable */
return set_nullable(true);
}
/// <summary>set (and return) nullability
/// </summary>
public virtual bool set_nullable(bool v)
{
_nullable_known = true;
_nullable = v;
return v;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Update (and return) the first set based on current NT firsts.
/// This assumes that nullability has already been computed for all non
/// terminals and productions.
/// </summary>
public virtual terminal_set check_first_set()
{
int part;
symbol sym;
/* walk down the right hand side till we get past all nullables */
for (part = 0; part < rhs_length(); part++)
{
/* only look at non-actions */
if (!rhs(part).is_action())
{
sym = ((symbol_part) rhs(part)).the_symbol();
/* is it a non-terminal?*/
if (sym.is_non_term())
{
/* add in current firsts from that NT */
_first_set.add(((non_terminal) sym).first_set());
/* if its not nullable, we are done */
if (!((non_terminal) sym).nullable())
break;
}
else
{
/* its a terminal -- add that to the set */
_first_set.add((terminal) sym);
/* we are done */
break;
}
}
}
/* return our updated first set */
return first_set();
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Equality comparison.
/// </summary>
public virtual bool equals(production other)
{
if (other == null)
return false;
return other._index == _index;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Generic equality comparison.
/// </summary>
public override bool Equals(object other)
{
if (!(other is production))
return false;
else
return equals((production) other);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Produce a hash code.
/// </summary>
public override int GetHashCode()
{
/* just use a simple function of the index */
return _index * 13;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Convert to a string.
/// </summary>
public override string ToString()
{
System.String result;
/* catch any internal errors */
try
{
result = "production [" + index() + "]: ";
//UPGRADE_TODO: The equivalent in .NET for method 'java.Object.toString' may return a different value. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1043"'
result += ((lhs() != null)?lhs().ToString():"$$NULL-LHS$$");
result += " :: = ";
for (int i = 0; i < rhs_length(); i++)
result += rhs(i) + " ";
result += ";";
if (action() != null && action().code_string() != null)
result += " {" + action().code_string() + "}";
if (nullable_known())
if (nullable())
result += "[NULLABLE]";
else
result += "[NOT NULLABLE]";
}
catch (internal_error e)
{
/* crash on internal error since we can't throw it from here (because
superclass does not throw anything. */
e.crash();
result = null;
}
return result;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Convert to a simpler string.
/// </summary>
public virtual string to_simple_string()
{
System.String result;
result = ((lhs() != null)?lhs().the_symbol().name_Renamed_Method():"NULL_LHS");
result += " ::= ";
for (int i = 0; i < rhs_length(); i++)
if (!rhs(i).is_action())
result += ((symbol_part) rhs(i)).the_symbol().name_Renamed_Method() + " ";
return result;
}
/*-----------------------------------------------------------*/
}
}