Hi all,

I've been trying to actually remove ambiguities, rather than papering
over the cracks with backtrack (as I realised there are some fundamental
ways in which this isn't going to work). I realised that there are a lot
of cases of value expressions being redundant between types. The
standard is *really* not aimed at implementors, which is a little
worrying... anyway, the grammar has been getting simpler, but I've hit a
snag when I get to the bits about constraints.

The grammar is attached; the difficulty I'm having is that 'type' and
'value' are apparently ambiguous with one another, as given by the
errors for the rule 'test' on line 715. I'm getting better at tracing
through to find the ambiguities and left-factoring them or reducing
redundancy. However, in this case I can't seem to find the situations
where CAPID is a valid value or LCID is a valid type, or the other
strange results it gives.

Any help gratefully appreciated - a pointer at the leaf-rules that are
at issue, and how one gets to them, would be ideal.

Thanks,

Sam

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

/**
 * ASN.1 Parsing
 */
grammar ASN_1;

options {
        language = Java;
        output = AST;
        ASTLabelType = 'CommonTree';
        memoize = true;
}

tokens {
  // literals
  ABSENT='ABSENT';
  ABSTRACT_SYNTAX='ABSTRACT-SYNTAX';
  ALL='ALL';
  APPLICATION='APPLICATION';
  AUTOMATIC='AUTOMATIC';
  BEGIN='BEGIN';
  BIT='BIT';
  BMPString='BMPSTRING';
  BOOLEAN='BOOLEAN';
  BY='BY';
  CHARACTER='CHARACTER';
  CHOICE='CHOICE';
  CLASS='CLASS';
  COMPONENT='COMPONENT';
  COMPONENTS='COMPONENTS';
  CONSTRAINED='CONSTRAINED';
  CONTAINING='CONTAINING';
  DEFAULT='DEFAULT';
  DEFINITIONS='DEFINITIONS';
  EMBEDDED='EMBEDDED';
  ENCODED='ENCODED';
  ENCODING_CONTROL='ENCODING-CONTROL';
  END='END';
  ENUMERATED='ENUMERATED';
  EXCEPT='EXCEPT';
  EXPLICIT='EXPLICIT';
  EXPORTS='EXPORTS';
  EXTENSIBILITY='EXTENSIBILITY';
  EXTERNAL='EXTERNAL';
  FALSE='FALSE';
  FROM='FROM';
  GeneralizedTime='GeneralizedTime';
  GeneralString='GeneralString';
  GraphicString='GraphicString';
  IA5String='IA5String';
  IDENTIFIER='IDENTIFIER'; 
  IMPLICIT='IMPLICIT';
  IMPLIED='IMPLIED';
  IMPORTS='IMPORTS';
  INCLUDES='INCLUDES';
  INSTANCE='INSTANCE';
  INSTRUCTIONS='INSTRUCTIONS';
  INTEGER='INTEGER';
  INTERSECTION='INTERSECTION';
  ISO646String='ISO646String';
  MAX='MAX';
  MIN='MIN';
  MINUS_INFINITY='MINUS-INFINITY';
  NOT_A_NUMBER='NOT-A-NUMBER';
  NULL='NULL';
  NumericString='NumericString';
  OBJECT='OBJECT';
  ObjectDescriptor='ObjectDescriptor';
  OCTET='OCTET';
  OF='OF';
  OPTIONAL='OPTIONAL';
  PATTERN='PATTERN';
  PDV='PDV';
  PLUS_INFINITY='PLUS-INFINITY';
  PRESENT='PRESENT';
  PrintableString='PrintableString';
  PRIVATE='PRIVATE';
  REAL='REAL';
  RELATIVE_OID='RELATIVE-OID';
  SEQUENCE='SEQUENCE';
  SET='SET';
  SIZE='SIZE';
  STRING='STRING';
  SYNTAX='SYNTAX';
  T61String='T61String';
  TAGS='TAGS';
  TeletexString='TeletexString';
  TRUE='TRUE';
  TYPE_IDENTIFIER='TYPE-IDENTIFIER';
  UNION='UNION';
  UNIQUE='UNIQUE';
  UNIVERSAL='UNIVERSAL';
  UniversalString='UniversalString';
  UTCTime='UTCTime';
  UTF8String='UTF8String';
  VideotexString='VideotexString';
  VisibleString='VisibleString';
  WITH='WITH';
  
  // virtual tokens
  ID;
  MODREF;
  ENCREF;
  VALREF;
  TYPEREF;
  MODULE;
  BODY;
  DEFID;
  NAMENUM;
  EXTIMPLIED;
  EXTEXPLICIT;
  MODSYMB;
  EXTTREF;
  EXTVREF;
  TASS;
  VASS;
  XVASS;
  SYMBOLICS;
  VSTASS;
  ITEMS;
  BITSTRING;
  SETBITS;
  BITSTRINGV;
}

@header {
  package com.gobito.asn_1.parse;
  import static com.gobito.asn_1.parse.ParseUtil.*;
}
@lexer::header {
  package com.gobito.asn_1.parse;
  import static com.gobito.asn_1.parse.ParseUtil.*;
}

@members {

}

@lexer::members {

}

// Lexical elements

//"real" stuff

/**
 * Matches whitespace and causes resultant token to be hidden
 */
WS : (WSNONL | NL) {$channel=HIDDEN;};

fragment
NL : ('\n' | '\r' | '\u000B' | '\f');

fragment
WSNONL : (' ' | '\t');

fragment
LETTER: 'a'..'z' | 'A'..'Z';

fragment
DIGIT: '0'..'9';

fragment
IDHYPHEN: '-' (LETTER|DIGIT);

fragment
IDBODY : (LETTER|DIGIT|IDHYPHEN);

CAPID : 'A'..'Z' IDBODY*;

LCID : 'a'..'z' IDBODY*;

fragment
MLCOMMENTf : '/*' .* (MLCOMMENTf .*)* '*/'; 

MLCOMMENT : MLCOMMENTf {$channel=HIDDEN;};

SLCOMMENT : ('--' .*  ('--'|NL)) {$channel=HIDDEN;};

fragment
NUMBERf : '0' | '1'..'9' DIGIT*;

fragment
NUMBER : ;

REALNUMBER
  : (NUMBERf '.')=> NUMBERf ('.'(DIGIT+)?)? (('e'|'E')('+'|'-')? NUMBERf)?
  | NUMBERf {$type=NUMBER;};

fragment
HSTRINGCONT : ('0'..'9' | 'A'..'F' | WS)*;

HorBSTRING : '\'' cont=HSTRINGCONT '\'' ('H' {$type=HSTRING;}|'B' 
{$type=BSTRING;}) {($type == HSTRING) || isValidBSTRING($cont.text)}?;

fragment HSTRING : ;
fragment BSTRING : ;

/**
 * Matches a newline and surrounding non-newline whitespace
 */
fragment
CSTRINGNL : WSNONL* NL WSNONL*; 

CSTRING : '"' 
              (
                (CSTRINGNL)=>CSTRINGNL
                | ('""')=>'""'
                | ~'"'
              )*
          '"'
        {setText(parseCString($text));};

// handling xml

fragment
XMLNAMECHAR : 'A'..'Z' | 'a'..'z' | '0'..'9' | '-' | ':' | '_' | '.' ;

fragment
XMLNAMESTART: 'A'..'Z' | 'a'..'z' | '_' | ':' ;

fragment
INVALIDINXML : '<' | '&' ;

fragment
XMLENTREF : '&' XMLNAME ';' ;

fragment
XMLNAME : XMLNAMESTART XMLNAMECHAR+;

fragment
XMLSATTVAL : '\'' ( ('&')=>XMLENTREF | (~('\''|INVALIDINXML) ) )* '\'';

fragment
XMLDATTVAL : '"' ( ('&')=>XMLENTREF | (~('"'|INVALIDINXML) ) )* '"';

fragment
XMLATTVAL : XMLDATTVAL | XMLSATTVAL ;

fragment
XMLATTRIB : XMLNAME '=' XMLATTVAL ;

fragment
WSBLOCK : WS+;

fragment
XMLATTRIBS
      : XMLATTRIB 
      | (XMLATTRIB WS)=>XMLATTRIB WSBLOCK XMLATTRIBS;

fragment
XMLTAGATTS
      : WSBLOCK XMLATTRIBS ;

fragment
XMLOPENTAG : '<' XMLNAME XMLTAGATTS? WS* '>';

fragment
XMLCLOSETAG : '</' XMLNAME '>';

fragment
XMLSCLOSETAG : '<' XMLNAME XMLTAGATTS? WS* '/>';

fragment
XMLNONEMPTYELEMENT : XMLOPENTAG XMLCONTENT XMLCLOSETAG;

fragment
XMLEMPTYELEMENT : XMLSCLOSETAG;

fragment
XMLELEMENT options {
  backtrack=true;
}
 
      : XMLEMPTYELEMENT | XMLNONEMPTYELEMENT ;

fragment
XMLCONTENT : (XMLELEMENT | XMLENTREF | ~INVALIDINXML) *;

XMLFRAG : XMLELEMENT;

// Parser rules

/**
 * A module reference
 */
modulereference : CAPID;

typereference : CAPID;

encodingreference : CAPID {ucidIsAllUC($CAPID.text)}? -> ENCREF[$CAPID];

identifier : LCID;

valuereference : LCID;

moduleDefinition
  : id=moduleIdentifier DEFINITIONS enc=encodingReferenceDefault td=tagDefault 
ext=extensionDefault '::=' BEGIN body=moduleBody encCon=encodingControlSections 
END
  ->  ^(MODULE[$DEFINITIONS] $id $enc $td $ext $body $encCon)
  ;

encodingReferenceDefault : (encodingreference INSTRUCTIONS)? -> ^(INSTRUCTIONS 
encodingreference);
  
moduleIdentifier : modulereference definitiveIdentifier -> ^(modulereference 
definitiveIdentifier);

definitiveIdentifier 
 : o='{' definitiveObjIdComponent+ '}' -> ^(DEFID[$o] definitiveObjIdComponent+)
 | -> DEFID ; 

definitiveObjIdComponent
options{backtrack=true;}
  : nameAndNumberForm
  | numberForm
  | nameForm {isStandardOIDName($nameForm.text)}? ;
    
numberForm : NUMBER ;

nameAndNumberForm : id=identifier o='(' num=numberForm ')' -> ^(NAMENUM[$o] $id 
$num); 

nameForm : identifier ;

tagDefault
  : val=(EXPLICIT | IMPLICIT | AUTOMATIC) TAGS -> ^(TAGS $val)
  | -> ^(TAGS EXPLICIT);

extensionDefault
  : EXTENSIBILITY IMPLIED -> EXTIMPLIED[$EXTENSIBILITY]
  | -> EXTEXPLICIT ;

moduleBody
  : (exports imports assignmentList) -> ^(BODY exports imports assignmentList) 
  | -> ^(BODY)
  ;

exports
    : EXPORTS symbolsExported ';' -> ^(EXPORTS symbolsExported)
    | EXPORTS ALL ';' -> ^(EXPORTS ALL)
    | -> ^(EXPORTS);

symbolsExported : symbolList?;

imports
  : IMPORTS symbolsImported -> ^(IMPORTS symbolsImported) 
  | -> ^(IMPORTS);

symbolsImported : symbolsFromModule+ ;

symbolsFromModule : symbolList FROM globalModuleReference -> ^(MODSYMB[$FROM] 
globalModuleReference symbolList);

globalModuleReference
  : modulereference
    (
        objectIdentifierValue -> ^(modulereference objectIdentifierValue)
      | (LCID)=>definedValue -> ^(modulereference definedValue)
    )?
  -> ^(modulereference);

symbolList : symbol (',' symbol)* -> symbol+;

symbol : reference /*| parameterizedReference*/ ;

reference
    : typereference
    | valuereference
   /* | objectclassreference
    | objectReference
    | objectSetReference */;

assignmentList : assignment+ ;

assignment /*options {backtrack=true;}*/
    : typeOrValueSetTypeAssignment
    | valueAssignment
    | xmlValueAssignment
    /*| objectClassAssignment
    | objectAssignment
    | objectSetAssignment
    | parameterizedAssignment*/ ;

definedType
    : externalTypeReference
    | typereference
    /*| parameterizedType
    | parameterizedValueSetType*/ ;

definedValue
    : externalValueReference
    | valuereference
    /*| parameterizedValue*/ ;

externalTypeReference : modulereference d='.' typereference -> ^(EXTTREF[$d] 
modulereference typereference);

externalValueReference : modulereference d='.' valuereference -> ^(EXTVREF[$d] 
modulereference valuereference);

typeOrValueSetTypeAssignment
  : (typereference '::=')=>typereference a='::=' type 
    (
      '{' elementSetSpecs '}' -> ^(VSTASS[$a] typereference type 
elementSetSpecs)
    )?
  -> ^(TASS[$a] typereference type)
  | typereference (type a='::='|a='::=' type) '{' elementSetSpecs '}'
  -> ^(VSTASS[$a] typereference type elementSetSpecs)
  ;

// typeAssignment : typereference a='::=' type -> ^(TASS[$a] typereference 
type);

