[antlr-interest] Syntactic predicates cause unexplainable compilation errors in different partsof the code

Loring Craymer Loring.G.Craymer at jpl.nasa.gov
Wed Jan 26 13:48:47 PST 2005


This is one of those cases that is usually handled by factoring out the comma:

gene_ref:
       gene_refline ("," gene_refline)*
       ;

However, I think what you are really running into is ANTLR 2's approximate 
LLk.  If you look at the generated code (without the synpreds), I think 
that you will find that it does the right thing.

--Loring


At 12:34 PM 1/26/2005, Peter Robinson wrote:


>Gene-ref ::= SEQUENCE {
>      A VisibleString OPTIONAL ,
>      B VisibleString OPTIONAL ,
>     C VisibleString OPTIONAL ,
>     D VisibleString OPTIONAL ,
>     E BOOLEAN DEFAULT FALSE ,
>     F SET OF Dbtag OPTIONAL ,
>     G SET OF VisibleString OPTIONAL ,
>     H  VisibleString OPTIONAL }
>   END
>
>
>
>
>Dear ANTLR list,
>
>First of all thanks to you all for being a helpful and informative list.
>I recently have been trying to learn antlr and cannot now imagine using
>things like lex/yacc with which I previously occasionally did things.
>
>I am now trying to parse a file structure from NCBI in ASN.1 format. The
>specification of a small part of the entire thing is as follows  ( I
>have replaced some keywords with the letters A-H for clarity). Any one
>of the entries is optional and is followed by a comma if there is going
>to be another line. There are Gene-ref entries with only one entry (and
>no comma).
>
>
>Gene-ref ::= SEQUENCE {
>      A VisibleString OPTIONAL ,
>      B VisibleString OPTIONAL ,
>     C VisibleString OPTIONAL ,
>     D VisibleString OPTIONAL ,
>     E BOOLEAN DEFAULT FALSE ,
>     F SET OF Dbtag OPTIONAL ,
>     G SET OF VisibleString OPTIONAL ,
>     H  VisibleString OPTIONAL }
>   END
>
>After trying constructs such as (",")? and getting nondeterminateness
>warnings, I tried my hand at a syntactic predicate as follows:
>
>generef_line returns [myJavaObject ... ]
>{
>         String s;
>         Dbtag d;
>}: GENE_KW "{"
>        (        ( A STRING ",")=>
>            A  s1:STRING { System.out.println(s1.getText()); }  ","
>         |  A  s2:STRING { System.out.println(s2.getText()); }
>         )?
>         (  (B STRING ",")=>
>            B s3:STRING { System.out.println(s3.getText()); } ","
>         |  B s4:STRING {  System.out.println(s4.getText()); }
>         )?
>         AND SO ON...
>
>         "}"
>;
>
>
>However, this now causes unexplainable compilation errors in other parts
>of the code (about 400 lines of grammar etc) to appear, in code that
>**worked perfectly fine** before. What is going on?? and is there a
>better way to parse the above construct? Thanks, Peter
>
>--
>Peter N. Robinson
>peter.robinson at t-online.de
>peter.robinson at charite.de
>http://www.charite.de/ch/medgen/robinson/




More information about the antlr-interest mailing list