antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.26k stars 3.72k forks source link

Error while parsing valid Fortran90 program #3710

Open akoerner opened 1 year ago

akoerner commented 1 year ago

I am receiving a parser error when parsing a legal Fortran90 test program:

program addition
    implicit none

    integer :: a, b, result

    a = 1
    b = 3

    result = a + b

    write(*,*) "The sum of", a, "and", b, "is", result

end program addition

The program compiles with gfortran and gives the following output once run:

source(master) ✗ (0)> gfortran simple_calc.f90                                                                                       
source(master) ✗ (0)> ./a.out                                                                                                          
 The sum of           1 and           3 is           4

When parsed with the Fortran90Parser and lexer I receive the following error:

line 4:21 no viable alternative at input 'implicit none\n\n    integer :: a, b, result'

https://github.com/akoerner/FortranAS/blob/master/fortran_code_samples/simple_calc.f90

kaby76 commented 1 year ago

Strictly speaking, if you are using gfortran and not using the -std option, you're not forcing the compiler to choose a particular language compliance. NB: gfortran does not strictly support F90. I can do gfortran -std=f95 test.f90 -o my_program of your program, but gfortran -std=f90 test.f90 -o my_program gives an error: "gfortran: error: unrecognized command line option ‘-std=f90’; did you mean ‘-std=c90’?". In fact, it says on the webpage "Gfortran is the name of the GNU Fortran project, developing a free [Fortran 95/2003/2008/2018](https://gcc.gnu.org/wiki/GFortranStandards) compiler for GCC, the GNU Compiler Collection." F90 is not listed there. And, we know that F95 is a different standard.

I think the problem here is that objectName : NAME ; is too restrictive. It should allow for certain keywords, like return.

akoerner commented 1 year ago

@kaby76 Thanks for the comment, clarification and time spent educating. Next time I'll make sure to compile against a standard.

As you pointed out this does appear to be a valid issue though.