valueAssignment : valuereference type a='::=' value -> ^(VASS[$a] 
valuereference type value);

xmlValueAssignment : valuereference a='::=' xmlTypedValue -> ^(XVASS[$a] 
valuereference xmlTypedValue);

xmlTypedValue : XMLFRAG ; // needs some clever semantic predicating, probably

/*valueSetTypeAssignment
  : typereference (type a='::='|a='::=' type) '{' elementSetSpecs '}'
  -> ^(VSTASS[$a] typereference type elementSetSpecs); */

type : (typeWithConstraint | builtinType | referencedType)^ 
(('(')=>constraint)* ;

builtinType
    : bitStringType
    | booleanType
    | characterStringType
    | choiceType
    | embeddedPDVType
    | enumeratedType
    | externalType
    /*| instanceOfType*/
    | integerType
    | nullType
    /*| objectClassFieldType*/
    | objectIdentifierType
    | octetStringType
    | realType
    | relativeOIDType
    | sequenceType
    | sequenceOfType
    | setType
    | setOfType
    | prefixedType ;

referencedType
    : (usefulType)=>usefulType
    | definedType
    | selectionType
    /*| typeFromObject */
    /*| valueSetFromObjects*/ ;
    
namedType : identifier type -> ^(identifier type);

value : builtinValue | referencedValue /*| objectClassFieldValue*/ ;

