[antlr-interest] program with ast grammar

William B. Clodius wclodius at los-alamos.net
Tue Nov 3 21:05:03 PST 2009


On Nov 3, 2009, at 5:18 AM, Михаил Юрушкин wrote:

> good day,
> fortran supports "if construct" with following syntax:
>
> [name:] if (..) then
> [block]
>
> else if (..) [name:] then
> [block]....
>
> else if (..) [name:] then
> [block]
> else [name:]
> [block]
> end if [name]
>
> for example:
> firstIf : if (a>10) then
> a = 10
> else if (b>10) : secondIf then
> b = 10;
> else secondif:
> b = 20;
>
> else firstif:
> a = 20;
> endif firstif
>


Your syntax is incorrect. The correct syntax (from a recent draft of  
F09) is context free (unlike some other aspects of Fortran)
    if-construct is if-then-stmt
    block
    [ else-if-stmt
        block ] ...
    [ else-stmt
      block ]
    end-if-stmt

    if-then-stmt is [ if-construct-name : ] IF ( scalar-logical-expr )  
THEN

    else-if-stmt is ELSE IF ( scalar-logical-expr ) THEN [ if- 
construct-name ]

    else-stmt is ELSE [ if-construct-name ]

    end-if-stmt is END IF [ if-construct-name ]

or in your form
[name:] if (..) then
[block]

else if (..) then [name]
[block]....

else if (..) then  [name]
[block]
else [name]
[block]
end if [name]

Further the appearance of the if-construct-names must satisfy the  
constraint

If the if-then-stmt of an if-construct specifies an if-construct-name,  
the corresponding end-if
stmt shall specify the same if-construct-name. If the if-then-stmt of  
an if-construct does not specify an
if-construct-name, the corresponding end-if-stmt shall not specify an  
if-construct-name. If an else-if
stmt or else-stmt specifies an if-construct-name, the corresponding if- 
then-stmt shall specify the same
if-construct-name.

i.e. there can only be one name used in all those contexts. If the  
programmer wants to name the if blocks implicit in the else ifs they  
must be explicitly moved to the level of a block. i.e. as legal code  
you can have either

first example:
mainIf : if (a>10) then
    a = 10
else if (b>10)  then mainIf
    b = 10;
else mainIf
    b = 20;
endif mainIf

or

second example:
firstIf : if (a>10) then
    a = 10
else firstIf
    secondIf : if (b>10) then
        b = 10
    else secondif
        b = 20
    endif secondIf
endif firstif

Verifying the consistency of the names is best put off until after the  
syntactic analysis.

<snip>


More information about the antlr-interest mailing list