[antlr-interest] Order independent keywords

Indhu Bharathi indhu.b at s7software.com
Sun Mar 22 04:53:54 PDT 2009


And if you want to make some attribute compulsory, you can do it this way:

attributes
scope {
    boolean typeAttrSeen;
}
@init {
    $attributes::typeAttrSeen = false;
}
@after {
    if ( $attributes::typeAttrSeen == false ) {
        System.out.println("Required attribute 'type' is not present");
    }
}
    :    attribute+
    ;


typeAttr    :    'TYPE' ID ';'
        {
            $attributes::typeAttrSeen = true;
        }
    ;

- Indhu


Indhu Bharathi wrote:
> Maybe you are looking for something like this:
>
>     attributes
>         :    attribute+
>         ;
>
>     attribute
>         :    typeAttr
>         |    lengthAttr
>         |    pathAttr
>         ;
>
> Here is a complete sample:
>
>     grammar Test;
>
>     attributes
>         :    attribute+
>         ;
>
>     attribute
>         :    typeAttr
>         |    lengthAttr
>         |    pathAttr
>         ;
>
>     typeAttr    :    'TYPE' ID ';'
>         ;
>        
>     lengthAttr
>         :    'LENGTH' NUMBER ';'
>         ;
>        
>     pathAttr    :    'PATH' ID ';'
>         ;
>        
>     ID    :    'a'..'z'+
>         ;
>        
>     NUMBER    :    '0'..'9'+
>         ;
>        
>     WS    :    (' ' | '\t' | '\n')+ {$channel = HIDDEN;}
>         ;
>        
>
>
> - Indhu
>
>
> register reg.ini wrote:
>> Hi everybody,
>> I am trying to parse a custom configuration file format using antlr.
>> This file format describes properties of several Paramaters using C 
>> like structs, for example:
>>
>> Record
>> {
>>        TYPE typeid;
>>        LENGHT 80;
>>        PATH= pathtofile
>> }
>>
>> . The order of the attributes is irrelevant but some of them are 
>> required other are simply optional. Is it possible to embody the 
>> requirement of order independent attributes directly in the parser 
>> grammar, without using actions?
>> Thanks in advance,
>> Luca
>>
>> ------------------------------------------------------------------------
>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>   
>
> ------------------------------------------------------------------------
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090322/2630d55f/attachment.html 


More information about the antlr-interest mailing list