builtinValue
    : (listValue)=>listValue
    | objectIdentifierValue
    | bitStringValue
    | booleanValue
    | characterStringValue
    | choiceValue
    /*| instanceOfValue*/
    | signedNumber
    | nullValue
    | realValue
    /*| prefixedValue*/ ;
    
referencedValue : definedValue /*| valueFromObject */;

namedValue : identifier value -> ^(identifier VALUE);

booleanType : BOOLEAN ;

booleanValue : TRUE | FALSE ;

integerType : INTEGER ('{' namedNumberList '}' -> ^(INTEGER namedNumberList))? 
-> INTEGER;

namedNumberList : namedNumber (',' namedNumber)* -> ^(SYMBOLICS namedNumber+);

namedNumber 
  : identifier '(' 
    (
        signedNumber -> ^(identifier signedNumber)
      | definedValue -> ^(identifier definedValue)
    ) ')';

signedNumber
  : NUMBER -> $NUMBER
  | m='-' NUMBER {$NUMBER.text != "0"}? -> NUMBER[$m,$m.text+$NUMBER.text]
  ;

integerValue : signedNumber | identifier ;

enumeratedType : ENUMERATED '{' enumerations '}' -> ^(ENUMERATED enumerations);

enumerations
  : enumerationItem (',' enumerationItem)* (',' '...' exceptionSpec (',' 
enumerationItem (',' enumerationItem)*)?)?
  -> ^(ITEMS enumerationItem+) exceptionSpec;

