source: CIVL/mods/dev.civl.abc/grammar/fortran/FortranParserExtras.g

main
Last change on this file was aad342c, checked in by Stephen Siegel <siegel@…>, 3 years ago

Performing huge refactor to incorporate ABC, GMC, and SARL into CIVL repo and use Java modules.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5664 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 15.8 KB
Line 
1/**
2 * FortranParserExtras.g - this file is needed because adding more rules to FortranParser08
3 * currently will cause javac to fail with a "Code too large" error. Removing some of
4 * the rules to an inherited grammar is a workaround to the problem.
5 */
6
7parser grammar FortranParserExtras;
8
9options {
10 language=Java;
11 superClass=AbstractFortranParser;
12 tokenVocab=FortranParser08;
13}
14
15import FortranParser08;
16
17/* ANTLR 3.5 doesn't allow redefinition of headers in composite grammars.
18Our solution for this is: add the header (package, imported package)
19to the generated java file in ant.
20@header {
21 package dev.civl.abc.front.fortran.old.parse;
22 import dev.civl.abc.front.fortran.parse.IActionEnums;
23}*/
24
25@members {
26 int gCount1;
27 int gCount2;
28
29 public void initialize(String[] args, String filename, String path) {
30 action = new FortranParserActionTreeMaker(args, this, filename);
31
32 initialize(this, action, filename, path);
33 gFortranParser08.initialize(this, action, filename, path);
34
35 action.start_of_file(filename, path);
36 }
37
38 public void eofAction() {
39 gFortranParser08.eofAction();
40 }
41
42} // end members
43
44
45/**
46 * Section/Clause 1: Overview
47 */
48
49
50/*
51 * Section/Clause 2: Fortran concepts
52 */
53
54/*
55 * R204 specification-part
56 * is [ use-stmt ] ...
57 * [ import-stmt ] ...
58 * [ implicit-part ]
59 * [ declaration-construct ] ...
60 */
61
62/*
63 * C201-F08 (R208) An execution-part shall not contain an end-function-stmt,
64 * end-mp-subprogram-stmt, end-program-stmt, or end-subroutine-stmt.
65 */
66
67
68////////////
69// R204-F08
70//
71specification_part
72@init{int numUseStmts=0; int numImportStmts=0; gCount1=0; gCount2=0;}
73 : ( use_stmt {numUseStmts++;} )*
74 ( import_stmt {numImportStmts++;} )*
75 implicit_part_recursion // making nonoptional with predicates fixes ambiguity
76 ( declaration_construct {gCount2++;} )*
77 {action.specification_part(numUseStmts, numImportStmts, gCount1, gCount2);}
78 ;
79
80/*
81 * R205-F08 implicit-part is [ implicit-part-stmt ] ...
82 * implicit-stmt
83 */
84
85/*
86 * R206-F08 implicit-part-stmt is implicit-stmt
87 * or parameter-stmt
88 * or format-stmt
89 * or entry-stmt
90 */
91
92////////////
93// R205-F08
94// R206-F08 combined
95//
96implicit_part_recursion
97 : ((label)? T_IMPLICIT) => implicit_stmt {gCount1++;} implicit_part_recursion
98 | ((label)? T_PARAMETER) => parameter_stmt {gCount2++;} implicit_part_recursion
99 | ((label)? T_FORMAT) => format_stmt {gCount2++;} implicit_part_recursion
100 | ((label)? T_ENTRY) => entry_stmt {gCount2++;} implicit_part_recursion
101 | // empty
102 ;
103
104/*
105 * R213-F08 executable-construct
106 * is action-stmt
107 * or associate-construct
108 * or block-construct // NEW_TO_2008
109 * or case-construct
110 * or critical-construct // NEW_TO_2008
111 * or do-construct
112 * or forall-construct
113 * or if-construct
114 * or select-type-construct
115 * or where-construct
116 */
117
118////////////
119// R213-F08
120//
121executable_construct
122@after {action.executable_construct();}
123 : action_stmt
124 | associate_construct
125 | block_construct // NEW_TO_2008
126 | case_construct
127 | critical_construct // NEW_TO_2008
128 | do_construct
129 | forall_construct
130 | if_construct
131 | select_type_construct
132 | where_construct
133 | pragma_stmt //For OMP STATEMENT (W.Wu)
134 ;
135
136/*
137 * R214-F08 action-stmt
138 * is allocate-stmt
139 * or assignment-stmt
140 * or backspace-stmt
141 * or call-stmt
142 * or close-stmt
143 * or continue-stmt
144 * or cycle-stmt
145 * or deallocate-stmt
146 * or end-function-stmt
147 * or end-mp-subprogram-stmt // NEW_TO_2008
148 * or end-program-stmt
149 * or end-subroutine-stmt
150 * or endfile-stmt
151 * or errorstop-stmt // NEW_TO_2008
152 * or exit-stmt
153 * or flush-stmt
154 * or forall-stmt
155 * or goto-stmt
156 * or if-stmt
157 * or inquire-stmt
158 * or lock-stmt // NEW_TO_2008
159 * or nullify-stmt
160 * or open-stmt
161 * or pointer-assignment-stmt
162 * or print-stmt
163 * or read-stmt
164 * or return-stmt
165 * or rewind-stmt
166 * or stop-stmt
167 * or sync-all-stmt // NEW_TO_2008
168 * or sync-images-stmt // NEW_TO_2008
169 * or sync-memory-stmt // NEW_TO_2008
170 * or unlock-stmt // NEW_TO_2008
171 * or wait-stmt
172 * or where-stmt
173 * or write-stmt
174 * or arithmetic-if-stmt
175 * or computed-goto-stmt
176 */
177
178////////////
179// R214-F08
180//
181// C201-F08 (R208) An execution-part shall not contain an end-function-stmt,
182// end-mp-subprogram-stmt, end-program-stmt, or end-subroutine-stmt.
183//
184// (But they can be in a branch target statement, which is not in the grammar,
185// so the end-xxx-stmts deleted.)
186// TODO continue-stmt is ambiguous with same in end-do, check for label and if
187// label matches do-stmt label, then match end-do there
188// the original generated rules do not allow the label, so add (label)?
189//
190action_stmt
191@after {action.action_stmt();}
192// Removed backtracking by inserting extra tokens in the stream by the prepass
193// that signals whether we have an assignment-stmt, a pointer-assignment-stmt,
194// or an arithmetic if. This approach may work for other parts of backtracking
195// also. However, need to see if there is a way to define tokens w/o defining
196// them in the lexer so that the lexer doesn't have to add them to it's parsing.
197// 02.05.07
198 : allocate_stmt
199 | assignment_stmt
200 | backspace_stmt
201 | call_stmt
202 | close_stmt
203 | continue_stmt
204 | cycle_stmt
205 | deallocate_stmt
206//////////
207// These end functions are not needed because the initiating constructs are called
208// explicitly to avoid ambiguities.
209// | end_function_stmt
210// | end_mp_subprogram_stmt // NEW_TO_2008
211// | end_program_stmt
212// | end_subroutine_stmt
213 | endfile_stmt
214 | errorstop_stmt // NEW_TO_2008
215 | exit_stmt
216 | flush_stmt
217 | forall_stmt
218 | goto_stmt
219 | if_stmt
220 | inquire_stmt
221 | lock_stmt // NEW_TO_2008
222 | nullify_stmt
223 | open_stmt
224 | pointer_assignment_stmt
225 | print_stmt
226 | read_stmt
227 | return_stmt
228 | rewind_stmt
229 | stop_stmt
230 | sync_all_stmt // NEW_TO_2008
231 | sync_images_stmt // NEW_TO_2008
232 | sync_memory_stmt // NEW_TO_2008
233 | unlock_stmt // NEW_TO_2008
234 | wait_stmt
235 | where_stmt
236 | write_stmt
237 | arithmetic_if_stmt
238 | computed_goto_stmt
239 | assign_stmt // ADDED?
240 | assigned_goto_stmt // ADDED?
241 | pause_stmt // ADDED?
242 ;
243
244
245/**
246 * Section/Clause 3: Lexical tokens and source form
247 */
248
249
250/*
251 * Section/Clause 4: Types
252 */
253
254
255/*
256 * Section/Clause 5: Attribute declarations and specifications
257 */
258
259// R501
260type_declaration_stmt
261@init {Token lbl = null; int numAttrSpecs = 0;}
262@after{checkForInclude();}
263 : (label {lbl=$label.tk;})? declaration_type_spec
264 ( (T_COMMA attr_spec {numAttrSpecs += 1;})* T_COLON_COLON )?
265 entity_decl_list end_of_stmt
266 { action.type_declaration_stmt(lbl, numAttrSpecs, $end_of_stmt.tk); }
267 ;
268
269/*
270 * R510-F08 deferred-coshape-spec
271 * is :
272 */
273
274////////////
275// R510-F08
276//
277// deferred_coshape_spec is replaced by array_spec (see R509-F08)
278//
279
280/*
281 * R511-08 explicit-coshape-spec
282 * is [ [ lower-cobound : ] upper-cobound, ]...
283 * [ lower-cobound : ] *
284 */
285
286////////////
287// R511-F08
288//
289// explicit_coshape_spec is replaced by array_spec (see R509-F08)
290//
291
292
293/**
294 * Section/Clause 6: Use of data objects
295 */
296
297/*
298 * R612-F08 part-ref
299 * is part-name [ ( section-subscript-list ) ] [ image-selector]
300 */
301
302////////////
303// R612-F08, R613-F03
304//
305// This rule is implemented in FortranParserExtras grammar
306//
307// T_IDENT inlined for part_name
308// with k=2, this path is chosen over T_LPAREN substring_range T_RPAREN
309// TODO error: if a function call, should match id rather than
310// (section_subscript_list)
311// a = foo(b) is ambiguous YUK...
312part_ref
313options {k=2;}
314@init{boolean hasSSL = false; boolean hasImageSelector = false;}
315 : (T_IDENT T_LPAREN) => T_IDENT T_LPAREN section_subscript_list T_RPAREN
316 (image_selector {hasImageSelector=true;})?
317 {hasSSL=true; action.part_ref($T_IDENT, hasSSL, hasImageSelector);}
318 | (T_IDENT T_LBRACKET) => T_IDENT image_selector
319 {hasImageSelector=true; action.part_ref($T_IDENT, hasSSL, hasImageSelector);}
320 | T_IDENT
321 {action.part_ref($T_IDENT, hasSSL, hasImageSelector);}
322 ;
323
324part_ref_no_image_selector
325options{k=2;}
326@init{boolean hasSSL = false; boolean hasImageSelector = false;}
327 : (T_IDENT T_LPAREN) => T_IDENT T_LPAREN section_subscript_list T_RPAREN
328 {hasSSL=true; action.part_ref($T_IDENT, hasSSL, hasImageSelector);}
329 | T_IDENT
330 {action.part_ref($T_IDENT, hasSSL, hasImageSelector);}
331 ;
332
333
334/**
335 * R620-F08 section-subscript
336 * is subscript
337 * or subscript-triplet
338 * or vector-subscript
339 */
340
341////////////
342// R620-F08, R619-F03
343//
344// expr inlined for subscript, vector_subscript, and stride (thus deleted option 3)
345// refactored first optional expr from subscript_triplet modified to also match
346// actual_arg_spec_list to reduce ambiguities and need for backtracking
347section_subscript returns [boolean isEmpty]
348@init {
349 boolean hasLowerBounds = false;
350 boolean hasUpperBounds = false;
351 boolean hasStride = false;
352 boolean hasExpr = false;
353}
354 : expr section_subscript_ambiguous
355 | T_COLON (expr {hasUpperBounds=true;})? (T_COLON expr {hasStride=true;})?
356 { action.section_subscript(hasLowerBounds, hasUpperBounds, hasStride, false); }
357 | T_COLON_COLON expr
358 { hasStride=true;
359 action.section_subscript(hasLowerBounds, hasUpperBounds, hasStride, false);}
360 | T_IDENT T_EQUALS expr // could be an actual-arg, see R1220
361 { hasExpr=true; action.actual_arg(hasExpr, null);
362 action.actual_arg_spec($T_IDENT); }
363 | T_IDENT T_EQUALS T_ASTERISK label // could be an actual-arg, see R1220
364 { action.actual_arg(hasExpr, $label.tk); action.actual_arg_spec($T_IDENT); }
365 | T_ASTERISK label /* could be an actual-arg, see R1220 */
366 { action.actual_arg(hasExpr, $label.tk); action.actual_arg_spec(null); }
367 | { isEmpty = true; /* empty could be an actual-arg, see R1220 */ }
368 ;
369
370section_subscript_ambiguous
371@init {
372 boolean hasLowerBound = true;
373 boolean hasUpperBound = false;
374 boolean hasStride = false;
375 boolean isAmbiguous = false;
376}
377 : T_COLON (expr {hasUpperBound=true;})? (T_COLON expr {hasStride=true;})?
378 { action.section_subscript(hasLowerBound, hasUpperBound, hasStride, isAmbiguous);}
379 // this alternative is necessary because if alt1 above has no expr
380 // following the first : and there is the optional second : with no
381 // WS between the two, the lexer will make a T_COLON_COLON token
382 // instead of two T_COLON tokens. in this case, the second expr is
383 // required. for an example, see J3/04-007, Note 7.44.
384 | T_COLON_COLON expr
385 { hasStride=true;
386 action.section_subscript(hasLowerBound, hasUpperBound, hasStride, isAmbiguous);}
387 | { /* empty, could be an actual-arg, see R1220 */
388 isAmbiguous=true;
389 action.section_subscript(hasLowerBound, hasUpperBound, hasStride, isAmbiguous);
390 }
391 ;
392
393
394/**
395 * R620-F08 section-subscript
396 * is subscript
397 * or subscript-triplet
398 * or vector-subscript
399 */
400
401////////////
402// R620-F08 list
403//
404// This rule must be kept here with part-ref, otherwise parsing errors will occur.
405// It is unknown why this happens.
406//
407section_subscript_list
408@init{int count = 0;}
409 : { action.section_subscript_list__begin(); }
410 isEmpty=section_subscript
411 {
412 if (isEmpty == false) count += 1;
413 }
414 (T_COMMA section_subscript {count += 1;})*
415 { action.section_subscript_list(count); }
416 ;
417
418
419/*
420 * R624-F08 image-selector
421 * is lbracket cosubscript-list rbracket
422 */
423
424////////////
425// R624-F08
426//
427image_selector
428 : T_LBRACKET cosubscript_list T_RBRACKET
429 {action.image_selector($T_LBRACKET, $T_RBRACKET);}
430 ;
431
432/*
433 * R625-F08 cosubscript
434 * is scalar-int-expr
435 */
436
437////////////
438// R625-F08
439//
440cosubscript
441 : scalar_int_expr
442 ;
443
444cosubscript_list
445@init{int count=0;}
446 : {action.cosubscript_list__begin();}
447 cosubscript {count++;} ( T_COMMA cosubscript {count++;} )*
448 {action.cosubscript_list(count, null);}
449 ;
450
451/*
452 * R631-08 allocation
453 * is allocate-object [ ( allocate-shape-spec-list ) ]
454 * [ lbracket allocate-coarray-spec rbracket ] // NEW_TO_2008
455 */
456
457////////////
458// R631-F08, R628-F03
459//
460// C644 (R632) An allocate-object shall not be a coindexed object.
461//
462allocation
463@init{boolean hasAllocateShapeSpecList = false; boolean hasAllocateCoarraySpec = false;}
464 : (allocate_object T_LBRACKET)
465 => allocate_object T_LBRACKET allocate_coarray_spec T_RBRACKET
466 {hasAllocateCoarraySpec=true;}
467 {action.allocation(hasAllocateShapeSpecList, hasAllocateCoarraySpec);}
468// This option (with allocate_shape_spec_list) is caught by the allocate object. If so,
469// the section-subscript-list must be changed into a allocate-shape-spec-list)
470//
471// | (allocate_object T_LPAREN)
472// => allocate_object T_LPAREN allocate_shape_spec_list {hasAllocateShapeSpecList=true;}
473// T_RPAREN
474// T_LBRACKET allocate_coarray_spec {hasAllocateCoarraySpec=true;}
475// T_RBRACKET
476// {action.allocation(hasAllocateShapeSpecList, hasAllocateCoarraySpec);}
477 | (allocate_object)
478 => allocate_object
479 {action.allocation(hasAllocateShapeSpecList, hasAllocateCoarraySpec);}
480 ;
481
482
483/**
484 * R632-F08 allocate-object
485 * is variable-name
486 * structure-component
487 */
488
489////////////
490// R636-F08, R629-F03
491//
492// C644 (R632) An allocate-object shall not be a coindexed object.
493//
494// T_IDENT inlined for variable_name
495// data_ref inlined for structure_component
496// data_ref isa T_IDENT so T_IDENT deleted
497// data_ref inlined and part_ref_no_image_selector called directly
498//
499allocate_object
500@init{int numPartRefs = 0;}
501 : part_ref_no_image_selector {numPartRefs += 1;}
502 (T_PERCENT part_ref_no_image_selector {numPartRefs += 1;})*
503 {action.data_ref(numPartRefs); action.allocate_object();}
504 ;
505
506/*
507 * R636-F08 allocate-coarray-spec
508 * is [ allocate-coshape-spec-list , ] [ lower-bound-expr : ] *
509 */
510
511////////////
512// R636-F08
513//
514allocate_coarray_spec
515options{k=3;}
516@after {action.allocate_coarray_spec();}
517 : (T_ASTERISK) => T_ASTERISK
518 | (expr T_COLON T_ASTERISK) => expr T_COLON T_ASTERISK
519//PUTBACK | allocate_coshape_spec_list T_COMMA ( expr T_COLON )? T_ASTERISK
520// | T_ASTERISK // TESTING
521 ;
522
523
524/**
525 * Section/Clause 7: Expressions and assignment
526 */
527
528/*
529 * R724-F08 logical-expr
530 * is expr
531 */
532
533////////////
534// R724-F08, R724-F03
535//
536logical_expr
537 : expr
538 ;
539
540scalar_logical_expr
541 : expr
542 ;
543
544
545/*
546 * R726-08 int-expr
547 * is expr
548 */
549
550////////////
551// R726-F08, R727-F03
552//
553int_expr
554 : expr
555 ;
556
557scalar_int_expr
558 : expr
559 ;
560
561
562//----------------------------------------------------------------------------
563// additional rules following standard and useful for error checking
564//----------------------------------------------------------------------------
565
566scalar_variable
567 : expr
568 ;
569
570
571/**
572 * Section/Clause 8: Execution control
573 */
574
575
576/*
577 * R866-F08 lock-variable
578 * is scalar-variable
579 */
580
581////////////
582// R866-F08
583//
584lock_variable
585 : scalar_variable
586 { action.lock_variable(); }
587 ;
588
Note: See TracBrowser for help on using the repository browser.