[antlr-interest] Interesting parser grammar issue

Bill Andersen bill.andersen at mac.com
Tue Jun 28 13:06:04 PDT 2011


(Using 3.3)

Check out this partial grammar for SPARQL 1.1 (http://www.w3.org/TR/sparql11-query/)

// [2]
query
	: prologue 
	  ( select_query
	  | construct_query
	  | describe_query
	  | ask_query
	  )
	  bindings_clause 
	;
	
// [4]
prologue
	: prologue_decl*
	; 
	
// [4.1]
prologue_decl
  : prefix_decl
  | base_decl
  ;
	
// [5]
base_decl
	: BASE IRI_REF
	;
	
// [6]
prefix_decl
	: PREFIX PNAME_NS IRI_REF
	;

The above version works - at least for SELECT queries.  The original production [4] that I replaced with [4] and [4.1] looked like

// [4]
prologue
	: ( base_decl | prefix_decl )*
	;

The grammar with this production will fail to parse SPARQL queries like the following, with the error "NoViableAltException: line 1:0 no viable alternative at input 'PREFIX'"

PREFIX  data:  <http://example.org/foaf/>
PREFIX  foaf:  <http://xmlns.com/foaf/0.1/>
PREFIX  rdfs:  <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?mbox ?nick ?ppd
FROM NAMED <http://example.org/foaf/aliceFoaf>
FROM NAMED <http://example.org/foaf/bobFoaf>
WHERE
{
  GRAPH data:aliceFoaf
  {
    ?alice foaf:mbox <alice> ;
           foaf:knows ?whom .
    ?whom  foaf:mbox ?mbox ;
           rdfs:seeAlso ?ppd .
    ?ppd  a foaf:PersonalProfileDocument .
  } .
  GRAPH ?ppd
  {
      ?w foaf:mbox ?mbox ;
         foaf:nick ?nick
  }
}

So, why should the new [4] + [4.1] combination work where the original [4] would not?




More information about the antlr-interest mailing list