enumerationItem : identifier | namedNumber ;

enumeratedValue : identifier ;

realType : REAL ;

realValue : numericRealValue | specialRealValue ;

numericRealValue
    : REALNUMBER
    | m='-' r=REALNUMBER {Double.parseDouble($REALNUMBER.text) != 0.0}? -> 
REALNUMBER[$m,$m.text+$r.text]
    ;
    
specialRealValue : PLUS_INFINITY | MINUS_INFINITY | NOT_A_NUMBER;

bitStringType : BIT STRING ('{' namedBit (',' namedBit)* '}')? -> ^(BITSTRING 
namedBit*);

namedBit
  : identifier '(' 
    (
        NUMBER -> ^(identifier $NUMBER)
      | definedValue -> ^(identifier definedValue)
    ) ')'
  ;

bitStringValue
    : BSTRING -> ^(BITSTRINGV $BSTRING)
    | HSTRING -> ^(BITSTRINGV $HSTRING)
    | CONTAINING value -> ^(BITSTRINGV ^($CONTAINING value));

identifierList : identifier (',' identifier)* -> ^(SETBITS identifier+);

octetStringType : OCTET STRING -> OCTSTRING;

nullType : NULL;

nullValue : NULL;

sequenceType : SEQUENCE '{' componentTypeLists '}' -> ^(SEQUENCE 
componentTypeLists);

