kaby76 / fortran

MIT License
0 stars 0 forks source link

Ambiguities and full context fallbacks #1

Open kaby76 opened 2 months ago

kaby76 commented 2 months ago

entity_decl

https://github.com/kaby76/fortran/blob/ba846bbdea9df98428d094137d2adbe156f86207/FortranParser.g4#L926-L931

The parser cannot decide between alt 1 and alt 2 of entity_decl for an id because for some object_name => name and function_name => name. Either expansion works.

The solution is to just comment out (or delete) the second alt because the first alt covers it.

entity_decl
    : object_name (LPAREN array_spec RPAREN)? (lbracket coarray_spec rbracket)? (
        ASTERIK char_length
    )? initialization?
//    | function_name ( ASTERIK char_length)?
    ;

array_spec

https://github.com/kaby76/fortran/blob/ba846bbdea9df98428d094137d2adbe156f86207/FortranParser.g4#L977-L987

The parser cannot distinguish between alt 1 and 2 of array_spec because input 1 matches either.

explicit-shape-spec (explicit-chape-spec) and explicit-shape-bounds-spec are described in Section 8.5.8.2.

It is probably safe to comment out (or delete) the alt for explicit_shape_bounds_spec (and corresponding rule) because the syntax is subsumed by explicit_shape_spec_list/explicit_shape_spec.

array_spec
    : explicit_shape_spec_list
//    | explicit_shape_bounds_spec
    | assumed_shape_spec_list
    | assumed_shape_bounds_spec
    | deferred_shape_spec_list
    | assumed_size_spec
    | implied_shape_spec
    | implied_shape_or_assumed_size_spec
    | assumed_rank_spec
    ;

designator

https://github.com/kaby76/fortran/blob/ba846bbdea9df98428d094137d2adbe156f86207/FortranParser.g4#L1277-L1286

This rule has a number of problems.

It seems I can rewrite designator as follows:

designator
: (  data_ref ( LPAREN substring_range RPAREN)?
    | substring
) (PERCENT (RE|IM))?
    ;

primary

https://github.com/kaby76/fortran/blob/ba846bbdea9df98428d094137d2adbe156f86207/FortranParser.g4#L1496-L1508

This rule has conflicting alts 2 (designator), 4 (structure_constructor), and 7 (function_reference) for input reshape(arr2,(/3,3/))if. The problem is that they all can derive the same damn string. The only difference between them is based on type information, which does not--and should not--be in the parser grammar. These alts need to be merged.

primary
    : literal_constant
//    | designator
    | array_constructor
//    | structure_constructor
    | enum_constructor
    | enumeration_constructor
    | function_reference
    | type_param_inquiry
    | type_param_name
    | LPAREN expr RPAREN
    | conditional_expr
    ;

With these four changes, examples//flang/test/f90_correct/src/intrinsic_pack.f90 runtime drops from ~20s to ~3s.