[Mulgara-svn] r616 - branches/mgr-61-sparql/src/jar/sparql/grammar
pag at mulgara.org
pag at mulgara.org
Thu Dec 13 21:13:00 UTC 2007
Author: pag
Date: 2007-12-13 15:12:57 -0600 (Thu, 13 Dec 2007)
New Revision: 616
Modified:
branches/mgr-61-sparql/src/jar/sparql/grammar/expr.grammar
Log:
Adding in all SPARQL constructs. Only up to 31 (of 69), and not all data types have been dealt with.
Modified: branches/mgr-61-sparql/src/jar/sparql/grammar/expr.grammar
===================================================================
--- branches/mgr-61-sparql/src/jar/sparql/grammar/expr.grammar 2007-12-13 21:11:55 UTC (rev 615)
+++ branches/mgr-61-sparql/src/jar/sparql/grammar/expr.grammar 2007-12-13 21:12:57 UTC (rev 616)
@@ -6,6 +6,7 @@
%terminals MULT, DIV, PLUS, MINUS, LPAREN, RPAREN;
%terminals EQUALS, NOT_EQUALS, LESS_THAN, GREATER_THAN, LESS_THAN_EQUALS, GREATER_THAN_EQUALS;
+%terminals BASE, PREFIX;
%terminals SELECT, DISTINCT, REDUCED, FROM, WHERE, GRAPH;
%terminals LBRACE, RBRACE, DOT, OPTIONAL, SEMI_COLON, COMMA, LETTER_A, FILTER;
%terminals OR, AND, NOT, STR, LANG, LANGMATCHES, DATATYPE, BOUND, SAME_TERM, IS_IRI, IS_URI, IS_BLANK, IS_LITERAL, REGEX;
@@ -31,20 +32,27 @@
%typeof BOOLEAN = "Boolean";
%typeof BLANK_NODE = "BlankNode";
-%typeof from_clause = "From";
+%typeof prefix_decl_list = "ArrayList<String>";
+%typeof dataset_clause_list = "ArrayList<From>";
%typeof where_clause = "GroupGraphPattern";
+%typeof triples_block = "ArrayList<Triple>";
+%typeof graph_pattern_not_triples = "NonTriple";
+%typeof group_graph_pattern = "GroupGraphPattern";
+%typeof group_or_union_graph_pattern = "ArrayList<GroupGraphPattern>";
+%typeof filter = "Constraint";
+
+%typeof triples_same_subject = "ArrayList<Triple>";
+%typeof construct_triples = "ArrayList<TriplesSameSubject>";
+
%typeof var_or_iriref = "Predicate";
%typeof object_list = "ArrayList";
%typeof object = "RDFObject";
-%typeof group_graph_pattern = "GroupGraphPattern";
%typeof graph_element_list = "ArrayList<GraphElement>";
%typeof graph_element = "GraphElement";
-%typeof optional_graph_element = "OptionalGraphPatternElement";
-%typeof triples_same_subject = "ArrayList<Triple>";
+%typeof optional_graph_pattern = "OptionalGraphPatternElement";
%typeof verb_object_list = "ArrayList<Triple>";
%typeof verb_object_list_list, verb_object_list_list_element = "ArrayList<Triple>";
%typeof prop_list_not_empty = "ArrayList<Triple>";
-%typeof triples_block = "ArrayList<Triple>";
%typeof var_list = "ArrayList<String>";
%typeof var_or_term, var = "String";
%typeof verb = "Predicate";
@@ -52,207 +60,386 @@
%typeof expression, numeric_literal, primary_expression, bracketted_expression = "Expression";
%typeof builtin_call, regex_expression = "BuiltinCall";
%typeof group_graph_pattern_union = "ArrayList<GroupGraphPattern>";
-%typeof group_or_union_graph_element = "GroupOrUnionGraphPatternElement";
-%typeof graph_pattern_not_triples = "NonTriple";
%typeof iri_ref = "IRIReference";
-%typeof filter, constraint = "Constraint";
+%typeof constraint = "Constraint";
%typeof function_call = "FunctionCall";
%typeof arg_list, expression_list = "ArrayList<Expression>";
%typeof limit_clause, offset_clause = "Integer";
%typeof solution_modifier = "SolutionModifier";
-%typeof limit_offset_clause = "LimitOffset";
+%typeof limit_offset_clauses = "LimitOffset";
%typeof order_clause, order_condition_list = "ArrayList<OrderByClause>";
%typeof order_condition = "OrderByClause";
-%goal select_query;
+%goal query;
+// [1] Query ::= Prologue ( SelectQuery | ConstructQuery | DescribeQuery | AskQuery )
+query
+ = prologue select_query
+// | prologue construct_query
+// | prologue describe_query
+// | prologue ask_query
+ ;
+
+// [2] Prologue ::= BaseDecl? PrefixDecl*
+prologue
+ = base_decl prefix_decl_list
+ | base_decl
+ | prefix_decl_list
+ | // empty
+ ;
+
+// [3] BaseDecl ::= 'BASE' IRI_REF
+base_decl
+ = BASE IRI_REF
+ ;
+
+// [4] PrefixDecl ::= 'PREFIX' PNAME_NS IRI_REF
+prefix_decl_list
+ = PREFIX PNAME_NS.pname IRI_REF.iri {:
+ ArrayList<String> prefixes = new ArrayList<String>();
+ prefixes.add(pname + iri);
+ return new Symbol(prefixes);
+ :}
+ | prefix_decl_list.pl PREFIX PNAME_NS.pname IRI_REF.iri {:
+ pl.add(pname + iri);
+ return _symbol_pl;
+ :}
+ ;
+
+// [5] SelectQuery ::= 'SELECT' ( 'DISTINCT' | 'REDUCED' )? ( Var+ | '*' ) DatasetClause* WhereClause SolutionModifier
select_query
- = SELECT distinct_or_reduced.d select_vars.v dataset_clause.f where_clause.w solution_modifier.s
- {:
- return new SelectQuery(v, d, f, w, s);
- :}
+ = SELECT DISTINCT select_vars.v dataset_clause_list.f where_clause.w solution_modifier.s
+ {: return new SelectQuery(v, true, false, f, w, s); :}
+ | SELECT DISTINCT select_vars.v where_clause.w solution_modifier.s
+ {: return new SelectQuery(v, true, false, null, w, s); :}
+ | SELECT REDUCED select_vars.v dataset_clause.f where_clause.w solution_modifier.s
+ {: return new SelectQuery(v, false, true, f, w, s); :}
+ | SELECT REDUCED select_vars.v where_clause.w solution_modifier.s
+ {: return new SelectQuery(v, false, true, null, w, s); :}
+ | SELECT select_vars.v dataset_clause.f where_clause.w solution_modifier.s
+ {: return new SelectQuery(v, false, false, f, w, s); :}
+ | SELECT select_vars.v where_clause.w solution_modifier.s
+ {: return new SelectQuery(v, false, false, null, w, s); :}
;
-distinct_or_reduced
- = DISTINCT
- | REDUCED
- |
- ;
-
+// [5a] from SelectQuery -> ( Var+ | '*' )
select_vars
= var_list
| MULT
;
-
+
+// [5b] from SelectQuery -> Var+
var_list
= var.v {:
- ArrayList<String> variables = new ArrayList<String>();
- variables.add(v);
- return new Symbol(variables);
- :}
- | var_list.vl var.v
- {:
- vl.add(v);
- return _symbol_vl;
- :}
+ ArrayList<String> variables = new ArrayList<String>();
+ variables.add(v);
+ return new Symbol(variables);
+ :}
+ | var_list.vl var.v {:
+ vl.add(v);
+ return _symbol_vl;
+ :}
;
+// [6] ConstructQuery ::= 'CONSTRUCT' ConstructTemplate DatasetClause* WhereClause SolutionModifier
+// [7] DescribeQuery ::= 'DESCRIBE' ( VarOrIRIref+ | '*' ) DatasetClause* WhereClause? SolutionModifier
+// [8] AskQuery ::= 'ASK' DatasetClause* WhereClause
-dataset_clause
- = from_clause
- |
+
+// [9] DatasetClause ::= 'FROM' ( DefaultGraphClause | NamedGraphClause )
+dataset_clause_list
+ = FROM data_graph_clause_or_named_graph_clause.e {:
+ ArrayList<From> froms = new ArrayList<From>();
+ froms.add(From(e));
+ return new Symbol(froms);
+ :}
+ | dataset_clause_list.dcl FROM data_graph_clause_or_named_graph_clause.e {:
+ dcl.add(new From(e));
+ return _symbol_vl;
+ :}
;
-
-from_clause
- = FROM data_graph_clause_or_named_graph_clause.e
- {:
- return new From(e);
- :}
- ;
-
+
+
+// [9a] from DatasetClause -> ( DefaultGraphClause | NamedGraphClause )
data_graph_clause_or_named_graph_clause
= default_graph_clause
| named_graph_clause
;
-
+
+
+// [10] DefaultGraphClause ::= SourceSelector
default_graph_clause
- = iri_ref;
+ = source_selector
+ ;
+
+// [11] NamedGraphClause ::= 'NAMED' SourceSelector
named_graph_clause
- = NAMED iri_ref
+ = NAMED source_selector
;
+
+// [12] SourceSelector ::= IRIref
+source_selector
+ = iri_ref
+ ;
+
+
+// [13] WhereClause ::= 'WHERE'? GroupGraphPattern
where_clause
= WHERE group_graph_pattern
| group_graph_pattern
;
+
+// [14] SolutionModifier ::= OrderClause? LimitOffsetClauses?
+solution_modifier
+ = order_clause.oc limit_offset_clauses.loc {: return new SolutionModifier(oc, loc); :}
+ | order_clause.oc {: return new SolutionModifier(oc); :}
+ | limit_offset_clauses.loc {: return new SolutionModifier(loc); :}
+ |
+ ;
+
+
+// [15] LimitOffsetClauses ::= ( LimitClause OffsetClause? | OffsetClause LimitClause? )
+limit_offset_clauses
+ = limit_clause.l offset_clause.o {:
+ return new LimitOffset(l, o);
+ :}
+ | limit_clause.l {:
+ LimitOffset limo = new LimitOffset();
+ limo.setLimit(l);
+ return limo;
+ :}
+ | offset_clause.o {:
+ LimitOffset limo = new LimitOffset();
+ limo.setOffset(o);
+ return limo;
+ :}
+ | offset_clause.o limit_clause.l {:
+ return new LimitOffset(l, o);
+ :}
+ ;
+
+
+// [16] OrderClause ::= 'ORDER' 'BY' OrderCondition+
+order_clause
+ = ORDER_BY order_condition_list.ocl { return _symbol_ocl; :}
+ ;
+
+
+// [16a] from OrderClaus -> OrderCondition+
+order_condition_list
+ = order_condition.oc {:
+ ArrayList<OrderByClause> ocl = new ArrayList<OrderByClause>();
+ ocl.add(oc);
+ return new Symbol(ocl);
+ :}
+ | order_condition_list.oclist order_condition.oc {:
+ oclist.add(oc);
+ return _symbol_oclist;
+ :}
+ ;
+
+
+// [17] OrderCondition ::= (('ASC' | 'DESC') BrackettedExpression) | (Constraint | Var)
+order_condition
+ = ASC bracketted_expression.e {:
+ return new OrderByClause(e, OrderByClause.OrderDirection.ASCENDING);
+ :}
+ | DESC bracketted_expression.e {:
+ return new OrderByClause(e, OrderByClause.OrderDirection.DESCENDING);
+ :}
+ | constraint.c {: return new OrderByClause(c); :}
+ | var.v {: return new OrderByClause(new VariableExpression(v)); :}
+ ;
+
+
+// [18] LimitClause ::= 'LIMIT' INTEGER
+limit_clause
+ = LIMIT INTEGER.e {: return _symbol_e; :}
+ ;
+
+
+// [19] OffsetClause ::= 'OFFSET' INTEGER
+offset_clause
+ = OFFSET INTEGER.e {: return _symbol_e; :}
+ ;
+
+
+
+// [20] GroupGraphPattern ::= '{' TriplesBlock? ((GraphPatternNotTriples | Filter) '.'? TriplesBlock?)* '}'
group_graph_pattern
- = LBRACE triples_block.triplesBlock graph_element_list.graphElementList RBRACE
- {:
- return new GroupGraphPattern(triplesBlock, graphElementList);
- :}
- | LBRACE triples_block.triplesBlock RBRACE
- {:
- return new GroupGraphPattern(triplesBlock);
- :}
- | LBRACE graph_element_list.graphElementList RBRACE
- {:
- return new GroupGraphPattern(graphElementList, true);
- :}
- | LBRACE RBRACE
- {:
- System.out.println("got brace brace");
- return new GroupGraphPattern();
- :}
+ = LBRACE triples_block.triplesBlock graph_element_list.graphElementList RBRACE {:
+ return new GroupGraphPattern(triplesBlock, graphElementList);
+ :}
+ | LBRACE triples_block.triplesBlock RBRACE {:
+ return new GroupGraphPattern(triplesBlock);
+ :}
+ | LBRACE graph_element_list.graphElementList RBRACE {:
+ return new GroupGraphPattern(graphElementList, true);
+ :}
+ | LBRACE RBRACE {:
+ System.out.println("got brace brace");
+ return new GroupGraphPattern();
+ :}
;
+
+// [20a] from GroupGraphPattern -> ((GraphPatternNotTriples | Filter) '.'? TriplesBlock?)*
graph_element_list
- = graph_element.ge {:
- ArrayList<GraphElement> graphElements = new ArrayList<GraphElement>();
- graphElements.add(ge);
- System.out.println("expr.grammar: graph_element: " + ge);
- return new Symbol(graphElements);
- :}
- | graph_element_list.gel graph_element.ge
- {:
- System.out.println("expr.grammar: graph_element (list): " + ge);
- gel.add(ge);
- return _symbol_gel;
- :}
+ = graph_element.ge {:
+ ArrayList<GraphElement> graphElements = new ArrayList<GraphElement>();
+ graphElements.add(ge);
+ System.out.println("expr.grammar: graph_element: " + ge);
+ return new Symbol(graphElements);
+ :}
+ | graph_element_list.gel graph_element.ge {:
+ System.out.println("expr.grammar: graph_element (list): " + ge);
+ gel.add(ge);
+ return _symbol_gel;
+ :}
;
-
+
+
+// [20b] from GroupGraphPattern -> ((GraphPatternNotTriples | Filter) '.'? TriplesBlock?)
graph_element
- = graph_pattern_not_triples.gp DOT triples_block.tb
- {:
- return new GraphElement(tb, true, gp);
- :}
- | filter.f DOT triples_block.tb
- {:
- return new GraphElement(tb, true, f);
- :}
- | graph_pattern_not_triples.gp triples_block.tb
- {:
- return new GraphElement(tb, false, gp);
- :}
- | filter.f triples_block.tb
- {:
- return new GraphElement(tb, false, f);
- :}
- | graph_pattern_not_triples.gp
- {:
- System.out.println("expr.grammar: graph_pattern_not_triples: " + gp);
- return new GraphElement(gp);
- :}
- | filter.f
- {:
- return new GraphElement(f);
- :}
+ = graph_pattern_not_triples.gp DOT triples_block.tb {: return new GraphElement(tb, true, gp); :}
+ | filter.f DOT triples_block.tb {: return new GraphElement(tb, true, f); :}
+ | graph_pattern_not_triples.gp triples_block.tb {: return new GraphElement(tb, false, gp); :}
+ | filter.f triples_block.tb {: return new GraphElement(tb, false, f); :}
+ | graph_pattern_not_triples.gp DOT {:
+ System.out.println("expr.grammar: graph_pattern_not_triples: " + gp);
+ return new GraphElement(gp);
+ :}
+ | graph_pattern_not_triples.gp {:
+ System.out.println("expr.grammar: graph_pattern_not_triples: " + gp);
+ return new GraphElement(gp);
+ :}
+ | filter.f DOT {: return new GraphElement(f); :}
+ | filter.f {: return new GraphElement(f); :}
;
+
+// [21] TriplesBlock ::= TriplesSameSubject ('.' TriplesBlock?)?
+triples_block
+ = triples_same_subject
+ | triples_same_subject DOT
+ | triples_same_subject.oneSubjectTriples triples_block.anotherSubjectTriples {:
+ oneSubjectTriples.addAll((List<Triple>)anotherSubjectTriples.value);
+ return _symbol_oneSubjectTriples;
+ :}
+ ;
+
+
+// [22] GraphPatternNotTriples ::= OptionalGraphPattern | GroupOrUnionGraphPattern | GraphGraphPattern
graph_pattern_not_triples
- = optional_graph_element
- | group_or_union_graph_element
+ = optional_graph_pattern
+ | group_or_union_graph_pattern
| graph_graph_pattern
;
-optional_graph_element
- = OPTIONAL group_graph_pattern.ggp
- {:
- return new OptionalGraphPatternElement(ggp);
- :}
+
+// [23] OptionalGraphPattern ::= 'OPTIONAL' GroupGraphPattern
+optional_graph_pattern
+ = OPTIONAL group_graph_pattern.ggp {: return new OptionalGraphPatternElement(ggp); :}
;
-group_or_union_graph_element
- = group_graph_pattern.ggp
- {:
- ArrayList<GroupGraphPattern> patterns = new ArrayList<GroupGraphPattern>();
- patterns.add(ggp);
- return new GroupOrUnionGraphPatternElement(patterns);
+
+// [24] GraphGraphPattern ::= 'GRAPH' VarOrIRIref GroupGraphPattern
+graph_graph_pattern
+ = GRAPH var_or_iriref.v group_graph_pattern.ggp {: return new GraphGraphPatternElement(v, ggp); :}
+ ;
+
+
+// [25] GroupOrUnionGraphPattern ::= GroupGraphPattern ('UNION' GroupGraphPattern)*
+group_or_union_graph_pattern
+ = group_graph_pattern.ggp {:
+ ArrayList<GroupGraphPattern> ggPattern = new ArrayList<GroupGraphPattern>();
+ graphElements.add(ggp);
+ return new Symbol(ggPattern);
+ :}
+ | group_or_union_graph_pattern.ggpul UNION group_graph_pattern.ggp {:
+ ggpul.add(ggp);
+ return _symbol_ggpul;
:}
- | group_graph_pattern.ggp group_graph_pattern_union.ggpu
- {:
- ggpu.add(ggp);
- return new GroupOrUnionGraphPatternElement(ggpu);
- :}
;
-group_graph_pattern_union
- = UNION group_graph_pattern.ggp
- {:
- ArrayList<GroupGraphPattern> patterns = new ArrayList<GroupGraphPattern>();
- patterns.add(ggp);
- return new Symbol(patterns);
- :}
- | group_graph_pattern_union.ggpu UNION group_graph_pattern.ggp
- {:
- ggpu.add(ggp);
- return _symbol_ggpu;
- :}
- ;
-
-graph_graph_pattern
- = GRAPH var_or_iriref.v group_graph_pattern.ggp
- {:
- return new GraphGraphPatternElement(v, ggp);
- :}
- ;
+// [26] Filter ::= 'FILTER' Constraint
filter
= FILTER constraint
;
+// [27] Constraint ::= BrackettedExpression | BuiltInCall | FunctionCall
constraint
- = bracketted_expression.e
+ = bracketted_expression
| builtin_call
| function_call
;
+
+// [28] FunctionCall ::= IRIref ArgList
+function_call
+ = iri_ref.iri arg_list.al {:
+ return new FunctionCall(iri, al);
+ :}
+ ;
+
+
+// [29] ArgList ::= (NIL | '(' Expression (',' Expression)* ')')
+arg_list
+ = NIL {: return new Symbol(new ArrayList<Expression>()); :}
+ | LPAREN expression_list.el RPAREN {: return _symbol_el; :}
+ ;
+
+
+// [29a] from ArgList -> Expression (',' Expression)*
+expression_list
+ = expression.e {:
+ ArrayList<Expression> expressions = new ArrayList<Expression>();
+ expressions.add(e);
+ return new Symbol(expressions);
+ :}
+ | expression_list.el COMMA expression.e {:
+ el.add(e);
+ return _symbol_el;
+ :}
+ ;
+
+
+// [30] ConstructTemplate ::= '{' ConstructTriples? '}'
+construct_template
+ = LBRACE RBRACE
+ | LBRACE construct_triples RBRACE
+ ;
+
+
+// [31] ConstructTriples ::= TriplesSameSubject ( '.' ConstructTriples? )?
+construct_triples
+ = triples_same_subject.tss {:
+ ArrayList<TriplesSameSubject> constructTriples = new ArrayList<TriplesSameSubject>();
+ constructTriples.add(tss);
+ return new Symbol(constructTriples);
+ :}
+ | construct_triples.cs DOT triples_same_subject.tss {:
+ cd.add(tss);
+ return _symbol_cs;
+ :}
+ | triples_same_subject DOT {:
+ ArrayList<TriplesSameSubject> constructTriples = new ArrayList<TriplesSameSubject>();
+ constructTriples.add(tss);
+ return new Symbol(constructTriples);
+ :}
+ ;
+
+
bracketted_expression
= LPAREN expression.e RPAREN {: return e; :}
;
+
expression
= expression.e1 OR expression.e2
{: return new OrExpression(e1, e2); :}
@@ -320,146 +507,7 @@
| REGEX LPAREN expression.e1 COMMA expression.e2 COMMA expression.e3 RPAREN
{: return new RegularExpression(e1, e2, e3); :}
;
-
-function_call
- = iri_ref.iri arg_list.al
- {:
- return new FunctionCall(iri, al);
- :}
- ;
-
-arg_list
- = NIL
- {:
- return new Symbol(new ArrayList<Expression>());
- :}
- | LPAREN expression_list.el RPAREN
- {:
- return _symbol_el;
- :}
- ;
-
-expression_list
- = expression.e
- {:
- ArrayList<Expression> expressions = new ArrayList<Expression>();
- expressions.add(e);
- return new Symbol(expressions);
- :}
- | expression_list.el COMMA expression.e
- {:
- el.add(e);
- return _symbol_el;
- :}
- ;
-solution_modifier
- = order_clause.oc limit_offset_clause.loc
- {:
- return new SolutionModifier(oc, loc);
- :}
- | order_clause.oc
- {:
- return new SolutionModifier(oc);
- :}
- | limit_offset_clause.loc
- {:
- return new SolutionModifier(loc);
- :}
- |
- ;
-
-order_clause
- = ORDER_BY order_condition_list.ocl
- {:
- return _symbol_ocl;
- :}
- ;
-
-order_condition_list
- = order_condition.oc
- {:
- ArrayList<OrderByClause> ocl = new ArrayList<OrderByClause>();
- ocl.add(oc);
- return new Symbol(ocl);
- :}
- | order_condition_list.oclist order_condition.oc
- {:
- oclist.add(oc);
- return _symbol_oclist;
- :}
- ;
-
-order_condition
- = ASC bracketted_expression.e
- {:
- return new OrderByClause(e, OrderByClause.OrderDirection.ASCENDING);
- :}
- | DESC bracketted_expression.e
- {:
- return new OrderByClause(e, OrderByClause.OrderDirection.DESCENDING);
- :}
- | constraint.c
- {:
- return new OrderByClause(c);
- :}
- | var.v
- {:
- return new OrderByClause(new VariableExpression(v));
- :}
- ;
-
-limit_offset_clause
- = limit_clause.l offset_clause.o
- {:
- return new LimitOffset(l, o);
- :}
- | limit_clause.l
- {:
- LimitOffset limo = new LimitOffset();
- limo.setLimit(l);
- return limo;
- :}
- | offset_clause.o
- {:
- LimitOffset limo = new LimitOffset();
- limo.setOffset(o);
- return limo;
- :}
- | offset_clause.o limit_clause.l
- {:
- return new LimitOffset(l, o);
- :}
- ;
-
-limit_clause
- = LIMIT INTEGER.e
- {:
- return _symbol_e;
- :}
- ;
-
-offset_clause
- = OFFSET INTEGER.e
- {:
- return _symbol_e;
- :}
- ;
-
-triples_block
- = triples_same_subject
- | triples_same_subject.oneSubjectTriples another_triples_block.anotherSubjectTriples
- {:
- oneSubjectTriples.addAll((List<Triple>)anotherSubjectTriples.value);
- return _symbol_oneSubjectTriples;
- :}
- ;
-
-another_triples_block
- = DOT
- | DOT triples_block
- ;
-
triples_same_subject
= var_or_term.v prop_list_not_empty.triples
{:
More information about the Mulgara-svn
mailing list