extensionAndException : '...' exceptionSpec ;

optionalExtensionMarker : (',' '...')? ;

ctlExtensionStuff
    : extensionAndException extensionAdditions
      ( optionalExtensionMarker
      | extensionEndMarker ','  rootComponentTypeList
      ) ;

componentTypeLists
    : ctlExtensionStuff
    | rootComponentTypeList (',' ctlExtensionStuff)? 
    |
    ;

rootComponentTypeList : componentTypeList ;

extensionEndMarker : ',' '...';

extensionAdditions : (',' extensionAdditionList)? ;

extensionAdditionList : extensionAddition (',' extensionAddition)* ;

extensionAddition : componentType | extensionAdditionGroup ;

extensionAdditionGroup : '[[' versionNumber componentTypeList ']]' ;

componentTypeList : componentType (',' componentType)* ;

componentType
    : namedType (OPTIONAL | DEFAULT value)?
    | COMPONENTS OF type ;

componentValueList : namedValue (',' namedValue)* ;

sequenceOfType : SEQUENCE OF (type | namedType) ;

sequenceValue : '{' namedValueList? '}' ;

valueListValue : '{' valueList? '}' ;

listValue : ('{' '}')=>sequenceValue | valueListValue ;

valueList : value (',' value)* ;

namedValueList : namedValue (',' namedValue)* ;

setType : '{' componentTypeLists '}' ;

setOfType : SET OF (type | namedType);

choiceType : CHOICE '{' alternativeTypeLists '}' ;

alternativeTypeLists : rootAlternativeTypeList
      (',' extensionAndException extensionAdditionAlternatives 
optionalExtensionMarker)? ;

rootAlternativeTypeList : alternativeTypeList ;

extensionAdditionAlternatives : (',' extensionAdditionAlternativesList)? ;

extensionAdditionAlternativesList : extensionAdditionAlternative (',' 
extensionAdditionAlternative)* ;

extensionAdditionAlternative : extensionAdditionAlternativesGroup | namedType ;

extensionAdditionAlternativesGroup : '[[' versionNumber alternativeTypeList 
']]';

choiceValue : identifier ':' value ;

alternativeTypeList : namedType (',' namedType)* ;

selectionType : identifier '<' type ;

prefixedType : taggedType | encodingPrefixedType ;

taggedType : tag (IMPLICIT|EXPLICIT)? type ;

tag : '[' encodingReference clazz classNumber ']' ;

encodingReference : (encodingreference ':')? ;

encodingPrefixedType : encodingPrefix type ;

encodingPrefix : '[' encodingReference /*encodingInstruction*/ ']' ;

classNumber : NUMBER | definedValue ;

clazz : (UNIVERSAL | APPLICATION | PRIVATE)? ;

objectIdentifierType : OBJECT IDENTIFIER ;

objectIdentifierValue : '{' c=objIdComponents '}' {$c.hasNandN}?;

objIdComponents returns [boolean hasNandN]
@init {
  hasNandN = false;
}
  : (c=objIdComponent {if $c.nAndN $hasNandN=true;})+ ;

objIdComponent returns [boolean nAndN]
  : nameAndNumberForm {$nAndN = true;}
  | numberForm {$nAndN = false;}
  | definedValue {$nAndN = false;} ;

relativeOIDType : RELATIVE_OID ;

embeddedPDVType : EMBEDDED PDV ;

externalType : EXTERNAL ;

characterStringType : restrictedCharacterStringType | 
unrestrictedCharacterStringType ;

characterStringValue : restrictedCharacterStringValue ;

restrictedCharacterStringType
    : BMPString
    | GeneralString
    | GraphicString
    | IA5String
    | ISO646String
    | NumericString
    | PrintableString
    | TeletexString
    | T61String
    | UniversalString
    | UTF8String
    | VideotexString
    | VisibleString ;
    
restrictedCharacterStringValue : CSTRING  ;

unrestrictedCharacterStringType : CHARACTER STRING ;

usefulType : a=typereference {$a.text == "GeneralizedTime" || $a.text == 
"UTCTime" || $a.text == "ObjectDescriptor" }? ; 

typeWithConstraint : (SET | SEQUENCE) (constraint | sizeConstraint) OF type ;

constraint : '(' constraintSpec exceptionSpec ')' ;

constraintSpec : subtypeConstraint /*| generalConstraint*/ ;

subtypeConstraint : elementSetSpecs ;

elementSetSpecs : rootElementSetSpec (',' '...' (',' 
additionalElementSetSpec)?)? ;

rootElementSetSpec : elementSetSpec;

additionalElementSetSpec : elementSetSpec;

elementSetSpec : unions | ALL exclusions ;

unions : intersections (unionMark intersections)* ;

//uElems : unions ;

intersections : intersectionElements (intersectionMark intersectionElements)* ;

//iElems : intersections ;

intersectionElements : elements exclusions? ;

exclusions : EXCEPT elements ;

unionMark : '|' | UNION ;

intersectionMark : '^' | INTERSECTION ;

elements : subtypeElements /*| objectSetElements*/ | '(' elementSetSpec ')' ;

subtypeElements
/*options {backtrack=true;}*/
    : (valueRange)=>valueRange
    | singleValue
    | containedSubtype
    | permittedAlphabet
    | sizeConstraint
    | innerTypeConstraints
    | patternConstraint;

test : value | type;

singleValue : value;

containedSubtype : INCLUDES? type;

valueRange : lowerEndpoint '..' upperEndpoint ;

lowerEndpoint : lowerEndValue '<'? ;
upperEndpoint : '<'? upperEndValue ;

lowerEndValue : value | MIN ;
upperEndValue : value | MAX ;

sizeConstraint : SIZE constraint ;

permittedAlphabet : FROM constraint;

innerTypeConstraints : WITH (COMPONENT singleTypeConstraint | COMPONENTS 
multipleTypeConstraints) ;

singleTypeConstraint : constraint ;

multipleTypeConstraints : fullSpecification | partialSpecification ;

fullSpecification : '{' typeConstraints '}' ;

partialSpecification : '{' '...' ',' typeConstraints '}' ;

typeConstraints : namedConstraint (',' namedConstraint)* ;

namedConstraint : identifier componentConstraint ;

componentConstraint : valueConstraint presenceConstraint ;

valueConstraint : constraint? ;

presenceConstraint : (PRESENT|ABSENT|OPTIONAL)? ;

patternConstraint : PATTERN value ;

exceptionSpec : ('!' exceptionIdentification)? ;

exceptionIdentification : signedNumber | definedValue | type ':' value ;

versionNumber : (NUMBER ':')? ;

encodingControlSections: encodingControlSection* ;

encodingControlSection : ENCODING_CONTROL encodingreference 
encodingInstructionAssignmentList;

encodingInstructionAssignmentList : ~(END|ENCODING_CONTROL)* ;

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to