← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~zorba-coders/zorba/feature-jsoniq_parser into lp:zorba

 

Matthias Brantner has proposed merging lp:~zorba-coders/zorba/feature-jsoniq_parser into lp:zorba.

Commit message:
introduce new jsoniq parser + an option in the Zorba_CompilerHints to enable it

Requested reviews:
  Matthias Brantner (matthias-brantner)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/feature-jsoniq_parser/+merge/153648
-- 
https://code.launchpad.net/~zorba-coders/zorba/feature-jsoniq_parser/+merge/153648
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'include/zorba/options.h'
--- include/zorba/options.h	2013-02-07 17:24:36 +0000
+++ include/zorba/options.h	2013-03-16 01:56:23 +0000
@@ -65,6 +65,12 @@
    */
   bool for_serialization_only;
 
+  /**
+   * \brief By default, the flag is set to false. If set to true, the JSONiq
+   * parser will be used.
+   */
+  bool jsoniq_mode;
+
 #ifdef __cplusplus
   /** \brief Default constructor for CompilerHints which assigns default values to all hints (C++ only).
    *

=== modified file 'src/api/options.cpp'
--- src/api/options.cpp	2013-02-07 17:24:36 +0000
+++ src/api/options.cpp	2013-03-16 01:56:23 +0000
@@ -23,7 +23,8 @@
   :
   opt_level(ZORBA_OPT_LEVEL_O1),
   lib_module(false),
-  for_serialization_only(false)
+  for_serialization_only(false),
+  jsoniq_mode(false)
 {
 }
 

=== modified file 'src/api/xqueryimpl.cpp'
--- src/api/xqueryimpl.cpp	2013-02-07 17:24:36 +0000
+++ src/api/xqueryimpl.cpp	2013-03-16 01:56:23 +0000
@@ -587,6 +587,8 @@
   // If lib_module is set to true the query will be considered a library module
   theCompilerCB->theConfig.lib_module = aHints.lib_module;
   theCompilerCB->theConfig.for_serialization_only = aHints.for_serialization_only;
+  theCompilerCB->theConfig.jsoniq_mode = aHints.jsoniq_mode;
+
   CompilerCB::config::opt_level_t optLevel;
   if (aHints.opt_level == ZORBA_OPT_LEVEL_O0)
     optLevel = CompilerCB::config::O0;

=== modified file 'src/compiler/api/compiler_api.cpp'
--- src/compiler/api/compiler_api.cpp	2013-02-07 17:24:36 +0000
+++ src/compiler/api/compiler_api.cpp	2013-03-16 01:56:23 +0000
@@ -39,6 +39,7 @@
 #include "compiler/api/compilercb.h"
 
 #include "compiler/parser/xquery_driver.h"
+#include "compiler/parser/jsoniq_driver.h"
 #include "compiler/parsetree/parsenodes.h"
 #include "compiler/parsetree/parsenodes.h"
 #include "compiler/parsetree/parsenode_print_xml_visitor.h"
@@ -179,11 +180,22 @@
 
   theCompilerCB->setPhase(CompilerCB::PARSING);
 
-  xquery_driver lDriver(&*theCompilerCB);
-  lDriver.parse_stream(*xquery_stream, aFileName);
-
+  parsenode_t node;
   theCompilerCB->setPhase(CompilerCB::NONE);
 
+  if (theCompilerCB->theConfig.jsoniq_mode)
+  {
+    jsoniq_driver lDriver(&*theCompilerCB);
+    lDriver.parse_stream(*xquery_stream, aFileName);
+    node =  lDriver.get_expr();
+  }
+  else
+  {
+    xquery_driver lDriver(&*theCompilerCB);
+    lDriver.parse_stream(*xquery_stream, aFileName);
+    node =  lDriver.get_expr();
+  }
+
 #ifdef ZORBA_XQUERYX
   delete xquery_stream;
   if (is_xqueryx)
@@ -192,7 +204,7 @@
   }
 #endif
 
-  parsenode_t node = lDriver.get_expr();
+
 
   if (typeid (*node) == typeid (ParseErrorNode))
   {

=== modified file 'src/compiler/api/compilercb.h'
--- src/compiler/api/compilercb.h	2013-02-07 17:24:36 +0000
+++ src/compiler/api/compilercb.h	2013-03-16 01:56:23 +0000
@@ -156,6 +156,7 @@
     expr_callback  translate_cb;
     expr_callback  optimize_cb;
     bool           print_item_flow;  // TODO: move to RuntimeCB
+    bool           jsoniq_mode;
 
    public:
     SERIALIZABLE_CLASS(config);

=== modified file 'src/compiler/parser/CMakeLists.txt'
--- src/compiler/parser/CMakeLists.txt	2013-02-07 17:24:36 +0000
+++ src/compiler/parser/CMakeLists.txt	2013-03-16 01:56:23 +0000
@@ -19,8 +19,11 @@
     ft_types.cpp
     symbol_table.cpp
     xquery_driver.cpp
+    jsoniq_driver.cpp
     query_loc.cpp
-    xqdoc_comment.cpp)
+    xqdoc_comment.cpp
+    parser_helpers.cpp
+    zorba_parser_error.cpp)
 
 SET(PARSER_BUILD_SRCS)
 
@@ -49,13 +52,15 @@
     SET(BISON_GENERATE_DEFINES TRUE)
     BISON_FILE(compiler/parser/xquery_parser.y)
     LIST(APPEND PARSER_BUILD_SRCS ${CMAKE_BINARY_DIR}/src/compiler/parser/xquery_parser.cpp)
+    BISON_FILE(compiler/parser/jsoniq_parser.y)
+    LIST(APPEND PARSER_BUILD_SRCS ${CMAKE_BINARY_DIR}/src/compiler/parser/jsoniq_parser.cpp)
 ELSE (GENERATE_BISON_FILES)
     IF (BISON_EXECUTABLE)
         MESSAGE(STATUS "         GNU Bison's version " ${BISON_VERSION_FULL} " is less than required (2.4) -- the parser will not be regenerated")
     ELSE (BISON_EXECUTABLE)
         MESSAGE(STATUS "         GNU Bison is not available -- the parser will not be regenerated")
     ENDIF (BISON_EXECUTABLE)
-    MESSAGE(STATUS "         Using repository files " ${CMAKE_CURRENT_SOURCE_DIR}/xquery_parser.cpp)
+    MESSAGE(STATUS "         Using repository files " ${CMAKE_CURRENT_SOURCE_DIR}/xquery_parser.cpp ${CMAKE_CURRENT_SOURCE_DIR}/jsoniq_parser.cpp)
     LIST(APPEND PARSER_SRCS xquery_parser.cpp)
 ENDIF (GENERATE_BISON_FILES)
 
@@ -67,7 +72,7 @@
 MACRO(COMPARE_FLEX_VERSION a b)
   COMPARE_VERSION_STRINGS("${a}" "${b}" result)
     IF(result LESS 0)
-        SET (GENERATE_FLEX_FILES FALSE)
+        SET(GENERATE_FLEX_FILES FALSE)
         MESSAGE(STATUS "Flex: ${a} <  ${b}")
      ELSE(result LESS 0)
        IF(result GREATER 0)
@@ -84,8 +89,9 @@
 IF (GENERATE_FLEX_FILES AND ZORBA_HAVE_FLEXLEXER_H)
     SET(FLEX_PREFIX_OUTPUTS TRUE)
     FLEX_FILE(compiler/parser/xquery_scanner.l)
+    FLEX_FILE(compiler/parser/jsoniq_scanner.l)
     ADD_DEFINITIONS(-DFLEX_FILES_REGENERATED)
-    LIST(APPEND PARSER_BUILD_SRCS ${CMAKE_BINARY_DIR}/src/compiler/parser/xquery_scanner.cpp)
+    LIST(APPEND PARSER_BUILD_SRCS ${CMAKE_BINARY_DIR}/src/compiler/parser/xquery_scanner.cpp ${CMAKE_BINARY_DIR}/src/compiler/parser/jsoniq_scanner.cpp)
 ELSE (GENERATE_FLEX_FILES AND ZORBA_HAVE_FLEXLEXER_H)
     IF (NOT ZORBA_HAVE_FLEXLEXER_H)
         MESSAGE(STATUS "         FlexLexer.h has not been found -- the lexer will not be regenerated")
@@ -96,8 +102,8 @@
             MESSAGE(STATUS "         GNU Flex is not available -- the lexer will not be regenerated")
         ENDIF (FLEX_EXECUTABLE)
     ENDIF (NOT ZORBA_HAVE_FLEXLEXER_H)
-    MESSAGE(STATUS "         Using repository file " ${CMAKE_CURRENT_SOURCE_DIR}/xquery_scanner.cpp)
-    LIST(APPEND PARSER_SRCS xquery_scanner.cpp)
+    MESSAGE(STATUS "         Using repository file " ${CMAKE_CURRENT_SOURCE_DIR}/xquery_scanner.cpp ${CMAKE_CURRENT_SOURCE_DIR}/jsoniq_scanner.cpp)
+    LIST(APPEND PARSER_SRCS xquery_scanner.cpp jsoniq_scanner.cpp)
 ENDIF (GENERATE_FLEX_FILES AND ZORBA_HAVE_FLEXLEXER_H)
 
 # generate a script that copies the generated parser and scanner files from the build

=== modified file 'src/compiler/parser/copyparser.sh.cmake'
--- src/compiler/parser/copyparser.sh.cmake	2013-02-07 17:24:36 +0000
+++ src/compiler/parser/copyparser.sh.cmake	2013-03-16 01:56:23 +0000
@@ -20,8 +20,11 @@
 
 cp $BINARY_DIR/compiler/parser/xquery_parser.hpp $SOURCE_DIR/compiler/parser
 cp $BINARY_DIR/compiler/parser/xquery_parser.cpp $SOURCE_DIR/compiler/parser
+cp $BINARY_DIR/compiler/parser/jsoniq_parser.hpp $SOURCE_DIR/compiler/parser
+cp $BINARY_DIR/compiler/parser/jsoniq_parser.cpp $SOURCE_DIR/compiler/parser
 cp $BINARY_DIR/FlexLexer.h $SOURCE_DIR/compiler/parser/FlexLexer.h
 sed -e 's|^#include <FlexLexer.h>|#include "compiler/parser/FlexLexer.h"|g' $BINARY_DIR/compiler/parser/xquery_scanner.cpp > $SOURCE_DIR/compiler/parser/xquery_scanner.cpp
+sed -e 's|^#include <FlexLexer.h>|#include "compiler/parser/FlexLexer.h"|g' $BINARY_DIR/compiler/parser/jsoniq_scanner.cpp > $SOURCE_DIR/compiler/parser/jsoniq_scanner.cpp
 
 echo "Copying Done!"
 echo "Important Hint:"

=== added file 'src/compiler/parser/jsoniq_driver.cpp'
--- src/compiler/parser/jsoniq_driver.cpp	1970-01-01 00:00:00 +0000
+++ src/compiler/parser/jsoniq_driver.cpp	2013-03-16 01:56:23 +0000
@@ -0,0 +1,223 @@
+/*
+ * Copyright 2006-2008 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "stdafx.h"
+
+#include <fstream>
+#include "system/properties.h"
+#include "compiler/parser/jsoniq_driver.h"
+
+#ifdef __GNUC__
+  // disable a warning in location.hh which comes with bison
+  // position.hh:141: warning: suggest parentheses around && within ||
+#  pragma GCC diagnostic ignored "-Wparentheses"
+#endif
+
+#include "compiler/parser/jsoniq_scanner.h"
+#include "compiler/parser/jsoniq_parser.hpp"
+
+#ifdef __GNUC__
+#  pragma GCC diagnostic warning "-Wparentheses"
+#endif
+
+#include "compiler/parser/zorba_parser_error.h"
+#include "compiler/api/compilercb.h"
+#include "context/static_context.h"
+#include "diagnostics/xquery_diagnostics.h"
+#include "util/xml_util.h"
+
+
+namespace zorba
+{
+
+jsoniq_driver::jsoniq_driver(CompilerCB* aCompilerCB, uint32_t initial_heapsize)
+  :
+  symtab(initial_heapsize),
+  expr_p (NULL),
+  theCompilerCB(aCompilerCB),
+  parserError(NULL)
+{
+}
+
+jsoniq_driver::~jsoniq_driver()
+{
+  if (parserError)
+    delete parserError;
+}
+
+// Error generators
+ZorbaParserError* jsoniq_driver::unrecognizedCharErr(const char* _error_token, const location& loc)
+{
+  std::string token;
+    // translate some common non-printable characters for better readability.
+  if (*_error_token == '\t')
+    token = "\\t";
+  else if (*_error_token == '\n')
+    token = "\\n";
+  else if (*_error_token == '\r')
+    token = "\\r";
+  else if (*_error_token == ' ')
+    token = "<blank>";
+  else
+    token = _error_token;
+
+  parserError = new ZorbaParserError("syntax error, unexpected character \"" + token + "\"", loc);
+  return parserError;
+};
+
+ZorbaParserError* jsoniq_driver::unterminatedCommentErr(const location& loc)
+{
+  parserError = new ZorbaParserError("syntax error, unexpected end of file, unterminated comment", loc);
+  return parserError;
+}
+
+ZorbaParserError* jsoniq_driver::unterminatedElementConstructor(const location& loc)
+{
+  parserError = new ZorbaParserError("syntax error, unexpected end of file, unterminated direct element constructor", loc);
+  return parserError;
+}
+
+ZorbaParserError* jsoniq_driver::noClosingTagForElementConstructor(const location& loc)
+{
+  parserError = new ZorbaParserError("syntax error, unexpected end of file, no closing tag for direct element constructor", loc);
+  return parserError;
+}
+
+ZorbaParserError* jsoniq_driver::unrecognizedToken(const char* _error_token, const location& loc)
+{
+  parserError = new ZorbaParserError(std::string("syntax error, unexpected \"") + _error_token + "\"", loc);
+  return parserError;
+}
+
+ZorbaParserError* jsoniq_driver::invalidCharRef(const char* _message, const location& loc)
+{
+  std::string ref = "";
+  std::string temp = _message;
+  std::string out;
+  temp = temp.substr(temp.find("&"));
+
+  while (temp.size()>0 && xml::parse_entity(temp, &out) != -1)
+  {
+    temp = temp.substr(temp.find(";") + 1);
+    if (temp.find("&") != std::string::npos)
+      temp = temp.substr(temp.find("&"));
+  }
+
+  if (temp.find("&") != std::string::npos)
+  {
+    ref = "\"" + temp.substr(temp.find("&"), 6);
+    if (temp.size() == 7)
+      ref += "\"";
+    else if (temp.size() > 7)
+      ref += "...\"";
+    ref += " ";
+  }
+
+  parserError = new ZorbaParserError(std::string("syntax error, invalid character or entity reference "
+      + ref + "in the string literal ") + _message + ".", loc);
+  return parserError;
+}
+
+ZorbaParserError* jsoniq_driver::parserErr(const std::string& _message, const location& loc, Error const &code)
+{
+  parserError = new ZorbaParserError(_message, loc, code);
+  return parserError;
+}
+
+ZorbaParserError* jsoniq_driver::parserErr(const std::string& _message, const QueryLoc& loc, Error const &code)
+{
+  parserError = new ZorbaParserError(_message, loc, code);
+  return parserError;
+}
+
+bool jsoniq_driver::parse_stream(std::istream& in, const zstring& aFilename)
+{
+  int ch[3];
+
+  theFilename = aFilename;
+  theFilename2 = theFilename.str();
+
+  // process the UTF16 Byte Order Mark = \xEF\xBB\xBF
+  if (in.peek() == 0xEF)
+  {
+    int i;
+    for (i=0; i<3; i++)
+      if (in.good())
+        ch[i] = in.get();
+      else
+        break;
+
+    if (i<3 || ch[0] != 0xEF || ch[1] != 0xBB || ch[2] != 0xBF)
+    {
+      if (i==3) i--;
+      for ( ; i>=0; i--)
+        in.putback(ch[i]);
+    }
+  }
+
+  // process the UTF16 (LE) Byte Order Mark = \xFF\xFE, not supported yet
+  //else if (in.peek() == 0xFF)
+  //{
+    // transcode the input to UTF8
+  //}
+
+  jsoniq_scanner scanner(this, &in);
+  scanner.set_yy_flex_debug(Properties::instance()->traceScanning());
+  this->lexer = &scanner;
+  // scanner.set_yy_flex_debug(true); // debugging purposes
+
+  jsoniq_parser parser(*this);
+  parser.set_debug_level(Properties::instance()->traceParsing());
+  // parser.set_debug_level(true); // debugging purposes
+
+  return (parser.parse() == 0);
+}
+
+bool jsoniq_driver::parse_file(const zstring& aFilename)
+{
+  std::ifstream in(aFilename.c_str());
+  return parse_stream(in, aFilename);
+}
+
+bool jsoniq_driver::parse_string(const zstring& input)
+{
+  std::istringstream iss(input.str());
+  return parse_stream(iss);
+}
+
+void jsoniq_driver::set_expr(parsenode* e_p)
+{
+  if (theCompilerCB->theConfig.parse_cb != NULL)
+  {
+    zstring uri;
+    theCompilerCB->theRootSctx->get_entity_retrieval_uri(uri);
+    theCompilerCB->theConfig.parse_cb(e_p, uri.str());
+  }
+  expr_p = e_p;
+}
+
+QueryLoc jsoniq_driver::createQueryLoc(const location& aLoc) const
+{
+  QueryLoc lLoc;
+  lLoc.setFilename(theFilename);
+  lLoc.setLineBegin(aLoc.begin.line);
+  lLoc.setColumnBegin(aLoc.begin.column);
+  lLoc.setLineEnd(aLoc.end.line);
+  lLoc.setColumnEnd(aLoc.end.column);
+  return lLoc;
+}
+
+}	/* namespace zorba */
+/* vim:set et sw=2 ts=2: */

=== added file 'src/compiler/parser/jsoniq_driver.h'
--- src/compiler/parser/jsoniq_driver.h	1970-01-01 00:00:00 +0000
+++ src/compiler/parser/jsoniq_driver.h	2013-03-16 01:56:23 +0000
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2006-2008 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+#ifndef ZORBA_JSONIQ_DRIVER_H
+#define ZORBA_JSONIQ_DRIVER_H
+
+#include <ostream>
+#include <string>
+#include <zorba/config.h>
+#include "compiler/parser/symbol_table.h"
+#include "compiler/parser/zorba_parser_error.h"
+
+// needed because we have to delete the main module node
+#include "compiler/parsetree/parsenode_base.h"
+
+namespace zorba {
+
+class location;
+class parsenode;
+class CompilerCB;
+class ZorbaParserError;
+
+
+// exported for unit testing only
+class ZORBA_DLL_PUBLIC jsoniq_driver
+{
+public:
+  std::stringstream theDocComment;
+  std::string theMainModuleDocComment;
+  zstring theFilename;
+  std::string theFilename2;
+  symbol_table symtab;
+  rchandle<parsenode> expr_p;
+  CompilerCB* theCompilerCB;
+  ZorbaParserError* parserError;
+  class jsoniq_scanner* lexer;
+
+  jsoniq_driver(CompilerCB* aCompilerCB, uint32_t initial_heapsize = 1024);
+
+  virtual ~jsoniq_driver();
+
+  bool parse_stream(std::istream& in, const zstring& aFilename = "");
+
+  bool parse_string(const zstring& input);
+
+  bool parse_file(const zstring& aFilename);
+
+  void set_expr(parsenode* e_p);
+
+  parsenode* get_expr() { return expr_p; }
+
+  QueryLoc createQueryLoc(const location& aLoc) const;
+
+  // Error generators
+  ZorbaParserError* unrecognizedCharErr(const char* _error_token, const location& loc);
+  ZorbaParserError* unterminatedCommentErr(const location& loc);
+  ZorbaParserError* unterminatedElementConstructor(const location& loc);
+  ZorbaParserError* noClosingTagForElementConstructor(const location& loc);
+  ZorbaParserError* unrecognizedToken(const char* _error_token, const location& loc);
+  ZorbaParserError* invalidCharRef(const char* _error_token, const location& loc);
+  ZorbaParserError* parserErr(const std::string& _message, const location& loc, Error const &code = err::XPST0003);
+  ZorbaParserError* parserErr(const std::string& _message, const QueryLoc& loc, Error const &code = err::XPST0003);
+};
+
+}	/* namespace zorba */
+#endif /* ZORBA_JSONIQ_DRIVER_H */
+
+/*
+ * Local variables:
+ * mode: c++
+ * End:
+ */
+/* vim:set et sw=2 ts=2: */

=== added file 'src/compiler/parser/jsoniq_parser.y'
--- src/compiler/parser/jsoniq_parser.y	1970-01-01 00:00:00 +0000
+++ src/compiler/parser/jsoniq_parser.y	2013-03-16 01:56:23 +0000
@@ -0,0 +1,7041 @@
+/*
+ * Copyright 2006-2008 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+**  The parser definition file starts by asking for the C++ LALR(1)
+**  skeleton, the creation of the parser header file, and specifies the
+**  name of the parser class.  Because the C++ skeleton changes, it is
+**  safer to require the version.
+*/
+
+%skeleton "lalr1.cc"  /*  -*- C++ -*- */
+%require "2.4"
+%defines
+%name-prefix="zorba"
+%define "parser_class_name" "jsoniq_parser"
+%error-verbose
+
+// Expect 3 shift/reduce conflicts
+%expect 3
+
+
+%code requires {
+
+/*
+ * Copyright 2006-2008 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <list>
+#include <string>
+#include <vector>
+
+#include "compiler/parsetree/parsenodes.h"
+#include "compiler/parser/zorba_parser_error.h"
+#include "zorbatypes/zstring.h"
+
+#ifdef __GNUC__
+    // disable a warning in location.hh which comes with bison
+    // position.hh:141: warning: suggest parentheses around && within ||
+#   pragma GCC diagnostic ignored "-Wparentheses"
+#endif
+
+#include "location.hh"
+
+#ifdef __GNUC__
+#  pragma GCC diagnostic warning "-Wparentheses"
+#endif
+
+typedef std::list<zorba::zstring> string_list_t;
+typedef std::pair<zorba::zstring,zorba::zstring> string_pair_t;
+
+
+} // %code requires
+
+
+/*
+**  Because the parser uses the jsoniq_driver and reciprocally, both
+**  cannot include the header of the other. Because the driver's header
+**  needs detailed knowledge about the parser class (in particular its
+**  inner types), it is the parser's header which will use a forward
+**  declaration of the driver.
+*/
+
+%{
+
+#include "common/common.h"
+
+#ifdef WIN32
+#pragma warning(disable: 4786)
+#endif
+
+#include "compiler/parsetree/parsenodes.h"
+#include "compiler/parser/parse_constants.h"
+#include "compiler/api/compilercb.h"
+#include "store/api/update_consts.h"
+#include "compiler/parser/jsoniq_driver.h"
+#include "compiler/parser/parser_helpers.h"
+
+#define SYMTAB( n ) driver.symtab.get( (off_t)n )
+#define SYMTAB_PUT( s ) driver.symtab.put( s )
+#define LOC( p ) driver.createQueryLoc( p )
+
+
+#define YYDEBUG 1
+
+using namespace std;
+using namespace zorba;
+
+%}
+
+%pure-parser
+
+/*
+**  The driver is passed by reference to the parser and to the scanner.
+**  This provides a simple but effective pure interface, not relying on
+**  global variables.
+*/
+%parse-param { jsoniq_driver& driver }
+
+
+/*
+**  Request the location tracking feature, and initialize the
+**  first location's file name. Afterwards new locations are computed
+**  relatively to the previous locations: the file name will be
+**  automatically propagated.
+*/
+%locations
+%initial-action
+{
+    @$.begin.filename = @$.end.filename = &(driver.theFilename2);
+};
+
+/*
+%{
+static void print_token_value(FILE *, int, YYSTYPE);
+#define YYPRINT(file, type, value) print_token_value(file, type, value)
+%}
+*/
+
+/*
+**  Semantic values cannot use real objects, but only pointers to them.
+*/
+%union
+{
+    zorba::parsenode *node;
+    zorba::exprnode *expr;
+    zorba::FunctionSig *fnsig;
+    zorba::VarNameAndType *varnametype;
+    off_t sval;
+    const char *strval;
+    xs_integer *ival;
+    xs_double *dval;
+    xs_decimal *decval;
+    ZorbaParserError *err;
+    string_list_t *strlist;
+    string_pair_t *strpair;
+    std::vector<string_pair_t> *vstrpair;
+    CatchExpr::NameTestList *name_test_list;
+    FTStopWordOption::incl_excl_list_t *incl_excl_list;
+    FTSelection::pos_filter_list_t *pos_filter_list;
+    FTThesaurusOption::thesaurus_id_list_t *thesaurus_id_list;
+    ft_anyall_mode::type ft_anyall_value;
+};
+
+
+/*
+**  The token numbered as 0 corresponds to end of file; the following line
+**  allows for nicer error messages referring to end of file instead of
+**  $end. Similarly user friendly names are provided for each symbol.
+*/
+
+%token _EOF   0                             "'end of file'"
+
+%token <err>  UNRECOGNIZED                  "'unrecognized'"
+
+%type <node>  ERROR                         "'error'"
+
+
+/* constant string tokens */
+%type <strval> WindowType
+%type <strval> FLWORWinCondType
+
+/* tokens that contain embedded string literals */
+/* -------------------------------------------- */
+%token <sval> APOS_ATTR_CONTENT             "'apos attribute content'"
+%token <sval> CHAR_LITERAL                  "'char literal'"
+%token <sval> CHAR_LITERAL_AND_CDATA_END    "'char literal]]>'"
+%token <sval> CHAR_LITERAL_AND_PI_END       "'char literal and pi end'"
+%token <sval> CHAR_REF_LITERAL              "'&#charref;'"
+%token <sval> ELEMENT_CONTENT               "'element content'"
+%token <sval> ELEM_WILDCARD                 "'pref:*'"
+%token <sval> ELEM_EQNAME_WILDCARD          "'ns:*'"
+%token <sval> ENTITY_REF                    "'&entity;'"
+%token <sval> EXPR_COMMENT_LITERAL          "'comment literal'"
+
+%token <sval> PI_NCNAME_LBRACE              "'pi <NCName {>'"
+%token <sval> NCNAME_SVAL                   "'NCName_sval'"
+%token <sval> PRAGMA_LITERAL_AND_END_PRAGMA "'pragma literal'"
+%token <sval> QNAME_SVAL_AND_END_PRAGMA     "'QName #)'"
+%token <sval> EQNAME_SVAL_AND_END_PRAGMA    "'EQName #)'"
+%token <sval> PREFIX_WILDCARD               "'*:QName'"
+%token <sval> COMP_ELEMENT_QNAME_LBRACE     "'element QName {'"
+%token <sval> COMP_ATTRIBUTE_QNAME_LBRACE   "'attribute QName {'"
+%token <sval> COMP_PI_NCNAME_LBRACE         "'processing-instruction NCName {'"
+%token <sval> COMP_NS_NCNAME_LBRACE         "'namespace NCName {'"
+%token <sval> QNAME_SVAL                    "'QName'"
+%token <sval> EQNAME_SVAL                   "'EQName'"
+%token <sval> ANNOTATION_QNAME_SVAL         "'%QName'"
+%token <sval> ANNOTATION_EQNAME_SVAL        "'%EQName'"
+
+%token <sval> QUOTE_ATTR_CONTENT            "'quote attribute content'"
+%token <sval> STRING_LITERAL                "'STRING'"
+%token <sval> XML_COMMENT_LITERAL           "'XML comment'"
+
+%type <sval> URI_LITERAL                    "'URI'"
+%type <sval> NCNAME                         "'NCName'"
+
+%token <sval> DECLARE                       "'declare'"
+%token <sval> MODULE                        "'module'"
+
+%type <strval> DecimalFormatParamName
+%type <node> DecimalFormatDecl
+%type <strpair> DecimalFormatParam
+%type <vstrpair> DecimalFormatParamList
+%type <name_test_list> NameTestList
+
+%type <fnsig> FunctionSig
+%type <varnametype> VarNameAndType
+%type <strlist> STRING_LITERAL_list;
+
+
+/* simple tokens */
+/* ------------- */
+
+%token BLANK                            "'<blank>'"
+
+%token ALLOWING                         "'allowing'"
+%token BASE_URI                         "'base-uri'"
+%token BOUNDARY_SPACE                   "'boundary-space'"
+%token COMMENT                          "'comment'"
+%token CONSTRUCTION                     "'construction'"
+%token COPY_NAMESPACES                  "'copy-namespaces'"
+%token COUNT                            "'count'"
+%token DOCUMENT                         "'document'"
+%token DOCUMENT_NODE                    "'document-node'"
+%token ELEMENT                          "'element'"
+%token FOR                              "'for'"
+%token FUNCTION                         "'function'"
+%token IF                               "'if'"
+%token IMPORT                           "'import'"
+%token INSTANCE                         "'instance'"
+%token LET                              "'let'"
+%token MOST                             "'most'"
+%token NS_NODE                          "'namespace-node'"
+%token NEXT                             "'next'"
+%token NO                               "'no'"
+%token ONLY                             "'only'"
+%token OPTION                           "'option'"
+%token ORDERING                         "'ordering'"
+%token PREVIOUS                         "'previous'"
+%token PROCESSING_INSTRUCTION           "'processing-instruction'"
+%token SCHEMA                           "'schema'"
+%token SCHEMA_ATTRIBUTE                 "'schema-attribute'"
+%token SCHEMA_ELEMENT                   "'schema-element'"
+%token SEQUENTIAL                       "'sequential'"
+%token SET                              "'set'"
+%token SIMPLE                           "'simple'"
+%token SLIDING                          "'sliding'"
+%token SOME                             "'some'"
+%token SPACE                            "'space'"
+%token STABLE                           "'stable'"
+%token TEXT                             "'text'"
+%token TUMBLING                         "'tumbling'"
+%token SWITCH                           "'switch'"
+%token TYPESWITCH                       "'typeswitch'"
+%token UPDATING                         "'updating'"
+%token VALIDATE                         "'validate'"
+%token TYPE                             "'type'"
+%token WHEN                             "'when'"
+%token WORD                             "'word'"
+
+/* Decimal format tokens */
+/* --------------------- */
+%token DECIMAL_FORMAT                   "'decimal-format'"
+%token DECIMAL_SEPARATOR                "'decimal-separator'"
+%token GROUPING_SEPARATOR               "'grouping-separator'"
+%token INFINITY_VALUE                   "'infinity'"
+%token MINUS_SIGN                       "'minus-sign'"
+%token NaN                              "'NaN'"
+%token PERCENT                          "'percent'"
+%token PER_MILLE                        "'per-mille'"
+%token ZERO_DIGIT                       "'zero-digit'"
+%token DIGIT                            "'digit'"
+%token PATTERN_SEPARATOR                "'pattern-separator'"
+
+%token ANCESTOR                         "'ancestor'"
+%token ANCESTOR_OR_SELF                 "'ancestor-or-self'"
+%token AND                              "'and'"
+%token APOS                             "'''"
+%token AS                               "'as'"
+%token ASCENDING                        "'ascending'"
+%token AT                               "'at'"
+%token ATTRIBUTE                        "'attribute'"
+%token AT_SIGN                          "'@'"
+%token CONCAT                           "'||'"
+%token CASE                             "'case'"
+%token CASTABLE                         "'castable'"
+%token CAST                             "'cast'"
+%token CDATA_BEGIN                      "'CDATA[['"
+%token CDATA_END                        "']]'"
+%token CHILD                            "'child'"
+%token COLLATION                        "'collation'"
+%token COMMA                            "','"
+%token COMMENT_BEGIN                    "'(:'"
+%token COMMENT_END                      "':)'"
+%token <decval> DECIMAL_LITERAL         "'DECIMAL'"
+%token CONTEXT                          "'context'"
+%token VARIABLE                         "'variable'"
+%token DEFAULT                          "'default'"
+%token DESCENDANT                       "'descendant'"
+%token DESCENDANT_OR_SELF               "'descendant-or-self'"
+%token DESCENDING                       "'descending'"
+%token DIV                              "'div'"
+%token DOLLAR                           "'$'"
+%token DOT                              "'.'"
+%token DOT_DOT                          "'..'"
+%token COLON                            "':'"
+%token DOUBLE_COLON                     "'::'"
+%token DOUBLE_LBRACE                    "'{{'"
+%token <dval> DOUBLE_LITERAL            "'DOUBLE'"
+%token DOUBLE_RBRACE                    "'<double {>'"
+%token ELSE                             "'else'"
+%token _EMPTY                           "'empty'"
+%token GREATEST                         "'greatest'"
+%token LEAST                            "'least'"
+%token EMPTY_TAG_END                    "'/>'"
+%token ENCODING                         "'encoding'"
+%token EQUALS                           "'='"
+%token ESCAPE_APOS                      "''''"
+%token ESCAPE_QUOTE                     "'\"\"'"
+%token EVERY                            "'every'"
+%token EXCEPT                           "'except'"
+%token EXTERNAL                         "'external'"
+%token FOLLOWING                        "'following'"
+%token FOLLOWING_SIBLING                "'following-sibling'"
+%token FOLLOWS                          "'follows'"
+%token GE                               "'>='"
+%token GETS                             "':='"
+%token GT                               "'>'"
+%token HOOK                             "'?'"
+%token HASH                             "'#'"
+%token IDIV                             "'idiv'"
+%token _IN                              "'in'"
+%token INHERIT                          "'inherit'"
+%token <ival> INTEGER_LITERAL           "'INTEGER'"
+%token INTERSECT                        "'intersect'"
+%token IS                               "'is'"
+%token ITEM                             "'item'"
+%token LBRACE                           "'{'"
+%token LBRACK                           "'['"
+%token LE                               "'<='"
+%token LPAR                             "'('"
+%token LT_OR_START_TAG                  "'<'"
+%token MINUS                            "'-'"
+%token MOD                              "'mod'"
+%token NAMESPACE                        "'namespace'"
+%token _NAN                             "'nan'"
+%token NE                               "'!='"
+%token NODECOMP                         "'nodecomp'"
+%token NOT_OPERATOR_KEYWORD             "'??'"
+%token NO_INHERIT                       "'<no inherit>'"
+%token NO_PRESERVE                      "'<no preserve>'"
+%token OR                               "'or'"
+%token ORDER                            "'order'"
+%token ORDERED                          "'ordered'"
+%token BY                               "'by'"
+%token GROUP                            "'group'"
+%token PARENT                           "'parent'"
+%token PERCENTAGE                       "'%'"
+%token PI_BEGIN                         "'<?'"
+%token PI_END                           "'?>'"
+%token PLUS                             "'+'"
+%token PRAGMA_BEGIN                     "'(#'"
+%token PRAGMA_END                       "'#)'"
+%token PRECEDES                         "'<<'"
+%token PRECEDING                        "'preceding'"
+%token PRECEDING_SIBLING                "'preceding-sibling'"
+%token PRESERVE                         "'preserve'"
+%token QUOTE                            "'\"'"
+%token RBRACE                           "'}'"
+%token RBRACK                           "']'"
+%token RETURN                           "'return'"
+%token RPAR                             "')'"
+%token SATISFIES                        "'satisfies'"
+%token SELF                             "'self'"
+%token SEMI                             "';'"
+%token SLASH                            "'/'"
+%token SLASH_SLASH                      "'//'"
+%token BANG                             "'!'"
+%token STAR                             "'*'"
+%token START_TAG_END                    "'</ (start tag end)'"
+%token STRIP                            "'strip'"
+%token TAG_END                          "'> (tag end)'"
+%token THEN                             "'then'"
+%token TO                               "'to'"
+%token TREAT                            "'treat'"
+%token UNION                            "'union'"
+%token UNORDERED                        "'unordered'"
+%token VAL_EQ                           "'eq'"
+%token VAL_GE                           "'ge'"
+%token VAL_GT                           "'gt'"
+%token VAL_LE                           "'le'"
+%token VAL_LT                           "'lt'"
+%token VAL_NE                           "'ne'"
+%token VALUECOMP                        "'VALUECOMP'"
+%token VBAR                             "'|'"
+%token EMPTY_SEQUENCE                   "'empty-sequence'"
+%token WHERE                            "'where'"
+%token XML_COMMENT_BEGIN                "'<!--'"
+%token XML_COMMENT_END                  "'-->'"
+%token XQUERY                           "'xquery'"
+%token VERSION                          "'version'"
+%token START                            "'start'"
+
+/* update-related */
+/* -------------- */
+%token AFTER                            "'after'"
+%token BEFORE                           "'before'"
+%token REVALIDATION                     "'revalidation'"
+%token _STRICT                          "'strict'"
+%token LAX                              "'lax'"
+%token SKIP                             "'skip'"
+%token _DELETE                          "'delete'"
+%token NODE                             "'node'"
+%token INSERT                           "'insert'"
+%token NODES                            "'nodes'"
+%token RENAME                           "'rename'"
+%token REPLACE                          "'replace'"
+%token VALUE                            "'value'"
+%token OF                               "'of'"
+%token FIRST                            "'first'"
+%token INTO                             "'into'"
+%token LAST                             "'last'"
+%token MODIFY                           "'modify'"
+%token COPY                             "'copy'"
+%token WITH                             "'with'"
+
+/* scripting-related */
+/* ----------------- */
+%token BREAK                            "'break'"
+%token CONTINUE                         "'continue'"
+%token EXIT                             "'exit'"
+%token LOOP                             "'loop'"
+%token RETURNING                        "'returning'"
+%token WHILE                            "'while'"
+
+/* try-catch-related */
+/* ----------------- */
+%token TRY                              "'try'"
+%token CATCH                            "'catch'"
+
+/* eval-related */
+/* ------------ */
+%token USING                            "'using'"
+
+/* full-text-related */
+/* ----------------- */
+%token ALL                              "'all'"
+%token ANY                              "'any'"
+%token CONTAINS                         "'contains'"
+%token CONTENT                          "'content'"
+%token DIACRITICS                       "'diacritics'"
+%token DIFFERENT                        "'different'"
+%token DISTANCE                         "'distance'"
+%token END                              "'end'"
+%token ENTIRE                           "'entire'"
+%token EXACTLY                          "'exactly'"
+%token FROM                             "'from'"
+%token FTAND                            "'ftand'"
+%token FTNOT                            "'ftnot'"
+%token NOT                              "'not'"
+%token FT_OPTION                        "'ft-option'"
+%token FTOR                             "'ftor'"
+%token INSENSITIVE                      "'insensitive'"
+%token LANGUAGE                         "'language'"
+%token LEVELS                           "'levels'"
+%token LOWERCASE                        "'lowercase'"
+%token OCCURS                           "'occurs'"
+%token PARAGRAPH                        "'paragraph'"
+%token PARAGRAPHS                       "'paragraphs'"
+%token PHRASE                           "'phrase'"
+%token RELATIONSHIP                     "'relationship'"
+%token SAME                             "'same'"
+%token SCORE                            "'score'"
+%token SENSITIVE                        "'sensitive'"
+%token SENTENCE                         "'sentence'"
+%token SENTENCES                        "'sentences'"
+%token STEMMING                         "'stemming'"
+%token STOP                             "'stop'"
+%token THESAURUS                        "'thesaurus'"
+%token TIMES                            "'times'"
+%token UPPERCASE                        "'uppercase'"
+%token WEIGHT                           "'weight'"
+%token WILDCARDS                        "'wildcards'"
+%token WINDOW                           "'window'"
+%token WITHOUT                          "'without'"
+%token WORDS                            "'words'"
+
+/* Data Definition Facility */
+/* ------------------------ */
+%token COLLECTION                       "'collection'"
+%token CONSTOPT                         "'const'"
+%token APPEND_ONLY                      "'append-only'"
+%token QUEUE                            "'queue'"
+%token MUTABLE                          "'mutable'"
+%token READ_ONLY                        "'read-only'"
+
+%token UNIQUE                           "'unique'"
+%token NON                              "'non'"
+%token INDEX                            "'index'"
+%token MANUALLY                         "'manually'"
+%token AUTOMATICALLY                    "'automatically'"
+%token MAINTAINED                       "'maintained'"
+%token ON                               "'on'"
+%token RANGE                            "'range'"
+%token EQUALITY                         "'equality'"
+%token GENERAL                          "'general'"
+
+%token INTEGRITY                        "'integrity'"
+%token CONSTRAINT                       "'constraint'"
+%token CHECK                            "'check'"
+%token KEY                              "'key'"
+%token FOREACH                          "'foreach'"
+%token FOREIGN                          "'foreign'"
+%token KEYS                             "'keys'"
+
+/* JSON */
+/* ---- */
+%token L_SIMPLE_OBJ_UNION               "'{|'"
+%token R_SIMPLE_OBJ_UNION               "'|}'"
+%token L_ACCUMULATOR_OBJ_UNION          "'{['"
+%token R_ACCUMULATOR_OBJ_UNION          "']}'"
+%token JSON                             "'json'"
+%token APPEND                           "'append'"
+%token POSITION                         "'position'"
+%token OBJECT                           "'object'"
+%token ARRAY                            "'array'"
+%token JSON_ITEM                        "'json-item'"
+%token STRUCTURED_ITEM                  "'structured-item'"
+
+/* Byte Order Marks                  */
+/* --------------------------------- */
+%token BYTE_ORDER_MARK_UTF8             "'BOM_UTF8'"
+
+/* Unix Shebang -- ignored by Zorba  */
+/* --------------------------------- */
+%token SHEBANG                          "'#!/shebang"
+
+/* Leading slash handling expression */
+/* --------------------------------- */
+//%type <expr> LeadingSlash
+
+/* left-hand sides: syntax only */
+/* ---------------------------- */
+//%type <node> AbbrevForwardStep
+%type <node> AnyKindTest
+%type <node> Annotation
+%type <node> AnnotationList
+%type <node> AnnotationLiteralList
+%type <node> AposAttrContentList
+%type <node> opt_AposAttrContentList
+%type <node> AposAttrValueContent
+%type <node> ArgList
+%type <node> GeneralizedAtomicType
+%type <node> SimpleType
+%type <node> AttributeTest
+%type <node> BaseURIDecl
+%type <node> BoundarySpaceDecl
+%type <node> CaseClause
+%type <node> CaseClauseList
+%type <node> CommentTest
+%type <node> ConstructionDecl
+%type <node> CopyNamespacesDecl
+%type <node> DefaultCollationDecl
+%type <node> DefaultNamespaceDecl
+%type <node> DirAttr
+%type <node> DirAttributeList
+%type <node> DirAttributeValue
+%type <node> DirElemContentList
+%type <node> DocumentTest
+%type <node> ElementTest
+%type <node> EmptyOrderDecl
+%type <node> WindowClause
+%type <node> ForClause
+%type <node> ForLetWinClause
+%type <node> FLWORClause
+%type <node> FLWORClauseList
+//%type <node> ForwardAxis
+//%type <node> ForwardStep
+%type <node> FunctionDecl
+%type <node> FunctionDecl2
+%type <node> FunctionDeclSimple
+%type <node> FunctionDeclUpdating
+%type <node> Import
+%type <node> ItemType
+%type <node> KindTest
+%type <node> LetClause
+%type <node> LibraryModule
+%type <node> MainModule
+%type <node> Module
+%type <node> ModuleWithoutBOM
+%type <node> ModuleDecl
+%type <node> ModuleImport
+%type <node> NamespaceTest
+%type <node> NameTest
+%type <node> NamespaceDecl
+%type <node> NodeComp
+//%type <node> NodeTest
+%type <node> OccurrenceIndicator
+%type <node> OptionDecl
+%type <node> GroupByClause
+%type <node> GroupSpecList
+%type <node> GroupSpec
+%type <node> GroupCollationSpec
+%type <node> OrderByClause
+%type <node> OrderCollationSpec
+%type <node> OrderDirSpec
+%type <node> OrderEmptySpec
+%type <node> OrderModifier
+%type <node> OrderSpec
+%type <node> OrderSpecList
+%type <node> OrderingModeDecl
+%type <node> PITest
+%type <node> Param
+%type <node> ParamList
+%type <node> PositionalVar
+%type <node> Pragma
+%type <node> Pragma_list
+%type <node> PredicateList
+%type <node> QVarInDecl
+%type <node> QVarInDeclList
+%type <node> QuoteAttrValueContent
+%type <node> QuoteAttrContentList
+%type <node> opt_QuoteAttrContentList
+//%type <node> ReverseAxis
+//%type <node> ReverseStep
+%type <node> SIND_Decl
+%type <node> SIND_DeclList
+%type <node> SchemaAttributeTest
+%type <node> SchemaElementTest
+%type <node> SchemaImport
+%type <node> SchemaPrefix
+%type <node> SequenceType
+%type <node> SequenceTypeList
+%type <node> Setter
+%type <node> SignList
+%type <node> SingleType
+%type <node> SwitchCaseClause
+%type <node> SwitchCaseClauseList
+%type <node> SwitchCaseOperandList
+%type <node> CaseStatement
+%type <node> CaseStatementList
+%type <node> SwitchCaseStatement
+%type <node> SwitchCaseStatementList
+%type <node> TextTest
+%type <node> TypeDeclaration
+%type <node> TypeName
+%type <node> TypeName_WITH_HOOK
+%type <node> URILiteralList
+%type <node> ValueComp
+%type <node> CtxItemDecl
+%type <node> CtxItemDecl2
+%type <node> CtxItemDecl3
+%type <node> CtxItemDecl4
+%type <node> VarDecl
+%type <node> VarGetsDecl
+%type <node> VarGetsDeclList
+%type <node> VarInDecl
+%type <node> VarInDeclList
+%type <node> WindowVarDecl
+%type <node> WindowVars
+%type <node> WindowVars2
+%type <node> WindowVars3
+%type <node> FLWORWinCond
+%type <node> VersionDecl
+%type <node> VFO_Decl
+%type <node> VFO_DeclList
+%type <node> BlockVarDecl
+%type <node> WhereClause
+%type <node> CountClause
+%type <node> Wildcard
+
+/* left-hand sides: expressions */
+/* ---------------------------- */
+%type <expr> AdditiveExpr
+%type <expr> AndExpr
+//%type <expr> AxisStep
+%type <expr> CDataSection
+%type <expr> CastExpr
+%type <expr> CastableExpr
+%type <expr> CommonContent
+%type <expr> ComparisonExpr
+%type <expr> CompAttrConstructor
+%type <expr> CompCommentConstructor
+%type <expr> CompDocConstructor
+%type <expr> CompElemConstructor
+%type <expr> CompPIConstructor
+%type <expr> CompNamespaceConstructor
+%type <expr> CompTextConstructor
+%type <expr> ComputedConstructor
+%type <expr> Constructor
+%type <expr> ContextItemExpr
+%type <expr> DirCommentConstructor
+%type <expr> DirElemConstructor
+%type <expr> DirElemContent
+%type <expr> DirPIConstructor
+%type <expr> DirectConstructor
+%type <expr> BracedExpr
+%type <expr> BlockStatement
+%type <expr> Statement
+%type <expr> Statements
+%type <expr> StatementsAndExpr
+%type <expr> StatementsAndOptionalExpr
+%type <expr> StatementsAndOptionalExprTop
+%type <expr> StringConcatExpr
+%type <expr> SwitchStatement
+%type <expr> TypeswitchStatement
+%type <expr> TryStatement
+%type <expr> CatchListStatement
+%type <expr> CatchStatement
+%type <expr> ApplyStatement
+%type <expr> IfStatement
+%type <expr> FLWORStatement
+%type <expr> ReturnStatement
+%type <expr> VarDeclStatement
+%type <expr> BlockVarDeclList
+%type <expr> BlockExpr
+%type <expr> EnclosedStatementsAndOptionalExpr
+%type <expr> Expr
+%type <expr> ExprSingle
+%type <expr> ExprSimple
+%type <expr> ExtensionExpr
+%type <expr> FLWORExpr
+%type <expr> ReturnExpr
+%type <expr> FilterExpr
+%type <expr> FunctionCall
+%type <expr> IfExpr
+%type <expr> InstanceofExpr
+%type <expr> IntersectExceptExpr
+%type <expr> Literal
+%type <expr> MultiplicativeExpr
+%type <expr> NumericLiteral
+%type <expr> OrExpr
+%type <expr> OrderedExpr
+%type <expr> ParenthesizedExpr
+%type <expr> PathExpr
+%type <expr> Predicate
+%type <expr> PrimaryExpr
+%type <expr> QuantifiedExpr
+%type <expr> QueryBody
+%type <expr> RangeExpr
+%type <expr> RelativePathExpr
+%type <expr> StepExpr
+%type <expr> StringLiteral
+%type <expr> SwitchExpr
+%type <expr> TreatExpr
+%type <expr> TypeswitchExpr
+%type <expr> UnaryExpr
+%type <expr> UnionExpr
+%type <expr> UnorderedExpr
+%type <expr> ValidateExpr
+%type <expr> ValueExpr
+%type <expr> SimpleMapExpr
+%type <expr> VarRef
+%type <expr> ExitStatement
+%type <expr> WhileStatement
+%type <expr> AssignStatement
+%type <expr> FlowCtlStatement
+%type <expr> QNAME
+%type <expr> EQNAME
+%type <expr> FUNCTION_NAME
+
+%type <expr> FunctionItemExpr
+%type <expr> LiteralFunctionItem
+%type <expr> InlineFunction
+%type <node> FunctionTest
+%type <node> TypedFunctionTest
+%type <node> AnyFunctionTest
+%type <node> TypeList
+%type <node> ParenthesizedItemType
+
+/* update-related */
+/* -------------- */
+%type <expr> DeleteExpr
+%type <expr> InsertExpr
+%type <expr> RenameExpr
+%type <expr> ReplaceExpr
+%type <node> RevalidationDecl
+%type <expr> TransformExpr
+%type <expr> VarNameList
+%type <expr> VarNameDecl
+
+/* try-catch-related */
+/* ----------------- */
+%type <expr> TryExpr
+%type <expr> CatchListExpr
+%type <expr> CatchExpr
+
+/* collection-reladed */
+%type <node> CollectionDecl
+%type <node> CollectionTypeDecl
+
+/* index-related     */
+/* ----------------- */
+%type <node> IndexDecl
+%type <node> IndexKeySpec
+%type <node> IndexKeyList
+
+/* integrityconstraint-related */
+/* --------------------------- */
+%type <node> IntegrityConstraintDecl
+
+/* full-text-related */
+/* ----------------- */
+%type <node> FTAnd
+%type <node> FTAnyallOption opt_FTAnyallOption
+%type <ft_anyall_value> opt_word opt_words
+%type <node> FTBigUnit
+%type <node> FTCaseOption
+%type <expr> FTContainsExpr
+%type <node> FTContent
+%type <node> FTDiacriticsOption
+%type <node> FTDistance
+%type <node> FTExtensionOption
+%type <node> FTExtensionSelection
+%type <node> FTIgnoreOption opt_FTIgnoreOption
+%type <node> FTLanguageOption
+%type <node> FTMatchOption
+%type <node> FTMatchOptions opt_FTMatchOptions
+%type <node> FTMildNot
+%type <node> FTOptionDecl
+%type <node> FTOr
+%type <node> FTOrder
+%type <node> FTPosFilter
+%type <pos_filter_list> FTPosFilter_list opt_FTPosFilter_list
+%type <node> FTPrimary
+%type <node> FTPrimaryWithOptions
+%type <node> FTRange
+%type <node> FTScope
+%type <node> FTScoreVar
+%type <node> FTSelection opt_FTSelection
+%type <node> FTStemOption
+%type <node> FTStopWords
+%type <node> FTStopWordOption
+%type <node> FTStopWordsInclExcl
+%type <incl_excl_list> FTStopWordsInclExcl_list opt_FTStopWordsInclExcl_list
+%type <node> FTThesaurusID FTThesaurusID_or_default
+%type <sval> opt_relationship
+%type <node> opt_levels
+%type <thesaurus_id_list> FTThesaurus_list opt_FTThesaurus_list
+%type <node> FTThesaurusOption
+%type <node> FTTimes opt_FTTimes
+%type <node> FTUnaryNot
+%type <node> FTUnit
+%type <node> FTWeight opt_FTWeight
+%type <node> FTWildCardOption
+%type <node> FTWindow
+%type <node> FTWords
+%type <node> FTWordsValue
+
+/* JSON-related */
+/* ------------ */
+%type <expr> JSONArrayConstructor
+%type <expr> JSONSimpleObjectUnion
+%type <expr> JSONAccumulatorObjectUnion
+%type <expr> JSONObjectConstructor
+%type <node> JSONPairList
+%type <expr> JSONDeleteExpr
+%type <expr> JSONInsertExpr
+%type <expr> JSONRenameExpr
+%type <expr> JSONReplaceExpr
+%type <expr> JSONAppendExpr
+
+%type <node> JSONTest
+%type <node> JSONItemTest
+%type <node> JSONObjectTest
+%type <node> JSONArrayTest
+
+/*
+ *  To enable memory deallocation during error recovery, use %destructor.
+ */
+
+// Module must not be destroyed since it is returned by the parser
+
+/*%printer    { debug_stream() << *$$; }    */
+/*%printer    { debug_stream () << $$; }    */
+/*%destructor { delete $$; }              */
+
+// destructors for token values
+%destructor { delete $$; } INTEGER_LITERAL
+%destructor { delete $$; } DOUBLE_LITERAL
+%destructor { delete $$; } DECIMAL_LITERAL
+
+%{
+// HACK to trigger rchandle release: rchandles are freed when refcount == 0
+// (not <= 0); but Bison never increments the refcount, so we do it manually...
+template<typename T> inline void release_hack( T *ref ) {
+    if ( ref ) {
+        RCHelper::addReference( ref );
+        RCHelper::removeReference( ref );
+    }
+}
+%}
+
+//// parsenodes
+//%destructor { release_hack( $$ ); } AbbrevForwardStep ForwardAxis ForwardStep NodeTest ReverseAxis ReverseStep
+
+// parsenodes
+%destructor { release_hack( $$ ); } AnyKindTest Annotation AnnotationList AnnotationLiteralList AposAttrContentList opt_AposAttrContentList AposAttrValueContent ArgList GeneralizedAtomicType SimpleType AttributeTest BaseURIDecl BoundarySpaceDecl CaseClause CaseClauseList CommentTest ConstructionDecl CopyNamespacesDecl DefaultCollationDecl DefaultNamespaceDecl DirAttr DirAttributeList DirAttributeValue DirElemContentList DocumentTest ElementTest EmptyOrderDecl WindowClause ForClause ForLetWinClause FLWORClauseList FunctionDecl FunctionDecl2 FunctionDeclSimple FunctionDeclUpdating Import ItemType KindTest LetClause LibraryModule MainModule /* Module */ ModuleDecl ModuleImport NameTest NamespaceDecl NodeComp OccurrenceIndicator OptionDecl GroupByClause GroupSpecList GroupSpec GroupCollationSpec OrderByClause OrderCollationSpec OrderDirSpec OrderEmptySpec OrderModifier OrderSpec OrderSpecList OrderingModeDecl PITest Param ParamList PositionalVar Pragma Pragma_list PredicateList QVarInDecl QVarInDeclList QuoteAttrValueContent QuoteAttrContentList opt_QuoteAttrContentList SIND_Decl SIND_DeclList SchemaAttributeTest SchemaElementTest SchemaImport SchemaPrefix SequenceType SequenceTypeList Setter SignList SingleType TextTest NamespaceTest TypeDeclaration TypeName TypeName_WITH_HOOK URILiteralList ValueComp CollectionDecl IndexDecl IndexKeySpec IndexKeyList IntegrityConstraintDecl CtxItemDecl CtxItemDecl2 CtxItemDecl3 CtxItemDecl4 VarDecl VarGetsDecl VarGetsDeclList VarInDecl VarInDeclList WindowVarDecl WindowVars WindowVars2 WindowVars3 FLWORWinCond VersionDecl VFO_Decl VFO_DeclList WhereClause CountClause Wildcard DecimalFormatDecl TypedFunctionTest AnyFunctionTest TypeList SwitchCaseClause SwitchCaseClauseList SwitchCaseOperandList
+
+// parsenodes: Full-Text
+%destructor { release_hack( $$ ); } FTAnd FTAnyallOption FTBigUnit FTCaseOption FTContent FTDiacriticsOption FTDistance FTExtensionOption FTExtensionSelection FTIgnoreOption opt_FTIgnoreOption FTLanguageOption FTMatchOption FTMatchOptions opt_FTMatchOptions FTMildNot FTOptionDecl FTOr FTOrder FTPosFilter FTPrimary FTPrimaryWithOptions FTRange FTScope FTScoreVar FTSelection FTStemOption FTStopWords FTStopWordOption FTStopWordsInclExcl FTThesaurusID FTThesaurusOption FTTimes opt_FTTimes FTUnaryNot FTUnit FTWeight FTWildCardOption FTWindow FTWords FTWordsValue
+
+// parsenodes: JSON
+%destructor { release_hack( $$ ); } JSONObjectConstructor JSONPairList JSONArrayConstructor JSONSimpleObjectUnion JSONAccumulatorObjectUnion JSONDeleteExpr JSONInsertExpr JSONRenameExpr JSONReplaceExpr JSONAppendExpr
+
+//// exprnodes: AxisStep
+//%destructor { release_hack( $$ ); } AxisStep
+//
+// exprnodes
+%destructor { release_hack( $$ ); } AdditiveExpr AndExpr CDataSection CastExpr CastableExpr CommonContent ComparisonExpr CompAttrConstructor CompCommentConstructor CompDocConstructor CompElemConstructor CompPIConstructor CompNamespaceConstructor CompTextConstructor ComputedConstructor Constructor ContextItemExpr DirCommentConstructor DirElemConstructor DirElemContent DirPIConstructor DirectConstructor BracedExpr BlockExpr EnclosedStatementsAndOptionalExpr BlockStatement Statement Statements StatementsAndExpr StatementsAndOptionalExpr StatementsAndOptionalExprTop SwitchStatement TypeswitchStatement TryStatement CatchListStatement CatchStatement ApplyStatement IfStatement FLWORStatement ReturnStatement VarDeclStatement Expr ExprSingle ExprSimple ExtensionExpr FLWORExpr ReturnExpr FilterExpr FunctionCall IfExpr InstanceofExpr IntersectExceptExpr Literal MultiplicativeExpr NumericLiteral OrExpr OrderedExpr ParenthesizedExpr PathExpr Predicate PrimaryExpr QuantifiedExpr QueryBody RangeExpr RelativePathExpr StepExpr StringLiteral TreatExpr StringConcatExpr SwitchExpr TypeswitchExpr UnaryExpr UnionExpr UnorderedExpr ValidateExpr ValueExpr SimpleMapExpr VarRef TryExpr CatchListExpr CatchExpr DeleteExpr InsertExpr RenameExpr ReplaceExpr TransformExpr VarNameList VarNameDecl AssignStatement ExitStatement WhileStatement FlowCtlStatement QNAME EQNAME FUNCTION_NAME FTContainsExpr
+
+// internal non-terminals with values
+%destructor { delete $$; } FunctionSig VarNameAndType NameTestList DecimalFormatParam DecimalFormatParamList
+
+/*_____________________________________________________________________
+ *
+ *  Precedence
+ *_____________________________________________________________________*/
+
+/*_____________________________________________________________________
+ *
+ * resolve shift-reduce conflict for
+ * [49] RangeExpr ::= AdditiveExpr ( "to" AdditiveExpr )?
+ *_____________________________________________________________________*/
+%nonassoc RANGE_REDUCE
+%nonassoc TO
+
+/*_____________________________________________________________________
+ *
+ * resolve shift-reduce conflict for
+ * [50] AdditiveExpr ::= MultiplicativeExpr ( ("+" | "-") MultiplicativeExpr )*
+ *_____________________________________________________________________*/
+%nonassoc SEQUENCE_TYPE_REDUCE
+%nonassoc ADDITIVE_REDUCE
+%left PLUS MINUS HOOK
+
+/*_____________________________________________________________________
+ *
+ * resolve shift-reduce conflict for
+ * [52] UnionExpr ::= IntersectExceptExpr ( ("union" | "|") IntersectExceptExpr )*
+ *_____________________________________________________________________*/
+%nonassoc UNION_REDUCE
+%left UNION VBAR
+
+/*_____________________________________________________________________
+ *
+ * resolve shift-reduce conflict for
+ * [53] IntersectExceptExpr ::= InstanceofExpr ( ("intersect" | "except") InstanceofExpr )*
+ *_____________________________________________________________________*/
+%nonassoc INTERSECT_EXCEPT_REDUCE
+%left INTERSECT EXCEPT
+
+/*_____________________________________________________________________
+ *
+ * resolve shift-reduce conflict for
+ * [42a] QVarInDeclList ::= QVarInDecl ( "," "$" QVarInDeclList )*
+ *_____________________________________________________________________*/
+%nonassoc QVARINDECLLIST_REDUCE
+// TODO: COMMA_DOLLAR is not defined anymore
+%left COMMA_DOLLAR
+%nonassoc UNARY_PREC
+
+/*_____________________________________________________________________
+ *
+ * resolve shift-reduce conflict for
+ * [69] RelativePathExpr ::= StepExpr (("/" | "//") StepExpr)*
+ *_____________________________________________________________________*/
+%nonassoc STEP_REDUCE
+%left SLASH SLASH_SLASH
+
+/*_____________________________________________________________________
+ *
+ * resolve shift-reduce conflict for
+ * SimpleMapExpr ::= PathExpr ("!" PathExpr)*
+ *_____________________________________________________________________*/
+%nonassoc SIMPLEMAPEXPR_REDUCE
+%left BANG
+
+/*_____________________________________________________________________
+ *
+ * resolve shift-reduce conflict for
+ * [51] MultiplicativeExpr ::= UnionExpr ( ("*" | "div" | "idiv" | "mod") UnionExpr )*
+ *_____________________________________________________________________*/
+%nonassoc MULTIPLICATIVE_REDUCE
+%left STAR DIV IDIV MOD
+
+
+/*_____________________________________________________________________
+ *
+ * resolve various other shift/reduce conflicts
+ *
+ *_____________________________________________________________________*/
+%right LBRACK
+%right LPAR
+%right CATCH
+
+%nonassoc RBRACE
+
+
+%right FOR WORDS LET COUNT INSTANCE ONLY STABLE AND AS ASCENDING CASE CASTABLE CAST COLLATION DEFAULT
+%right DESCENDING ELSE _EMPTY IS OR ORDER  BY GROUP RETURN SATISFIES TREAT WHERE START AFTER BEFORE INTO
+%right AT
+%right MODIFY WITH CONTAINS END LEVELS PARAGRAPHS SENTENCES TIMES
+%right LT_OR_START_TAG VAL_EQ VAL_GE VAL_GT VAL_LE VAL_LT VAL_NE
+
+%left COMMA
+
+
+/*
+**  The code between `%{' and `%}' after the introduction of the `%union'
+**  is output in the *.cc file; it needs detailed knowledge about the
+**  driver.
+*/
+%{
+#include "compiler/parser/jsoniq_scanner.h"
+
+#undef yylex
+#define yylex driver.lexer->lex
+
+%}
+
+/*
+    The grammar
+*/
+
+%%
+%start Module;
+
+Module :
+      ModuleWithoutBOM
+  |   BYTE_ORDER_MARK_UTF8  ModuleWithoutBOM
+      {
+        $$ = $2;
+      }
+  |   SHEBANG  ModuleWithoutBOM
+      {
+        $$ = $2;
+      }
+  |   BYTE_ORDER_MARK_UTF8  SHEBANG  ModuleWithoutBOM
+      {
+        $$ = $3;
+      }
+;
+
+
+ERROR :
+      error
+      {
+        $$ = NULL;
+      }
+      // Special rules to get Bison out of some infinte loops. This can happen when the lexer
+      // throws an error and Bison finds an error too.
+   |  error UNRECOGNIZED
+      {
+        $$ = NULL; YYABORT;
+      }
+   |  ERROR error
+      {
+        $$ = NULL; YYABORT;
+      }
+;
+
+
+// [1]
+ModuleWithoutBOM :
+    MainModule
+    {
+      $$ = $1;
+      driver.set_expr( $$ );
+    }
+  |
+    VersionDecl MainModule
+    {
+      MainModule* mm = dynamic_cast<MainModule*>($2);
+      mm->set_version_decl( static_cast<VersionDecl*>($1) );
+      $$ = $2;
+      driver.set_expr( $$ );
+    }
+  |
+    LibraryModule
+    {
+      $$ = $1;
+      driver.set_expr( $$ );
+    }
+  |
+    VersionDecl LibraryModule
+    {
+      LibraryModule* lm = dynamic_cast<LibraryModule*>($2);
+      lm->set_version_decl( static_cast<VersionDecl*>($1) );
+      $$ = $2;
+      driver.set_expr( $$ );
+    }
+;
+
+
+VersionDecl :
+    XQUERY VERSION STRING_LITERAL SEMI
+    {
+      $$ = new VersionDecl( LOC(@$), SYMTAB($3), "utf-8" );
+    }
+  |
+    XQUERY VERSION STRING_LITERAL ENCODING STRING_LITERAL SEMI
+    {
+      $$ = new VersionDecl( LOC(@$), SYMTAB($3), SYMTAB($5) );
+    }
+;
+
+
+MainModule :
+    SIND_DeclList SEMI QueryBody
+    {
+      Prolog* prolog = new Prolog(LOC(@$), static_cast<SIND_DeclList*>($1), NULL);
+
+      $$ = new MainModule(LOC(@$), static_cast<QueryBody*>($3), prolog);
+    }
+  |
+    VFO_DeclList SEMI QueryBody
+    {
+      Prolog* prolog = new Prolog(LOC(@$), NULL, static_cast<VFO_DeclList*>($1));
+
+      $$ = new MainModule(LOC(@$), static_cast<QueryBody*>($3), prolog);
+    }
+  |
+    SIND_DeclList SEMI VFO_DeclList SEMI QueryBody
+    {
+      Prolog* prolog = new Prolog(LOC(@$),
+                                  static_cast<SIND_DeclList*>($1),
+                                  static_cast<VFO_DeclList*>($3));
+
+      $$ = new MainModule(LOC(@$), static_cast<QueryBody*>($5), prolog);
+    }
+  |
+    QueryBody
+    {
+      $$ = new MainModule( LOC(@$), static_cast<QueryBody*>($1), NULL );
+    }
+
+  //  ============================ Improved error messages ============================
+  |
+    SIND_DeclList ERROR QueryBody
+    {
+      $$ = $1; $$ = $3; // to prevent the Bison warning
+      @1.step();
+      error(@1, "syntax error, missing semicolon \";\" after statement.");
+      YYERROR;
+    }
+  |
+    VFO_DeclList ERROR QueryBody
+    {
+      $$ = $1; $$ = $3; // to prevent the Bison warning
+      @1.step();
+      error(@1, "syntax error, missing semicolon \";\" after declaration.");
+      YYERROR;
+    }
+  |
+    SIND_DeclList SEMI VFO_DeclList ERROR QueryBody
+    {
+      $$ = $1; $$ = $3; $$ = $5; // to prevent the Bison warning
+      @3.step();
+      error(@3, "syntax error, missing semicolon \";\" after declaration.");
+      YYERROR;
+    }
+  |
+    SIND_DeclList ERROR VFO_DeclList SEMI QueryBody
+    {
+      $$ = $1; $$ = $3; $$ = $5; // to prevent the Bison warning
+      @1.step();
+      error(@1, "syntax error, missing semicolon \";\" after statement.");
+      YYERROR;
+    }
+;
+
+
+LibraryModule :
+    ModuleDecl
+    {
+      $$ = new LibraryModule(LOC(@$), static_cast<ModuleDecl*>($1), NULL);
+    }
+  |
+    ModuleDecl SIND_DeclList SEMI
+    {
+      Prolog* prolog = new Prolog(LOC(@$), static_cast<SIND_DeclList*>($2), NULL);
+
+      $$ = new LibraryModule(LOC(@$), static_cast<ModuleDecl*>($1), prolog);
+    }
+  |
+    ModuleDecl VFO_DeclList SEMI
+    {
+      Prolog* prolog = new Prolog(LOC(@$), NULL, static_cast<VFO_DeclList*>($2));
+
+      $$ = new LibraryModule(LOC(@$), static_cast<ModuleDecl*>($1), prolog);
+    }
+  |
+    ModuleDecl SIND_DeclList SEMI VFO_DeclList SEMI
+    {
+      Prolog* prolog = new Prolog(LOC(@$),
+                                  static_cast<SIND_DeclList*>($2),
+                                  static_cast<VFO_DeclList*>($4));
+
+      $$ = new LibraryModule(LOC(@$), static_cast<ModuleDecl*>($1), prolog);
+    }
+;
+
+
+ModuleDecl :
+    MODULE NAMESPACE NCNAME EQUALS URI_LITERAL SEMI
+    {
+      $$ = new ModuleDecl( LOC(@$), SYMTAB($3), SYMTAB($5) );
+
+      dynamic_cast<ModuleDecl*>($$)->setComment( SYMTAB($1) );
+    }
+;
+
+
+SIND_DeclList :
+    SIND_Decl
+    {
+      SIND_DeclList *sdl = new SIND_DeclList( LOC(@$) );
+      sdl->push_back( $1 );
+      $$ = sdl;
+    }
+  |
+    SIND_DeclList SEMI SIND_Decl
+    {
+      ((SIND_DeclList*)$1)->push_back( $3 );
+      $$ = $1;
+    }
+  //  ============================ Improved error messages ============================
+  |
+    SIND_DeclList ERROR SIND_Decl
+    {
+      // error
+      $$ = $1; $$ = $3; // to prevent the Bison warning
+      @1.step();
+      error(@1, "syntax error, missing semicolon \";\" after declaration.");
+      YYERROR;
+    }
+;
+
+
+SIND_Decl :
+    Setter
+  | Import
+  | NamespaceDecl
+  | DefaultNamespaceDecl
+  | FTOptionDecl
+;
+
+
+Setter :
+    BoundarySpaceDecl
+  | DefaultCollationDecl
+  | BaseURIDecl
+  | ConstructionDecl
+  | OrderingModeDecl
+  | EmptyOrderDecl
+  | CopyNamespacesDecl
+
+  /* update extension */
+  | RevalidationDecl
+;
+
+
+BoundarySpaceDecl :
+    DECLARE BOUNDARY_SPACE PRESERVE
+    {
+      $$ = new BoundarySpaceDecl(LOC(@$), StaticContextConsts::preserve_space);
+    }
+  |
+    DECLARE BOUNDARY_SPACE STRIP
+    {
+      $$ = new BoundarySpaceDecl(LOC(@$), StaticContextConsts::strip_space);
+    }
+;
+
+
+DefaultCollationDecl :
+    DECLARE DEFAULT COLLATION URI_LITERAL
+    {
+      $$ = new DefaultCollationDecl( LOC(@$), SYMTAB($4) );
+    }
+;
+
+
+BaseURIDecl :
+    DECLARE BASE_URI URI_LITERAL
+    {
+      $$ = new BaseURIDecl( LOC(@$), SYMTAB($3) );
+    }
+;
+
+
+ConstructionDecl :
+    DECLARE CONSTRUCTION PRESERVE
+    {
+      $$ = new ConstructionDecl(LOC(@$), StaticContextConsts::cons_preserve);
+    }
+  |
+    DECLARE CONSTRUCTION STRIP
+    {
+      $$ = new ConstructionDecl(LOC(@$), StaticContextConsts::cons_strip);
+    }
+;
+
+
+OrderingModeDecl :
+    DECLARE ORDERING ORDERED
+    {
+      $$ = new OrderingModeDecl(LOC(@$), StaticContextConsts::ordered);
+    }
+  |
+    DECLARE ORDERING UNORDERED
+    {
+      $$ = new OrderingModeDecl(LOC(@$), StaticContextConsts::unordered);
+    }
+;
+
+
+EmptyOrderDecl :
+    DECLARE DEFAULT ORDER _EMPTY GREATEST
+    {
+      $$ = new EmptyOrderDecl(LOC(@$), StaticContextConsts::empty_greatest);
+    }
+  |
+    DECLARE DEFAULT ORDER _EMPTY LEAST
+    {
+      $$ = new EmptyOrderDecl(LOC(@$), StaticContextConsts::empty_least);
+    }
+;
+
+
+CopyNamespacesDecl :
+    DECLARE COPY_NAMESPACES  PRESERVE  COMMA  INHERIT
+    {
+      $$ = new CopyNamespacesDecl(LOC(@$), true, true);
+    }
+  |
+    DECLARE COPY_NAMESPACES  PRESERVE  COMMA  NO_INHERIT
+    {
+      $$ = new CopyNamespacesDecl(LOC(@$), true, false);
+    }
+  |
+    DECLARE COPY_NAMESPACES  NO_PRESERVE  COMMA  INHERIT
+    {
+      $$ = new CopyNamespacesDecl(LOC(@$), false, true);
+    }
+  |
+    DECLARE COPY_NAMESPACES  NO_PRESERVE  COMMA  NO_INHERIT
+    {
+      $$ = new CopyNamespacesDecl(LOC(@$), false, false);
+    }
+;
+
+
+Import :
+    SchemaImport
+  |
+    ModuleImport
+  //  ============================ Improved error messages ============================
+  |
+    IMPORT QNAME_SVAL error
+    {
+      $$ = $$; // to prevent the Bison warning
+      error(@2, "syntax error, \"import\" should be followed by either \"schema\" or \"module\".");
+      YYERROR;
+    }
+;
+
+
+SchemaImport :
+    IMPORT SCHEMA URI_LITERAL
+    {
+      $$ = new SchemaImport( LOC(@$), NULL, SYMTAB($3), NULL );
+    }
+  |
+    IMPORT SCHEMA SchemaPrefix URI_LITERAL
+    {
+      $$ = new SchemaImport(LOC(@$),
+                            dynamic_cast<SchemaPrefix*>($3),
+                            SYMTAB($4),
+                            NULL);
+    }
+  |
+    IMPORT SCHEMA URI_LITERAL AT URILiteralList
+    {
+      $$ = new SchemaImport(LOC(@$),
+                            NULL,
+                            SYMTAB($3),
+                            dynamic_cast<URILiteralList*>($5));
+    }
+  |
+    IMPORT SCHEMA SchemaPrefix URI_LITERAL AT URILiteralList
+    {
+      $$ = new SchemaImport(LOC(@$),
+                            dynamic_cast<SchemaPrefix*>($3),
+                            SYMTAB($4),
+                            dynamic_cast<URILiteralList*>($6));
+    }
+;
+
+
+URILiteralList :
+    URI_LITERAL
+    {
+      URILiteralList *ull = new URILiteralList( LOC(@$));
+      ull->push_back( SYMTAB($1) );
+      $$ = ull;
+    }
+  |
+    URILiteralList COMMA URI_LITERAL
+    {
+      if ( URILiteralList *ull = dynamic_cast<URILiteralList*>($1) )
+        ull->push_back( SYMTAB($3) );
+
+      $$ = $1;
+    }
+;
+
+
+SchemaPrefix :
+    NAMESPACE NCNAME EQUALS
+    {
+      $$ = new SchemaPrefix( LOC(@$), SYMTAB($2) );
+    }
+  |
+    DEFAULT ELEMENT NAMESPACE
+    {
+      $$ = new SchemaPrefix( LOC(@$), true );
+    }
+;
+
+
+ModuleImport :
+    IMPORT MODULE URI_LITERAL
+    {
+      $$ = new ModuleImport(LOC(@$), SYMTAB($3), NULL);
+
+      dynamic_cast<ModuleImport *>($$)->setComment(SYMTAB($2));
+    }
+  |
+    IMPORT MODULE NAMESPACE  NCNAME  EQUALS  URI_LITERAL
+    {
+      $$ = new ModuleImport(LOC(@$), SYMTAB($4), SYMTAB($6), NULL);
+
+      dynamic_cast<ModuleImport *>($$)->setComment(SYMTAB($2));
+    }
+  |
+    IMPORT MODULE URI_LITERAL AT URILiteralList
+    {
+      $$ = new ModuleImport(LOC(@$),
+                            SYMTAB($3),
+                            dynamic_cast<URILiteralList*>($5));
+
+      dynamic_cast<ModuleImport *>($$)->setComment(SYMTAB($2));
+    }
+  |
+    IMPORT MODULE NAMESPACE  NCNAME  EQUALS  URI_LITERAL  AT  URILiteralList
+    {
+      $$ = new ModuleImport(LOC(@$),
+                            SYMTAB($4),
+                            SYMTAB($6),
+                            dynamic_cast<URILiteralList*>($8));
+
+      dynamic_cast<ModuleImport *>($$)->setComment(SYMTAB($2));
+    }
+;
+
+
+NamespaceDecl :
+    DECLARE NAMESPACE NCNAME EQUALS URI_LITERAL
+    {
+      $$ = new NamespaceDecl( LOC(@$), SYMTAB($3), SYMTAB($5) );
+    }
+;
+
+
+DefaultNamespaceDecl :
+    DECLARE DEFAULT ELEMENT NAMESPACE URI_LITERAL
+    {
+      $$ = new DefaultNamespaceDecl(LOC(@$),
+                                    ParseConstants::ns_element_default,
+                                    SYMTAB($5));
+    }
+  |
+    DECLARE DEFAULT FUNCTION NAMESPACE URI_LITERAL
+    {
+      $$ = new DefaultNamespaceDecl(LOC(@$),
+                                    ParseConstants::ns_function_default,
+                                    SYMTAB($5));
+    }
+;
+
+
+VFO_DeclList :
+    VFO_Decl
+    {
+      VFO_DeclList *vdl = new VFO_DeclList( LOC(@$));
+      vdl->push_back( $1 );
+      $$ = vdl;
+    }
+  |
+    VFO_DeclList SEMI VFO_Decl
+    {
+      ((VFO_DeclList*)$1)->push_back( $3 );
+      $$ = $1;
+    }
+  //  ============================ Improved error messages ============================
+  |
+    VFO_DeclList ERROR VFO_Decl    //error catching
+    {
+      $$ = $1; $$ = $3; // to prevent the Bison warning
+      @1.step();
+      error(@1, "syntax error, missing semicolon \";\" after declaration.");
+      YYERROR;
+    }
+;
+
+
+VFO_Decl :
+    DecimalFormatDecl
+  | OptionDecl
+  | CtxItemDecl
+  | VarDecl
+  | FunctionDecl
+  | CollectionDecl
+  | IndexDecl
+  | IntegrityConstraintDecl
+;
+
+
+DecimalFormatDecl :
+    DECLARE  DEFAULT  DECIMAL_FORMAT  DecimalFormatParamList
+    {
+      $$ = new DecimalFormatNode(LOC(@$), $4);
+      delete $4;
+    }
+  |
+    DECLARE  DECIMAL_FORMAT  QNAME  DecimalFormatParamList
+    {
+      $$ = new DecimalFormatNode(LOC(@$), static_cast<QName*>($3), $4);
+      delete $4;
+    }
+;
+
+
+DecimalFormatParamList :
+    DecimalFormatParam
+    {
+      $$ = new vector<string_pair_t>();
+      $$->push_back( *$1 );
+      delete $1;
+    }
+  |
+    DecimalFormatParamList DecimalFormatParam
+    {
+      $1->push_back( *$2 );
+      delete $2;
+      $$ = $1;
+    }
+;
+
+
+DecimalFormatParam :
+    DecimalFormatParamName EQUALS StringLiteral
+    {
+      StringLiteral *sl = static_cast<StringLiteral*>($3);
+      $$ = new string_pair_t( $1, sl->get_strval().str() );
+      delete sl;
+    }
+;
+
+
+DecimalFormatParamName :
+      DECIMAL_SEPARATOR   { $$ = "decimal-separator"; }
+    | DIGIT               { $$ = "digit"; }
+    | GROUPING_SEPARATOR  { $$ = "grouping-separator"; }
+    | INFINITY_VALUE      { $$ = "infinty"; }
+    | MINUS_SIGN          { $$ = "minus-sign"; }
+    | NaN                 { $$ = "NaN"; }
+    | PATTERN_SEPARATOR   { $$ = "pattern-separator"; }
+    | PERCENT             { $$ = "percent"; }
+    | PER_MILLE           { $$ = "per-mille"; }
+    | ZERO_DIGIT          { $$ = "zero-digit"; }
+    ;
+
+
+OptionDecl :
+    DECLARE OPTION QNAME STRING_LITERAL
+    {
+      $$ = new OptionDecl(LOC(@$), static_cast<QName*>($3), SYMTAB($4));
+    }
+;
+
+
+FTOptionDecl :
+    DECLARE FT_OPTION FTMatchOptions
+    {
+      $$ = new FTOptionDecl( LOC(@$), dynamic_cast<FTMatchOptions*>($3) );
+    }
+;
+
+
+CtxItemDecl :
+    DECLARE CONTEXT ITEM CtxItemDecl2
+    {
+      $$ = $4;
+    }
+;
+
+
+CtxItemDecl2 :
+    AS ItemType CtxItemDecl3
+    {
+      CtxItemDecl* d = dynamic_cast<CtxItemDecl*>($3);
+      d->theType = $2;
+      $$ = d;
+    }
+  |
+    CtxItemDecl3
+    {
+      $$ = $1;
+    }
+;
+
+
+CtxItemDecl3 :
+    CtxItemDecl4
+    {
+      CtxItemDecl* d = dynamic_cast<CtxItemDecl*>($1);
+      d->theIsExternal = false;
+      $$ = d;
+    }
+  |
+    EXTERNAL
+    {
+      $$ = new CtxItemDecl(LOC(@$), NULL);
+    }
+  |
+    EXTERNAL CtxItemDecl4
+    {
+      $$ = $2;
+    }
+;
+
+
+CtxItemDecl4 :
+    GETS ExprSingle
+    {
+      $$ = new CtxItemDecl(LOC(@$), $2);
+    }
+;
+
+
+VarDecl :
+    DECLARE VarNameAndType GETS ExprSingle
+    {
+      std::auto_ptr<VarNameAndType> nt(dynamic_cast<VarNameAndType *>($2));
+
+      $$ = new GlobalVarDecl(LOC(@$),
+                             nt->theName,
+                             nt->theType,
+                             $4,
+                             nt->get_annotations(),
+                             false);  // not external
+
+      static_cast<GlobalVarDecl*>($$)->setComment(SYMTAB($1));
+    }
+  |
+    DECLARE VarNameAndType EXTERNAL
+    {
+      std::auto_ptr<VarNameAndType> nt(dynamic_cast<VarNameAndType *>($2));
+
+      $$ = new GlobalVarDecl(LOC(@$),
+                             nt->theName,
+                             nt->theType,
+                             NULL,   // no init expr
+                             nt->get_annotations(),
+                             true);  // external
+
+      static_cast<GlobalVarDecl*>($$)->setComment(SYMTAB($1));
+    }
+  |
+    DECLARE VarNameAndType EXTERNAL GETS ExprSingle
+    {
+      std::auto_ptr<VarNameAndType> nt(dynamic_cast<VarNameAndType *>($2));
+
+      $$ = new GlobalVarDecl(LOC(@$),
+                             nt->theName,
+                             nt->theType,
+                             $5,     // init expr
+                             nt->get_annotations(),
+                             true);  // external
+
+      static_cast<GlobalVarDecl*>($$)->setComment(SYMTAB($1));
+    }
+;
+
+
+VarNameAndType :
+    VARIABLE DOLLAR QNAME
+    {
+      $$ = new VarNameAndType(LOC(@$), static_cast<QName*>($3), NULL, NULL);
+    }
+  |
+    VARIABLE DOLLAR QNAME TypeDeclaration
+    {
+      $$ = new VarNameAndType(LOC(@$),
+                              static_cast<QName*>($3),
+                              dynamic_cast<SequenceType *>($4),
+                              NULL);
+    }
+  |
+    AnnotationList VARIABLE DOLLAR QNAME
+    {
+      $$ = new VarNameAndType(LOC(@$),
+                              static_cast<QName*>($4),
+                              NULL,
+                              static_cast<AnnotationListParsenode*>($1));
+    }
+  |
+    AnnotationList VARIABLE DOLLAR QNAME TypeDeclaration
+    {
+      $$ = new VarNameAndType(LOC(@$),
+                              static_cast<QName*>($4),
+                              dynamic_cast<SequenceType *>($5),
+                              static_cast<AnnotationListParsenode*>($1));
+    }
+;
+
+
+AnnotationList :
+    Annotation
+    {
+      $$ = new AnnotationListParsenode(LOC(@$), static_cast<AnnotationParsenode*>($1));
+    }
+  |
+    AnnotationList Annotation
+    {
+      static_cast<AnnotationListParsenode*>($1)->push_back(static_cast<AnnotationParsenode*>($2));
+      $$ = $1;
+    }
+;
+
+
+Annotation :
+    ANNOTATION_QNAME_SVAL
+    {
+      $$ = new AnnotationParsenode(LOC(@$), new QName(LOC(@$), SYMTAB($1)), NULL);
+    }
+  |
+    ANNOTATION_QNAME_SVAL LPAR AnnotationLiteralList RPAR
+    {
+      $$ = new AnnotationParsenode(LOC(@$),
+                                   new QName(LOC(@$), SYMTAB($1)),
+                                   static_cast<AnnotationLiteralListParsenode*>($3));
+    }
+  |
+    ANNOTATION_EQNAME_SVAL
+    {
+      $$ = new AnnotationParsenode(LOC(@$), new QName(LOC(@$), SYMTAB($1), true), NULL);
+    }
+  |
+    ANNOTATION_EQNAME_SVAL LPAR AnnotationLiteralList RPAR
+    {
+      $$ = new AnnotationParsenode(LOC(@$),
+                                   new QName(LOC(@$), SYMTAB($1), true),
+                                   static_cast<AnnotationLiteralListParsenode*>($3));
+    }
+;
+
+
+AnnotationLiteralList :
+    Literal
+    {
+      $$ = new AnnotationLiteralListParsenode(LOC(@$), $1);
+    }
+  |
+    AnnotationLiteralList COMMA Literal
+    {
+      static_cast<AnnotationLiteralListParsenode*>($1)->push_back($3);
+      $$ = $1;
+    }
+;
+
+
+FunctionDecl :
+    DECLARE FunctionDecl2
+    {
+      static_cast<FunctionDecl*>($2)->setComment( SYMTAB($1) );
+      $$ = $2;
+    }
+  |
+    DECLARE AnnotationList FunctionDecl2
+    {
+      FunctionDecl* fdecl = static_cast<FunctionDecl*>($3);
+
+      fdecl->setComment( SYMTAB($1) );
+
+      fdecl->set_annotations(static_cast<AnnotationListParsenode*>($2));
+      $$ = $3;
+    }
+;
+
+
+FunctionDecl2 :
+    FunctionDeclSimple
+    {
+      $$ = $1;
+    }
+  |
+    FunctionDeclUpdating
+    {
+      $$ = $1;
+    }
+;
+
+
+FunctionDeclSimple :
+    FUNCTION FUNCTION_NAME FunctionSig EnclosedStatementsAndOptionalExpr
+    {
+      $$ = new FunctionDecl(LOC(@$),
+                            static_cast<QName*>($2),
+                            &* $3->theParams,
+                            &* $3->theReturnType,
+                            $4,      // body
+                            false,   // not explicitly updating
+                            false);  // not external
+      delete $3;
+    }
+  | FUNCTION FUNCTION_NAME FunctionSig EXTERNAL
+    {
+      $$ = new FunctionDecl(LOC (@$),
+                            static_cast<QName*>($2),
+                            $3->theParams.getp(),
+                            $3->theReturnType.getp(),
+                            NULL,   // no body
+                            false,   // not explicitly updating
+                            true);  // external
+      delete $3;
+    }
+  ;
+
+
+FunctionDeclUpdating :
+    UPDATING FUNCTION FUNCTION_NAME FunctionSig EnclosedStatementsAndOptionalExpr
+    {
+      $$ = new FunctionDecl(LOC (@$),
+                            static_cast<QName*>($3),
+                            $4->theParams.getp(),
+                            $4->theReturnType.getp(),
+                            $5,    // body
+                            true,  // updating
+                            false);// not external
+      delete $4;
+    }
+  |
+    UPDATING FUNCTION FUNCTION_NAME FunctionSig EXTERNAL
+    {
+      $$ = new FunctionDecl(LOC(@$),
+                            static_cast<QName*>($3),
+                            $4->theParams.getp(),
+                            $4->theReturnType.getp(),
+                            NULL,  // no body
+                            true,  // updating
+                            true); // external
+      delete $4;
+    }
+;
+
+
+FunctionSig :
+    LPAR RPAR
+    {
+      $$ = new FunctionSig(NULL);
+    }
+  |
+    LPAR ParamList RPAR
+    {
+      $$ = new FunctionSig(dynamic_cast<ParamList*>($2));
+    }
+  |
+    LPAR RPAR AS SequenceType
+    {
+      $$ = new FunctionSig(NULL, dynamic_cast<SequenceType*>($4));
+    }
+  |
+    LPAR ParamList RPAR AS SequenceType
+    {
+      $$ = new FunctionSig(dynamic_cast<ParamList*>($2), dynamic_cast<SequenceType*>($5));
+    }
+;
+
+
+ParamList :
+    Param
+    {
+      ParamList *pl = new ParamList( LOC(@$) );
+      pl->push_back( dynamic_cast<Param*>($1) );
+      $$ = pl;
+    }
+  |
+    ParamList COMMA Param
+    {
+      if ( ParamList *pl = dynamic_cast<ParamList*>($1) )
+        pl->push_back( dynamic_cast<Param*>($3) );
+
+      $$ = $1;
+    }
+;
+
+
+Param :
+    DOLLAR  QNAME
+    {
+      $$ = new Param(LOC(@$), static_cast<QName*>($2), NULL);
+    }
+  |
+    DOLLAR  QNAME  TypeDeclaration
+    {
+      $$ = new Param(LOC(@$),
+                     static_cast<QName*>($2),
+                     dynamic_cast<SequenceType *>($3));
+    }
+;
+
+
+CollectionDecl :
+    DECLARE COLLECTION QNAME
+    {
+      $$ = new CollectionDecl( LOC(@$),
+                              static_cast<QName*>($3),
+                              NULL,
+                              NULL);
+
+      static_cast<CollectionDecl*>($$)->setComment(SYMTAB($1));
+    }
+  | DECLARE COLLECTION QNAME AS CollectionTypeDecl
+    {
+      $$ = new CollectionDecl( LOC(@$),
+                              static_cast<QName*>($3),
+                              0,
+                              static_cast<SequenceType*>($5));
+
+      static_cast<CollectionDecl*>($$)->setComment(SYMTAB($1));
+    }
+  | DECLARE AnnotationList COLLECTION QNAME
+    {
+      $$ = new CollectionDecl( LOC(@$),
+                               static_cast<QName*>($4),
+                               static_cast<AnnotationListParsenode*>($2),
+                               0);
+
+      static_cast<CollectionDecl*>($$)->setComment(SYMTAB($1));
+    }
+  | DECLARE AnnotationList COLLECTION QNAME AS CollectionTypeDecl
+    {
+      $$ = new CollectionDecl( LOC(@$),
+                               static_cast<QName*>($4),
+                               static_cast<AnnotationListParsenode*>($2),
+                               static_cast<SequenceType*>($6));
+
+      static_cast<CollectionDecl*>($$)->setComment(SYMTAB($1));
+    }
+;
+
+CollectionTypeDecl :
+    KindTest
+    {
+      $$ = static_cast<parsenode*>(new SequenceType(LOC(@$), $1, NULL));
+    }
+  | KindTest OccurrenceIndicator
+    {
+      $$ = static_cast<parsenode*>(new SequenceType(LOC(@$),
+                                                    $1,
+                                                    dynamic_cast<OccurrenceIndicator*>($2)));
+    }
+  | JSONTest
+    {
+      $$ = static_cast<parsenode*>(new SequenceType(LOC(@$), $1, NULL));
+    }
+  | JSONTest OccurrenceIndicator
+    {
+      $$ = static_cast<parsenode*>(new SequenceType(LOC(@$),
+                                                    $1,
+                                                    dynamic_cast<OccurrenceIndicator*>($2)));
+
+    };
+
+
+IndexDecl :
+    DECLARE INDEX QNAME ON NODES PathExpr BY IndexKeyList
+    {
+      $$ = new AST_IndexDecl(LOC(@$),
+                             static_cast<QName*>($3),
+                             $6,
+                             dynamic_cast<IndexKeyList*>($8),
+                             NULL);
+
+      static_cast<AST_IndexDecl*>($$)->setComment( SYMTAB($1) );
+    }
+  | DECLARE AnnotationList INDEX QNAME ON NODES PathExpr BY IndexKeyList
+    {
+      $$ = new AST_IndexDecl(LOC(@$),
+                             static_cast<QName*>($4),
+                             $7,
+                             dynamic_cast<IndexKeyList*>($9),
+                             static_cast<AnnotationListParsenode*>($2));
+
+      static_cast<AST_IndexDecl*>($$)->setComment( SYMTAB($1) );
+    }
+  ;
+
+IndexKeyList :
+    IndexKeySpec
+    {
+      IndexKeyList* keyList = new IndexKeyList(LOC(@$));
+      keyList->addKeySpec(dynamic_cast<IndexKeySpec*>($1));
+      $$ = keyList;
+    }
+  | IndexKeyList COMMA IndexKeySpec
+    {
+      dynamic_cast<IndexKeyList*>($1)->addKeySpec(dynamic_cast<IndexKeySpec*>($3));
+      $$ = $1;
+    }
+;
+
+
+IndexKeySpec :
+    PathExpr
+    {
+      $$ = new IndexKeySpec(LOC(@$), $1, NULL, NULL);
+    }
+  |
+    PathExpr TypeDeclaration
+    {
+      $$ = new IndexKeySpec(LOC(@$),
+                            $1,
+                            dynamic_cast<SequenceType*>($2),
+                            NULL);
+    }
+  | PathExpr OrderCollationSpec
+    {
+      $$ = new IndexKeySpec(LOC(@$),
+                            $1,
+                            NULL,
+                            dynamic_cast<OrderCollationSpec*>($2));
+    }
+  | PathExpr TypeDeclaration OrderCollationSpec
+    {
+      $$ = new IndexKeySpec(LOC(@$),
+                            $1,
+                            dynamic_cast<SequenceType*>($2),
+                            dynamic_cast<OrderCollationSpec*>($3));
+    }
+  ;
+
+
+IntegrityConstraintDecl :
+    DECLARE INTEGRITY CONSTRAINT QNAME ON COLLECTION QNAME
+    DOLLAR QNAME CHECK ExprSingle
+    {
+      $$ = new ICCollSimpleCheck(LOC(@$),
+                                 static_cast<QName*>($4),
+                                 static_cast<QName*>($7),
+                                 static_cast<QName*>($9),
+                                 $11);
+    }
+  |
+    DECLARE INTEGRITY CONSTRAINT QNAME ON COLLECTION QNAME
+    NODE DOLLAR QNAME CHECK UNIQUE KEY PathExpr
+    {
+      $$ = new ICCollUniqueKeyCheck(LOC(@$),
+                                    static_cast<QName*>($4),
+                                    static_cast<QName*>($7),
+                                    static_cast<QName*>($10),
+                                    $14);
+    }
+  |
+    DECLARE INTEGRITY CONSTRAINT QNAME ON COLLECTION QNAME
+    FOREACH NODE DOLLAR QNAME CHECK ExprSingle
+    {
+      $$ = new ICCollForeachNode(LOC(@$),
+                                 static_cast<QName*>($4),
+                                 static_cast<QName*>($7),
+                                 static_cast<QName*>($11),
+                                 $13);
+    }
+  |
+    DECLARE INTEGRITY CONSTRAINT QNAME FOREIGN KEY
+      FROM COLLECTION QNAME NODE DOLLAR QNAME KEY PathExpr
+      TO   COLLECTION QNAME NODE DOLLAR QNAME KEY PathExpr
+    {
+      $$ = new ICForeignKey( LOC(@$),
+                            static_cast<QName*>($4),
+                            static_cast<QName*>($9),
+                            static_cast<QName*>($12),
+                            $14,
+                            static_cast<QName*>($17),
+                            static_cast<QName*>($20),
+                            $22);
+    }
+  ;
+
+
+
+QueryBody :
+    StatementsAndOptionalExprTop
+    {
+      if ($1 == NULL)
+      {
+        error(@1, "syntax error, unexpected end of file, the query body should not be empty");
+        YYERROR;
+      }
+
+      if (dynamic_cast<BlockBody*>($1) != NULL)
+      {
+        BlockBody* blk = static_cast<BlockBody*>($1);
+        blk->setTopLevel(true);
+      }
+
+      $$ = new QueryBody(LOC(@$), $1);
+    }
+;
+
+
+StatementsAndOptionalExprTop :
+    StatementsAndExpr
+    {
+      $$ = $1;
+    }
+  | Statements
+    {
+      $$ = $1;
+    }
+  | /* empty */
+    {
+      $$ =  NULL;
+    }
+;
+
+
+StatementsAndOptionalExpr :
+    StatementsAndExpr
+    {
+      $$ = $1;
+    }
+  | Statements
+    {
+      $$ = $1;
+    }
+  | /* empty */
+    {
+      $$ =  new BlockBody(LOC(@$));
+    }
+;
+
+
+StatementsAndExpr :
+    Expr
+    {
+      $$ = $1;
+    }
+  |
+    Statements Expr
+    {
+      BlockBody* blk = static_cast<BlockBody*>($1);
+
+      blk->add($2);
+
+      $$ = blk;
+    }
+;
+
+
+Statements :
+    Statement
+    {
+      BlockBody* blk = new BlockBody(LOC(@$));
+      blk->add($1);
+      $$ = blk;
+    }
+  |
+    Statements Statement
+    {
+      BlockBody* blk = static_cast<BlockBody*>($1);
+
+      blk->add($2);
+
+      $$ = blk;
+    } 
+  //  ============================ Improved error messages ============================
+  | 
+    Statements Expr ERROR Statement
+    {
+      $$ = $1; // to prevent the Bison warning
+      $$ = $2; // to prevent the Bison warning
+      $$ = $4; // to prevent the Bison warning
+      error(@3, "syntax error, unexpected statement (missing semicolon \";\" between statements?)");
+      delete $1; // these need to be deleted here because the parser deallocator will skip them
+      delete $2;
+      delete $4;
+      YYERROR;
+    }
+;
+
+
+Statement :
+    BlockStatement
+  | VarDeclStatement
+  | AssignStatement
+  | ApplyStatement
+  | ExitStatement
+  | WhileStatement
+  | FlowCtlStatement
+
+  | FLWORStatement
+  | IfStatement
+  | TypeswitchStatement
+  | SwitchStatement
+  | TryStatement
+;
+
+
+BlockStatement :
+    LBRACE Statements RBRACE
+    {
+      $$ = $2;
+    }
+  |
+    LBRACE RBRACE
+    {
+      $$ = new BlockBody(LOC(@$));
+    }
+;
+
+
+BlockExpr :
+    LBRACE StatementsAndOptionalExpr RBRACE
+    {
+      if (dynamic_cast<BlockBody*>($2) == NULL)
+      {
+        BlockBody* blk = new BlockBody(LOC(@$));
+        blk->add($2);
+        $$ = blk;
+      }
+      else
+      {
+        $$ = $2;
+      }
+    }
+;
+
+
+EnclosedStatementsAndOptionalExpr :
+    LBRACE StatementsAndOptionalExpr RBRACE
+    {
+      $$ = $2;
+    }
+;
+
+
+VarDeclStatement :
+    BlockVarDeclList SEMI
+    {
+      $$ = $1;
+    }
+;
+
+
+BlockVarDeclList :
+    BlockVarDeclList COMMA BlockVarDecl
+    {
+      VarDeclStmt* vdecl = static_cast<VarDeclStmt*>($1);
+      vdecl->add($3);
+      $$ = vdecl;
+    }
+  |
+    VARIABLE BlockVarDecl
+    {
+      VarDeclStmt* vdecl = new VarDeclStmt(LOC(@$), NULL);
+      vdecl->add($2);
+      $$ = vdecl;
+    }
+  |
+    AnnotationList VARIABLE BlockVarDecl
+    {
+      VarDeclStmt* vdecl = new VarDeclStmt(LOC(@$),
+                                           static_cast<AnnotationListParsenode*>($1));
+      vdecl->add($3);
+      $$ = vdecl;
+    }
+  ;
+
+
+BlockVarDecl :
+    DOLLAR QNAME
+    {
+      LocalVarDecl* vd = new LocalVarDecl(LOC(@$),
+                                          static_cast<QName*>($2),
+                                          NULL,  // no type
+                                          NULL,  // no init expr
+                                          NULL); // no annotations
+      $$ = vd;
+    }
+  | DOLLAR QNAME TypeDeclaration
+    {
+      LocalVarDecl* vd = new LocalVarDecl(LOC(@$),
+                                          static_cast<QName*>($2),
+                                          dynamic_cast<SequenceType*>($3), // type
+                                          NULL,  // no init expr
+                                          NULL); // no annotations
+      $$ = vd;
+    }
+  | DOLLAR QNAME GETS ExprSingle
+    {
+      LocalVarDecl* vd = new LocalVarDecl(LOC(@$),
+                                          static_cast<QName*>($2),
+                                          NULL,  // no type
+                                          $4,    // init expr
+                                          NULL); // no annotations
+      $$ = vd;
+    }
+  | DOLLAR QNAME TypeDeclaration GETS ExprSingle
+    {
+      LocalVarDecl* vd = new LocalVarDecl(LOC(@$),
+                                          static_cast<QName*>($2),
+                                          dynamic_cast<SequenceType*>($3), // type
+                                          $5,    // init expr
+                                          NULL); // no annotations
+      $$ = vd;
+    }
+  ;
+
+
+AssignStatement :
+    DOLLAR QNAME GETS ExprSingle SEMI
+    {
+      $$ = new AssignExpr(LOC(@$), static_cast<QName*>($2), $4);
+    }
+;
+
+
+ApplyStatement :
+    ExprSimple SEMI
+    {
+      $$ = new ApplyExpr(LOC(@$), $1);
+    }
+;
+
+
+ExitStatement :
+    EXIT RETURNING ExprSingle SEMI
+    {
+      $$ = new ExitExpr(LOC(@$), $3);
+    }
+;
+
+
+WhileStatement :
+    WHILE LPAR Expr RPAR Statement
+    {
+      BlockBody* bb = dynamic_cast<BlockBody *>($5);
+      if (bb == NULL)
+      {
+        bb = new BlockBody($5->get_location());
+        bb->add($5);
+      }
+
+      $$ = new WhileExpr(LOC(@$), $3, bb);
+    }
+;
+
+
+FlowCtlStatement :
+    BREAK LOOP SEMI
+    {
+      $$ = new FlowCtlStatement(LOC(@$), FlowCtlStatement::BREAK);
+    }
+  |
+    CONTINUE LOOP SEMI
+    {
+      $$ = new FlowCtlStatement( LOC(@$), FlowCtlStatement::CONTINUE );
+    }
+;
+
+
+FLWORStatement :
+    FLWORClauseList ReturnStatement
+    {
+      ReturnExpr* re = dynamic_cast<ReturnExpr*>($2);
+      $$ = new FLWORExpr(LOC(@$),
+                         dynamic_cast<FLWORClauseList*>($1),
+                         re->get_return_val(),
+                         re->get_location(),
+                         driver.theCompilerCB->theConfig.force_gflwor);
+      delete $2;
+    }
+;
+
+
+ReturnStatement :
+    RETURN Statement
+    {
+      exprnode* retExpr = $2;
+
+      if (dynamic_cast<VarDeclStmt*>(retExpr) != NULL)
+      {
+        BlockBody* blk = new BlockBody(LOC(@$));
+        blk->add(retExpr);
+        retExpr = blk;
+      }
+
+      $$ = new ReturnExpr(LOC(@$), retExpr);
+    }
+;
+
+
+IfStatement :
+    IF LPAR Expr RPAR THEN Statement ELSE Statement
+    {
+      exprnode* thenExpr = $6;
+      exprnode* elseExpr = $8;
+
+      if (dynamic_cast<VarDeclStmt*>(thenExpr) != NULL)
+      {
+        BlockBody* blk = new BlockBody(LOC(@$));
+        blk->add(thenExpr);
+        thenExpr = blk;
+      }
+
+      if (dynamic_cast<VarDeclStmt*>(elseExpr) != NULL)
+      {
+        BlockBody* blk = new BlockBody(LOC(@$));
+        blk->add(elseExpr);
+        elseExpr = blk;
+      }
+
+      $$ = new IfExpr(LOC(@$), $3, thenExpr, elseExpr);
+    }
+;
+
+
+TryStatement :
+    TRY BlockStatement CatchListStatement %prec CATCH
+    {
+      $$ = new TryExpr(LOC(@$), $2, $3);
+    }
+;
+
+
+CatchListStatement :
+    CatchStatement
+    {
+      CatchListExpr* cle = new CatchListExpr( LOC(@$) );
+      cle->push_back( static_cast<CatchExpr*>($1) );
+      $$ = cle;
+    }
+  |
+    CatchListStatement CatchStatement
+    {
+      CatchListExpr *cle = dynamic_cast<CatchListExpr*>($1);
+      if ( cle )
+        cle->push_back( static_cast<CatchExpr*>($2) );
+      $$ = $1;
+    }
+;
+
+
+CatchStatement :
+    CATCH NameTestList BlockStatement
+    {
+       $$ = new CatchExpr(LOC(@$), *$2, $3);
+       delete $2;
+    }
+  ;
+
+
+
+Expr :
+    ExprSingle
+    {
+      $$ = $1;
+    }
+  |
+    Expr COMMA ExprSingle
+    {
+      Expr* expr = dynamic_cast<Expr*>($1);
+      if ( !expr )
+      {
+        expr = new Expr( LOC(@$) );
+        expr->push_back( $1 );
+      }
+      expr->push_back( $3 );
+      $$ = expr;
+    }
+  //  ============================ Improved error messages ============================
+  | 
+    Expr ERROR ExprSingle
+    {
+      $$ = $1; // to prevent the Bison warning
+      $$ = $3; // to prevent the Bison warning
+      // Heuristics to improve the error message: if the $1 Expr is a QName (which in turn gets
+      // promoted to a PathExpr), chances are that it's not a missing comma, so don't modify
+      // the error message.
+      if (dynamic_cast<PathExpr*>($1) == NULL)
+        error(@2, "syntax error, unexpected expression (missing comma \",\" between expressions?)");
+      delete $1; // these need to be deleted here because the parser deallocator will skip them
+      delete $3;
+      YYERROR;
+    }
+;
+
+
+ExprSingle :
+        FLWORExpr
+    |   SwitchExpr
+    |   TypeswitchExpr
+    |   IfExpr
+    |   TryExpr
+    |   ExprSimple
+;
+
+
+ExprSimple :
+        OrExpr
+    |   QuantifiedExpr
+
+    /* update extensions */
+    |   InsertExpr
+    |   DeleteExpr
+    |   RenameExpr
+    |   ReplaceExpr
+    |   TransformExpr
+
+    /* JSON update extension */
+    |   JSONDeleteExpr
+    |   JSONInsertExpr
+    |   JSONRenameExpr
+    |   JSONReplaceExpr
+    |   JSONAppendExpr
+;
+
+
+FLWORExpr :
+    FLWORClauseList ReturnExpr
+    {
+      ReturnExpr *re = dynamic_cast<ReturnExpr*>($2);
+      $$ = new FLWORExpr(LOC(@$),
+                         dynamic_cast<FLWORClauseList*>($1),
+                         re->get_return_val(),
+                         re->get_location(),
+                         driver.theCompilerCB->theConfig.force_gflwor);
+      delete $2;
+    }
+;
+
+
+ReturnExpr :
+    RETURN ExprSingle
+    {
+      $$ = new ReturnExpr( LOC(@$), $2 );
+    }
+;
+
+
+WindowType :
+    SLIDING WINDOW
+    {
+      $$ = parser::the_sliding;
+    }
+  |
+    TUMBLING WINDOW
+    {
+      $$ = parser::the_tumbling;
+    }
+;
+
+
+FLWORWinCondType :
+    START
+    {
+      $$ = parser::the_start;
+    }
+  |
+    END
+    {
+      $$ = parser::the_end;
+    }
+  |
+    ONLY END
+  {
+    $$ = parser::the_only_end;
+  }
+;
+
+
+FLWORWinCond :
+    FLWORWinCondType WindowVars WHEN ExprSingle
+    {
+      $$ = new FLWORWinCond(LOC(@$),
+                            dynamic_cast<WindowVars*>($2),
+                            $4,
+                            $1 == parser::the_start,
+                            $1 == parser::the_only_end);
+    }
+  |
+    FLWORWinCondType WHEN ExprSingle
+    {
+      $$ = new FLWORWinCond(LOC(@$),
+                            NULL,
+                            $3,
+                            $1 == parser::the_start,
+                            $1 == parser::the_only_end);
+    }
+;
+
+
+WindowClause :
+    FOR WindowType WindowVarDecl FLWORWinCond FLWORWinCond
+    {
+      $$ = new WindowClause (LOC (@$),
+                             ($2 == parser::the_tumbling ?
+                              WindowClause::tumbling_window :
+                              WindowClause::sliding_window),
+                             dynamic_cast<WindowVarDecl *> ($3),
+                             dynamic_cast<FLWORWinCond *> ($4),
+                             dynamic_cast<FLWORWinCond *> ($5));
+    }
+  | FOR WindowType WindowVarDecl FLWORWinCond
+    {
+      $$ = new WindowClause (LOC (@$),
+                             ($2 == parser::the_tumbling ?
+                              WindowClause::tumbling_window :
+                              WindowClause::sliding_window),
+                             dynamic_cast<WindowVarDecl *> ($3),
+                             dynamic_cast<FLWORWinCond *> ($4), NULL);
+    }
+;
+
+
+CountClause :
+    COUNT DOLLAR QNAME
+    {
+      $$ = new CountClause(LOC(@$), static_cast<QName*>($3));
+    }
+;
+
+
+ForLetWinClause :
+    ForClause
+  | LetClause
+  | WindowClause
+;
+
+
+FLWORClause :
+    ForLetWinClause
+  | WhereClause
+  | OrderByClause
+  | GroupByClause
+  | CountClause
+;
+
+
+FLWORClauseList :
+    ForLetWinClause
+    {
+      FLWORClauseList *fcl = new FLWORClauseList( LOC(@$) );
+      fcl->push_back( dynamic_cast<FLWORClause*>($1) );
+      $$ = fcl;
+    }
+  |
+    FLWORClauseList FLWORClause
+    {
+      FLWORClauseList *fcl = dynamic_cast<FLWORClauseList*>($1);
+      fcl->push_back( dynamic_cast<FLWORClause*>($2) );
+      $$ = fcl;
+    }
+;
+
+
+ForClause :
+    FOR DOLLAR VarInDeclList
+    {
+      $$ = new ForClause(LOC(@$), dynamic_cast<VarInDeclList*>($3));
+    }
+  //  ============================ Improved error messages ============================ 
+  |
+    FOR error VarInDeclList
+    {
+      $$ = $3; // to prevent the Bison warning
+      error(@2, "syntax error, unexpected qualified name \""
+          + static_cast<VarInDeclList*>($3)->operator[](0)->get_var_name()->get_qname().str() + "\" (missing \"$\" sign?)");
+      delete $3;
+      YYERROR;
+    }
+  |
+    FOR UNRECOGNIZED
+    {
+      $$ = NULL; // to prevent the Bison warning
+      error(@2, ""); // the error message is already set in the driver's parseError member
+      YYERROR;
+    }
+;
+
+
+VarInDeclList :
+    VarInDecl
+    {
+      VarInDeclList* vdl = new VarInDeclList( LOC(@$) );
+      vdl->push_back( dynamic_cast<VarInDecl*>($1) );
+      $$ = vdl;
+    }
+  |
+    VarInDeclList COMMA DOLLAR VarInDecl
+    {
+      if ( VarInDeclList* vdl = dynamic_cast<VarInDeclList*>($1) )
+        vdl->push_back( dynamic_cast<VarInDecl*>($4) );
+      $$ = $1;
+    }
+  //  ============================ Improved error messages ============================
+  |
+    VarInDeclList COMMA VarInDecl
+    {
+      $$ = $1; // to prevent the Bison warning
+      error(@3, "syntax error, unexpected QName \""
+          + static_cast<VarInDecl*>($3)->get_var_name()->get_qname().str() + "\" (missing \"$\" sign?)");
+      delete $1;
+      YYERROR;
+    }
+;
+
+
+VarInDecl :
+    QNAME  _IN  ExprSingle
+    {
+      $$ = new VarInDecl(LOC(@$),
+                         static_cast<QName*>($1),
+                         NULL,
+                         NULL,
+                         NULL,
+                         $3,
+                         false);
+    }
+  | QNAME  ALLOWING  _EMPTY  _IN  ExprSingle
+    {
+      $$ = new VarInDecl(LOC(@$),
+                         static_cast<QName*>($1),
+                         NULL,
+                         NULL,
+                         NULL,
+                         $5,
+                         true);
+    }
+  | QNAME  TypeDeclaration  _IN  ExprSingle
+    {
+      $$ = new VarInDecl(LOC(@$),
+                         static_cast<QName*>($1),
+                         dynamic_cast<SequenceType *>($2),
+                         NULL,
+                         NULL,
+                         $4,
+                         false);
+    }
+  | QNAME  TypeDeclaration  ALLOWING  _EMPTY  _IN  ExprSingle
+    {
+      $$ = new VarInDecl(LOC(@$),
+                         static_cast<QName*>($1),
+                         dynamic_cast<SequenceType *>($2),
+                         NULL,
+                         NULL,
+                         $6,
+                         true);
+    }
+  | QNAME  PositionalVar  _IN  ExprSingle
+    {
+      $$ = new VarInDecl(LOC(@$),
+                         static_cast<QName*>($1),
+                         NULL,
+                         dynamic_cast<PositionalVar*>($2),
+                         NULL,
+                         $4,
+                         false);
+    }
+  | QNAME  ALLOWING  _EMPTY  PositionalVar  _IN  ExprSingle
+    {
+      $$ = new VarInDecl(LOC(@$),
+                         static_cast<QName*>($1),
+                         NULL,
+                         dynamic_cast<PositionalVar*>($4),
+                         NULL,
+                         $6,
+                         true);
+    }
+  | QNAME  TypeDeclaration  PositionalVar  _IN  ExprSingle
+    {
+      $$ = new VarInDecl(LOC(@$),
+                         static_cast<QName*>($1),
+                         dynamic_cast<SequenceType *>($2),
+                         dynamic_cast<PositionalVar*>($3),
+                         NULL,
+                         $5,
+                         false);
+    }
+  | QNAME  TypeDeclaration  ALLOWING  _EMPTY  PositionalVar  _IN  ExprSingle
+    {
+      $$ = new VarInDecl(LOC(@$),
+                         static_cast<QName*>($1),
+                         dynamic_cast<SequenceType *>($2),
+                         dynamic_cast<PositionalVar*>($5),
+                         NULL,
+                         $7,
+                         true);
+    }
+/* full-text extensions */
+  | QNAME FTScoreVar _IN ExprSingle
+    {
+      $$ = new VarInDecl(LOC(@$),
+                         static_cast<QName*>($1),
+                         NULL,NULL,
+                         dynamic_cast<FTScoreVar*>($2),
+                         $4,
+                         false);
+    }
+  | QNAME  TypeDeclaration  FTScoreVar  _IN  ExprSingle
+    {
+      $$ = new VarInDecl(LOC(@$),
+                         static_cast<QName*>($1),
+                         dynamic_cast<SequenceType *>($2),
+                         NULL,
+                         dynamic_cast<FTScoreVar*>($3),
+                         $5,
+                         false);
+    }
+  | QNAME  PositionalVar  FTScoreVar  _IN  ExprSingle
+    {
+      $$ = new VarInDecl(LOC (@$),
+                         static_cast<QName*>($1),
+                         NULL,
+                         dynamic_cast<PositionalVar*>($2),
+                         dynamic_cast<FTScoreVar*>($3),
+                         $5,
+                         false);
+    }
+  | QNAME  TypeDeclaration  PositionalVar  FTScoreVar  _IN  ExprSingle
+    {
+      $$ = new VarInDecl(LOC (@$),
+                         static_cast<QName*>($1),
+                         dynamic_cast<SequenceType *>($2),
+                         dynamic_cast<PositionalVar*>($3),
+                         dynamic_cast<FTScoreVar*>($4),
+                         $6,
+                         false);
+    }
+;
+
+
+// [35] PositionalVar
+// ------------------
+PositionalVar :
+    AT  DOLLAR  QNAME
+    {
+      $$ = new PositionalVar(LOC(@$), static_cast<QName*>($3));
+    }
+;
+
+
+// [37]
+FTScoreVar :
+    SCORE DOLLAR QNAME
+    {
+      $$ = new FTScoreVar(LOC(@$), static_cast<QName*>($3));
+    }
+;
+
+
+// [36]
+LetClause :
+        LET  VarGetsDeclList
+        {
+            $$ = new LetClause( LOC(@$), dynamic_cast<VarGetsDeclList*>($2) );
+        }
+;
+
+// [36a]
+VarGetsDeclList :
+        VarGetsDecl
+        {
+            VarGetsDeclList *vgdl = new VarGetsDeclList( LOC(@$) );
+            vgdl->push_back( dynamic_cast<VarGetsDecl*>($1) );
+            $$ = vgdl;
+        }
+    |   VarGetsDeclList COMMA VarGetsDecl
+        {
+            if( VarGetsDeclList *vgdl = dynamic_cast<VarGetsDeclList*>($1) )
+                vgdl->push_back( dynamic_cast<VarGetsDecl*>($3) );
+            $$ = $1;
+        }
+;
+
+
+// [36b] VarGetsDecl
+// ------------------
+VarGetsDecl :
+    DOLLAR  QNAME  GETS  ExprSingle
+    {
+      $$ = new VarGetsDecl(LOC (@$),
+                           static_cast<QName*>($2),
+                           NULL,
+                           NULL,
+                           $4);
+    }
+  | DOLLAR  QNAME  TypeDeclaration  GETS  ExprSingle
+    {
+      $$ = new VarGetsDecl(LOC (@$),
+                           static_cast<QName*>($2),
+                           dynamic_cast<SequenceType *>($3),
+                           NULL,
+                           $5);
+    }
+
+    /* full-text extensions */
+  | FTScoreVar  GETS  ExprSingle
+    {
+      $$ = new VarGetsDecl(LOC (@$),
+                           dynamic_cast<FTScoreVar*>($1)->get_var_name(),
+                           NULL,
+                           dynamic_cast<FTScoreVar*>($1),
+                           $3);
+     }
+  | DOLLAR  QNAME  TypeDeclaration  FTScoreVar  GETS  ExprSingle
+    {
+      $$ = new VarGetsDecl(LOC (@$),
+                           static_cast<QName*>($2),
+                           dynamic_cast<SequenceType *>($3),
+                           dynamic_cast<FTScoreVar*>($4),
+                           $6);
+    }
+;
+
+
+WindowVarDecl :
+    DOLLAR QNAME  _IN  ExprSingle
+    {
+      $$ = new WindowVarDecl(LOC (@$),
+                             static_cast<QName*>($2),
+                             NULL, $4);
+    }
+  | DOLLAR QNAME  TypeDeclaration  _IN  ExprSingle
+    {
+      $$ = new WindowVarDecl(LOC (@$),
+                             static_cast<QName*>($2),
+                             dynamic_cast<SequenceType *>($3),
+                             $5);
+    }
+;
+
+
+WindowVars :
+    WindowVars3
+
+  | DOLLAR QNAME
+    {
+      $$ = new WindowVars(LOC(@$), NULL, static_cast<QName*>($2), NULL, NULL);
+    }
+  | DOLLAR QNAME WindowVars3
+    {
+      $$ = $3;
+      dynamic_cast<WindowVars *>($$)->set_curr(static_cast<QName*>($2));
+    }
+  ;
+
+WindowVars3 :
+    PositionalVar
+    {
+      $$ = new WindowVars(LOC(@$), dynamic_cast<PositionalVar*>($1), NULL, NULL, NULL);
+    }
+  | PositionalVar WindowVars2
+    {
+      $$ = $2;
+      dynamic_cast<WindowVars *>($$)->set_posvar(dynamic_cast<PositionalVar*>($1));
+    }
+  | WindowVars2
+  ;
+
+WindowVars2 :
+    PREVIOUS DOLLAR QNAME NEXT DOLLAR QNAME
+    {
+      $$ = new WindowVars(LOC(@$), NULL, NULL, static_cast<QName*>($3), static_cast<QName*>($6));
+    }
+  | NEXT DOLLAR QNAME
+    {
+      $$ = new WindowVars(LOC(@$), NULL, NULL, NULL, static_cast<QName*>($3));
+    }
+  | PREVIOUS DOLLAR QNAME
+    {
+      $$ = new WindowVars(LOC(@$), NULL, NULL, static_cast<QName*>($3), NULL);
+    }
+;
+
+
+// [37] WhereClause
+// ----------------
+WhereClause :
+    WHERE  ExprSingle
+    {
+      $$ = new WhereClause(LOC (@$), $2);
+    }
+;
+
+
+GroupByClause :
+    GROUP BY GroupSpecList
+    {
+      $$ = new GroupByClause(LOC(@$), dynamic_cast<GroupSpecList*>($3));
+    }
+  ;
+
+GroupSpecList :
+    GroupSpec
+    {
+      GroupSpecList* gsl = new GroupSpecList(LOC(@$));
+      gsl->push_back(static_cast<GroupSpec*>($1));
+      $$ = gsl;
+    }
+  | GroupSpecList COMMA GroupSpec
+    {
+      GroupSpecList* gsl = static_cast<GroupSpecList*>($1);
+      gsl->push_back(static_cast<GroupSpec*>($3));
+      $$ = gsl;
+    }
+  ;
+
+
+GroupSpec :
+    DOLLAR QNAME
+    {
+      $$ = new GroupSpec(LOC(@$), static_cast<QName*>($2), NULL, NULL, NULL);
+    }
+  | DOLLAR QNAME GETS ExprSingle
+    {
+      $$ = new GroupSpec(LOC(@$), static_cast<QName*>($2), NULL, $4, NULL);
+    }
+  | DOLLAR QNAME TypeDeclaration GETS ExprSingle
+    {
+      $$ = new GroupSpec(LOC(@$),
+                         static_cast<QName*>($2),
+                         static_cast<SequenceType*>($3),
+                         $5,
+                         NULL);
+    }
+  | DOLLAR QNAME TypeDeclaration GETS ExprSingle GroupCollationSpec
+    {
+      $$ = new GroupSpec(LOC(@$),
+                         static_cast<QName*>($2),
+                         static_cast<SequenceType*>($3),
+                         $5,
+                         static_cast<GroupCollationSpec*>($6));
+    }
+  | DOLLAR QNAME GETS ExprSingle GroupCollationSpec
+    {
+      $$ = new GroupSpec(LOC(@$),
+                         static_cast<QName*>($2),
+                         NULL,
+                         $4,
+                         static_cast<GroupCollationSpec*>($5));
+    }
+  | DOLLAR QNAME GroupCollationSpec
+    {
+      $$ = new GroupSpec(LOC(@$),
+                         static_cast<QName*>($2),
+                         NULL,
+                         NULL,
+                         static_cast<GroupCollationSpec*>($3));
+    }
+  ;
+
+
+GroupCollationSpec :
+    COLLATION URI_LITERAL
+    {
+      $$ = new GroupCollationSpec( LOC(@$), SYMTAB($2) );
+    }
+  ;
+
+
+OrderByClause :
+        ORDER BY OrderSpecList
+        {
+            $$ = new OrderByClause(
+                LOC(@$), dynamic_cast<OrderSpecList*>($3)
+            );
+        }
+    |   STABLE ORDER BY OrderSpecList
+        {
+            $$ = new OrderByClause(
+                LOC(@$), dynamic_cast<OrderSpecList*>($4), true
+            );
+        }
+    ;
+
+// [39]
+OrderSpecList :
+        OrderSpec
+        {
+            OrderSpecList *osl = new OrderSpecList( LOC(@$) );
+            osl->push_back( dynamic_cast<OrderSpec*>($1) );
+            $$ = osl;
+        }
+    |   OrderSpecList  COMMA  OrderSpec
+        {
+            if ( OrderSpecList* osl = dynamic_cast<OrderSpecList*>($1) )
+                osl->push_back( dynamic_cast<OrderSpec*>($3) );
+            $$ = $1;
+        }
+    ;
+
+// [40]
+OrderSpec :
+        ExprSingle
+        {
+            $$ = new OrderSpec( LOC(@$), $1, NULL );
+        }
+    |   ExprSingle OrderModifier
+        {
+            $$ = new OrderSpec(
+                LOC(@$), $1, dynamic_cast<OrderModifierPN*>($2)
+            );
+        }
+    ;
+
+// [41]
+OrderModifier :
+        OrderDirSpec
+        {
+            $$ = new OrderModifierPN(
+                LOC(@$), dynamic_cast<OrderDirSpec*>($1), NULL, NULL
+            );
+        }
+    |   OrderEmptySpec
+        {
+            $$ = new OrderModifierPN(
+                LOC(@$), NULL, dynamic_cast<OrderEmptySpec*>($1), NULL
+            );
+        }
+    |   OrderCollationSpec
+        {
+            $$ = new OrderModifierPN(
+                LOC(@$), NULL, NULL, dynamic_cast<OrderCollationSpec*>($1)
+            );
+        }
+    |   OrderDirSpec OrderEmptySpec
+        {
+            $$ = new OrderModifierPN(
+                LOC(@$),
+                dynamic_cast<OrderDirSpec*>($1),
+                dynamic_cast<OrderEmptySpec*>($2),
+                NULL
+            );
+        }
+    |   OrderDirSpec OrderCollationSpec
+        {
+            $$ = new OrderModifierPN(
+                LOC(@$),
+                dynamic_cast<OrderDirSpec*>($1),
+                NULL,
+                dynamic_cast<OrderCollationSpec*>($2)
+            );
+        }
+    |   OrderEmptySpec OrderCollationSpec
+        {
+            $$ = new OrderModifierPN(
+                LOC(@$),
+                NULL,
+                dynamic_cast<OrderEmptySpec*>($1),
+                dynamic_cast<OrderCollationSpec*>($2)
+            );
+        }
+    |   OrderDirSpec OrderEmptySpec OrderCollationSpec
+        {
+            $$ = new OrderModifierPN(
+                LOC(@$),
+                dynamic_cast<OrderDirSpec*>($1),
+                dynamic_cast<OrderEmptySpec*>($2),
+                dynamic_cast<OrderCollationSpec*>($3)
+            );
+        }
+    ;
+
+// [41a]
+OrderDirSpec :
+        ASCENDING
+        {
+            $$ = new OrderDirSpec( LOC(@$), ParseConstants::dir_ascending );
+        }
+    |   DESCENDING
+        {
+            $$ = new OrderDirSpec( LOC(@$), ParseConstants::dir_descending );
+        }
+    ;
+
+// [41b]
+OrderEmptySpec :
+        _EMPTY GREATEST
+        {
+            $$ = new OrderEmptySpec(
+                LOC(@$), StaticContextConsts::empty_greatest
+            );
+        }
+    |   _EMPTY LEAST
+        {
+            $$ = new OrderEmptySpec(
+                LOC(@$), StaticContextConsts::empty_least
+            );
+        }
+    ;
+
+// [41c]
+OrderCollationSpec :
+        COLLATION URI_LITERAL
+        {
+            $$ = new OrderCollationSpec( LOC(@$), SYMTAB($2) );
+        }
+    ;
+
+// [42]
+QuantifiedExpr :
+        SOME DOLLAR QVarInDeclList SATISFIES ExprSingle
+        {
+            $$ = new QuantifiedExpr(
+                LOC(@$),
+                ParseConstants::quant_some,
+                dynamic_cast<QVarInDeclList*>($3),
+                $5
+            );
+        }
+    |   EVERY DOLLAR QVarInDeclList SATISFIES ExprSingle
+        {
+            $$ = new QuantifiedExpr(
+                LOC(@$),
+                ParseConstants::quant_every,
+                dynamic_cast<QVarInDeclList*>($3),
+                $5
+            );
+        }
+    ;
+
+// [42a]
+QVarInDeclList :
+    QVarInDecl %prec QVARINDECLLIST_REDUCE
+    {
+      QVarInDeclList *qvidl = new QVarInDeclList( LOC(@$) );
+      qvidl->push_back( dynamic_cast<QVarInDecl*>($1) );
+      $$ = qvidl;
+
+    }
+  | QVarInDeclList COMMA DOLLAR QVarInDecl
+    {
+      QVarInDeclList *qvidl = dynamic_cast<QVarInDeclList*>($1);
+      qvidl->push_back( dynamic_cast<QVarInDecl*>($4) );
+      $$ = $1;
+    }
+;
+
+
+// [42b] QVarInDecl
+// ----------------
+QVarInDecl :
+    QNAME _IN  ExprSingle
+    {
+      $$ = new QVarInDecl(LOC(@$), static_cast<QName*>($1), $3);
+    }
+  | QNAME TypeDeclaration _IN  ExprSingle
+    {
+      $$ = new QVarInDecl(LOC(@$),
+                          static_cast<QName*>($1),
+                          dynamic_cast<SequenceType *>($2),
+                          $4);
+    }
+;
+
+// SwitchExpr
+// -------------------
+SwitchExpr :
+    SWITCH  LPAR  Expr  RPAR  SwitchCaseClauseList  DEFAULT  RETURN  ExprSingle
+    {
+      $$ = new SwitchExpr(LOC(@$), $3, static_cast<SwitchCaseClauseList*>($5), $8);
+    }
+  ;
+
+SwitchCaseClauseList :
+    SwitchCaseClause
+    {
+      SwitchCaseClauseList* scc_list_p = new SwitchCaseClauseList(LOC(@$));
+      scc_list_p->push_back(static_cast<SwitchCaseClause*>($1));
+      $$ = scc_list_p;
+    }
+  | SwitchCaseClauseList SwitchCaseClause
+    {
+      SwitchCaseClauseList* scc_list_p = static_cast<SwitchCaseClauseList*>($1);
+      scc_list_p->push_back(static_cast<SwitchCaseClause*>($2));
+      $$ = $1;
+    }
+  ;
+
+SwitchCaseClause :
+    SwitchCaseOperandList  RETURN  ExprSingle
+    {
+      $$ = new SwitchCaseClause(LOC(@$), dynamic_cast<SwitchCaseOperandList*>($1), $3);
+    }
+  ;
+
+SwitchCaseOperandList :
+    CASE  ExprSingle
+    {
+      SwitchCaseOperandList* sco_list_p = new SwitchCaseOperandList(LOC(@$));
+      sco_list_p->push_back($2);
+      $$ = sco_list_p;
+    }
+  | SwitchCaseOperandList CASE  ExprSingle
+    {
+      SwitchCaseOperandList* sco_list_p = static_cast<SwitchCaseOperandList*>($1);
+      sco_list_p->push_back($3);
+      $$ = $1;
+    }
+  ;
+
+// SwitchStatement
+// -------------------
+SwitchStatement :
+    SWITCH  LPAR  Expr  RPAR  SwitchCaseStatementList  DEFAULT  RETURN  Statement
+    {
+      $$ = new SwitchExpr(LOC(@$), $3, static_cast<SwitchCaseClauseList*>($5), $8);
+    }
+  ;
+
+SwitchCaseStatementList :
+    SwitchCaseStatement
+    {
+      SwitchCaseClauseList* scc_list_p = new SwitchCaseClauseList(LOC(@$));
+      scc_list_p->push_back(static_cast<SwitchCaseClause*>($1));
+      $$ = scc_list_p;
+    }
+  | SwitchCaseStatementList SwitchCaseStatement
+    {
+      SwitchCaseClauseList* scc_list_p = static_cast<SwitchCaseClauseList*>($1);
+      scc_list_p->push_back(static_cast<SwitchCaseClause*>($2));
+      $$ = $1;
+    }
+  ;
+
+SwitchCaseStatement :
+    SwitchCaseOperandList  RETURN  Statement
+    {
+      $$ = new SwitchCaseClause(LOC(@$), dynamic_cast<SwitchCaseOperandList*>($1), $3);
+    }
+  ;
+
+// [43] TypeswitchExpr
+// -------------------
+TypeswitchExpr :
+    TYPESWITCH LPAR  Expr  RPAR  CaseClauseList  DEFAULT  RETURN  ExprSingle
+    {
+      $$ = new TypeswitchExpr(LOC(@$),
+                              $3,
+                              static_cast<CaseClauseList*>($5),
+                              $8);
+    }
+  | TYPESWITCH LPAR Expr RPAR CaseClauseList DEFAULT DOLLAR QNAME RETURN ExprSingle
+    {
+      $$ = new TypeswitchExpr(LOC (@$),
+                              $3,
+                              static_cast<CaseClauseList*>($5),
+                              static_cast<QName*>($8),
+                              $10);
+    }
+;
+
+TypeswitchStatement :
+    TYPESWITCH LPAR  Expr  RPAR  CaseStatementList  DEFAULT  RETURN  Statement
+    {
+      $$ = new TypeswitchExpr(LOC(@$),
+                              $3,
+                              static_cast<CaseClauseList*>($5),
+                              $8);
+    }
+  | TYPESWITCH LPAR Expr RPAR CaseClauseList DEFAULT DOLLAR QNAME RETURN Statement
+    {
+      $$ = new TypeswitchExpr(LOC (@$),
+                              $3,
+                              static_cast<CaseClauseList*>($5),
+                              static_cast<QName*>($8),
+                              $10);
+    }
+;
+
+// [43a]
+CaseClauseList :
+    CaseClause
+    {
+      CaseClauseList* cc_list_p = new CaseClauseList(LOC (@$));
+      cc_list_p->push_back(static_cast<CaseClause*>($1));
+      $$ = cc_list_p;
+    }
+  | CaseClauseList  CaseClause
+    {
+      CaseClauseList* cc_list_p = dynamic_cast<CaseClauseList*>($1);
+      cc_list_p->push_back(static_cast<CaseClause*>($2));
+      $$ = $1;
+    }
+;
+
+
+// [44] CaseClause
+// ---------------
+CaseClause :
+    CASE SequenceTypeList RETURN ExprSingle
+    {
+      $$ = new CaseClause(LOC (@$),
+                          static_cast<SequenceTypeList*>($2),
+                          $4);
+    }
+  | CASE DOLLAR QNAME AS SequenceTypeList RETURN ExprSingle
+    {
+      $$ = new CaseClause(LOC (@$),
+                          static_cast<QName*>($3),
+                          static_cast<SequenceTypeList*>($5),
+                          $7);
+     }
+;
+
+// [43a]
+CaseStatementList :
+    CaseStatement
+    {
+      CaseClauseList* cc_list_p = new CaseClauseList(LOC (@$));
+      cc_list_p->push_back(static_cast<CaseClause*>($1));
+      $$ = cc_list_p;
+    }
+  | CaseStatementList  CaseStatement
+    {
+      CaseClauseList* cc_list_p = static_cast<CaseClauseList*>($1);
+      cc_list_p->push_back(static_cast<CaseClause*>($2));
+      $$ = $1;
+    }
+;
+
+// [44] CaseClause
+// ---------------
+CaseStatement :
+    CASE SequenceTypeList RETURN Statement
+    {
+      $$ = new CaseClause(LOC (@$),
+                          static_cast<SequenceTypeList*>($2),
+                          $4);
+    }
+  | CASE DOLLAR QNAME AS SequenceTypeList RETURN Statement
+    {
+      $$ = new CaseClause(LOC (@$),
+                          static_cast<QName*>($3),
+                          static_cast<SequenceTypeList*>($5),
+                          $7);
+     }
+;
+
+
+SequenceTypeList :
+    SequenceType
+    {
+      SequenceTypeList* seqList = new SequenceTypeList(LOC(@$));
+      seqList->push_back(static_cast<SequenceType*>($1));
+      $$ = seqList;
+    }
+  | SequenceTypeList VBAR SequenceType
+    {
+      SequenceTypeList* seqList = static_cast<SequenceTypeList*>($1);
+      seqList->push_back(static_cast<SequenceType*>($3));
+      $$ = $1;
+    }
+;
+
+
+// [45]
+IfExpr :
+    IF LPAR Expr RPAR THEN ExprSingle ELSE ExprSingle
+    {
+      $$ = new IfExpr(LOC (@$), $3, $6, $8);
+    }
+;
+
+
+// [46]
+OrExpr :
+    AndExpr
+    {
+      $$ = $1;
+    }
+  | OrExpr OR AndExpr
+    {
+      $$ = new OrExpr( LOC(@$), $1, $3 );
+    }
+;
+
+
+// [47]
+AndExpr :
+        ComparisonExpr
+        {
+            $$ = $1;
+        }
+    |   AndExpr  AND  ComparisonExpr
+        {
+            $$ = new AndExpr( LOC(@$), $1, $3 );
+        }
+    ;
+
+// [50]
+ComparisonExpr :
+        FTContainsExpr
+        {
+            $$ = $1;
+        }
+    |   FTContainsExpr ValueComp FTContainsExpr
+        {
+            /*  ::=  "eq" | "ne" | "lt" | "le" | "gt" | "ge" */
+            $$ = new ComparisonExpr(
+                LOC(@$),
+                dynamic_cast<ValueComp*>($2),
+                $1,
+                $3
+            );
+        }
+    |   FTContainsExpr NodeComp FTContainsExpr
+        {
+            /*  ::=  "is" | "<<" | ">>" */
+            $$ = new ComparisonExpr(
+                LOC(@$), dynamic_cast<NodeComp*>($2), $1, $3
+            );
+        }
+    |   FTContainsExpr EQUALS FTContainsExpr
+        {
+            $$ = new ComparisonExpr(
+                LOC(@$),
+                new GeneralComp( LOC(@$), ParseConstants::op_eq ),
+                $1,
+                $3
+            );
+        }
+    |   FTContainsExpr NE FTContainsExpr
+        {
+            $$ = new ComparisonExpr(
+                LOC(@$),
+                new GeneralComp( LOC(@$), ParseConstants::op_ne ),
+                $1,
+                $3
+            );
+        }
+    |   FTContainsExpr LT_OR_START_TAG
+        {
+            /* this call is needed */
+            driver.lexer->interpretAsLessThan();
+        }
+        FTContainsExpr
+        {
+            $$ = new ComparisonExpr(
+                LOC(@$),
+                new GeneralComp( LOC(@$), ParseConstants::op_lt ),
+                $1,
+                $4
+            );
+        }
+    |   FTContainsExpr LE FTContainsExpr
+        {
+            $$ = new ComparisonExpr(
+                LOC(@$),
+                new GeneralComp( LOC(@$), ParseConstants::op_le ),
+                $1,
+                $3
+            );
+        }
+    |   FTContainsExpr GT FTContainsExpr
+        {
+            $$ = new ComparisonExpr(
+                LOC(@$),
+                new GeneralComp( LOC(@$), ParseConstants::op_gt ),
+                $1,
+                $3
+            );
+        }
+    |   FTContainsExpr GE FTContainsExpr
+        {
+            $$ = new ComparisonExpr(
+                LOC(@$),
+                new GeneralComp( LOC(@$), ParseConstants::op_ge ),
+                $1,
+                $3
+            );
+        }
+    ;
+
+// [51]
+FTContainsExpr :
+        StringConcatExpr
+        {
+            $$ = $1;
+        }
+    |   StringConcatExpr CONTAINS TEXT FTSelection opt_FTIgnoreOption
+        {
+            $$ = new FTContainsExpr(
+                LOC(@$),
+                $1,
+                dynamic_cast<FTSelection*>($4),
+                dynamic_cast<FTIgnoreOption*>($5)
+            );
+        }
+    ;
+
+StringConcatExpr :
+        RangeExpr
+        {
+          $$ = $1;
+        }
+    |   RangeExpr CONCAT StringConcatExpr
+        {
+          $$ = new StringConcatExpr(LOC(@$), $1, $3);
+        }
+    ;
+
+opt_FTIgnoreOption :
+        /* empty */
+        {
+            $$ = NULL;
+        }
+    |   FTIgnoreOption
+        {
+            $$ = $1;
+        }
+    ;
+
+RangeExpr :
+        AdditiveExpr %prec RANGE_REDUCE
+        {
+            $$ = $1;
+        }
+    |   AdditiveExpr TO AdditiveExpr
+        {
+            $$ = new RangeExpr( LOC(@$), $1, $3 );
+        }
+    ;
+
+
+// [50]
+AdditiveExpr :
+        MultiplicativeExpr %prec ADDITIVE_REDUCE
+        {
+            $$ = $1;
+        }
+    |   AdditiveExpr PLUS MultiplicativeExpr
+        {
+            $$ = new AdditiveExpr( LOC(@$), ParseConstants::op_plus, $1, $3 );
+        }
+    |   AdditiveExpr MINUS MultiplicativeExpr
+        {
+            $$ = new AdditiveExpr( LOC(@$), ParseConstants::op_minus, $1, $3 );
+        }
+    ;
+
+// [51]
+MultiplicativeExpr :
+        UnionExpr %prec MULTIPLICATIVE_REDUCE
+        {
+            $$ = $1;
+        }
+    |   MultiplicativeExpr STAR UnionExpr
+        {
+            $$ = new MultiplicativeExpr(
+                LOC(@$), ParseConstants::op_mul, $1, $3
+            );
+        }
+    |   MultiplicativeExpr DIV UnionExpr
+        {
+            $$ = new MultiplicativeExpr(
+                LOC(@$), ParseConstants::op_div, $1, $3
+            );
+        }
+    |   MultiplicativeExpr IDIV UnionExpr
+        {
+            $$ = new MultiplicativeExpr(
+                LOC(@$), ParseConstants::op_idiv, $1, $3
+            );
+        }
+    |   MultiplicativeExpr MOD UnionExpr
+        {
+            $$ = new MultiplicativeExpr(
+                LOC(@$), ParseConstants::op_mod, $1, $3
+            );
+        }
+    ;
+
+// [52]
+UnionExpr :
+        IntersectExceptExpr %prec UNION_REDUCE
+        {
+            $$ = $1;
+        }
+    |   UnionExpr UNION IntersectExceptExpr
+        {
+            $$ = new UnionExpr( LOC(@$), $1, $3 );
+        }
+    |   UnionExpr VBAR IntersectExceptExpr
+        {
+            $$ = new UnionExpr( LOC(@$), $1, $3 );
+        }
+    ;
+
+// [53]
+IntersectExceptExpr :
+        InstanceofExpr %prec INTERSECT_EXCEPT_REDUCE
+        {
+            $$ = $1;
+        }
+    |   IntersectExceptExpr INTERSECT InstanceofExpr
+        {
+            $$ = new IntersectExceptExpr(
+                LOC(@$), ParseConstants::op_intersect, $1, $3
+            );
+        }
+    |   IntersectExceptExpr EXCEPT InstanceofExpr
+        {
+            $$ = new IntersectExceptExpr(
+                LOC(@$), ParseConstants::op_except, $1, $3
+            );
+        }
+    ;
+
+// [54]
+InstanceofExpr :
+        TreatExpr
+        {
+            $$ = $1;
+        }
+    |   TreatExpr INSTANCE OF SequenceType
+        {
+            $$ = new InstanceofExpr(
+                LOC(@$), $1, dynamic_cast<SequenceType*>($4)
+            );
+        }
+    ;
+
+// [55]
+TreatExpr :
+        CastableExpr
+        {
+            $$ = $1;
+        }
+    |   CastableExpr TREAT AS SequenceType
+        {
+            $$ = new TreatExpr(
+                LOC(@$), $1, dynamic_cast<SequenceType*>($4)
+            );
+        }
+    ;
+
+// [56]
+CastableExpr :
+        CastExpr
+        {
+            $$ = $1;
+        }
+    |   CastExpr CASTABLE AS SingleType
+        {
+            $$ = new CastableExpr(
+                LOC(@$), $1, dynamic_cast<SingleType*>($4)
+            );
+        }
+    ;
+
+// [57]
+CastExpr :
+        UnaryExpr
+        {
+            $$ = $1;
+        }
+    |   UnaryExpr CAST AS SingleType
+        {
+            $$ = new CastExpr(
+                LOC(@$), $1, dynamic_cast<SingleType*>($4)
+            );
+        }
+    ;
+
+
+SingleType :
+    SimpleType
+    {
+      $$ = new SingleType(LOC(@$), dynamic_cast<SimpleType*>($1), false);
+    }
+  | SimpleType HOOK
+    {
+      $$ = new SingleType(LOC(@$), dynamic_cast<SimpleType*>($1), true);
+    }
+;
+
+
+// [58]
+UnaryExpr :
+        ValueExpr
+        {
+            $$ = $1;
+        }
+    |   SignList ValueExpr
+        {
+            $$ = new UnaryExpr( LOC(@$), dynamic_cast<SignList*>($1), $2 );
+        }
+    ;
+
+// [58a]
+SignList :
+        PLUS
+        {
+            $$ = new SignList( LOC(@$), true );
+        }
+    |   MINUS
+        {
+            $$ = new SignList( LOC(@$), false );
+        }
+    |   SignList PLUS
+        {
+            $$ = $1;
+        }
+    |   SignList MINUS
+        {
+            if ( SignList *sl = dynamic_cast<SignList*>($1) )
+                sl->negate();
+            $$ = $1;
+        }
+    ;
+
+// [59]
+ValueExpr :
+        ValidateExpr
+        {
+            $$ = $1;
+        }
+    |   SimpleMapExpr
+        {
+            $$ = $1;
+        }
+    |   ExtensionExpr
+        {
+            $$ = $1;
+        }
+    ;
+
+SimpleMapExpr :
+      PathExpr %prec SIMPLEMAPEXPR_REDUCE
+      {
+        $$ = $1;
+      }
+    |
+      SimpleMapExpr BANG PathExpr
+      {
+        $$ = new SimpleMapExpr(LOC(@$), $1, $3);
+      }
+    ;
+
+// [61]
+ValueComp :
+        VAL_EQ
+        {
+            $$ = new ValueComp( LOC(@$), ParseConstants::op_val_eq );
+        }
+    |   VAL_NE
+        {
+            $$ = new ValueComp( LOC(@$), ParseConstants::op_val_ne );
+        }
+    |   VAL_LT
+        {
+            $$ = new ValueComp( LOC(@$), ParseConstants::op_val_lt );
+        }
+    |   VAL_LE
+        {
+            $$ = new ValueComp( LOC(@$), ParseConstants::op_val_le );
+        }
+    |   VAL_GT
+        {
+            $$ = new ValueComp( LOC(@$), ParseConstants::op_val_gt );
+        }
+    |   VAL_GE
+        {
+            $$ = new ValueComp( LOC(@$), ParseConstants::op_val_ge );
+        }
+    ;
+
+// [62]
+NodeComp :
+        IS
+        {
+            $$ = new NodeComp( LOC(@$), ParseConstants::op_is );
+        }
+    |   PRECEDES
+        {
+            $$ = new NodeComp( LOC(@$), ParseConstants::op_precedes );
+        }
+    |   FOLLOWS
+        {
+            $$ = new NodeComp( LOC(@$), ParseConstants::op_follows );
+        }
+    ;
+
+// [63]
+ValidateExpr :
+        VALIDATE LBRACE StatementsAndExpr RBRACE
+        {
+            $$ = new ValidateExpr( LOC(@$), "strict", $3 );
+        }
+    |   VALIDATE LAX LBRACE StatementsAndExpr RBRACE
+        {
+            $$ = new ValidateExpr( LOC(@$), "lax", $4 );
+        }
+    |   VALIDATE _STRICT LBRACE StatementsAndExpr RBRACE
+        {
+            $$ = new ValidateExpr( LOC(@$), "strict", $4 );
+        }
+    |   VALIDATE TYPE TypeName LBRACE StatementsAndExpr RBRACE
+        {
+            $$ = new ValidateExpr(
+                LOC(@$), dynamic_cast<TypeName*>($3)->get_name(), $5
+            );
+            delete $3;
+        }
+    ;
+
+// [64]
+ExtensionExpr :
+        Pragma_list LBRACE RBRACE
+        {
+            $$ = new ExtensionExpr(
+                LOC(@$), dynamic_cast<PragmaList*>($1), NULL
+            );
+        }
+    |   Pragma_list LBRACE Expr RBRACE
+        {
+            $$ = new ExtensionExpr(
+                LOC(@$), dynamic_cast<PragmaList*>($1), $3
+            );
+        }
+    ;
+
+// [64a]
+Pragma_list :
+        Pragma
+        {
+            PragmaList *pl = new PragmaList( LOC(@$) );
+            pl->push_back( dynamic_cast<Pragma*>($1) );
+            $$ = pl;
+        }
+    |   Pragma_list Pragma
+        {
+            if ( PragmaList *pl = dynamic_cast<PragmaList*>($1) )
+                pl->push_back( dynamic_cast<Pragma*>($2) );
+            $$ = $1;
+        }
+    ;
+
+// [69]
+Pragma :
+        PRAGMA_BEGIN QNAME PRAGMA_LITERAL_AND_END_PRAGMA
+        {
+            $$ = new Pragma( LOC(@$), static_cast<QName*>($2), SYMTAB($3) );
+        }
+    |   PRAGMA_BEGIN QNAME_SVAL_AND_END_PRAGMA
+        {
+            $$ = new Pragma( LOC(@$), new QName( LOC(@$), SYMTAB($2) ), "" );
+        }
+    |   PRAGMA_BEGIN EQNAME_SVAL_AND_END_PRAGMA
+        {
+            $$ = new Pragma( LOC(@$), new QName( LOC(@$), SYMTAB($2), true ), "" );
+        }
+    ;   /* ws: explicit */
+
+// [66] PragmaContents
+// -------------------
+/* folded into [65] */
+
+/*______________________________________________________________________
+|
+| Constraint: leading-lone-slash
+|
+|   A single slash may appear either as a complete path expression or as
+|   the first part of a path expression in which it is followed by a
+|   RelativePathExpr, which can take the form of a NameTest ("*" or a
+|   QName). In contexts where operators like "*", "union", etc., can
+|   occur, parsers may have difficulty distinguishing operators from
+|   NameTests. For example, without lookahead the first part of the
+|   expression "/ * 5", for example is easily taken to be a complete
+|   expression, "/ *", which has a very different interpretation (the
+|   child nodes of "/").
+|
+|   To reduce the need for lookahead, therefore, if the token immediately
+|   following a slash is "*" or a keyword, then the slash must be the
+|   beginning, but not the entirety, of a PathExpr (and the following
+|   token must be a NameTest, not an operator).
+|
+|   A single slash may be used as the left-hand argument of an operator by
+|   parenthesizing it: (/) * 5. The expression 5 * /, on the other hand,
+|   is legal without parentheses.
+|
+|_______________________________________________________________________*/
+
+
+// [67]
+PathExpr :
+//    LeadingSlash %prec STEP_REDUCE
+//    {
+//      $$ = new PathExpr(LOC(@$), ParseConstants::path_leading_lone_slash, NULL);
+//    }
+//  | LeadingSlash RelativePathExpr
+//    {
+//      RelativePathExpr* rpe;
+//
+//      rpe = new RelativePathExpr(LOC(@$), ParseConstants::st_slash, NULL, $2, false);
+//
+//      $$ = new PathExpr(LOC(@$),
+//                        ParseConstants::path_leading_slash,
+//                        rpe);
+//    }
+//  | SLASH_SLASH RelativePathExpr
+//    {
+//      RelativePathExpr* rpe;
+//
+//      rpe = new RelativePathExpr(LOC(@$), ParseConstants::st_slashslash, NULL, $2, false);
+//
+//      $$ = new PathExpr(LOC(@$),
+//                        ParseConstants::path_leading_slashslash,
+//                        rpe);
+//    }
+//  | 
+  RelativePathExpr        /* gn: leading-lone-slashXQ */
+    {
+      RelativePathExpr* rpe = dynamic_cast<RelativePathExpr*>($1);
+      $$ = (!rpe ?
+            $1 :
+            new PathExpr( LOC(@$), ParseConstants::path_relative, $1));
+    }
+;
+
+
+// Leading slash promotion
+// -----------------------
+//LeadingSlash :
+//    SLASH
+//    {
+//      $$ = NULL;
+//    }
+//;
+
+
+// [68]
+RelativePathExpr :
+    StepExpr %prec STEP_REDUCE
+    {
+      AxisStep* as = dynamic_cast<AxisStep*>($1);
+      $$ = (as ?
+            new RelativePathExpr(LOC(@$),
+                                 ParseConstants::st_slash,
+                                 new ContextItemExpr( LOC(@$), true ), $1, true)
+            :
+            $1);
+    }
+//  | StepExpr SLASH RelativePathExpr
+//    {
+//      $$ = new RelativePathExpr(LOC(@$), ParseConstants::st_slash, $1, $3, false);
+//    }
+//  | StepExpr SLASH_SLASH RelativePathExpr
+//    {
+//      $$ = new RelativePathExpr(LOC(@$), ParseConstants::st_slashslash, $1, $3, false);
+//    }
+;
+
+
+// [69]
+StepExpr :
+//    AxisStep
+//    {
+//      $$ = $1;
+//    }
+//  |
+  FilterExpr
+    {
+      $$ = $1;
+    }
+;
+
+
+// [70]
+//AxisStep :
+//        ForwardStep
+//        {
+//            $$ = new AxisStep(
+//                LOC(@$), dynamic_cast<ForwardStep*>($1), NULL
+//            );
+//        }
+//    |   ForwardStep PredicateList
+//        {
+//            $$ = new AxisStep(
+//                LOC(@$),
+//                dynamic_cast<ForwardStep*>($1),
+//                dynamic_cast<PredicateList*>($2)
+//            );
+//        }
+//    |   ReverseStep
+//        {
+//            $$ = new AxisStep(
+//                LOC(@$), dynamic_cast<ReverseStep*>($1), NULL
+//            );
+//        }
+//    |   ReverseStep PredicateList
+//        {
+//            $$ = new AxisStep(
+//                LOC(@$),
+//                dynamic_cast<ReverseStep*>($1),
+//                dynamic_cast<PredicateList*>($2)
+//            );
+//        }
+//    ;
+//
+//// [71]
+//ForwardStep :
+//        ForwardAxis NodeTest
+//        {
+//            $$ = new ForwardStep(
+//                LOC(@$), dynamic_cast<ForwardAxis*>($1), $2
+//            );
+//        }
+//    |   AbbrevForwardStep
+//        {
+//            $$ = new ForwardStep(
+//                LOC(@$), dynamic_cast<AbbrevForwardStep*>($1)
+//            );
+//        }
+//    ;
+//
+//// [72]
+//ForwardAxis :
+//        CHILD  DOUBLE_COLON
+//        {
+//            $$ = new ForwardAxis( LOC(@$), ParseConstants::axis_child );
+//        }
+//    |   DESCENDANT  DOUBLE_COLON
+//        {
+//            $$ = new ForwardAxis( LOC(@$), ParseConstants::axis_descendant);
+//        }
+//    |   ATTRIBUTE  DOUBLE_COLON
+//        {
+//            $$ = new ForwardAxis( LOC(@$), ParseConstants::axis_attribute );
+//        }
+//    |   SELF  DOUBLE_COLON
+//        {
+//            $$ = new ForwardAxis( LOC(@$), ParseConstants::axis_self );
+//        }
+//    |   DESCENDANT_OR_SELF  DOUBLE_COLON
+//        {
+//            $$ = new ForwardAxis(
+//                LOC(@$), ParseConstants::axis_descendant_or_self
+//            );
+//        }
+//    |   FOLLOWING_SIBLING  DOUBLE_COLON
+//        {
+//            $$ = new ForwardAxis(
+//                LOC(@$), ParseConstants::axis_following_sibling
+//            );
+//        }
+//    |   FOLLOWING  DOUBLE_COLON
+//        {
+//            $$ = new ForwardAxis( LOC(@$), ParseConstants::axis_following );
+//        }
+//    ;
+//
+//// [73]
+//AbbrevForwardStep :
+//        NodeTest
+//        {
+//            $$ = new AbbrevForwardStep( LOC(@$), $1, false );
+//        }
+//    |   AT_SIGN NodeTest
+//        {
+//            $$ = new AbbrevForwardStep( LOC(@$), $2, true );
+//        }
+//    ;
+//
+//// [74]
+//ReverseStep :
+//        ReverseAxis NodeTest
+//        {
+//            $$ = new ReverseStep( LOC(@$), dynamic_cast<ReverseAxis*>($1), $2 );
+//        }
+//    |   DOT_DOT
+//        {
+//            ReverseAxis *ra = new ReverseAxis(
+//                LOC(@$), ParseConstants::axis_parent
+//            );
+//            $$ = new ReverseStep( LOC(@$), ra, NULL );
+//        }
+//    ;
+//
+//// [75]
+//ReverseAxis :
+//        PARENT  DOUBLE_COLON
+//        {
+//            $$ = new ReverseAxis( LOC(@$), ParseConstants::axis_parent );
+//        }
+//    |   ANCESTOR  DOUBLE_COLON
+//        {
+//            $$ = new ReverseAxis( LOC(@$), ParseConstants::axis_ancestor );
+//        }
+//    |   PRECEDING_SIBLING  DOUBLE_COLON
+//        {
+//            $$ = new ReverseAxis(
+//                LOC(@$), ParseConstants::axis_preceding_sibling
+//            );
+//        }
+//    |   PRECEDING  DOUBLE_COLON
+//        {
+//            $$ = new ReverseAxis( LOC(@$), ParseConstants::axis_preceding );
+//        }
+//    |   ANCESTOR_OR_SELF  DOUBLE_COLON
+//        {
+//            $$ = new ReverseAxis(
+//                LOC(@$), ParseConstants::axis_ancestor_or_self
+//            );
+//        }
+//    ;
+//
+//// [76]
+//// AbbrevReverseStep
+//// folded into [74]
+//
+//// [77]
+//NodeTest :
+//        KindTest
+//        {
+//            $$ = $1;
+//        }
+//    |   NameTest
+//        {
+//            $$ = $1;
+//        }
+//    ;
+//
+// [78]
+NameTest :
+        QNAME
+        {
+            $$ = new NameTest( LOC(@$), static_cast<QName*>($1) );
+        }
+    |   Wildcard
+        {
+            $$ = new NameTest( LOC(@$), dynamic_cast<Wildcard*>($1) );
+        }
+    ;
+
+
+// [79] Wildcard
+// -------------
+Wildcard :
+    STAR
+    {
+      $$ = new Wildcard(LOC(@$), "", "", ParseConstants::wild_all, false);
+    }
+  | ELEM_WILDCARD
+    {
+      $$ = new Wildcard(LOC(@$), SYMTAB($1), "", ParseConstants::wild_elem, false);
+    }
+  | ELEM_EQNAME_WILDCARD
+    {
+      $$ = new Wildcard(LOC(@$), SYMTAB($1), "", ParseConstants::wild_elem, true);
+    }
+  | PREFIX_WILDCARD   /* ws: explicitXQ */
+    {
+      $$ = new Wildcard(LOC(@$), "", SYMTAB($1), ParseConstants::wild_prefix, false);
+    }
+;
+
+
+// [80]
+FilterExpr :
+     PrimaryExpr
+     {
+       $$ = $1;
+     }
+  |  FilterExpr PredicateList %prec LBRACK
+     {
+       $$ = new FilterExpr(LOC(@$), $1, dynamic_cast<PredicateList*>($2));
+     }
+  |  FilterExpr LPAR RPAR
+     {
+       $$ = new DynamicFunctionInvocation(LOC (@$), $1);
+     }
+  |  FilterExpr LPAR ArgList RPAR
+     {
+       $$ = new DynamicFunctionInvocation(LOC (@$), $1, dynamic_cast<ArgList*>($3));
+     }
+;
+
+// [81]
+PredicateList :
+        Predicate
+        {
+            PredicateList *pl = new PredicateList( LOC(@$) );
+            pl->push_back( dynamic_cast<exprnode*>($1) );
+            $$ = pl;
+        }
+    |   PredicateList Predicate
+        {
+            if ( PredicateList *pl = dynamic_cast<PredicateList*>($1) )
+                pl->push_back( dynamic_cast<exprnode*>($2) );
+            $$ = $1;
+        }
+    ;
+
+// [82]
+Predicate :
+        LBRACK Expr RBRACK
+        {
+            $$ = $2;
+        }
+    ;
+
+// [83]
+PrimaryExpr :
+        Literal
+        {
+          $$ = $1;
+        }
+    |   VarRef
+        {
+          $$ = $1;
+        }
+    |   ParenthesizedExpr
+        {
+          $$ = $1;
+        }
+    |   ContextItemExpr
+        {
+          $$ = $1;
+        }
+    |   FunctionCall
+        {
+          $$ = $1;
+        }
+    |   Constructor
+        {
+          $$ = $1;
+        }
+    |   OrderedExpr
+        {
+          $$ = $1;
+        }
+    |   UnorderedExpr
+        {
+          $$ = $1;
+        }
+    |   FunctionItemExpr
+        {
+          $$ = $1;
+        }
+    |   BlockExpr
+        {
+          $$ = $1;
+        }
+        /* JSON grammar rules */
+    |   JSONObjectConstructor
+        {
+          $$ = $1;
+        }
+    |   JSONArrayConstructor
+        {
+          $$ = $1;
+        }
+    |   JSONSimpleObjectUnion
+        {
+          $$ = $1;
+        }
+    |   JSONAccumulatorObjectUnion
+        {
+          $$ = $1;
+        }
+    |   PrimaryExpr DOT NCNAME
+        {
+          StringLiteral* sl = new StringLiteral( LOC(@$), SYMTAB($3) );
+          ArgList *al = new ArgList( LOC(@$) );
+          al->push_back(sl);
+          $$ = new DynamicFunctionInvocation(LOC(@$), $1, al); 
+        }
+    ;
+
+// [84]
+Literal :
+        NumericLiteral
+        {
+            $$ = $1;
+        }
+    |   StringLiteral
+        {
+            $$ = $1;
+        }
+    ;
+
+// [85]
+NumericLiteral :
+        DECIMAL_LITERAL
+        {
+            $$ = NumericLiteral::new_literal(
+                LOC(@$), ParseConstants::num_decimal, *$1
+            );
+            delete yylval.decval;
+        }
+    |   INTEGER_LITERAL
+        {
+            $$ = NumericLiteral::new_literal(
+                LOC(@$), ParseConstants::num_integer, *$1
+            );
+            delete yylval.ival;
+        }
+    |   DOUBLE_LITERAL
+        {
+            $$ = NumericLiteral::new_literal(
+                LOC(@$), ParseConstants::num_double, *$1
+            );
+            delete yylval.dval;
+        }
+    ;
+
+// [86]
+VarRef :
+        DOLLAR QNAME
+        {
+            $$ = new VarRef(LOC(@$), static_cast<QName*>($2));
+        }
+    ;
+
+// [87]
+ParenthesizedExpr :
+        LPAR RPAR
+        {
+            $$ = new ParenthesizedExpr( LOC(@$), NULL);
+        }
+    |   LPAR Expr RPAR
+        {
+            $$ = new ParenthesizedExpr( LOC(@$), $2 );
+        }
+    ;
+
+// [88]
+ContextItemExpr :
+        DOT
+        {
+            $$ = new ContextItemExpr( LOC(@$) );
+        }
+    ;
+
+// [89]
+OrderedExpr :
+        ORDERED LBRACE Expr RBRACE
+        {
+            $$ = new OrderedExpr( LOC(@$), $3 );
+        }
+    ;
+
+// [90]
+UnorderedExpr :
+        UNORDERED LBRACE Expr RBRACE
+        {
+            $$ = new UnorderedExpr( LOC(@$), $3 );
+        }
+    ;
+
+// [91] FunctionCall
+// -----------------
+/*___________________________________________________________________
+|   gn: reserved-function-namesXQ
+|   Constraint: reserved-function-names
+|
+|   Unprefixed function names spelled the same way as language keywords
+|   could make the language harder to recognize. For instance, if(foo)
+|   could be taken either as a FunctionCall or as the beginning of an
+|   IfExpr. Therefore it is not legal syntax for a user to invoke
+|   functions with unprefixed names which match any of the names:
+|       attribute
+|       comment
+|       document-node
+|       element
+|       empty-sequence
+|       if
+|       item
+|       node
+|       processing-instruction
+|       schema-attribute
+|       schema-element
+|       text
+|       typeswitch
+|
+|   A function named "if" can be called by binding its namespace to a
+|   prefix and using the prefixed form: "library:if(foo)" instead of
+|   "if(foo)".
+|____________________________________________________________________*/
+
+/*___________________________________________________________________
+|   gn: parensXQ
+|   Grammar-note: parens
+|
+|   Look-ahead is required to distinguish FunctionCall from a QName or
+|   keyword followed by a Pragma or Comment. For example:
+|
+|       address (: this may be empty :)
+|
+|   may be mistaken for a call to a function named
+|   "address" unless this lookahead is employed. Another example is
+|
+|       for (: whom the bell :) $tolls in 3 return $tolls,
+|
+|   where the keyword "for" must not be mistaken for a function name.
+|
+|____________________________________________________________________*/
+FunctionCall :
+    FUNCTION_NAME LPAR RPAR
+    {
+      $$ = new FunctionCall( LOC(@$), static_cast<QName*>($1), NULL );
+    }
+|   FUNCTION_NAME LPAR ArgList RPAR
+    {
+      $$ = new FunctionCall(LOC(@$),
+                            static_cast<QName*>($1),
+                            dynamic_cast<ArgList*>($3));
+    }
+;
+
+
+ArgList :
+    ExprSingle
+    {
+      ArgList *al = new ArgList( LOC(@$) );
+      al->push_back( $1 );
+      $$ = al;
+    }
+|   ArgList COMMA ExprSingle
+    {
+      if ( ArgList *al = dynamic_cast<ArgList*>($1) )
+        al->push_back( $3 );
+      $$ = $1;
+    }
+;
+
+
+Constructor :
+    DirectConstructor
+    {
+      $$ = $1;
+    }
+  | ComputedConstructor
+    {
+      $$ = $1;
+    }
+;
+
+
+DirectConstructor :
+    DirElemConstructor
+    {
+      $$ = $1;
+    }
+  | DirCommentConstructor
+    {
+      $$ = $1;
+    }
+  | DirPIConstructor
+    {
+      $$ = $1;
+    }
+;
+
+
+DirElemConstructor :
+    LT_OR_START_TAG QNAME OptionalBlank EMPTY_TAG_END /* ws: explicitXQ */
+    {
+      $$ = new DirElemConstructor(LOC(@$),
+                                  static_cast<QName*>($2),
+                                  NULL,
+                                  NULL,
+                                  NULL);
+    }
+  | LT_OR_START_TAG QNAME OptionalBlank TAG_END START_TAG_END QNAME OptionalBlank TAG_END
+    {
+      if (static_cast<QName*>($2)->get_qname() != static_cast<QName*>($6)->get_qname())
+      {
+        error(@5, "syntax error, end tag </" +
+                  static_cast<QName*>($6)->get_qname().str() +
+                  "> does not match start tag <" +
+                  static_cast<QName*>($2)->get_qname().str() + ">");
+        YYERROR;
+      }
+
+      $$ = new DirElemConstructor(LOC(@$),
+                                  static_cast<QName*>($2),
+                                  static_cast<QName*>($6),
+                                  NULL,
+                                  NULL);
+    }
+  | LT_OR_START_TAG QNAME DirAttributeList OptionalBlank EMPTY_TAG_END /* ws:explicitXQ */
+    {
+      $$ = new DirElemConstructor(LOC(@$),
+                                  static_cast<QName*>($2),
+                                  NULL,
+                                  dynamic_cast<DirAttributeList*>($3),
+                                  NULL);
+    }
+  | LT_OR_START_TAG QNAME DirAttributeList OptionalBlank TAG_END START_TAG_END QNAME OptionalBlank TAG_END
+    {
+      if (static_cast<QName*>($2)->get_qname() != static_cast<QName*>($7)->get_qname())
+      {
+        error(@5, "syntax error, end tag </" +
+                  static_cast<QName*>($7)->get_qname().str() +
+                  "> does not match start tag <" +
+                  static_cast<QName*>($2)->get_qname().str() + ">");
+        YYERROR;
+      }
+
+      $$ = new DirElemConstructor(LOC(@$),
+                                  static_cast<QName*>($2),
+                                  static_cast<QName*>($7),
+                                  dynamic_cast<DirAttributeList*>($3),
+                                  NULL);
+    }
+  | LT_OR_START_TAG QNAME OptionalBlank TAG_END
+    DirElemContentList
+    START_TAG_END QNAME OptionalBlank TAG_END
+    {
+      if (static_cast<QName*>($2)->get_qname() != static_cast<QName*>($7)->get_qname())
+      {
+        error(@5, "syntax error, end tag </" +
+                  static_cast<QName*>($7)->get_qname().str() +
+                  "> does not match start tag <" +
+                  static_cast<QName*>($2)->get_qname().str() + ">");
+        YYERROR;
+      }
+
+      $$ = new DirElemConstructor(LOC(@$),
+                                  static_cast<QName*>($2),
+                                  static_cast<QName*>($7),
+                                  NULL,
+                                  dynamic_cast<DirElemContentList*>($5));
+    }
+  | LT_OR_START_TAG QNAME DirAttributeList OptionalBlank TAG_END
+    DirElemContentList
+    START_TAG_END QNAME OptionalBlank TAG_END
+    {
+      if (static_cast<QName*>($2)->get_qname() != static_cast<QName*>($8)->get_qname())
+      {
+        error(@5, "syntax error, end tag </" +
+                  static_cast<QName*>($8)->get_qname().str() +
+                  "> does not match start tag <" +
+                  static_cast<QName*>($2)->get_qname().str() + ">");
+        YYERROR;
+      }
+
+      $$ = new DirElemConstructor(LOC(@$),
+                                  static_cast<QName*>($2),
+                                  static_cast<QName*>($8),
+                                  dynamic_cast<DirAttributeList*>($3),
+                                  dynamic_cast<DirElemContentList*>($6));
+    }
+/* ws: explicitXQ */
+;
+
+
+DirElemContentList :
+    DirElemContent
+    {
+      DirElemContentList *decl = new DirElemContentList( LOC(@$) );
+      decl->push_back( dynamic_cast<DirElemContent*>($1) );
+      $$ = decl;
+    }
+  | DirElemContentList DirElemContent
+    {
+      DirElemContentList *decl = dynamic_cast<DirElemContentList*>($1);
+      if ( decl )
+        decl->push_back( dynamic_cast<DirElemContent*>($2) );
+      $$ = $1;
+    }
+;
+
+
+DirAttributeList :
+    DirAttr
+    {
+      DirAttributeList *dal = new DirAttributeList( LOC(@$) );
+      dal->push_back( dynamic_cast<DirAttr*>($1) );
+      $$ = dal;
+    }
+  | DirAttributeList DirAttr
+    {
+      DirAttributeList *dal = dynamic_cast<DirAttributeList*>($1);
+      if ( dal )
+        dal->push_back( dynamic_cast<DirAttr*>($2) );
+      $$ = $1;
+    }
+;
+
+
+DirAttr :
+    BLANK QNAME OptionalBlank EQUALS OptionalBlank DirAttributeValue /* ws: explicitXQ */
+    {
+      $$ = new DirAttr(LOC(@$),
+                       static_cast<QName*>($2),
+                       dynamic_cast<DirAttributeValue*>($6));
+    }
+;
+
+// OptionaBlank used in the DirElemConstr
+OptionalBlank :
+      /* empty */
+  |   BLANK;
+
+
+DirAttributeValue :
+    QUOTE opt_QuoteAttrContentList QUOTE
+    {
+      $$ = new DirAttributeValue(LOC(@$),
+                                 dynamic_cast<QuoteAttrContentList*>($2));
+    }
+  | APOS opt_AposAttrContentList APOS     /* ws: explicitXQ */
+    {
+      $$ = new DirAttributeValue( LOC(@$),
+                                  dynamic_cast<AposAttrContentList*>($2));
+    }
+;
+
+
+opt_QuoteAttrContentList :
+    /* empty */
+    {
+      $$ = new QuoteAttrContentList( LOC(@$) );
+    }
+  | QuoteAttrContentList
+    {
+      $$ = $1;
+    }
+;
+
+QuoteAttrContentList :
+    ESCAPE_QUOTE
+    {
+      QuoteAttrContentList *qacl = new QuoteAttrContentList( LOC(@$) );
+      qacl->push_back( new QuoteAttrValueContent( LOC(@$), "\"" ) );
+      $$ = qacl;
+    }
+  | QuoteAttrValueContent
+    {
+      QuoteAttrContentList *qacl = new QuoteAttrContentList( LOC(@$) );
+      qacl->push_back( dynamic_cast<QuoteAttrValueContent*>($1) );
+      $$ = qacl;
+    }
+  | QuoteAttrContentList ESCAPE_QUOTE
+    {
+      QuoteAttrContentList* qacl = dynamic_cast<QuoteAttrContentList*>($1);
+      if ( qacl )
+        qacl->push_back( new QuoteAttrValueContent( LOC(@$), "\"" ) );
+      $$ = $1;
+    }
+  | QuoteAttrContentList QuoteAttrValueContent
+    {
+      QuoteAttrContentList *qacl = dynamic_cast<QuoteAttrContentList*>($1);
+      if ( qacl )
+        qacl->push_back( dynamic_cast<QuoteAttrValueContent*>($2) );
+      $$ = $1;
+    }
+;
+
+
+opt_AposAttrContentList :
+    /* empty */
+    {
+      $$ = new AposAttrContentList( LOC(@$) );
+    }
+  | AposAttrContentList
+    {
+      $$ = $1;
+    }
+;
+
+AposAttrContentList :
+    ESCAPE_APOS
+    {
+      AposAttrContentList *aacl = new AposAttrContentList( LOC(@$) );
+      aacl->push_back( new AposAttrValueContent( LOC(@$),"'") );
+      $$ = aacl;
+    }
+  | AposAttrValueContent
+    {
+      AposAttrContentList *aacl = new AposAttrContentList( LOC(@$) );
+      aacl->push_back( dynamic_cast<AposAttrValueContent*>($1) );
+      $$ = aacl;
+    }
+  | AposAttrContentList ESCAPE_APOS
+    {
+      AposAttrContentList *aacl = dynamic_cast<AposAttrContentList*>($1);
+      if (aacl)
+        aacl->push_back( new AposAttrValueContent( LOC(@$),"'") );
+      $$ = $1;
+    }
+  | AposAttrContentList AposAttrValueContent
+    {
+      AposAttrContentList *aacl = dynamic_cast<AposAttrContentList*>($1);
+      if ( aacl )
+        aacl->push_back( dynamic_cast<AposAttrValueContent*>($2) );
+      $$ = $1;
+    }
+;
+
+
+QuoteAttrValueContent :
+    QUOTE_ATTR_CONTENT
+    {
+      $$ = new QuoteAttrValueContent( LOC(@$), SYMTAB($1) );
+    }
+  | CommonContent
+    {
+      $$ = new QuoteAttrValueContent(LOC(@$), dynamic_cast<CommonContent*>($1));
+    }
+;
+
+
+AposAttrValueContent :
+    APOS_ATTR_CONTENT
+    {
+      $$ = new AposAttrValueContent( LOC(@$), SYMTAB($1) );
+    }
+  | CommonContent
+    {
+      $$ = new AposAttrValueContent(LOC(@$), dynamic_cast<CommonContent*>($1));
+    }
+;
+
+
+DirElemContent :
+    DirectConstructor
+    {
+      $$ = new DirElemContent( LOC(@$), $1 );
+    }
+  | ELEMENT_CONTENT
+    {
+      $$ = new DirElemContent( LOC(@$), SYMTAB($1) );
+    }
+  | CDataSection
+    {
+      rchandle<CDataSection> cdata_h = dynamic_cast<CDataSection*>($1);
+      $$ = new DirElemContent( LOC(@$), cdata_h );
+    }
+  | CommonContent
+    {
+      rchandle<CommonContent> cont_h = dynamic_cast<CommonContent*>($1);
+      $$ = new DirElemContent( LOC(@$), cont_h );
+    }
+;
+
+
+CommonContent :
+    CHAR_REF_LITERAL
+    {
+      $$ = new CommonContent(LOC(@$), ParseConstants::cont_charref, SYMTAB($1));
+    }
+  | DOUBLE_LBRACE
+    {
+      $$ = new CommonContent(LOC(@$), ParseConstants::cont_escape_lbrace);
+    }
+  | DOUBLE_RBRACE
+    {
+      $$ = new CommonContent(LOC(@$), ParseConstants::cont_escape_rbrace);
+    }
+  | LBRACE StatementsAndExpr RBRACE
+    {
+      $$ = new CommonContent(LOC(@$), new EnclosedExpr(LOC(@$), $2));
+    }
+;
+
+
+DirCommentConstructor :
+    XML_COMMENT_BEGIN XML_COMMENT_LITERAL XML_COMMENT_END /* ws: explicitXQ */
+    {
+      $$ = new DirCommentConstructor( LOC(@$), SYMTAB($2) );
+    }
+  |
+    XML_COMMENT_BEGIN XML_COMMENT_END /* ws: explicitXQ */
+    {
+      $$ = new DirCommentConstructor( LOC(@$), "" );
+    }
+;
+
+
+DirPIConstructor :
+    PI_BEGIN NCNAME PI_END          /* ws: explicitXQ */
+    {
+      $$ = new DirPIConstructor( LOC(@$), SYMTAB($2) );
+    }
+  |
+    PI_BEGIN NCNAME CHAR_LITERAL_AND_PI_END /* ws: explicitXQ */
+    {
+      $$ = new DirPIConstructor( LOC(@$), SYMTAB($2), SYMTAB($3) );
+    }
+;
+
+
+CDataSection :
+    CDATA_BEGIN CHAR_LITERAL_AND_CDATA_END /* ws: explicitXQ */
+    {
+      $$ = new CDataSection( LOC(@$),SYMTAB($2) );
+    }
+;
+
+
+ComputedConstructor :
+    CompDocConstructor
+    {
+      $$ = $1;
+    }
+  |
+    CompElemConstructor
+    {
+      $$ = $1;
+    }
+  |
+    CompAttrConstructor
+    {
+      $$ = $1;
+    }
+  |
+    CompTextConstructor
+    {
+      $$ = $1;
+    }
+  |
+    CompCommentConstructor
+    {
+      $$ = $1;
+    }
+  |
+    CompPIConstructor
+    {
+      $$ = $1;
+    }
+  |
+    CompNamespaceConstructor
+;
+
+
+
+CompDocConstructor :
+    DOCUMENT LBRACE StatementsAndExpr RBRACE
+    {
+      $$ = new CompDocConstructor( LOC(@$), $3 );
+    }
+;
+
+
+CompElemConstructor :
+    COMP_ELEMENT_QNAME_LBRACE StatementsAndOptionalExpr RBRACE
+    {
+      $$ = new CompElemConstructor(LOC(@$), new QName(LOC(@$), SYMTAB($1)), $2);
+    }
+|   ELEMENT LBRACE Expr RBRACE LBRACE StatementsAndOptionalExpr RBRACE
+    {
+      $$ = new CompElemConstructor( LOC(@$), $3, $6 );
+    }
+;
+
+// [110]
+/*
+ContentExpr :
+        Expr
+        {
+        }
+    ;
+*/
+
+
+CompAttrConstructor :
+    COMP_ATTRIBUTE_QNAME_LBRACE StatementsAndOptionalExpr RBRACE
+    {
+      $$ = new CompAttrConstructor( LOC(@$), new QName(LOC(@$), SYMTAB($1)), $2 );
+    }
+|   ATTRIBUTE LBRACE Expr RBRACE LBRACE StatementsAndOptionalExpr RBRACE
+    {
+      $$ = new CompAttrConstructor( LOC(@$), $3, $6 );
+    }
+;
+
+
+CompTextConstructor :
+    TEXT LBRACE StatementsAndExpr RBRACE
+    {
+      $$ = new CompTextConstructor( LOC(@$), $3 );
+    }
+;
+
+
+CompCommentConstructor :
+    COMMENT LBRACE StatementsAndExpr RBRACE
+    {
+      $$ = new CompCommentConstructor( LOC(@$), $3 );
+    }
+;
+
+
+CompPIConstructor :
+    COMP_PI_NCNAME_LBRACE StatementsAndOptionalExpr RBRACE
+    {
+      $$ = new CompPIConstructor(LOC(@$), SYMTAB($1), $2);
+    }
+|   PROCESSING_INSTRUCTION LBRACE Expr RBRACE LBRACE StatementsAndOptionalExpr RBRACE
+    {
+      $$ = new CompPIConstructor(LOC(@$), $3, $6);
+    }
+;
+
+
+CompNamespaceConstructor :
+    COMP_NS_NCNAME_LBRACE StatementsAndExpr RBRACE
+    {
+      $$ = new CompNamespaceConstructor(LOC(@$), SYMTAB($1), $2);
+    }
+|   NAMESPACE LBRACE Expr RBRACE LBRACE StatementsAndExpr RBRACE
+    {
+      $$ = new CompNamespaceConstructor(LOC(@$), $3, $6);
+    }
+;
+
+
+TypeDeclaration :
+    AS SequenceType
+    {
+      $$ = $2;
+    }
+;
+
+
+SequenceType :
+        ItemType %prec SEQUENCE_TYPE_REDUCE
+        {
+            $$ = new SequenceType( LOC(@$), $1, NULL );
+        }
+    |   ItemType OccurrenceIndicator
+        {
+            $$ = new SequenceType(LOC(@$), $1, dynamic_cast<OccurrenceIndicator*>($2));
+        }
+    |   EMPTY_SEQUENCE LPAR RPAR
+        {
+            $$ = new SequenceType( LOC(@$), NULL, NULL );
+        }
+    ;
+
+// [118]
+/*________________________________________________________________________
+|
+|   gn: occurrence-indicatorsXQ
+|
+|   Constraint: occurrence-indicators
+|
+|   As written, the grammar in A XQuery Grammar is ambiguous for some
+|   forms using the '+' and '*' Kleene operators. The ambiguity is
+|   resolved as follows: these operators are tightly bound to the
+|   SequenceType expression, and have higher precedence than other uses of
+|   these symbols. Any occurrence of '+' and '*', as well as '?',
+|   following a sequence type is assumed to be an occurrence indicator.
+|   That is, a "+", "*", or "?" immediately following an ItemType must be
+|   an OccurrenceIndicator. Thus, 4 treat as item() + - 5 must be
+|   interpreted as (4 treat as item()+) - 5, taking the '+' as an
+|   OccurrenceIndicator and the '-' as a subtraction operator. To force
+|   the interpretation of "+" as an addition operator (and the
+|   corresponding interpretation of the "-" as a unary minus), parentheses
+|   may be used: the form (4 treat as item()) + -5 surrounds the
+|   SequenceType expression with parentheses and leads to the desired
+|   interpretation.
+|
+|   This rule has as a consequence that certain forms which would
+|   otherwise be legal and unambiguous are not recognized: in "4 treat as
+|   item() + 5", the "+" is taken as an OccurrenceIndicator, and not as an
+|   operator, which means this is not a legal expression.
+|_________________________________________________________________________*/
+OccurrenceIndicator :
+        HOOK
+        {
+            $$ = new OccurrenceIndicator(
+                LOC(@$), ParseConstants::occurs_optionally
+            );
+        }
+    |   STAR
+        {
+            $$ = new OccurrenceIndicator(
+                LOC(@$), ParseConstants::occurs_zero_or_more
+            );
+        }
+    |   PLUS                            /* gn: occurrence-indicatorsXQ */
+        {
+            $$ = new OccurrenceIndicator(
+                LOC(@$), ParseConstants::occurs_one_or_more
+            );
+        }
+    ;
+
+// [119]
+ItemType :
+        GeneralizedAtomicType
+        {
+            $$ = $1;
+        }
+    |   KindTest
+        {
+            $$ = $1;
+        }
+    |   ITEM LPAR RPAR
+        {
+            $$ = new ItemType( LOC(@$), true );
+        }
+    |   STRUCTURED_ITEM LPAR RPAR
+        {
+            $$ = new StructuredItemType(LOC(@$));
+        }
+    |   FunctionTest
+        {
+            $$ = $1;
+        }
+    |   ParenthesizedItemType
+        {
+            $$ = $1;
+        }
+    |   JSONTest
+        {
+            $$ = $1;
+        }
+    ;
+
+TypeList:
+        SequenceType
+        {
+          TypeList* aTypeList = new TypeList(LOC (@$));
+          aTypeList->push_back(dynamic_cast<SequenceType *>($1));
+          $$ = aTypeList;
+        }
+    |   TypeList COMMA SequenceType
+        {
+          TypeList* aTypeList = dynamic_cast<TypeList *>($1);
+          aTypeList->push_back(dynamic_cast<SequenceType *>($3));
+          $$ = $1;
+        }
+;
+
+
+GeneralizedAtomicType :
+    QNAME
+    {
+      $$ = new GeneralizedAtomicType( LOC(@$), static_cast<QName*>($1) );
+    }
+;
+
+
+SimpleType :
+    QNAME
+    {
+      $$ = new SimpleType( LOC(@$), static_cast<QName*>($1) );
+    }
+;
+
+
+KindTest :
+    DocumentTest
+    {
+      $$ = $1;
+    }
+|   ElementTest
+    {
+      $$ = $1;
+    }
+|   AttributeTest
+    {
+      $$ = $1;
+    }
+|   SchemaElementTest
+    {
+      $$ = $1;
+    }
+|   SchemaAttributeTest
+    {
+      $$ = $1;
+    }
+|   PITest
+    {
+      $$ = $1;
+    }
+|   CommentTest
+    {
+      $$ = $1;
+    }
+|   TextTest
+    {
+      $$ = $1;
+    }
+|   NamespaceTest
+    {
+      $$ = $1;
+    }
+|   AnyKindTest
+    {
+      $$ = $1;
+    }
+;
+
+
+AnyKindTest :
+    NODE LPAR RPAR
+    {
+      $$ = new AnyKindTest( LOC(@$) );
+    }
+;
+
+
+DocumentTest :
+    DOCUMENT_NODE LPAR RPAR
+    {
+      $$ = new DocumentTest(LOC(@$));
+    }
+|   DOCUMENT_NODE LPAR ElementTest RPAR
+    {
+      $$ = new DocumentTest(LOC(@$), dynamic_cast<ElementTest*>($3));
+    }
+|   DOCUMENT_NODE LPAR SchemaElementTest RPAR
+    {
+      $$ = new DocumentTest(LOC(@$), dynamic_cast<SchemaElementTest*>($3));
+    }
+;
+
+
+NamespaceTest :
+    NS_NODE LPAR RPAR
+    {
+      $$ = new NamespaceTest(LOC(@$));
+    }
+
+
+TextTest :
+    TEXT LPAR RPAR
+    {
+      $$ = new TextTest(LOC(@$));
+    }
+;
+
+
+CommentTest :
+    COMMENT LPAR RPAR
+    {
+      $$ = new CommentTest(LOC(@$));
+    }
+;
+
+
+PITest :
+        PROCESSING_INSTRUCTION LPAR RPAR
+        {
+            $$ = new PITest( LOC(@$), "" );
+        }
+    |   PROCESSING_INSTRUCTION LPAR NCNAME RPAR
+        {
+            $$ = new PITest( LOC(@$), SYMTAB($3) );
+        }
+    |   PROCESSING_INSTRUCTION LPAR STRING_LITERAL RPAR
+        {
+            $$ = new PITest( LOC(@$), SYMTAB($3) );
+        }
+    ;
+
+// [127]
+AttributeTest :
+        ATTRIBUTE LPAR RPAR
+        {
+            $$ = new AttributeTest( LOC(@$), NULL, NULL );
+        }
+    |   ATTRIBUTE LPAR QNAME RPAR
+        {
+            $$ = new AttributeTest(
+                LOC(@$), static_cast<QName*>($3), NULL
+            );
+        }
+    |   ATTRIBUTE LPAR QNAME COMMA TypeName RPAR
+        {
+            $$ = new AttributeTest(
+                LOC(@$), static_cast<QName*>($3), dynamic_cast<TypeName*>($5)
+            );
+        }
+    |   ATTRIBUTE LPAR STAR RPAR
+        {
+            $$ = new AttributeTest( LOC(@$), NULL, NULL );
+        }
+    |   ATTRIBUTE LPAR STAR COMMA TypeName RPAR
+        {
+            $$ = new AttributeTest(
+                LOC(@$), NULL, dynamic_cast<TypeName*>($5)
+            );
+        }
+    ;
+
+// [129]
+SchemaAttributeTest :
+        SCHEMA_ATTRIBUTE LPAR QNAME RPAR
+        {
+            $$ = new SchemaAttributeTest( LOC(@$), static_cast<QName*>($3) );
+        }
+    ;
+
+// [131]
+ElementTest :
+        ELEMENT LPAR RPAR
+        {
+            $$ = new ElementTest( LOC(@$), NULL, NULL, true );
+        }
+    |   ELEMENT LPAR QNAME RPAR
+        {
+            $$ = new ElementTest(
+                LOC(@$), static_cast<QName*>($3), NULL, true
+            );
+        }
+    |   ELEMENT LPAR QNAME COMMA TypeName RPAR
+        {
+            $$ = new ElementTest(
+                LOC(@$),
+                static_cast<QName*>($3),
+                dynamic_cast<TypeName*>($5),
+                false
+            );
+        }
+    |   ELEMENT LPAR QNAME COMMA TypeName_WITH_HOOK RPAR
+        {
+            $$ = new ElementTest(
+                LOC(@$),
+                static_cast<QName*>($3),
+                dynamic_cast<TypeName*>($5),
+                true
+            );
+        }
+    |   ELEMENT LPAR STAR RPAR
+        {
+            $$ = new ElementTest(
+                LOC(@$), NULL, NULL, true
+            );
+        }
+    |   ELEMENT LPAR STAR COMMA TypeName RPAR
+        {
+            $$ = new ElementTest(
+                LOC(@$), NULL, dynamic_cast<TypeName*>($5), false
+            );
+        }
+    |   ELEMENT LPAR STAR COMMA TypeName_WITH_HOOK RPAR
+        {
+            $$ = new ElementTest(
+                LOC(@$), NULL, dynamic_cast<TypeName*>($5), true
+            );
+        }
+    ;
+
+// [133]
+SchemaElementTest :
+        SCHEMA_ELEMENT LPAR QNAME RPAR
+        {
+            $$ = new SchemaElementTest( LOC(@$), static_cast<QName*>($3) );
+        }
+    ;
+
+/* productions inlined */
+/* ------------------- */
+// [128] AttribNameOrWildcard ::= AttributeName | "*"
+// [130] AttributeDeclaration ::= AttributeName
+// [132] ElementNameOrWildcard ::= ElementName | "*"
+// [134] ElementDeclaration ::= ElementName
+// [135] AttributeName ::= QName
+// [136] ElementName ::= QName
+
+// [137]
+TypeName :
+        QNAME
+        {
+            $$ = new TypeName( LOC(@$), static_cast<QName*>($1) );
+        }
+    ;
+
+TypeName_WITH_HOOK :
+        QNAME HOOK
+        {
+            $$ = new TypeName( LOC(@$), static_cast<QName*>($1), true );
+        }
+    ;
+
+/* lexical rules, see xquery.l */
+/* --------------------------- */
+// [138] IntegerLiteral
+// [139] DecimalLiteral
+// [140] DoubleLiteral
+// [141] URILiteral
+
+// [142]
+StringLiteral :
+        STRING_LITERAL
+        {
+            $$ = new StringLiteral( LOC(@$), SYMTAB($1) );
+        }
+    ;
+
+/* lexical rules, see xquery.l */
+/* --------------------------- */
+// [143] PITarget
+// [144] VarName
+// [145] ValidationMode
+// [146] Digits
+// [147] PredefinedEntityRef
+// [148] CharRef
+// [149] EscapeQuot
+// [150] EscapeApos
+// [151] ElementContentChar
+// [152] QuotAttrContentChar
+// [153] AposAttrContentChar
+// [154] Comment
+// [155] CommentContents
+// [156] QName
+// [157] NCName
+// [158] S  (WS)
+// [159] Char
+
+/*_______________________________________________________________________
+ *                                                                       *
+ *  XQuery 3.0 productions                                               *
+ *  [http://www.w3.org/TR/xquery-3/]                                     *
+ *                                                                       *
+ *_______________________________________________________________________*/
+
+// [161] FunctionItemExpr
+// ------------
+FunctionItemExpr :
+    LiteralFunctionItem
+    {
+      $$ = $1;
+    }
+  | InlineFunction
+    {
+      $$ = $1;
+    }
+;
+
+
+LiteralFunctionItem :
+    QNAME HASH INTEGER_LITERAL
+    {
+      $$ = new LiteralFunctionItem(LOC (@$), dynamic_cast<QName*>($1), $3);
+    }
+;
+
+
+InlineFunction :
+    FUNCTION FunctionSig EnclosedStatementsAndOptionalExpr
+    {
+      $$ = new InlineFunction(LOC(@$),
+                              &*$2->theParams,
+                              &*$2->theReturnType,
+                              $3);
+      delete $2;
+    }
+;
+
+
+FunctionTest :
+    AnyFunctionTest
+    {
+      $$ = $1;
+    }
+  | TypedFunctionTest
+    {
+      $$ = $1;
+    }
+;
+
+
+AnyFunctionTest :
+    FUNCTION LPAR STAR RPAR
+    {
+      $$ = new AnyFunctionTest(LOC(@$));
+    }
+;
+
+
+TypedFunctionTest :
+        FUNCTION LPAR  RPAR AS SequenceType
+        {
+          $$ = new TypedFunctionTest(LOC (@$), dynamic_cast<SequenceType *>($5));
+        }
+    |   FUNCTION LPAR TypeList RPAR AS SequenceType
+        {
+          $$ = new TypedFunctionTest(LOC (@$),
+              dynamic_cast<TypeList *>($3),
+              dynamic_cast<SequenceType *>($6));
+        }
+    ;
+
+// [192] ParenthesizedItemType
+// ------------
+ParenthesizedItemType :
+        LPAR ItemType RPAR
+        {
+           $$ = $2;
+        }
+    ;
+
+
+/*_______________________________________________________________________
+ *                                                                       *
+ *  Update productions                                                   *
+ *  [http://www.w3.org/TR/xqupdate/]                                     *
+ *                                                                       *
+ *_______________________________________________________________________*/
+
+
+// [241]
+RevalidationDecl :
+        DECLARE REVALIDATION _STRICT
+        {
+            $$ = new RevalidationDecl(
+                LOC(@$), StaticContextConsts::strict_validation
+            );
+        }
+    |   DECLARE REVALIDATION LAX
+        {
+            $$ = new RevalidationDecl(
+                LOC(@$), StaticContextConsts::lax_validation
+            );
+        }
+    |   DECLARE REVALIDATION SKIP
+        {
+            $$ = new RevalidationDecl(
+                LOC(@$), StaticContextConsts::skip_validation
+            );
+        }
+    ;
+
+// [242]
+InsertExpr :
+        INSERT NODE ExprSingle INTO ExprSingle
+        {
+            $$ = new InsertExpr( LOC(@$), store::UpdateConsts::INTO, $3, $5 );
+        }
+    |   INSERT NODE ExprSingle AS FIRST INTO ExprSingle
+        {
+            $$ = new InsertExpr(
+                LOC(@$), store::UpdateConsts::AS_FIRST_INTO, $3, $7
+            );
+        }
+    |   INSERT NODE ExprSingle AS LAST INTO ExprSingle
+        {
+            $$ = new InsertExpr(
+                LOC(@$), store::UpdateConsts::AS_LAST_INTO, $3, $7
+            );
+        }
+    |   INSERT NODE ExprSingle AFTER ExprSingle
+        {
+            $$ = new InsertExpr( LOC(@$), store::UpdateConsts::AFTER, $3, $5 );
+        }
+    |   INSERT NODE ExprSingle BEFORE ExprSingle
+        {
+            $$ = new InsertExpr(
+                LOC (@$), store::UpdateConsts::BEFORE, $3, $5
+            );
+        }
+    |   INSERT NODES ExprSingle INTO ExprSingle
+        {
+            $$ = new InsertExpr( LOC(@$), store::UpdateConsts::INTO, $3, $5 );
+        }
+    |   INSERT NODES ExprSingle AS FIRST INTO ExprSingle
+        {
+            $$ = new InsertExpr(
+                LOC(@$), store::UpdateConsts::AS_FIRST_INTO, $3, $7
+            );
+        }
+    |   INSERT NODES ExprSingle AS LAST INTO ExprSingle
+        {
+            $$ = new InsertExpr(
+                  LOC(@$), store::UpdateConsts::AS_LAST_INTO, $3, $7
+            );
+        }
+    |   INSERT NODES ExprSingle AFTER ExprSingle
+        {
+            $$ = new InsertExpr(
+                LOC (@$),
+                store::UpdateConsts::AFTER, $3, $5
+            );
+        }
+    |   INSERT NODES ExprSingle BEFORE ExprSingle
+        {
+            $$ = new InsertExpr(
+                LOC (@$), store::UpdateConsts::BEFORE, $3, $5
+            );
+        }
+    ;
+
+// [243]
+DeleteExpr :
+        _DELETE NODE ExprSingle
+        {
+            $$ = new DeleteExpr( LOC (@$), $3 );
+        }
+    |
+        _DELETE NODES ExprSingle
+        {
+            $$ = new DeleteExpr( LOC (@$), $3 );
+        }
+    ;
+
+// [244]
+ReplaceExpr :
+        REPLACE NODE ExprSingle WITH ExprSingle
+        {
+            $$ = new ReplaceExpr(
+                LOC(@$), store::UpdateConsts::NODE, $3, $5
+            );
+        }
+    |   REPLACE VALUE OF NODE ExprSingle WITH ExprSingle
+        {
+            $$ = new ReplaceExpr(
+                LOC(@$), store::UpdateConsts::VALUE_OF_NODE, $5, $7
+            );
+        }
+    ;
+
+// [245] RenameExpr
+RenameExpr :
+        RENAME NODE ExprSingle AS ExprSingle
+        {
+            $$ = new RenameExpr( LOC (@$), $3, $5 );
+        }
+    ;
+
+// [246]
+// SourceExpr
+// folded
+
+// [247]
+// TargetExpr
+// folded
+
+// [248]
+// NewNameExpr
+// folded into [245] RenameExpr
+
+
+// [249] TransformExpr
+// -------------------
+TransformExpr :
+    COPY DOLLAR VarNameList MODIFY ExprSingle RETURN ExprSingle
+    {
+      CopyVarList *cvl = dynamic_cast<CopyVarList*>($3);
+      $$ = new TransformExpr( LOC(@$), cvl, $5, $7 );
+    }
+;
+
+// [249a]
+VarNameList :
+    VarNameDecl
+    {
+      CopyVarList* lList = new CopyVarList(LOC(@$));
+      lList->push_back (dynamic_cast<VarBinding*> ($1));
+      $$ = lList;
+    }
+  | VarNameList  COMMA  DOLLAR  VarNameDecl
+    {
+      CopyVarList* lList = dynamic_cast<CopyVarList*>($1);
+      VarBinding* lBinding = dynamic_cast<VarBinding*>($4);
+      lList->push_back(lBinding);
+      $$ = lList;
+    }
+;
+
+
+// [249b] VarNameElem
+// ------------------
+VarNameDecl :
+    QNAME GETS ExprSingle
+    {
+       $$ = new VarBinding(LOC(@$), static_cast<QName*>($1), $3);
+    }
+  ;
+
+/*_______________________________________________________________________
+ *                                                                       *
+ *  Try-Catch productions                                                *
+ *  [http://www.w3.org/TR/xqupdate/]                                     *
+ *                                                                       *
+ *_______________________________________________________________________*/
+
+TryExpr :
+        TRY LBRACE Expr RBRACE CatchListExpr
+        {
+            $$ = new TryExpr( LOC(@$), $3, $5 );
+        }
+    ;
+
+CatchListExpr :
+        CatchExpr
+        {
+            CatchListExpr *cle = new CatchListExpr( LOC(@$) );
+            cle->push_back( static_cast<CatchExpr*>($1) );
+            $$ = cle;
+        }
+  |     CatchListExpr CatchExpr
+        {
+            CatchListExpr *cle = dynamic_cast<CatchListExpr*>($1);
+            if ( cle )
+                cle->push_back( static_cast<CatchExpr*>($2) );
+            $$ = $1;
+        }
+  ;
+
+CatchExpr :
+    CATCH NameTestList BracedExpr
+    {
+       $$ = new CatchExpr(LOC(@$), *$2, $3);
+       delete $2;
+    }
+  ;
+
+
+BracedExpr :
+    LBRACE Expr RBRACE
+    {
+      $$ = $2;
+    }
+;
+
+
+NameTestList :
+        NameTest
+        {
+            CatchExpr::NameTestList *ntl = new CatchExpr::NameTestList;
+            ntl->push_back( static_cast<NameTest*>($1) );
+            $$ = ntl;
+        }
+    |   NameTestList VBAR NameTest
+        {
+            CatchExpr::NameTestList *ntl =
+                static_cast<CatchExpr::NameTestList*>($1);
+            ntl->push_back( static_cast<NameTest*>($3) );
+            $$ = ntl;
+        }
+    ;
+
+
+/*_______________________________________________________________________
+ *                                                                       *
+ *  Full-text productions                                                *
+ *  [http://www.w3.org/TR/xpath-full-text-10/]                           *
+ *                                                                       *
+ *_______________________________________________________________________*/
+
+FTSelection :
+        FTOr opt_FTPosFilter_list
+        {
+            $$ = new FTSelection( LOC(@$), $1, $2 );
+            delete $2;
+        }
+    ;
+
+opt_FTPosFilter_list :
+        /* empty */      %prec AT
+        {
+            $$ = NULL;
+        }
+    |   FTPosFilter_list %prec AT
+        {
+            $$ = $1;
+        }
+    ;
+
+FTPosFilter_list :
+        FTPosFilter
+        {
+            $$ = new FTSelection::pos_filter_list_t;
+            $$->push_back( dynamic_cast<FTPosFilter*>($1) );
+        }
+    |   FTPosFilter_list FTPosFilter
+        {
+            $1->push_back( dynamic_cast<FTPosFilter*>($2) );
+            $$ = $1;
+        }
+    ;
+
+FTOr :
+        FTAnd
+        {
+            $$ = $1;
+        }
+    |   FTOr FTOR FTAnd
+        {
+            $$ = new FTOr( LOC(@$), $1, $3 );
+        }
+    ;
+
+FTAnd :
+        FTMildNot
+        {
+            $$ = $1;
+        }
+    |   FTAnd FTAND FTMildNot
+        {
+            $$ = new FTAnd( LOC(@$), $1, $3 );
+        }
+    ;
+
+FTMildNot :
+        FTUnaryNot
+        {
+            $$ = $1;
+        }
+    |   FTMildNot NOT _IN FTUnaryNot
+        {
+            $$ = new FTMildNot( LOC(@$), $1, $4 );
+        }
+    ;
+
+FTUnaryNot :
+        FTPrimaryWithOptions
+        {
+            $$ = $1;
+        }
+    |   FTNOT FTPrimaryWithOptions
+        {
+            $$ = new FTUnaryNot(
+                LOC(@$), dynamic_cast<FTPrimaryWithOptions*>($2)
+            );
+        }
+    ;
+
+FTPrimaryWithOptions :
+        FTPrimary opt_FTMatchOptions opt_FTWeight
+        {
+            $$ = new FTPrimaryWithOptions(
+                LOC(@$),
+                dynamic_cast<FTPrimary*>($1),
+                dynamic_cast<FTMatchOptions*>($2),
+                dynamic_cast<FTWeight*>($3)
+            );
+        }
+    ;
+
+opt_FTMatchOptions :
+        /* empty */
+        {
+            $$ = NULL;
+        }
+    |   FTMatchOptions
+        {
+            $$ = $1;
+        }
+    ;
+
+opt_FTWeight :
+        /* empty */
+        {
+            $$ = NULL;
+        }
+    |   FTWeight
+        {
+            $$ = $1;
+        }
+    ;
+
+// [145]
+FTWeight :
+        WEIGHT LBRACE Expr RBRACE
+        {
+            $$ = new FTWeight( LOC(@$), dynamic_cast<exprnode*>($3) );
+        }
+    ;
+
+// [151]
+FTPrimary :
+        FTWords opt_FTTimes
+        {
+            $$ = new FTWordsTimes(
+                LOC(@$),
+                dynamic_cast<FTWords*>($1),
+                dynamic_cast<FTTimes*>($2)
+            );
+        }
+    |   LPAR FTSelection RPAR
+        {
+            $$ = $2;
+        }
+    |   FTExtensionSelection
+        {
+            $$ = $1;
+        }
+    ;
+
+opt_FTTimes :
+        /* empty */
+        {
+            $$ = NULL;
+        }
+    |   FTTimes
+        {
+            $$ = $1;
+        }
+    ;
+
+// [154]
+FTExtensionSelection :
+        Pragma_list LBRACE opt_FTSelection RBRACE
+        {
+            $$ = new FTExtensionSelection(
+                LOC(@$),
+                dynamic_cast<PragmaList*>($1),
+                dynamic_cast<FTSelection*>($3)
+            );
+        }
+    ;
+
+opt_FTSelection :
+        /* empty */
+        {
+            $$ = NULL;
+        }
+    |   FTSelection
+        {
+            $$ = $1;
+        }
+    ;
+
+// [152]
+FTWords :
+        FTWordsValue opt_FTAnyallOption
+        {
+            $$ = new FTWords(
+                LOC(@$),
+                dynamic_cast<FTWordsValue*>($1),
+                dynamic_cast<FTAnyallOption*>($2)
+            );
+        }
+    ;
+
+// [153]
+FTWordsValue :
+        Literal
+        {
+            $$ = new FTWordsValue(
+                LOC(@$), static_cast<StringLiteral*>($1), NULL
+            );
+        }
+    |   LBRACE Expr RBRACE
+        {
+            $$ = new FTWordsValue(
+                LOC(@$), NULL, dynamic_cast<exprnode*>($2)
+            );
+        }
+    ;
+
+opt_FTAnyallOption :
+        /* empty */
+        {
+            $$ = new FTAnyallOption( LOC(@$), ft_anyall_mode::any );
+        }
+    |   FTAnyallOption
+        {
+            $$ = $1;
+        }
+    ;
+
+// [155]
+FTAnyallOption :
+        ANY opt_word
+        {
+            $$ = new FTAnyallOption( LOC(@$), $2 );
+        }
+    |   ALL opt_words
+        {
+            $$ = new FTAnyallOption( LOC(@$), $2 );
+        }
+    |   PHRASE
+        {
+            $$ = new FTAnyallOption( LOC(@$), ft_anyall_mode::phrase );
+        }
+    ;
+
+opt_word :
+        /* empty */
+        {
+            $$ = ft_anyall_mode::any;
+        }
+    |   WORD
+        {
+            $$ = ft_anyall_mode::any_word;
+        }
+    ;
+
+opt_words :
+        /* empty */
+        {
+            $$ = ft_anyall_mode::all;
+        }
+    |   WORDS
+        {
+            $$ = ft_anyall_mode::all_words;
+        }
+    ;
+
+// [158]
+FTPosFilter :
+        FTOrder
+        {
+            $$ = $1;
+        }
+    |   FTWindow
+        {
+            $$ = $1;
+        }
+    |   FTDistance
+        {
+            $$ = $1;
+        }
+    |   FTScope
+        {
+            $$ = $1;
+        }
+    |   FTContent
+        {
+            $$ = $1;
+        }
+    ;
+
+// [159]
+FTOrder :
+        ORDERED
+        {
+            $$ = new FTOrder( LOC(@$) );
+        }
+    ;
+
+// [160]
+FTWindow :
+        WINDOW AdditiveExpr FTUnit
+        {
+            $$ = new FTWindow(
+                LOC(@$),
+                static_cast<AdditiveExpr*>($2),
+                static_cast<FTUnit*>($3)
+            );
+        }
+    ;
+
+// [161]
+FTDistance :
+        DISTANCE FTRange FTUnit
+        {
+            $$ = new FTDistance(
+                LOC(@$),
+                dynamic_cast<FTRange*>($2),
+                dynamic_cast<FTUnit*>($3)
+            );
+        }
+    ;
+
+// [162]
+FTUnit :
+        WORDS
+        {
+            $$ = new FTUnit( LOC(@$), ft_unit::words );
+        }
+    |   SENTENCES
+        {
+            $$ = new FTUnit( LOC(@$), ft_unit::sentences );
+        }
+    |   PARAGRAPHS
+        {
+            $$ = new FTUnit( LOC(@$), ft_unit::paragraphs );
+        }
+    ;
+
+// [166]
+FTMatchOptions :
+        USING FTMatchOption
+        {
+            FTMatchOptions *mo = new FTMatchOptions( LOC(@$) );
+            mo->push_back( dynamic_cast<FTMatchOption*>($2) );
+            $$ = mo;
+        }
+    |   FTMatchOptions USING FTMatchOption
+        {
+            FTMatchOptions *mo = dynamic_cast<FTMatchOptions*>($1);
+            mo->push_back( dynamic_cast<FTMatchOption*>($3) );
+            $$ = $1;
+        }
+    ;
+
+// [167]
+FTMatchOption :
+        FTCaseOption
+        {
+            $$ = $1;
+        }
+    |   FTDiacriticsOption
+        {
+            $$ = $1;
+        }
+    |   FTExtensionOption
+        {
+            $$ = $1;
+        }
+    |   FTLanguageOption
+        {
+            $$ = $1;
+        }
+    |   FTStemOption
+        {
+            $$ = $1;
+        }
+    |   FTStopWordOption
+        {
+            $$ = $1;
+        }
+    |   FTThesaurusOption
+        {
+            $$ = $1;
+        }
+    |   FTWildCardOption
+        {
+            $$ = $1;
+        }
+    ;
+
+// [168]
+FTCaseOption :
+        CASE SENSITIVE
+        {
+            $$ = new FTCaseOption( LOC(@$), ft_case_mode::sensitive );
+        }
+    |   CASE INSENSITIVE
+        {
+            $$ = new FTCaseOption( LOC(@$), ft_case_mode::insensitive );
+        }
+    |   LOWERCASE
+        {
+            $$ = new FTCaseOption( LOC(@$), ft_case_mode::lower );
+        }
+    |   UPPERCASE
+        {
+            $$ = new FTCaseOption( LOC(@$), ft_case_mode::upper );
+        }
+    ;
+
+// [169]
+FTDiacriticsOption :
+        DIACRITICS SENSITIVE
+        {
+            $$ = new FTDiacriticsOption(
+                LOC(@$), ft_diacritics_mode::sensitive
+            );
+        }
+    |   DIACRITICS INSENSITIVE
+        {
+            $$ = new FTDiacriticsOption(
+                LOC(@$), ft_diacritics_mode::insensitive
+            );
+        }
+    ;
+
+// [178]
+FTExtensionOption :
+        OPTION QNAME STRING_LITERAL
+        {
+            $$ = new FTExtensionOption(
+                LOC(@$), static_cast<QName*>($2), SYMTAB($3)
+            );
+        }
+    ;
+
+// [170]
+FTStemOption :
+        STEMMING
+        {
+            $$ = new FTStemOption( LOC(@$), ft_stem_mode::stemming );
+        }
+    |   NO STEMMING
+        {
+            $$ = new FTStemOption( LOC(@$), ft_stem_mode::no_stemming );
+        }
+    ;
+
+// [171]
+FTThesaurusOption :
+        THESAURUS FTThesaurusID_or_default
+        {
+            FTThesaurusOption::thesaurus_id_list_t *til = NULL;
+            if ( $2 ) {
+                til = new FTThesaurusOption::thesaurus_id_list_t;
+                til->push_back( dynamic_cast<FTThesaurusID*>($2) );
+            }
+            $$ = new FTThesaurusOption( LOC(@$), til, !til );
+            delete til;
+        }
+    |   THESAURUS LPAR FTThesaurusID_or_default opt_FTThesaurus_list RPAR
+        {
+            FTThesaurusOption::thesaurus_id_list_t *til = $4;
+            if ( $3 ) {
+                if ( !til )
+                    til = new FTThesaurusOption::thesaurus_id_list_t;
+                til->push_back( dynamic_cast<FTThesaurusID*>($3) );
+            }
+            $$ = new FTThesaurusOption( LOC(@$), til, !$3 );
+            delete til;
+        }
+    |   NO THESAURUS
+        {
+            $$ = new FTThesaurusOption( LOC(@$), NULL, false, true );
+        }
+    ;
+
+FTThesaurusID_or_default :
+        FTThesaurusID
+        {
+            $$ = $1;
+        }
+    |   DEFAULT
+        {
+            $$ = NULL;
+        }
+    ;
+
+opt_FTThesaurus_list :
+        /* empty */
+        {
+            $$ = NULL;
+        }
+    |   COMMA FTThesaurus_list
+        {
+            $$ = $2;
+        }
+    ;
+
+FTThesaurus_list :
+        FTThesaurusID
+        {
+            $$ = new FTThesaurusOption::thesaurus_id_list_t;
+            $$->push_back( dynamic_cast<FTThesaurusID*>($1) );
+        }
+    |   FTThesaurus_list COMMA FTThesaurusID
+        {
+            $1->push_back( dynamic_cast<FTThesaurusID*>($3) );
+            $$ = $1;
+        }
+    ;
+
+// [172]
+FTThesaurusID :
+        AT STRING_LITERAL opt_relationship opt_levels
+        {
+            $$ = new FTThesaurusID(
+                LOC(@$), SYMTAB($2), SYMTAB($3), dynamic_cast<FTRange*>($4)
+            );
+        }
+    ;
+
+opt_relationship :
+        /* empty */
+        {
+            $$ = 0;
+        }
+    |   RELATIONSHIP STRING_LITERAL
+        {
+            $$ = $2;
+        }
+    ;
+
+opt_levels :
+        /* empty */ %prec AT
+        {
+            $$ = NULL;
+        }
+    |   FTRange LEVELS
+        {
+            $$ = $1;
+        }
+    ;
+
+// [173]
+FTStopWordOption :
+        STOP WORDS FTStopWords opt_FTStopWordsInclExcl_list
+        {
+            $$ = new FTStopWordOption(
+                LOC(@$),
+                dynamic_cast<FTStopWords*>($3), $4,
+                ft_stop_words_mode::with
+            );
+            delete $4;
+        }
+    |   STOP WORDS DEFAULT opt_FTStopWordsInclExcl_list
+        {
+            $$ = new FTStopWordOption(
+                LOC(@$), NULL, $4, ft_stop_words_mode::with_default
+            );
+            delete $4;
+        }
+    |   NO STOP WORDS
+        {
+            $$ = new FTStopWordOption(
+                LOC(@$), NULL, NULL, ft_stop_words_mode::without
+            );
+        }
+    ;
+
+// [174]
+FTStopWords :
+        AT STRING_LITERAL
+        {
+            $$ = new FTStopWords( LOC(@$), SYMTAB($2), NULL );
+        }
+    |   LPAR STRING_LITERAL_list RPAR
+        {
+            $$ = new FTStopWords( LOC(@$), "", $2 );
+        }
+    ;
+
+STRING_LITERAL_list :
+        STRING_LITERAL
+        {
+            string_list_t *sl = new string_list_t;
+            sl->push_back( SYMTAB($1) );
+            $$ = sl;
+        }
+    |   STRING_LITERAL_list COMMA STRING_LITERAL
+        {
+            if ( $1 )
+                $1->push_back( SYMTAB($3) );
+            $$ = $1;
+        }
+    ;
+
+opt_FTStopWordsInclExcl_list :
+        /* empty */
+        {
+            $$ = NULL;
+        }
+    |   FTStopWordsInclExcl_list
+        {
+            $$ = $1;
+        }
+    ;
+
+FTStopWordsInclExcl_list :
+        FTStopWordsInclExcl
+        {
+            $$ = new FTStopWordOption::incl_excl_list_t;
+            $$->push_back( dynamic_cast<FTStopWordsInclExcl*>($1) );
+        }
+    |   FTStopWordsInclExcl_list FTStopWordsInclExcl
+        {
+            FTStopWordOption::incl_excl_list_t *iel = $1;
+            if ( !iel )
+                iel = new FTStopWordOption::incl_excl_list_t;
+            iel->push_back( dynamic_cast<FTStopWordsInclExcl*>($2) );
+            $$ = iel;
+        }
+    ;
+
+// [175]
+FTStopWordsInclExcl :
+        UNION FTStopWords
+        {
+            $$ = new FTStopWordsInclExcl(
+                LOC(@$),
+                dynamic_cast<FTStopWords*>($2),
+                ft_stop_words_unex::union_
+            );
+        }
+    |   EXCEPT FTStopWords
+        {
+            $$ = new FTStopWordsInclExcl(
+                LOC(@$),
+                dynamic_cast<FTStopWords*>($2),
+                ft_stop_words_unex::except
+            );
+        }
+    ;
+
+// [176]
+FTLanguageOption :
+        LANGUAGE STRING_LITERAL
+        {
+            $$ = new FTLanguageOption( LOC(@$), SYMTAB($2) );
+        }
+    ;
+
+// [177]
+FTWildCardOption :
+        WILDCARDS
+        {
+            $$ = new FTWildCardOption( LOC(@$), ft_wild_card_mode::with );
+        }
+    |   NO WILDCARDS
+        {
+            $$ = new FTWildCardOption( LOC(@$), ft_wild_card_mode::without );
+        }
+    ;
+
+// [165]
+FTContent :
+        AT START
+        {
+            $$ = new FTContent( LOC(@$), ft_content_mode::at_start );
+        }
+    |   AT END
+        {
+            $$ = new FTContent( LOC(@$), ft_content_mode::at_end );
+        }
+    |   ENTIRE CONTENT
+        {
+            $$ = new FTContent( LOC(@$), ft_content_mode::entire );
+        }
+    ;
+
+// [156]
+FTTimes :
+        OCCURS FTRange TIMES
+        {
+            $$ = new FTTimes( LOC(@$), dynamic_cast<FTRange*>($2) );
+        }
+    ;
+
+// [157]
+FTRange :
+        EXACTLY AdditiveExpr
+        {
+            $$ = new FTRange( LOC(@$), ft_range_mode::exactly, $2 );
+        }
+    |   AT LEAST AdditiveExpr
+        {
+            $$ = new FTRange( LOC(@$), ft_range_mode::at_least, $3 );
+        }
+    |   AT MOST AdditiveExpr
+        {
+            $$ = new FTRange( LOC(@$), ft_range_mode::at_most, $3 );
+        }
+    |   FROM AdditiveExpr TO AdditiveExpr
+        {
+            $$ = new FTRange( LOC(@$), ft_range_mode::from_to, $2, $4 );
+        }
+    ;
+
+// [163]
+FTScope :
+        SAME FTBigUnit
+        {
+            $$ = new FTScope(
+                LOC(@$),
+                ft_scope::same,
+                dynamic_cast<FTBigUnit*>($2)
+            );
+        }
+    |   DIFFERENT FTBigUnit
+        {
+            $$ = new FTScope(
+                LOC(@$),
+                ft_scope::different,
+                dynamic_cast<FTBigUnit*>($2)
+            );
+        }
+    ;
+
+// [164]
+FTBigUnit :
+        SENTENCE
+        {
+            $$ = new FTBigUnit( LOC(@$), ft_big_unit::sentence );
+        }
+    |   PARAGRAPH
+        {
+            $$ = new FTBigUnit( LOC(@$), ft_big_unit::paragraph );
+        }
+    ;
+
+// [179]
+FTIgnoreOption :
+        WITHOUT CONTENT UnionExpr
+        {
+            $$ = new FTIgnoreOption( LOC(@$), static_cast<UnionExpr*>($3) );
+        }
+    ;
+
+/*_______________________________________________________________________
+ *                                                                       *
+ *  JSON                                                                 *
+ *                                                                       *
+ *_______________________________________________________________________*/
+
+
+JSONArrayConstructor :
+        LBRACK RBRACK
+        {
+          $$ = new JSONArrayConstructor( LOC(@$), NULL );
+        }
+    |   LBRACK Expr RBRACK
+        {
+          $$ = new JSONArrayConstructor( LOC(@$), $2 );
+        }
+    ;
+
+JSONSimpleObjectUnion :
+        L_SIMPLE_OBJ_UNION R_SIMPLE_OBJ_UNION
+        {
+          // TODO: fill in with the correct constructor
+          $$ = new JSONObjectConstructor(LOC(@$), NULL, false);
+        }
+    |   L_SIMPLE_OBJ_UNION Expr R_SIMPLE_OBJ_UNION
+        {
+          // TODO: fill in with the correct constructor
+          $$ = new JSONObjectConstructor(LOC(@$), $2, false);
+        }
+    ;
+
+JSONAccumulatorObjectUnion :
+        L_ACCUMULATOR_OBJ_UNION R_ACCUMULATOR_OBJ_UNION
+        {
+          // TODO: fill in with the correct constructor
+          $$ = new JSONObjectConstructor(LOC(@$), NULL, true);
+        }
+    |   L_ACCUMULATOR_OBJ_UNION Expr R_ACCUMULATOR_OBJ_UNION
+        {
+          // TODO: fill in with the correct constructor
+          $$ = new JSONObjectConstructor(LOC(@$), $2, true);
+        }
+    ;
+
+
+JSONObjectConstructor :
+        LBRACE JSONPairList RBRACE
+        {
+          $$ = new JSONDirectObjectConstructor(LOC(@$),
+                                               dynamic_cast<JSONPairList*>($2));
+        }
+    ;
+
+JSONPairList :
+        ExprSingle COLON ExprSingle
+        {
+          JSONPairList* jpl = new JSONPairList(LOC(@$));
+          jpl->push_back(new JSONPairConstructor(LOC(@$), $1, $3));
+          $$ = jpl;
+        }
+    |   JSONPairList COMMA ExprSingle COLON ExprSingle
+        {
+          JSONPairList* jpl = dynamic_cast<JSONPairList*>($1);
+          assert(jpl);
+          jpl->push_back(new JSONPairConstructor(LOC(@$), $3, $5));
+          $$ = jpl;
+        }
+    ;
+
+JSONInsertExpr :
+        INSERT JSON ExprSingle INTO ExprSingle
+        {
+          $$ = new JSONObjectInsertExpr(LOC(@$),
+                                        $3,
+                                        $5);
+        }
+    |   INSERT JSON JSONPairList INTO ExprSingle
+        {
+          JSONPairList* jpl = dynamic_cast<JSONPairList*>($3);
+          $$ = new JSONObjectInsertExpr(
+              LOC(@$),
+              new JSONDirectObjectConstructor(
+                  LOC(@$),
+                  jpl),
+              $5);
+        }
+    |   INSERT JSON ExprSingle INTO ExprSingle AT POSITION ExprSingle
+        {
+          $$ = new JSONArrayInsertExpr(LOC(@$), $3, $5, $8);
+        }
+    ;
+
+JSONAppendExpr :
+        APPEND JSON ExprSingle INTO ExprSingle
+        {
+          $$ = new JSONArrayAppendExpr(LOC(@$), $3, $5);
+        }
+    ;
+
+JSONDeleteExpr :
+        _DELETE JSON FilterExpr
+        {
+          rchandle<DynamicFunctionInvocation> lDynamicFunctionInvocation =
+          dynamic_cast<DynamicFunctionInvocation*>($3);
+
+          if (lDynamicFunctionInvocation == NULL)
+          {
+            error(@3, "An object invocation is expected. A filter was found instead.");
+            YYERROR;
+          }
+
+          if (lDynamicFunctionInvocation->getArgList()->size() != 1)
+          {
+            error(@3, "An object invocation with exactly one argument is expected. Zero or more than one argument were found.");
+            YYERROR;
+          }
+
+          $$ = new JSONDeleteExpr(
+                LOC(@$),
+                lDynamicFunctionInvocation->getPrimaryExpr(),
+                lDynamicFunctionInvocation->getArgList()->operator[](0));
+        }
+    ;
+
+JSONRenameExpr :
+        RENAME JSON FilterExpr AS ExprSingle
+        {
+          rchandle<DynamicFunctionInvocation> lDynamicFunctionInvocation =
+          dynamic_cast<DynamicFunctionInvocation*>($3);
+
+          if(lDynamicFunctionInvocation == NULL)
+          {
+            error(@3, "An object invocation is expected. A filter was found instead.");
+            delete $5;
+            YYERROR;
+          }
+
+          if (lDynamicFunctionInvocation->getArgList()->size() != 1)
+          {
+            error(@3, "An object invocation with exactly one argument is expected. Zero or more than one argument were found.");
+            delete $5;
+            YYERROR;
+          }
+
+          $$ = new JSONRenameExpr(
+                LOC(@$),
+                lDynamicFunctionInvocation->getPrimaryExpr(),
+                lDynamicFunctionInvocation->getArgList()->operator[](0),
+                $5);
+        }
+    ;
+
+JSONReplaceExpr :
+        REPLACE JSON VALUE OF FilterExpr WITH ExprSingle
+        {
+          rchandle<DynamicFunctionInvocation> lDynamicFunctionInvocation =
+          dynamic_cast<DynamicFunctionInvocation*>($5);
+
+          if(lDynamicFunctionInvocation == NULL)
+          {
+            error(@3, "An object invocation is expected. A filter was found instead.");
+            delete $7;
+            YYERROR;
+          }
+
+          if (lDynamicFunctionInvocation->getArgList()->size() != 1)
+          {
+            error(@3, "An object invocation with exactly one argument is expected. Zero or more than one argument were found.");
+            delete $7;
+            YYERROR;
+          }
+
+          $$ = new JSONReplaceExpr(
+                LOC(@$),
+                lDynamicFunctionInvocation->getPrimaryExpr(),
+                lDynamicFunctionInvocation->getArgList()->operator[](0),
+                $7);
+        }
+    ;
+
+JSONTest :
+        JSONItemTest
+        {
+          $$ = $1;
+        }
+    |   JSONObjectTest
+        {
+          $$ = $1;
+        }
+    |   JSONArrayTest
+        {
+          $$ = $1;
+        }
+;
+
+JSONItemTest :
+        JSON_ITEM LPAR RPAR
+        {
+          $$ = new JSON_Test(LOC(@$), store::StoreConsts::jsonItem);
+        }
+;
+
+JSONObjectTest :
+        OBJECT LPAR RPAR
+        {
+          $$ = new JSON_Test(LOC(@$), store::StoreConsts::jsonObject);
+        }
+;
+
+JSONArrayTest :
+        ARRAY LPAR RPAR
+        {
+          $$ = new JSON_Test(LOC(@$), store::StoreConsts::jsonArray);
+        }
+;
+
+/*_______________________________________________________________________
+ *                                                                       *
+ *  All QNames                                                           *
+ *                                                                       *
+ *_______________________________________________________________________*/
+URI_LITERAL :
+        STRING_LITERAL
+    ;
+
+NCNAME :
+        NCNAME_SVAL
+    |   QNAME
+        {
+          auto_ptr<QName> lQName( static_cast<QName*>($1) );
+          zstring const &tmp = lQName->get_qname();
+          if ( tmp.find (':') != string::npos ) {
+            error(@1, "A NCName is expected, found a QName");
+            YYERROR;
+          }
+          $$ = SYMTAB_PUT(tmp.c_str());
+        }
+    ;
+
+QNAME :
+        FUNCTION_NAME
+    |   ATTRIBUTE               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("attribute"))); }
+    |   COMMENT                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("comment"))); }
+    |   DOCUMENT_NODE           { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("document-node"))); }
+    |   NS_NODE                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("namespace-node"))); }
+    |   ELEMENT                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("element"))); }
+    |   ITEM                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("item"))); }
+    |   IF                      { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("if"))); }
+    |   NODE                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("node"))); }
+    |   PROCESSING_INSTRUCTION  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("processing-instruction"))); }
+    |   SCHEMA_ATTRIBUTE        { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("schema-attribute"))); }
+    |   SCHEMA_ELEMENT          { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("schema-element"))); }
+    |   TEXT                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("text"))); }
+    |   TYPESWITCH              { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("typeswitch"))); }
+    |   SWITCH                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("switch"))); }
+    |   EMPTY_SEQUENCE          { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("empty-sequence"))); }
+    |   WHILE                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("while"))); }
+    ;
+
+FUNCTION_NAME :
+        EQNAME
+    |   QNAME_SVAL              { $$ = new QName(LOC(@$), SYMTAB($1)); }
+    |   XQUERY                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("xquery"))); }
+    |   _EMPTY                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("empty"))); }
+    |   BOUNDARY_SPACE          { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("boundary-space"))); }
+    |   FT_OPTION               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("ft-option"))); }
+    |   BASE_URI                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("base-uri"))); }
+    |   LAX                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("lax"))); }
+    |   _STRICT                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("strict"))); }
+    |   IDIV                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("idiv"))); }
+    |   DOCUMENT                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("document"))); }
+    |   FTNOT                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("ftnot"))); }
+    |   NOT                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("not"))); }
+    |   SENSITIVE               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("sensitive"))); }
+    |   INSENSITIVE             { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("insensitive"))); }
+    |   DIACRITICS              { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("diacritics"))); }
+    |   WITHOUT                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("without"))); }
+    |   STEMMING                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("stemming"))); }
+    |   THESAURUS               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("thesaurus"))); }
+    |   STOP                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("stop"))); }
+    |   WILDCARDS               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("wildcards"))); }
+    |   ENTIRE                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("entire"))); }
+    |   CONTENT                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("content"))); }
+    |   WORD                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("word"))); }
+    |   START                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("start"))); }
+    |   END                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("end"))); }
+    |   MOST                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("most"))); }
+    |   SKIP                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("skip"))); }
+    |   COPY                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("copy"))); }
+    |   GENERAL                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("general"))); }
+    |   VALUE                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("value"))); }
+    |   VAL_EQ                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("eq"))); }
+    |   VAL_NE                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("ne"))); }
+    |   VAL_LT                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("lt"))); }
+    |   VAL_LE                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("le"))); }
+    |   VAL_GT                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("gt"))); }
+    |   VAL_GE                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("ge"))); }
+    |   AT                      { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("at"))); }
+    |   CONTEXT                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("context"))); }
+    |   VARIABLE                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("variable"))); }
+    |   RETURN                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("return"))); }
+    |   FOR                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("for"))); }
+    |   ALLOWING                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("allowing"))); }
+    |   SLIDING                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("sliding"))); }
+    |   TUMBLING                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("tumbling"))); }
+    |   PREVIOUS                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("previous"))); }
+    |   NEXT                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("next"))); }
+    |   ONLY                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("only"))); }
+    |   WHEN                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("when"))); }
+    |   COUNT                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("count"))); }
+    |   _IN                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("in"))); }
+    |   LET                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("let"))); }
+    |   WHERE                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("where"))); }
+    |   BY                      { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("by"))); }
+    |   GROUP                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("group"))); }
+    |   ORDER                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("order"))); }
+    |   STABLE                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("stable"))); }
+    |   ASCENDING               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("ascending"))); }
+    |   DESCENDING              { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("descending"))); }
+    |   GREATEST                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("greatest"))); }
+    |   LEAST                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("least"))); }
+    |   COLLATION               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("collation"))); }
+    |   SOME                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("some"))); }
+    |   EVERY                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("every"))); }
+    |   SATISFIES               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("satisfies"))); }
+    |   CASE                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("case"))); }
+    |   AS                      { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("as"))); }
+    |   THEN                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("then"))); }
+    |   ELSE                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("else"))); }
+    |   OR                      { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("or"))); }
+    |   AND                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("and"))); }
+    |   INSTANCE                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("instance"))); }
+    |   OF                      { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("of"))); }
+    |   CASTABLE                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("castable"))); }
+    |   TO                      { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("to"))); }
+    |   DIV                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("div"))); }
+    |   MOD                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("mod"))); }
+    |   UNION                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("union"))); }
+    |   INTERSECT               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("intersect"))); }
+    |   EXCEPT                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("except"))); }
+    |   VALIDATE                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("validate"))); }
+    |   TYPE                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("type"))); }
+    |   CAST                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("cast"))); }
+    |   TREAT                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("treat"))); }
+    |   IS                      { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("is"))); }
+    |   PRESERVE                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("preserve"))); }
+    |   STRIP                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("strip"))); }
+    |   NAMESPACE               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("namespace"))); }
+    |   EXTERNAL                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("external"))); }
+    |   ENCODING                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("encoding"))); }
+    |   NO_PRESERVE             { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("no-preserve"))); }
+    |   INHERIT                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("inherit"))); }
+    |   NO_INHERIT              { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("no-inherit"))); }
+    |   DECLARE                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("declare"))); }
+    |   CONSTRUCTION            { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("construction"))); }
+    |   ORDERING                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("ordering"))); }
+    |   DEFAULT                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("default"))); }
+    |   COPY_NAMESPACES         { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("copy-namespaces"))); }
+    |   OPTION                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("option"))); }
+    |   VERSION                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("version"))); }
+    |   IMPORT                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("import"))); }
+    |   SCHEMA                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("schema"))); }
+    |   MODULE                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("module"))); }
+    |   FUNCTION                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("function"))); }
+    |   SCORE                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("score"))); }
+    |   CONTAINS                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("contains"))); }
+    |   WEIGHT                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("weight"))); }
+    |   WINDOW                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("window"))); }
+    |   DISTANCE                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("distance"))); }
+    |   OCCURS                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("occurs"))); }
+    |   TIMES                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("times"))); }
+    |   SAME                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("same"))); }
+    |   DIFFERENT               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("different"))); }
+    |   LOWERCASE               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("lowercase"))); }
+    |   UPPERCASE               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("uppercase"))); }
+    |   RELATIONSHIP            { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("relationship"))); }
+    |   LEVELS                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("levels"))); }
+    |   LANGUAGE                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("language"))); }
+    |   ANY                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("any"))); }
+    |   ALL                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("all"))); }
+    |   EXACTLY                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("exactly"))); }
+    |   FROM                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("from"))); }
+    |   WORDS                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("words"))); }
+    |   SENTENCES               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("sentences"))); }
+    |   SENTENCE                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("sentence"))); }
+    |   PHRASE                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("phrase"))); }
+    |   PARAGRAPH               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("paragraph"))); }
+    |   PARAGRAPHS              { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("paragraphs"))); }
+    |   REPLACE                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("replace"))); }
+    |   MODIFY                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("modify"))); }
+    |   FIRST                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("first"))); }
+    |   INSERT                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("insert"))); }
+    |   BEFORE                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("before"))); }
+    |   AFTER                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("after"))); }
+    |   REVALIDATION            { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("revalidation"))); }
+    |   WITH                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("with"))); }
+    |   NODES                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("nodes"))); }
+    |   RENAME                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("rename"))); }
+    |   LAST                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("last"))); }
+    |   _DELETE                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("delete"))); }
+    |   INTO                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("into"))); }
+    |   SIMPLE                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("simple"))); }
+    |   SEQUENTIAL              { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("sequential"))); }
+    |   UPDATING                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("updating"))); }
+    |   ORDERED                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("ordered"))); }
+    |   UNORDERED               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("unordered"))); }
+    |   RETURNING               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("returning"))); }
+    |   EXIT                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("exit"))); }
+    |   LOOP                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("loop"))); }
+    |   BREAK                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("break"))); }
+    |   CONTINUE                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("continue"))); }
+    |   TRY                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("try"))); }
+    |   CATCH                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("catch"))); }
+    |   USING                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("using"))); }
+    |   SET                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("set"))); }
+    |   INDEX                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("index"))); }
+    |   UNIQUE                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("unique"))); }
+    |   NON                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("non"))); }
+    |   ON                      { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("on"))); }
+    |   RANGE                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("range"))); }
+    |   EQUALITY                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("equality"))); }
+    |   MANUALLY                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("manually"))); }
+    |   AUTOMATICALLY           { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("automatically"))); }
+    |   MAINTAINED              { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("maintained"))); }
+    |   DECIMAL_FORMAT          { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("decimal-format"))); }
+    |   DECIMAL_SEPARATOR       { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("decimal-separator"))); }
+    |   GROUPING_SEPARATOR      { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("grouping-separator"))); }
+    |   INFINITY_VALUE          { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("infinity"))); }
+    |   MINUS_SIGN              { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("minus-sign"))); }
+    |   NaN                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("NaN"))); }
+    |   PERCENT                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("percent"))); }
+    |   PER_MILLE               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("per-mille"))); }
+    |   ZERO_DIGIT              { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("zero-digit"))); }
+    |   DIGIT                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("digit"))); }
+    |   PATTERN_SEPARATOR       { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("pattern-separator"))); }
+    |   COLLECTION              { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("collection"))); }
+    |   CONSTOPT                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("const"))); }
+    |   APPEND_ONLY             { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("append-only"))); }
+    |   QUEUE                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("queue"))); }
+    |   MUTABLE                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("mutable"))); }
+    |   READ_ONLY               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("read-only"))); }
+    |   INTEGRITY               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("integrity"))); }
+    |   CONSTRAINT              { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("constraint"))); }
+    |   CHECK                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("check"))); }
+    |   KEY                     { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("key"))); }
+    |   FOREACH                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("foreach"))); }
+    |   FOREIGN                 { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("foreign"))); }
+    |   KEYS                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("keys"))); }
+    |   ANCESTOR                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("ancestor"))); }
+    |   CHILD                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("child"))); }
+    |   DESCENDANT              { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("descendant"))); }
+    |   PARENT                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("parent"))); }
+    |   PRECEDING               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("preceding"))); }
+    |   SELF                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("self"))); }
+    |   FOLLOWING               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("following"))); }
+    |   ANCESTOR_OR_SELF        { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("ancestor-or-self"))); }
+    |   DESCENDANT_OR_SELF      { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("descendant-or-self"))); }
+    |   FOLLOWING_SIBLING       { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("following-sibling"))); }
+    |   PRECEDING_SIBLING       { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("preceding-sibling"))); }
+    |   JSON                    { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("json"))); }
+    |   APPEND                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("append"))); }
+    |   POSITION                { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("position"))); }
+    |   JSON_ITEM               { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("json-item"))); }
+    |   ARRAY                   { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("array"))); }
+    |   OBJECT                  { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("object"))); }
+    |   STRUCTURED_ITEM         { $$ = new QName(LOC(@$), SYMTAB(SYMTAB_PUT("structured-item"))); }
+    ;
+
+// [196]
+EQNAME :
+    EQNAME_SVAL { $$ = new QName(LOC(@$), SYMTAB($1), true); }
+    ;
+
+
+%%
+
+namespace zorba {
+
+/*
+ *  The error member function registers the errors to the driver.
+ */
+void jsoniq_parser::error(zorba::jsoniq_parser::location_type const& loc, string const& msg)
+{
+  if (driver.parserError != NULL)
+  {
+    driver.set_expr(new ParseErrorNode(driver.parserError->loc, driver.parserError->err_code, driver.parserError->msg));
+  }
+  else
+  {
+    ParseErrorNode* prevErr = dynamic_cast<ParseErrorNode*>(driver.get_expr());
+
+    if (prevErr != NULL)
+    {
+      // Error message heuristics: if the current error message has the "(missing comma "," between expressions?)" text,
+      // and the old message has a "','" text, then replace the old message with the new one. Unfortunately this 
+      // makes the parser error messages harder to internationalize.
+      if (msg.find("(missing comma \",\" between expressions?)") != string::npos
+          &&
+          prevErr->msg.find(zstring("\",\"")) == zstring::npos)
+        return;
+    }
+
+    // Replace the first occurrence of "unexpected "'QName'"" with "unexpected qualified name %actual_qname%"
+    string message = msg;
+    int pos;
+    std::string unexpected_qname = "unexpected \"'QName'\"";
+    if ((pos = message.find(unexpected_qname)) != -1)
+      message = message.substr(0, pos) + "unexpected qualified name \"" + driver.symtab.get_last_qname() + "\"" + message.substr(pos+unexpected_qname.length());
+
+    // remove the double quoting "''" from every token description
+    while ((pos = message.find("\"'")) != -1 || (pos = message.find("'\"")) != -1)
+      message.replace(pos, 2, "\"");
+    driver.set_expr(new ParseErrorNode(driver.createQueryLoc(loc), err::XPST0003, message));
+  }
+}
+
+} // namespace zorba

=== added file 'src/compiler/parser/jsoniq_scanner.h'
--- src/compiler/parser/jsoniq_scanner.h	1970-01-01 00:00:00 +0000
+++ src/compiler/parser/jsoniq_scanner.h	2013-03-16 01:56:23 +0000
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2006-2008 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+#ifndef ZORBA_JSONIQ_SCANNER_H
+#define ZORBA_JSONIQ_SCANNER_H
+
+// Flex expects the signature of yylex to be defined in the macro YY_DECL, and
+ // the C++ parser expects it to be declared. We can factor both as follows.
+
+#ifndef YY_DECL
+
+#define YY_DECL                                              \
+  zorba::jsoniq_parser::token_type                         \
+  zorba::jsoniq_scanner::lex(                              \
+    zorba::jsoniq_parser::semantic_type* yylval,           \
+    zorba::jsoniq_parser::location_type* yylloc            \
+    )
+#endif
+
+#ifndef YY_USER_ACTION
+#define YY_USER_ACTION    YY_USER_ACTION_func(yylloc);
+#endif
+
+#ifndef __FLEX_LEXER_H
+# undef yyFlexLexer
+# define yyFlexLexer ZorbaJSONiqFlexLexer
+
+# if defined(ZORBA_HAVE_FLEXLEXER_H) && defined(FLEX_FILES_REGENERATED)
+#   include <FlexLexer.h>
+# else
+#   include "compiler/parser/FlexLexer.h"
+#  endif
+#endif
+
+
+#ifdef __GNUC__
+  // disable a warning in location.hh which comes with bison
+  // position.hh:141: warning: suggest parentheses around && within ||
+#  pragma GCC diagnostic ignored "-Wparentheses"
+#endif
+
+#include "compiler/parser/jsoniq_parser.hpp"
+
+#ifdef __GNUC__
+#  pragma GCC diagnostic warning "-Wparentheses"
+#endif
+
+
+namespace zorba {
+
+class jsoniq_driver;
+
+class jsoniq_scanner : public ZorbaJSONiqFlexLexer
+{
+protected:
+  jsoniq_driver* theDriver;
+  int cond_stk_depth;
+  std::string yy_comp_constr_qname; // used by the scanner to temporarely save the qname of a computed constructor expression
+
+public:
+  /** Create a new scanner object. The streams arg_yyin and arg_yyout default
+   * to cin and cout, but that assignment is only made when initializing in
+   * yylex(). */
+  jsoniq_scanner(jsoniq_driver* aDriver, std::istream* arg_yyin = 0, std::ostream* arg_yyout = 0);
+
+  /** Required for virtual functions */
+  virtual ~jsoniq_scanner();
+
+  /** This is the main lexing function. It is generated by flex according to
+   * the macro declaration YY_DECL above. The generated bison parser then
+   * calls this virtual function to fetch new tokens. */
+  virtual
+  zorba::jsoniq_parser::token_type lex (
+    zorba::jsoniq_parser::semantic_type* yylval,
+    zorba::jsoniq_parser::location_type* yylloc
+  );
+
+  jsoniq_driver* getDriver() const { return theDriver; }
+  void set_yy_flex_debug(bool aBool);
+
+  int interpretAsLessThan();
+
+  void YY_USER_ACTION_func(zorba::jsoniq_parser::location_type* yylloc);
+
+protected:
+  int yy_get_start_stack_ptr(void) const;
+};
+
+}
+
+#endif // EXAMPLE_SCANNER_H
+/* vim:set et sw=2 ts=2: */

=== added file 'src/compiler/parser/jsoniq_scanner.l'
--- src/compiler/parser/jsoniq_scanner.l	1970-01-01 00:00:00 +0000
+++ src/compiler/parser/jsoniq_scanner.l	2013-03-16 01:56:23 +0000
@@ -0,0 +1,1231 @@
+/*
+ * Copyright 2006-2008 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+%{ /* _*_ C++ _*_ */
+
+
+#if defined (WIN32)
+#pragma warning(disable: 4786)
+#endif
+
+#include <zorba/diagnostic_list.h>
+
+#include "zorbatypes/schema_types.h"
+#include "common/common.h"
+
+#include <cstdlib>
+#include <errno.h>
+#include <limits.h>
+#include <iostream>
+#include <string>
+
+#include "compiler/parser/jsoniq_driver.h"
+#include "util/xml_util.h"
+
+#ifdef __GNUC__
+  // disable a warning in location.hh which comes with bison
+  // position.hh:141: warning: suggest parentheses around && within ||
+#  pragma GCC diagnostic ignored "-Wparentheses"
+#endif
+
+#include "compiler/parser/jsoniq_parser.hpp"
+
+#ifdef __GNUC__
+#  pragma GCC diagnostic warning "-Wparentheses"
+#endif
+
+#include "compiler/parser/jsoniq_scanner.h"
+
+
+typedef zorba::jsoniq_parser::token token;
+typedef zorba::jsoniq_parser::token_type token_type;
+
+  /*______________________________________________________________________
+   |
+   |  Scanner states management
+   |______________________________________________________________________*/
+
+#define PUSH_STATE(state) \
+  do { \
+    yy_push_state(state); \
+  } while(0)
+
+#define POP_STATE() \
+  do { \
+    if (yy_get_start_stack_ptr() > 0) \
+      yy_pop_state(); \
+  } while(0)
+
+#define PUSH_AND_BEGIN( pushed, new ) \
+  do { \
+    BEGIN new; \
+    PUSH_STATE(pushed); \
+  } while(0)
+
+
+  /*______________________________________________________________________
+   |
+   |  String literals processing
+   |______________________________________________________________________*/
+
+#define TRY_TOKEN_INTERNAL(ttype, put, yytext, err_func)                \
+  do {                                                                  \
+    off_t res = getDriver()->symtab.put;                                \
+    if (res < 0) {                                                      \
+      yylval->err = getDriver()->err_func(yytext, *yylloc);             \
+      return token::UNRECOGNIZED;                                       \
+    } else {                                                            \
+      yylval->sval = res;                                               \
+      return token::ttype;                                              \
+    }                                                                   \
+  } while (0)
+
+#define TRY_SVAL_TOKEN(ttype, put, yytext) \
+    TRY_TOKEN_INTERNAL(ttype, put, yytext, unrecognizedToken)
+
+#define TRY_CHARREF_LITERAL(ttype, put, yytext, yyleng) \
+  TRY_TOKEN_INTERNAL(ttype, put (yytext, yyleng), yytext, unrecognizedToken)
+
+#define TRY_STRING_LITERAL(ttype, yytext, yyleng) \
+  TRY_TOKEN_INTERNAL(ttype, put_stringlit(yytext, yyleng), yytext, invalidCharRef)
+
+#define TRY_URI_LITERAL(ttype, yytext, yyleng ) \
+  TRY_TOKEN_INTERNAL(ttype, put_uri(yytext, yyleng), yytext, unrecognizedToken)
+
+  // Returns 0 on success, non-zero on error
+int checkXmlRefs(zorba::ZorbaParserError** err, char* yytext, int yyleng, zorba::jsoniq_scanner* scanner, zorba::jsoniq_parser::location_type* yylloc)
+{
+  std::string entity;
+  const char* temp;
+  char* pos = yytext;
+
+  while (pos < yytext+yyleng)
+  {
+    if (*pos == '&')
+    {
+      pos += zorba::xml::parse_entity((const char*)pos, &entity);
+      temp = entity.c_str();
+      zorba::unicode::code_point cp = zorba::utf8::next_char(temp);
+      if (!zorba::xml::is_valid(cp))
+      {
+        *err = scanner->getDriver()->parserErr(std::string("Invalid XML v1.0 codepoint in the string literal \"") + yytext + "\"", *yylloc, zorba::err::XQST0090);
+        return 1;
+      }
+    }
+    else
+      pos++;
+  }
+
+  return 0;
+}
+
+
+#define COMP_CONSTR_ROLLBACK(invoke_yy_less)                            \
+  do {                                                                  \
+    int _STATE = YY_START;                                              \
+    POP_STATE();                                                        \
+    if (invoke_yy_less) {                                               \
+      yylloc->columns(-1 * yyleng);                                     \
+      yyless(0);                                                        \
+    }                                                                   \
+    if (yy_comp_constr_qname != "") {                                   \
+      unput(' ');                                                       \
+      for (int i=yy_comp_constr_qname.size()-1; i>=0; i--)              \
+        unput(yy_comp_constr_qname[i]);                                 \
+      yylloc->columns(-1 * (yy_comp_constr_qname.size()+1));            \
+      yy_comp_constr_qname = "";                                        \
+    }                                                                   \
+    if (_STATE == MODE_ELEM_COMP_CONSTR)                                \
+      return token::ELEMENT;                                            \
+    else if (_STATE == MODE_ATTR_COMP_CONSTR)                           \
+      return token::ATTRIBUTE;                                          \
+    else if (_STATE == MODE_PI_COMP_CONSTR)                             \
+      return token::PROCESSING_INSTRUCTION;                             \
+    else                                                                \
+      return token::NAMESPACE;                                          \
+  } while (0)
+
+/*
+  Work around an incompatibility in flex (at least versions
+  2.5.31 through 2.5.33): it generates code that does
+  not conform to C89.  See Debian bug 333231
+  <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.
+*/
+#undef yywrap
+#define yywrap() 1
+
+/*
+  By default yylex returns int, we use token_type.
+  Unfortunately yyterminate by default returns 0, which is
+  not of token_type.
+*/
+#define yyterminate() return token::_EOF
+
+
+std::string start_jsoniq_state(int);   /* forward declaration, used by YY_USER_ACTION */
+
+/*
+  The macro YY_USER_ACTION can be defined to provide an action which is
+  always executed prior to the matched rule's action.
+
+  The following paragraph suffices to track locations accurately. Each
+  time yylex is invoked, the begin position is moved onto the end
+  position. Then when a pattern is matched, the end position is advanced
+  of its width. In case it matched ends of lines, the end cursor is
+  adjusted, and each time blanks are matched, the begin cursor is moved
+  onto the end cursor to effectively ignore the blanks preceding tokens.
+  Comments would be treated equally.
+*/
+#ifdef YY_USER_ACTION
+#undef YY_USER_ACTION
+#endif
+
+#define YY_USER_ACTION \
+{ \
+  if (yy_flex_debug) { \
+    std::cerr << "<" << start_jsoniq_state(YY_START) << ">" << "\""<<yytext<<"\"" << std::endl; \
+  } \
+  \
+  int last_endl = 0;\
+  for (int i=0; i<yyleng; i++) \
+    if (yytext[i] == '\n')  \
+    { \
+      yylloc->lines(1);\
+      last_endl = i+1;\
+    }\
+  \
+  yylloc->step();\
+  yylloc->columns(yyleng - last_endl);\
+}
+
+
+%}
+
+
+  /*______________________________________________________________________
+   *                                                                      *
+   *  Scanner options                                                     *
+   *______________________________________________________________________*/
+
+%option outfile="jsoniq_scanner.yy.cpp"
+%option noyywrap
+%option batch
+%option debug
+%option stack
+%option nounistd
+%option c++
+%option prefix="ZorbaJSONiq"
+
+  /*_____________________________________________________________________
+   |
+   |  UTF-8 characters definitions
+   |______________________________________________________________________*/
+
+
+  /* UTF8_1Byte    [\x00-\x7F] -- not used anywhere */
+
+BaseChar_1Byte  ([\x41-\x5A]|[\x61-\x7A])
+UTF8_2Bytes     ([\xC0-\xDF][\x80-\xBF])
+UTF8_3Bytes     ([\xE0-\xEF][\x80-\xBF][\x80-\xBF])
+UTF8_4Bytes     ([\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF])
+
+UTF8_MultiByte  ({UTF8_2Bytes}|{UTF8_3Bytes}|{UTF8_4Bytes})
+
+
+  /*_____________________________________________________________________
+   |
+   |  Whitespace definitions
+   |______________________________________________________________________*/
+CommentChars    ([^:]|":"+[^:)])*":)"
+Comment         "(:"{CommentChars}
+blank           [ \t]
+WS              [ \t\r\n\f]
+WSstar          {WS}*
+WSplus          {WS}+
+WSOrComment     ({WS}|{Comment})+
+WSOrCommentStar ({WS}|{Comment})*
+
+
+  /*_____________________________________________________________________
+   |
+   |  Basic character classes
+   |______________________________________________________________________*/
+Letter      ({BaseChar_1Byte}|{UTF8_MultiByte})
+Digit       [0-9]
+Apos        \'
+Quote       \"
+Dot         [.]
+CatchAll    [^ \t\r\n]
+
+  /*_____________________________________________________________________
+   |
+   |  XQuery allows '' to escape ', and "" to escape ".
+   |______________________________________________________________________*/
+EscapeApos  {Apos}{Apos}
+EscapeQuot  {Quote}{Quote}
+
+  /*_____________________________________________________________________
+   |
+   |  NCName definition
+   |______________________________________________________________________*/
+NCNameStartChar       ({Letter}|"_")
+NCNameChar            ({Letter}|{Digit}|[_-])
+
+NCName                ({NCNameStartChar}{NCNameChar}*)
+
+  /*_____________________________________________________________________
+   |
+   |  QName definition
+   |______________________________________________________________________*/
+QName                 ({NCName}":")?{NCName}
+
+  /*_____________________________________________________________________
+   |
+   |  Entity definitions
+   |______________________________________________________________________*/
+CharRef               "&#"([0-9]+|x([0-9]|[a-f]|[A-F])+)";"
+PredefinedEntityRef   "&"(lt|gt|amp|quot|apos|nbsp)";"
+Ref                   {CharRef}|{PredefinedEntityRef}
+
+  /*_____________________________________________________________________
+   |
+   |  EQName definition
+   |______________________________________________________________________*/
+BracedURILiteral      "Q{"({PredefinedEntityRef}|{CharRef}|[^&{}])*"}"
+EQName                {BracedURILiteral}{NCName}
+
+  /*_____________________________________________________________________
+   |
+   |  Annotation QName definition
+   |______________________________________________________________________*/
+AnnotationQName       "%"({NCName}":")?{NCName}
+AnnotationEQName      "%"{EQName}
+
+  /*_____________________________________________________________________
+   |
+   |  Numeric literal definitions
+   |______________________________________________________________________*/
+DecimalLiteral        ({Dot}[0-9]+)|([0-9]+({Dot}[0-9]*)?)
+DoubleLiteral         (({Dot}[0-9]+)|([0-9]+({Dot}[0-9]*)?))([eE][+-]?[0-9]+)?
+IntegerLiteral        [0-9]+
+
+  /*_____________________________________________________________________
+   |
+   |  String literal definitions
+   |______________________________________________________________________*/
+NonQuotAnd                      [^""&]
+NonAposAnd                      [^''&]
+StringLiteral                   ({Quote}({EscapeQuot}|{Ref}|{NonQuotAnd})*{Quote})|({Apos}({EscapeApos}|{Ref}|{NonAposAnd})*{Apos})
+
+  /* Invalid strings */
+InvalidRefStringLiteral         ({Quote}({EscapeQuot}|"&"|{NonQuotAnd})*{Quote})|({Apos}({EscapeApos}|"&"|{NonAposAnd})*{Apos})
+UnterminatedStringLiteral       ({Quote}({EscapeQuot}|"&"|{NonQuotAnd})*)|({Apos}({EscapeApos}|"&"|{NonAposAnd})*)
+
+
+  /*_____________________________________________________________________
+   |
+   |  Content character definitions
+   |______________________________________________________________________*/
+Char                  [\x09\x0A\x0D\x20-\xFD]
+NonHyphenChar         [\x09\x0D\x0A\x20-\x2C\x2E-\xFD]
+XMLCommentChar        ({NonHyphenChar}*)|(-{NonHyphenChar}+)
+NonRParChar           [\x09\x0D\x0A\x20-\x28\x2A-\xFD]
+NonColonChar          [\x09\x0D\x0A\x20-\x39\x3B-\xFD]
+ElementContentChar    [\x09\x0A\x0D\x20-\x25\x27-\x3B\x3D-\x7A\x7C\x7E-\xFD]
+QuotAttrContentChar   [\x09\x0A\x0D\x20-\x21\x23-\x25\x27-\x3B\x3D-\x7A\x7C\x7E-\xFD]
+AposAttrContentChar   [\x09\x0A\x0D\x20-\x25\x28-\x3B\x3D-\x7A\x7C\x7E-\xFD]
+
+PITarget              {NCName}
+
+NonQuestionMarkChar                 [\x09\x0A\x0D\x20-\x3E\x40-\xFD]
+NonGreaterThanNonQuestionMarkChar   [\x09\x0A\x0D\x20-\x3D\x40-\xFD]
+PIChars                             ({NonQuestionMarkChar}|"?"+{NonGreaterThanNonQuestionMarkChar})*"?"*
+
+NonSharpPragmaChar                  [\x09\x0A\x0D\x20-\x22\x24-\xFD]
+NonSharpNonParPragmaChar            [\x09\x0A\x0D\x20-\x28\x2A-\xFD]
+PragmaChars                         ({NonSharpPragmaChar}|"#"+{NonSharpNonParPragmaChar})*"#"*
+
+	/* Char ranges and definitions used for parsing CDATA contents */
+NonClosingSquareBracketChar					[\x09\x0A\x0D\x20-\x5C\x5E-\xFD]
+NonGreaterThenChar									[\x09\x0A\x0D\x20-\x3D\x3F-\xFD]
+NonSqBraNonGTChar				            [\x09\x0A\x0D\x20-\x3D\x3F-\x5C\x5E-\xFD]
+CDataChars													({NonClosingSquareBracketChar}|"]"{NonSqBraNonGTChar}|"]]"{NonGreaterThenChar})*"]"*
+
+
+  /*______________________________________________________________________
+   |
+   |  Exclusive start states
+   |______________________________________________________________________*/
+
+%x MODE_SHEBANG
+%x INITIAL_ACCUMULATOR
+%x MODE_APOS_ATTRIBUTE_CONTENT
+%x MODE_QUOTE_ATTRIBUTE_CONTENT
+%x MODE_ELEM_COMP_CONSTR
+%x MODE_ATTR_COMP_CONSTR
+%x MODE_PI_COMP_CONSTR
+%x MODE_NS_COMP_CONSTR
+%x MODE_CDATA_SECTION
+%x MODE_ELEMENT_CONTENT
+%x MODE_END_TAG
+%x MODE_EXPR_COMMENT
+%x MODE_EXPR_DOC_COMMENT
+%x MODE_OCCURRENCE_INDICATOR
+%x MODE_PRAGMA
+%x MODE_PRAGMACONTENTS
+%x MODE_PROCESSING_INSTRUCTION
+%x MODE_PROCESSING_INSTRUCTION_CONTENT
+%x MODE_START_TAG
+%x MODE_XML_COMMENT
+
+
+%{
+  typedef zorba::jsoniq_parser::token token;
+%}
+
+
+  /*______________________________________________________________________
+   |
+   |  Override Flex's starting state here.
+   |______________________________________________________________________*/
+%{
+#define YY_USER_INIT      \
+{                         \
+  BEGIN MODE_SHEBANG;     \
+}
+%}
+
+
+%%
+
+
+  /*______________________________________________________________________
+   |
+   |  MODE_SHEBANG state
+   |
+   |  Accepts the "#!/path/interpreter" unix script shebang string
+   |______________________________________________________________________*/
+
+<MODE_SHEBANG>{
+"#!".*[\n]  { return token::SHEBANG; }
+.|[\n]      { BEGIN INITIAL; yyless(0); }
+}
+
+
+  /*______________________________________________________________________
+   |
+   |  INITIAL State
+   |
+   |  This state is for patterns that occur at the beginning  of an
+   |  expression or subexpression.
+   |______________________________________________________________________*/
+
+<INITIAL,INITIAL_ACCUMULATOR>{
+
+"(" { return token::LPAR; }
+";" { return token::SEMI; }
+"," { return token::COMMA; }
+"-" { return token::MINUS; }
+"+" { return token::PLUS; }
+"/" { return token::SLASH; }
+"//" { return token::SLASH_SLASH; }
+"!" { return token::BANG; }
+"@" { return token::AT_SIGN; }
+")" { return token::RPAR; }
+"*" { return token::STAR; }
+".." { return token::DOT_DOT; }
+"." { return token::DOT; }
+"?" { return token::HOOK; }
+"$" { return token::DOLLAR; }
+"#" { return token::HASH; }
+":" { return token::COLON; }
+"::" { return token::DOUBLE_COLON; }
+"%" { return token::PERCENTAGE; }
+"||" { return token::CONCAT; }
+
+"declare" {
+  std::string lDocComment = theDriver->theDocComment.str();
+  yylval->sval = getDriver()->symtab.put_commentcontent(lDocComment.c_str(), lDocComment.length());
+  theDriver->theDocComment.str("");
+  return token::DECLARE;
+}
+
+"module" {
+  std::string lDocComment = theDriver->theDocComment.str();
+  yylval->sval = getDriver()->symtab.put_commentcontent(lDocComment.c_str(), lDocComment.length());
+  theDriver->theDocComment.str("");
+  return token::MODULE;
+}
+
+    /* Tokens with state transitions */
+"element" {  PUSH_STATE(MODE_ELEM_COMP_CONSTR); }
+"attribute" { PUSH_STATE(MODE_ATTR_COMP_CONSTR); }
+"processing-instruction" { PUSH_STATE(MODE_PI_COMP_CONSTR); }
+"namespace" { PUSH_STATE(MODE_NS_COMP_CONSTR); }
+
+
+"if" { return token::IF; }
+"returning" { return token::RETURNING; }
+
+"exit" { return token::EXIT; }
+"with" { return token::WITH; }
+"break" { return token::BREAK; }
+"loop" { return token::LOOP; }
+"continue" { return token::CONTINUE; }
+"while" { return token::WHILE; }
+"set" { return token::SET; }
+"validate" { return token::VALIDATE; }
+"type" { return token::TYPE; }
+"switch" { return token::SWITCH; }
+"typeswitch" { return token::TYPESWITCH; }
+"document" { return token::DOCUMENT; }
+"text" { return token::TEXT; }
+"comment" { return token::COMMENT; }
+"function" { return token::FUNCTION; }
+"simple" { return token::SIMPLE; }
+"updating" { return token::UPDATING; }
+"sequential" { return token::SEQUENTIAL; }
+"ordered" { return token::ORDERED;}
+"unordered" { return token::UNORDERED; }
+"schema-element"  { return token::SCHEMA_ELEMENT; }
+"schema-attribute" { return token::SCHEMA_ATTRIBUTE; }
+"node" { return token::NODE; }
+"document-node" { return token::DOCUMENT_NODE; }
+"namespace-node" { return token::NS_NODE; }
+"construction" { return token::CONSTRUCTION; }
+"default" { return token::DEFAULT; }
+"order" { return token::ORDER; }
+"collation" { return token::COLLATION; }
+"base-uri" { return token::BASE_URI; }
+"import" { return token::IMPORT; }
+"schema" { return token::SCHEMA; }
+"copy-namespaces" { return token::COPY_NAMESPACES; }
+"for" { return token::FOR; }
+"from" { return token::FOR; }
+"let" { return token::LET; }
+"allowing" { return token::ALLOWING; }
+"sliding" { return token::SLIDING; }
+"tumbling" { return token::TUMBLING; }
+"previous" { return token::PREVIOUS; }
+"next" { return token::NEXT; }
+"only" { return token::ONLY; }
+"when" { return token::WHEN; }
+"count" { return token::COUNT; }
+"using" { return token::USING; }
+"some" { return token::SOME; }
+"every" { return token::EVERY; }
+"context" { return token::CONTEXT; }
+"variable" { return token::VARIABLE; }
+"boundary-space" { return token::BOUNDARY_SPACE; }
+"ordering" { return token::ORDERING; }
+"xquery" { return token::XQUERY; }
+"version" { return token::VERSION; }
+"option" { return token::OPTION; }
+"at" { return token::AT; }
+"revalidation" { return token::REVALIDATION; }
+"as" { return token::AS; }
+"try" { return token::TRY; }
+
+  /* Axes */
+"ancestor-or-self" { return token::ANCESTOR_OR_SELF; }
+"ancestor" { return token::ANCESTOR; }
+"child" { return token::CHILD; }
+"descendant-or-self" { return token::DESCENDANT_OR_SELF; }
+"descendant" { return token::DESCENDANT; }
+"following-sibling" { return token::FOLLOWING_SIBLING; }
+"following" { return token::FOLLOWING; }
+"parent" { return token::PARENT; }
+"preceding-sibling" { return token::PRECEDING_SIBLING; }
+"preceding" { return token::PRECEDING; }
+"self" { return token::SELF;}
+
+  /* Decimal format */
+"decimal-format" { return token::DECIMAL_FORMAT; }
+"decimal-separator" { return token::DECIMAL_SEPARATOR; }
+"grouping-separator" { return token::GROUPING_SEPARATOR; }
+"infinity" { return token::INFINITY_VALUE; }
+"minus-sign" { return token::MINUS_SIGN; }
+"NaN" { return token::NaN; }
+"percent" { return token::PERCENT; }
+"per-mille" { return token::PER_MILLE; }
+"zero-digit" { return token::ZERO_DIGIT; }
+"digit" { return token::DIGIT; }
+"pattern-separator" { return token::PATTERN_SEPARATOR; }
+
+  /*______________________________________________________________________
+   |
+   |  Data Definition Facility tokens
+   |______________________________________________________________________*/
+
+"collection" { return token::COLLECTION; }
+"const" { return token::CONSTOPT; }
+"append-only" { return token::APPEND_ONLY; }
+"queue" { return token::QUEUE; }
+"mutable" { return token::MUTABLE; }
+"read-only" { return token::READ_ONLY; }
+
+"index" { return token::INDEX; }
+"unique" { return token::UNIQUE; }
+"non" { return token::NON; }
+"manually" { return token::MANUALLY; }
+"automatically" { return token::AUTOMATICALLY; }
+"maintained" { return token::MAINTAINED; }
+"range" { return token::RANGE; }
+"equality" { return token::EQUALITY; }
+"on" { return token::ON; }
+"general" { return token::GENERAL; }
+
+"integrity" { return token::INTEGRITY; }
+"constraint" { return token::CONSTRAINT; }
+"check" { return token::CHECK; }
+"key" { return token::KEY; }
+"foreach" { return token::FOREACH; }
+"foreign" { return token::FOREIGN; }
+"keys" { return token::KEYS; }
+
+
+  /*______________________________________________________________________
+   |
+   |  JSONIQ tokens
+   |______________________________________________________________________*/
+
+  /* "[" and "]" are not JSONiq tokens, but they have been moved here because
+     of the "{[ ]}" grammar construct */
+"[" { PUSH_STATE(INITIAL); return token::LBRACK; }
+"]" { POP_STATE(); return token::RBRACK; }
+
+"{[" { PUSH_STATE(INITIAL_ACCUMULATOR); return token::L_ACCUMULATOR_OBJ_UNION; }
+"]}"  {
+        // This if() disambiguates between the "{[ ]}" grammar construct and the
+        // plain "[ ]" predicate
+        if (YY_START == INITIAL_ACCUMULATOR)
+        {
+          POP_STATE();
+          return token::R_ACCUMULATOR_OBJ_UNION;
+        }
+        else
+        {
+          POP_STATE();
+          yyless(1);
+          return token::RBRACK;
+        }
+      }
+
+"{|" { return token::L_SIMPLE_OBJ_UNION; }
+"|}" { return token::R_SIMPLE_OBJ_UNION; }
+"json" { return token::JSON; }
+"append" { return token::APPEND; }
+"position" { return token::POSITION; }
+"json-item" { return token::JSON_ITEM; }
+"structured-item" { return token::STRUCTURED_ITEM; }
+"array" {  return token::ARRAY; }
+"object" {  return token::OBJECT; }
+
+
+    /*______________________________________________________________________
+     |
+     | FT tokens
+     |______________________________________________________________________*/
+
+"contains" { return token::CONTAINS; }
+"ftand" { return token::FTAND; }
+"ftor" { return token::FTOR; }
+"ftnot" { return token::FTNOT; }
+"not" { return token::NOT; }
+"in" { return token::_IN; }
+"all" { return token::ALL; }
+"words" { return token::WORDS; }
+"any" { return token::ANY; }
+"word" { return token::WORD; }
+"end" { return token::END; }
+"least" { return token::LEAST; }
+"most" { return token::MOST; }
+"start" { return token::START; }
+"case" { return token::CASE; }
+"insensitive" { return token::INSENSITIVE; }
+"sensitive" { return token::SENSITIVE; }
+"ft-option" { return token::FT_OPTION; }
+"diacritics" { return token::DIACRITICS; }
+"different" { return token::DIFFERENT; }
+"distance" { return token::DISTANCE; }
+"entire" { return token::ENTIRE; }
+"content" { return token::CONTENT; }
+"exactly" { return token::EXACTLY; }
+"from" { return token::FROM; }
+"language" { return token::LANGUAGE; }
+"levels" { return token::LEVELS; }
+"lowercase" { return token::LOWERCASE; }
+"no" { return token::NO; }
+"occurs" { return token::OCCURS; }
+"paragraph" { return token::PARAGRAPH; }
+"paragraphs" { return token::PARAGRAPHS; }
+"phrase" { return token::PHRASE; }
+"relationship" { return token::RELATIONSHIP; }
+"same" { return token::SAME; }
+"score" { return token::SCORE; }
+"sentence" { return token::SENTENCE; }
+"sentences" { return token::SENTENCES; }
+"times" { return token::TIMES; }
+"uppercase" { return token::UPPERCASE; }
+"weight" { return token::WEIGHT; }
+"window" { return token::WINDOW; }
+"without" { return token::WITHOUT; }
+"stemming" { return token::STEMMING; }
+"stop" { return token::STOP; }
+"thesaurus" { return token::THESAURUS; }
+"wildcards" { return token::WILDCARDS; }
+":=" { return token::GETS; }
+"div" { return token::DIV; }
+"=" { return token::EQUALS; }
+"except" { return token::EXCEPT; }
+"eq" { return token::VAL_EQ; }
+"ge" { return token::VAL_GE; }
+"gt" { return token::VAL_GT; }
+"le" { return token::VAL_LE; }
+"lt" { return token::VAL_LT; }
+"ne" { return token::VAL_NE; }
+">=" { return token::GE; }
+">>" { return token::FOLLOWS; }
+">" { return token::GT; }
+"idiv" { return token::IDIV; }
+"intersect" { return token::INTERSECT; }
+"is" { return token::IS; }
+"<=" { return token::LE; }
+"<<" { return token::PRECEDES; }
+"mod" { return token::MOD; }
+"!=" { return token::NE; }
+"group" { return token::GROUP; }
+"by" { return token::BY; }
+"stable" { return token::STABLE; }
+"or" { return token::OR; }
+"return" { return token::RETURN; }
+"select" { return token::RETURN; }
+"satisfies" { return token::SATISFIES; }
+"to" { return token::TO; }
+"union" { return token::UNION; }
+"|" { return token::VBAR; }
+"where" { return token::WHERE; }
+"preserve" { return token::PRESERVE; }
+"strip" { return token::STRIP; }
+
+
+  /*______________________________________________________________________
+   |
+   | Update rules
+   |______________________________________________________________________*/
+
+"insert" { return token::INSERT; }
+"delete" { return token::_DELETE; }
+"replace" { return token::REPLACE; }
+"value" { return token::VALUE; }
+"of" { return token::OF; }
+"rename" { return token::RENAME; }
+"copy" { return token::COPY; }
+"nodes" { return token::NODES; }
+"into" { return token::INTO; }
+"after" { return token::AFTER; }
+"before" { return token::BEFORE; }
+"modify" { return token::MODIFY; }
+
+"strict" { return token::_STRICT; }
+"lax" { return token::LAX; }
+"skip" { return token::SKIP; }
+"then" { return token::THEN; }
+"else" { return token::ELSE; }
+"external" { return token::EXTERNAL; }
+"and" { return token::AND; }
+
+"inherit" { return token::INHERIT; }
+"no-inherit" { return token::NO_INHERIT; }
+"no-preserve" { return token::NO_PRESERVE; }
+"empty-sequence" { return token::EMPTY_SEQUENCE; }
+"item" { return token::ITEM; }
+"cast" { return token::CAST; }
+"castable" { return token::CASTABLE; }
+"instance" { return token::INSTANCE;}
+"treat" { return token::TREAT; }
+"first" { return token::FIRST; }
+"last" { return token::LAST; }
+"catch" { return token::CATCH; }
+"empty" { return token::_EMPTY; }
+"greatest" { return token::GREATEST; }
+"ascending" { return token::ASCENDING; }
+"descending" { return token::DESCENDING; }
+"encoding" { return token::ENCODING; }
+
+
+  /*______________________________________________________________________
+   |
+   | Tokens with values
+   |______________________________________________________________________*/
+
+{IntegerLiteral}  {
+  yylval->ival = getDriver()->symtab.integerval(yytext, yyleng);
+  if (yylval->ival == NULL)
+  {
+    yylval->err = getDriver()->parserErr(yytext, *yylloc, err::FOAR0002);
+    return token::UNRECOGNIZED;
+  }
+  else
+    return token::INTEGER_LITERAL;
+}
+
+{DecimalLiteral}  {
+  yylval->decval = getDriver()->symtab.decimalval(yytext, yyleng);
+  return token::DECIMAL_LITERAL;
+}
+
+{DoubleLiteral}   {
+  yylval->dval = getDriver()->symtab.doubleval(yytext, yyleng);
+  if (yylval->dval == NULL)
+  {
+    // TODO: pjl: needs correct error code
+    yylval->err = getDriver()->parserErr(yytext, *yylloc, err::FOAR0002);
+    return token::UNRECOGNIZED;
+  }
+  else
+    return token::DOUBLE_LITERAL;
+}
+
+{IntegerLiteral}[a-zA-Z_][0-9a-zA-Z_]* {
+  /* invalid integer literal */
+  yylval->err = getDriver()->parserErr(std::string("syntax error, unexpected \"") + yytext + "\", separator needed after numeric literal", *yylloc);
+  return token::UNRECOGNIZED;
+}
+
+{NCName}":*"                  { TRY_SVAL_TOKEN(ELEM_WILDCARD, put_ncname(yytext, yyleng-2), yytext); }
+
+{BracedURILiteral}"*"         { TRY_SVAL_TOKEN(ELEM_EQNAME_WILDCARD, put_ncname(yytext+2, yyleng-4), yytext+2); }
+
+{QName}                       { TRY_SVAL_TOKEN(QNAME_SVAL, put_qname(yytext, yyleng), yytext); }
+
+{EQName}                      { TRY_SVAL_TOKEN(EQNAME_SVAL, put_qname(yytext, yyleng, false, false, true), yytext); }
+
+{AnnotationQName}             { TRY_SVAL_TOKEN(ANNOTATION_QNAME_SVAL, put_qname(yytext+1, yyleng-1), yytext+1); /* skip the % sign */ }
+
+{AnnotationEQName}            { TRY_SVAL_TOKEN(ANNOTATION_EQNAME_SVAL, put_qname(yytext+1, yyleng-1, false, false, true), yytext+1); /* skip the % sign */ }
+
+"*:"{NCName}                  { TRY_SVAL_TOKEN (PREFIX_WILDCARD, put_ncname(yytext+2, yyleng-2), yytext); }
+
+{StringLiteral}               { if (checkXmlRefs(&yylval->err, yytext, yyleng, this, yylloc)) return token::UNRECOGNIZED; TRY_STRING_LITERAL(STRING_LITERAL, yytext, yyleng); }
+
+  /* Invalid string literals */
+{InvalidRefStringLiteral}     { yylval->err = getDriver()->invalidCharRef(yytext, *yylloc); return token::UNRECOGNIZED; }
+{UnterminatedStringLiteral}   { yylval->err = getDriver()->parserErr(std::string("syntax error, unterminated string literal \"") + yytext + "\"", *yylloc); return token::UNRECOGNIZED; }
+
+  /*______________________________________________________________________
+   |
+   | State transitions
+   |______________________________________________________________________*/
+
+  /* transition to MODE_XML_COMMENT */
+  /* ------------------------------ */
+"<!--" { PUSH_STATE(MODE_XML_COMMENT); return token::XML_COMMENT_BEGIN; }
+
+
+  /* transition to PROCESSING_INSTRUCTION */
+  /* ------------------------------------ */
+"<?" { PUSH_STATE(MODE_PROCESSING_INSTRUCTION);return token::PI_BEGIN; }
+
+
+  /* transition to CDATA_SECTION */
+  /* --------------------------- */
+"<![CDATA[" { /* PUSH_AND_BEGIN (MODE_CDATA_SECTION, MODE_OPERATOR); */ return token::CDATA_BEGIN; }
+
+
+  /* transition to MODE_START_TAG */
+  /* ---------------------------- */
+"<" { PUSH_STATE(MODE_START_TAG); return token::LT_OR_START_TAG; }
+
+
+  /* transition to MODE_EXPR_DOC_COMMENT */
+  /* ----------------------------------- */
+"(:~" { PUSH_STATE(MODE_EXPR_DOC_COMMENT); }
+
+
+  /* transition to MODE_EXPR_COMMENT */
+  /* ------------------------------- */
+"(:" { PUSH_STATE(MODE_EXPR_COMMENT); }
+
+
+  /* transition to PRAGMA */
+  /* -------------------- */
+"(#" { BEGIN MODE_PRAGMA; return token::PRAGMA_BEGIN;}
+
+
+  /* push initial state */
+  /* ------------------ */
+"{" { PUSH_STATE(INITIAL); return token::LBRACE; }
+
+
+  /* pop previous state */
+  /* ------------------ */
+"}" { POP_STATE(); return token::RBRACE; }
+
+
+{WSstar} {
+  /* eat up whitespace */
+}
+
+} /* END <MODE INITIAL,INITIAL_ACCUMULATOR> */
+
+
+   /*______________________________________________________________________
+    |
+    | MODE_ELEM_COMP_CONSTR, MODE_ATTR_COMP_CONSTR, MODE_PI_COMP_CONSTR
+    | MODE_NS_COMP_CONSTR states
+    |
+    |______________________________________________________________________*/
+
+<MODE_ELEM_COMP_CONSTR,MODE_ATTR_COMP_CONSTR>{QName}                                         {
+  if (yy_comp_constr_qname == "")
+    yy_comp_constr_qname = yytext;
+  else
+    COMP_CONSTR_ROLLBACK(true);
+}
+<MODE_PI_COMP_CONSTR,MODE_NS_COMP_CONSTR>{NCName}                                            {
+  if (yy_comp_constr_qname == "")
+    yy_comp_constr_qname = yytext;
+  else
+    COMP_CONSTR_ROLLBACK(true);
+}
+<MODE_ELEM_COMP_CONSTR,MODE_ATTR_COMP_CONSTR,MODE_PI_COMP_CONSTR,MODE_NS_COMP_CONSTR>"{"     {
+  if ( yy_comp_constr_qname == "")
+    COMP_CONSTR_ROLLBACK(true);
+  else
+  {
+    int _STATE = YY_START;
+    BEGIN INITIAL;
+    std::string temp = yy_comp_constr_qname;
+    yy_comp_constr_qname = "";
+    if (_STATE == MODE_ELEM_COMP_CONSTR)
+      TRY_SVAL_TOKEN(COMP_ELEMENT_QNAME_LBRACE, put_qname(temp.c_str(), temp.size()), temp.c_str());
+    else if (_STATE == MODE_ATTR_COMP_CONSTR)
+      TRY_SVAL_TOKEN(COMP_ATTRIBUTE_QNAME_LBRACE, put_qname(temp.c_str(), temp.size()), temp.c_str());
+    else if (_STATE == MODE_PI_COMP_CONSTR)
+      TRY_SVAL_TOKEN(COMP_PI_NCNAME_LBRACE, put_ncname(temp.c_str(), temp.size()), temp.c_str());
+    else 
+      TRY_SVAL_TOKEN(COMP_NS_NCNAME_LBRACE, put_ncname(temp.c_str(), temp.size()), temp.c_str());
+  }
+}
+<MODE_ELEM_COMP_CONSTR,MODE_ATTR_COMP_CONSTR,MODE_PI_COMP_CONSTR,MODE_NS_COMP_CONSTR>"(:"       { PUSH_STATE(MODE_EXPR_COMMENT); }
+<MODE_ELEM_COMP_CONSTR,MODE_ATTR_COMP_CONSTR,MODE_PI_COMP_CONSTR,MODE_NS_COMP_CONSTR>{WSstar}   { /* continue lexing */ }
+<MODE_ELEM_COMP_CONSTR,MODE_ATTR_COMP_CONSTR,MODE_PI_COMP_CONSTR,MODE_NS_COMP_CONSTR><<EOF>>    { COMP_CONSTR_ROLLBACK(false); }
+<MODE_ELEM_COMP_CONSTR,MODE_ATTR_COMP_CONSTR,MODE_PI_COMP_CONSTR,MODE_NS_COMP_CONSTR>{CatchAll} { COMP_CONSTR_ROLLBACK(true); }
+
+
+  /*______________________________________________________________________
+   |
+   | PRAGMA State
+   |
+   | This state is entered in a a pragma expression, and recognizes
+   | a QName that transits to a PRAGMACONTENTS state rather than an
+   | OPERATOR state.
+   |______________________________________________________________________*/
+
+<MODE_PRAGMA>{QName}{WSplus}                         { BEGIN MODE_PRAGMACONTENTS; TRY_SVAL_TOKEN(QNAME_SVAL,  put_qname(yytext, yyleng, true, true), yytext); }
+<MODE_PRAGMA>{EQName}{WSplus}                        { BEGIN MODE_PRAGMACONTENTS; TRY_SVAL_TOKEN(EQNAME_SVAL, put_qname(yytext, yyleng, true, true), yytext); }
+<MODE_PRAGMA>{QName}"#)"                             { BEGIN INITIAL; TRY_SVAL_TOKEN(QNAME_SVAL_AND_END_PRAGMA,  put_qname(yytext, yyleng-2), yytext); }
+<MODE_PRAGMA>{StringLiteral}":"{NCName}"#)"          { BEGIN INITIAL; TRY_SVAL_TOKEN(EQNAME_SVAL_AND_END_PRAGMA, put_qname(yytext, yyleng-2), yytext); }
+<MODE_PRAGMA>{WSplus}                                { /* continue lexing */ }
+
+
+  /*______________________________________________________________________
+   |
+   | PRAGMACONTENTS State
+   |
+   | This state recognizes characters in pragma content and transitions
+   | out of this state when a '#)' pattern is recognized.
+   |______________________________________________________________________*/
+
+<MODE_PRAGMACONTENTS>{PragmaChars}"#)" { BEGIN INITIAL; TRY_SVAL_TOKEN(PRAGMA_LITERAL_AND_END_PRAGMA, put(yytext, yyleng-2), yytext); }
+
+
+  /*______________________________________________________________________
+   |
+   | START_TAG State
+   |
+   | This state allows attributes in the native XML syntax, and marks the
+   | beginning of an element construction. Element constructors also push
+   | the current state, popping it at the conclusion of an end tag. In
+   | the START_TAG state, the string ">" is recognized as a token which
+   | is associated with the transition to the original state.
+   |______________________________________________________________________*/
+
+<MODE_START_TAG>">" { BEGIN MODE_ELEMENT_CONTENT; return token::TAG_END; }
+<MODE_START_TAG>\" { BEGIN MODE_QUOTE_ATTRIBUTE_CONTENT; return token::QUOTE; }
+<MODE_START_TAG>\' { BEGIN MODE_APOS_ATTRIBUTE_CONTENT; return token::APOS; }
+<MODE_START_TAG>"=" { return token::EQUALS; }
+<MODE_START_TAG>{WSstar} { return token::BLANK; }
+<MODE_START_TAG>"/>" { POP_STATE(); return token::EMPTY_TAG_END; }
+<MODE_START_TAG>{QName} { TRY_SVAL_TOKEN (QNAME_SVAL, put_qname(yytext, yyleng), yytext); }
+<MODE_START_TAG>{CatchAll} { yylval->err = getDriver()->unrecognizedCharErr(yytext, *yylloc); return token::UNRECOGNIZED; }
+<MODE_START_TAG><<EOF>> { yylval->err = getDriver()->unterminatedElementConstructor(*yylloc); return token::UNRECOGNIZED; }
+
+
+  /*______________________________________________________________________
+   |
+   | ELEMENT_CONTENT State
+   |
+   | This state allows XML-like content, without these characters being
+   | misinterpreted as expressions. The character "{" marks a transition
+   | to the INITIAL state, i.e. the start of an embedded expression, and
+   | the "}" character pops back to the ELEMENT_CONTENT state. To allow
+   | curly braces to be used as character content, a double left or right
+   | curly brace is interpreted as a single curly brace character. The
+   | string "</" is interpreted as the beginning of an end tag, which is
+   | associated with a transition to the END_TAG state.
+   |______________________________________________________________________*/
+
+<MODE_ELEMENT_CONTENT>"</" { BEGIN MODE_END_TAG; return token::START_TAG_END; }
+<MODE_ELEMENT_CONTENT>"{" { PUSH_STATE(INITIAL); return token::LBRACE; }
+<MODE_ELEMENT_CONTENT>"<!--" { PUSH_STATE(MODE_XML_COMMENT); return token::XML_COMMENT_BEGIN; }
+<MODE_ELEMENT_CONTENT>"<?" { PUSH_STATE(MODE_PROCESSING_INSTRUCTION); return token::PI_BEGIN; }
+<MODE_ELEMENT_CONTENT>"<![CDATA[" { PUSH_STATE(MODE_CDATA_SECTION); return token::CDATA_BEGIN; }
+<MODE_ELEMENT_CONTENT>"<" { PUSH_STATE(MODE_START_TAG); return token::LT_OR_START_TAG; }
+<MODE_ELEMENT_CONTENT>{ElementContentChar}+ { TRY_SVAL_TOKEN(ELEMENT_CONTENT, put(yytext, yyleng, 1), yytext); }
+<MODE_ELEMENT_CONTENT>{PredefinedEntityRef} { TRY_SVAL_TOKEN(ELEMENT_CONTENT, put_entityref(yytext, yyleng), yytext); }
+<MODE_ELEMENT_CONTENT>{CharRef}+ { TRY_CHARREF_LITERAL(CHAR_REF_LITERAL, put_charref, yytext, yyleng); }
+<MODE_ELEMENT_CONTENT>"{{" { return token::DOUBLE_LBRACE; }
+<MODE_ELEMENT_CONTENT>"}}" { return token::DOUBLE_RBRACE; }
+<MODE_ELEMENT_CONTENT><<EOF>> { yylval->err = getDriver()->noClosingTagForElementConstructor(*yylloc); return token::UNRECOGNIZED; }
+
+
+  /*______________________________________________________________________
+   |
+   | END_TAG State
+   |
+   | When the end tag is terminated, the state is popped to the state
+   | that was pushed at the start of the corresponding start tag.
+   |______________________________________________________________________*/
+
+<MODE_END_TAG>">" { POP_STATE(); return token::TAG_END; }
+<MODE_END_TAG>{QName}{WSstar} { TRY_SVAL_TOKEN (QNAME_SVAL, put_qname(yytext, yyleng, false, true), yytext); }
+<MODE_END_TAG><<EOF>> { yylval->err = getDriver()->noClosingTagForElementConstructor(*yylloc); return token::UNRECOGNIZED; }
+
+
+  /*______________________________________________________________________
+   |
+   | XML_COMMENT State
+   | The "<--" token marks the beginning of an XML Comment, and the "-->"
+   | token marks the end. This allows no special interpretation of other
+   | characters in this state.
+   |______________________________________________________________________*/
+
+<MODE_XML_COMMENT>"-->" { POP_STATE(); return token::XML_COMMENT_END; }
+<MODE_XML_COMMENT>{XMLCommentChar}* { TRY_SVAL_TOKEN (XML_COMMENT_LITERAL, put(yytext, yyleng, 1), yytext); }
+
+
+  /*______________________________________________________________________
+   |
+   | EXPR_COMMENT State
+   |
+   | The "(:" token marks the beginning of an expression Comment, and
+   | the ":)" token marks the end. This allows no special interpretation
+   | of other characters in this state.
+   |______________________________________________________________________*/
+
+<MODE_EXPR_COMMENT>":)"    { POP_STATE(); }
+<MODE_EXPR_COMMENT>"(:"    { PUSH_STATE(MODE_EXPR_COMMENT); }
+<MODE_EXPR_COMMENT>[^:)(]+ { /* do nothing */ }
+<MODE_EXPR_COMMENT>.       { /* do nothing */ }
+<MODE_EXPR_COMMENT><<EOF>> { yylval->err = getDriver()->unterminatedCommentErr(*yylloc); return token::UNRECOGNIZED; }
+
+
+  /*______________________________________________________________________
+   |
+   | EXPR_DOC_COMMENT State
+   |
+   | The "(:~" token marks the beginning of a doc Comment, and
+   | the ":)" token marks the end. This allows no special interpretation
+   | of other characters in this state.
+   |______________________________________________________________________*/
+
+<MODE_EXPR_DOC_COMMENT>[^:]*    { getDriver()->theDocComment << yytext; }
+<MODE_EXPR_DOC_COMMENT>":"+[^)] { getDriver()->theDocComment << yytext; }
+<MODE_EXPR_DOC_COMMENT>":"+")"  { POP_STATE(); }
+<MODE_EXPR_DOC_COMMENT><<EOF>>  { yylval->err = getDriver()->unterminatedCommentErr(*yylloc); return token::UNRECOGNIZED; }
+
+
+  /*______________________________________________________________________
+   |
+   | PROCESSING_INSTRUCTION State
+   |
+   | In this state, only patterns that are legal in a processing
+   | instruction name are recognized.
+   |______________________________________________________________________*/
+
+<MODE_PROCESSING_INSTRUCTION>{WSOrComment} { BEGIN MODE_PROCESSING_INSTRUCTION_CONTENT; /* continue lexing */ }
+<MODE_PROCESSING_INSTRUCTION>"?>" { POP_STATE(); return token::PI_END; }
+<MODE_PROCESSING_INSTRUCTION>{NCName} /* PITarget */    { TRY_SVAL_TOKEN (NCNAME_SVAL, put(yytext, yyleng), yytext); }
+
+
+  /*______________________________________________________________________
+   |
+   | PROCESSING_INSTRUCTION_CONTENT State
+   |
+   | In this state, only characters are that are legal in processing
+   | instruction content are recognized.
+   |______________________________________________________________________*/
+
+<MODE_PROCESSING_INSTRUCTION_CONTENT>{PIChars}"?>" {
+  POP_STATE();
+  TRY_SVAL_TOKEN (CHAR_LITERAL_AND_PI_END, put(yytext, yyleng-2), yytext);
+}
+
+
+  /*______________________________________________________________________
+   |
+   | CDATA_SECTION State
+   |
+   | In this state, only lexemes that are legal in a CDATA section are
+   | recognized.
+   |______________________________________________________________________*/
+
+<MODE_CDATA_SECTION>{CDataChars}"]]>" { POP_STATE(); TRY_SVAL_TOKEN (CHAR_LITERAL_AND_CDATA_END, put(yytext, yyleng-3, 1), yytext); }
+
+
+  /*______________________________________________________________________
+   |
+   | QUOTE_ATTRIBUTE_CONTENT State
+   |
+   | This state allows content legal for attributes. The character "{"
+   | marks a transition to the INITIAL state, i.e. the start of an
+   | embedded expression, and the "}" character pops back to the original
+   | state.  To allow curly braces to be used as character content, a
+   | double left or right curly brace is interpreted as a single curly
+   | brace character. This state is the same as APOS_ATTRIBUTE_CONTENT,
+   | except that apostrophes are allowed without escaping, and an
+   | unescaped quote marks the end of the state.
+   |______________________________________________________________________*/
+
+<MODE_QUOTE_ATTRIBUTE_CONTENT>\" { BEGIN MODE_START_TAG; return token::QUOTE; }
+<MODE_QUOTE_ATTRIBUTE_CONTENT>"{" { PUSH_STATE(INITIAL); return token::LBRACE; }
+<MODE_QUOTE_ATTRIBUTE_CONTENT>{EscapeQuot} { return token::ESCAPE_QUOTE; }
+<MODE_QUOTE_ATTRIBUTE_CONTENT>{QuotAttrContentChar}+ { TRY_SVAL_TOKEN(QUOTE_ATTR_CONTENT, put(yytext, yyleng, 2), yytext); }
+<MODE_QUOTE_ATTRIBUTE_CONTENT>{PredefinedEntityRef} { TRY_SVAL_TOKEN(QUOTE_ATTR_CONTENT, put_entityref(yytext, yyleng), yytext); }
+<MODE_QUOTE_ATTRIBUTE_CONTENT>{CharRef}+ { TRY_CHARREF_LITERAL(CHAR_REF_LITERAL, put_charref, yytext, yyleng); }
+<MODE_QUOTE_ATTRIBUTE_CONTENT>"{{" { return token::DOUBLE_LBRACE; }
+<MODE_QUOTE_ATTRIBUTE_CONTENT>"}}" { return token::DOUBLE_RBRACE; }
+
+
+  /*______________________________________________________________________
+   |
+   | APOS_ATTRIBUTE_CONTENT State
+   |
+   | This state is the same as QUOT_ATTRIBUTE_CONTENT, except that
+   | quotes are allowed, and an unescaped apostrophe marks the end of
+   | the state.
+   |______________________________________________________________________*/
+
+<MODE_APOS_ATTRIBUTE_CONTENT>\'                     { BEGIN MODE_START_TAG; return token::APOS; }
+<MODE_APOS_ATTRIBUTE_CONTENT>"{"                    { PUSH_AND_BEGIN (INITIAL, MODE_APOS_ATTRIBUTE_CONTENT); return token::LBRACE; }
+<MODE_APOS_ATTRIBUTE_CONTENT>{EscapeApos}           { return token::ESCAPE_APOS; }
+<MODE_APOS_ATTRIBUTE_CONTENT>{AposAttrContentChar}+ { TRY_SVAL_TOKEN(APOS_ATTR_CONTENT, put(yytext, yyleng, 2), yytext); }
+<MODE_APOS_ATTRIBUTE_CONTENT>{PredefinedEntityRef}  { TRY_SVAL_TOKEN(APOS_ATTR_CONTENT, put_entityref(yytext, yyleng), yytext); }
+<MODE_APOS_ATTRIBUTE_CONTENT>{CharRef}+             { TRY_CHARREF_LITERAL(CHAR_REF_LITERAL, put_charref, yytext, yyleng); }
+<MODE_APOS_ATTRIBUTE_CONTENT>"{{"                   { return token::DOUBLE_LBRACE; }
+<MODE_APOS_ATTRIBUTE_CONTENT>"}}"                   { return token::DOUBLE_RBRACE; }
+
+
+  /*______________________________________________________________________
+   |
+   | Catch-all rule
+   |______________________________________________________________________*/
+
+
+<*>. {
+    yylval->err = getDriver()->unrecognizedCharErr(yytext, *yylloc);
+    return token::UNRECOGNIZED;
+}
+
+
+  /* END OF FLEX RULES */
+
+
+%%
+
+std::string start_jsoniq_state(int state)
+{
+  switch (state)
+  {
+    case INITIAL: return "INITIAL";
+    case MODE_SHEBANG: return "MODE_SHEBANG";
+    case INITIAL_ACCUMULATOR: return "INITIAL_ACCUMULATOR";
+    case MODE_APOS_ATTRIBUTE_CONTENT: return "MODE_APOS_ATTRIBUTE_CONTENT";
+    case MODE_ELEM_COMP_CONSTR: return "MODE_ELEM_COMP_CONSTR";
+    case MODE_ATTR_COMP_CONSTR: return "MODE_ATTR_COMP_CONSTR";
+    case MODE_PI_COMP_CONSTR: return "MODE_PI_COMP_CONSTR";
+    case MODE_NS_COMP_CONSTR: return "MODE_NS_COMP_CONSTR";
+    case MODE_CDATA_SECTION: return "MODE_CDATA_SECTION";
+    case MODE_ELEMENT_CONTENT: return "MODE_ELEMENT_CONTENT";
+    case MODE_END_TAG: return "MODE_END_TAG";
+    case MODE_EXPR_DOC_COMMENT: return "MODE_EXPR_DOC_COMMENT";
+    case MODE_EXPR_COMMENT: return "MODE_EXPR_COMMENT";
+    case MODE_OCCURRENCE_INDICATOR: return "MODE_OCCURRENCE_INDICATOR";
+    case MODE_PRAGMA: return "MODE_PRAGMA";
+    case MODE_PRAGMACONTENTS: return "MODE_PRAGMACONTENTS";
+    case MODE_PROCESSING_INSTRUCTION: return "MODE_PROCESSING_INSTRUCTION";
+    case MODE_PROCESSING_INSTRUCTION_CONTENT: return "MODE_PROCESSING_INSTRUCTION_CONTENT";
+    case MODE_QUOTE_ATTRIBUTE_CONTENT: return "MODE_QUOTE_ATTRIBUTE_CONTENT";
+    case MODE_START_TAG: return "MODE_START_TAG";
+    case MODE_XML_COMMENT: return "MODE_XML_COMMENT";
+    default: return "[zorba] Unrecognized start state. If a new state has been created, translation should be added to start_jsoniq_state() in xquery_scanner.l \n";
+  }
+}
+
+
+namespace zorba {
+
+jsoniq_scanner::jsoniq_scanner(
+  jsoniq_driver* aDriver,
+  std::istream* i,
+  std::ostream* o)
+  :
+  ZorbaJSONiqFlexLexer(i, o), theDriver(aDriver), cond_stk_depth(0), yy_comp_constr_qname("")
+{
+}
+
+jsoniq_scanner::~jsoniq_scanner()
+{
+}
+
+void jsoniq_scanner::set_yy_flex_debug(bool aBool)
+{
+  yy_flex_debug = aBool;
+}
+
+int jsoniq_scanner::interpretAsLessThan()
+{
+  BEGIN INITIAL;
+  POP_STATE();
+  return 0;
+}
+
+int jsoniq_scanner::yy_get_start_stack_ptr() const
+{
+  return yy_start_stack_ptr;
+}
+
+} /* namespace zorba */
+
+#ifdef yylex
+#undef yylex
+#endif
+
+int ZorbaJSONiqFlexLexer::yylex()
+{
+  return 0;
+}

=== added file 'src/compiler/parser/parser_helpers.cpp'
--- src/compiler/parser/parser_helpers.cpp	1970-01-01 00:00:00 +0000
+++ src/compiler/parser/parser_helpers.cpp	2013-03-16 01:56:23 +0000
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2013 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "compiler/parser/query_loc.h"
+#include "compiler/parser/location.hh"
+
+namespace zorba {
+
+namespace parser {
+
+const char *the_tumbling = "tumbling", *the_sliding = "sliding",
+  *the_start = "start", *the_end = "end", *the_only_end = "only end",
+  *the_declare = "declare", *the_create = "create";
+
+QueryLoc createQueryLoc(const location& aLoc)
+{
+  QueryLoc lLoc;
+  lLoc.setFilename(aLoc.begin.filename->c_str());
+  lLoc.setLineBegin(aLoc.begin.line);
+  lLoc.setColumnBegin(aLoc.begin.column);
+  lLoc.setLineEnd(aLoc.end.line);
+  lLoc.setColumnEnd(aLoc.end.column);
+  return lLoc;
+}
+
+} // namespace parser
+
+} // namespace zorba

=== added file 'src/compiler/parser/parser_helpers.h'
--- src/compiler/parser/parser_helpers.h	1970-01-01 00:00:00 +0000
+++ src/compiler/parser/parser_helpers.h	2013-03-16 01:56:23 +0000
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2013 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+#ifndef ZORBA_PARSER_HELPERS_H
+#define ZORBA_PARSER_HELPERS_H
+
+#include <ostream>
+#include <string>
+#include <zorba/config.h>
+
+namespace zorba {
+
+  namespace parser {
+
+    extern const char *the_tumbling, *the_sliding, *the_start, *the_end, *the_only_end, *the_declare, *the_create;
+
+    // transform a parser location into a QueryLoc
+    QueryLoc createQueryLoc(const location& aLoc);
+
+  }
+
+
+}
+#endif

=== modified file 'src/compiler/parser/symbol_table.cpp'
--- src/compiler/parser/symbol_table.cpp	2013-02-07 17:24:36 +0000
+++ src/compiler/parser/symbol_table.cpp	2013-03-16 01:56:23 +0000
@@ -19,9 +19,6 @@
 
 #include "compiler/parser/symbol_table.h"
 
-// Include the driver for the createQueryLoc() function
-#include "compiler/parser/xquery_driver.h"
-
 #include "util/ascii_util.h"
 #include "util/xml_util.h"
 

=== modified file 'src/compiler/parser/xquery_driver.cpp'
--- src/compiler/parser/xquery_driver.cpp	2013-02-07 17:24:36 +0000
+++ src/compiler/parser/xquery_driver.cpp	2013-03-16 01:56:23 +0000
@@ -32,32 +32,16 @@
 #  pragma GCC diagnostic warning "-Wparentheses"
 #endif
 
+#include "compiler/parser/zorba_parser_error.h"
 #include "compiler/api/compilercb.h"
 #include "context/static_context.h"
 #include "diagnostics/xquery_diagnostics.h"
 #include "util/xml_util.h"
 
+
 namespace zorba
 {
 
-ZorbaParserError::ZorbaParserError(std::string _msg, Error const &code)
-  :
-  msg(_msg), err_code(code)
-{
-}
-
-ZorbaParserError::ZorbaParserError(std::string _msg, const location& aLoc, Error const &code)
-  :
-  msg(_msg), loc(xquery_driver::createQueryLocStatic(aLoc)), err_code(code)
-{
-}
-
-ZorbaParserError::ZorbaParserError(std::string _msg, const QueryLoc& aLoc, Error const &code)
-  :
-  msg(_msg), loc(aLoc), err_code(code)
-{
-}
-
 xquery_driver::xquery_driver(CompilerCB* aCompilerCB, uint32_t initial_heapsize)
   :
   symtab(initial_heapsize),
@@ -235,16 +219,5 @@
   return lLoc;
 }
 
-QueryLoc xquery_driver::createQueryLocStatic(const location& aLoc)
-{
-  QueryLoc lLoc;
-  lLoc.setFilename(aLoc.begin.filename->c_str());
-  lLoc.setLineBegin(aLoc.begin.line);
-  lLoc.setColumnBegin(aLoc.begin.column);
-  lLoc.setLineEnd(aLoc.end.line);
-  lLoc.setColumnEnd(aLoc.end.column);
-  return lLoc;
-}
-
 }	/* namespace zorba */
 /* vim:set et sw=2 ts=2: */

=== modified file 'src/compiler/parser/xquery_driver.h'
--- src/compiler/parser/xquery_driver.h	2013-02-07 17:24:36 +0000
+++ src/compiler/parser/xquery_driver.h	2013-03-16 01:56:23 +0000
@@ -30,20 +30,7 @@
 class location;
 class parsenode;
 class CompilerCB;
-
-
-class ZorbaParserError
-{
-public:
-  std::string msg;
-  QueryLoc loc;
-  Error const &err_code;
-
-public:
-  ZorbaParserError(std::string _msg, Error const &code = err::XPST0003);
-  ZorbaParserError(std::string _msg, const location& aLoc, Error const &code = err::XPST0003);
-  ZorbaParserError(std::string _msg, const QueryLoc& aLoc, Error const &code = err::XPST0003);
-};
+class ZorbaParserError;
 
 
 // exported for unit testing only
@@ -85,10 +72,6 @@
   ZorbaParserError* invalidCharRef(const char* _error_token, const location& loc);
   ZorbaParserError* parserErr(const std::string& _message, const location& loc, Error const &code = err::XPST0003);
   ZorbaParserError* parserErr(const std::string& _message, const QueryLoc& loc, Error const &code = err::XPST0003);
-
-public:
-  // transform a parser location into a QueryLoc
-  static QueryLoc createQueryLocStatic(const location& aLoc);
 };
 
 }	/* namespace zorba */

=== modified file 'src/compiler/parser/xquery_parser.y'
--- src/compiler/parser/xquery_parser.y	2013-03-07 11:41:04 +0000
+++ src/compiler/parser/xquery_parser.y	2013-03-16 01:56:23 +0000
@@ -55,6 +55,7 @@
 #include <vector>
 
 #include "compiler/parsetree/parsenodes.h"
+#include "compiler/parser/zorba_parser_error.h"
 #include "zorbatypes/zstring.h"
 
 #ifdef __GNUC__
@@ -97,21 +98,13 @@
 #include "compiler/api/compilercb.h"
 #include "store/api/update_consts.h"
 #include "compiler/parser/xquery_driver.h"
+#include "compiler/parser/parser_helpers.h"
 
 #define SYMTAB( n ) driver.symtab.get( (off_t)n )
 #define SYMTAB_PUT( s ) driver.symtab.put( s )
 #define LOC( p ) driver.createQueryLoc( p )
 
 
-namespace zorba
-{
-namespace parser
-{
-  extern const char *the_tumbling, *the_sliding, *the_start, *the_end, *the_only_end, *the_declare, *the_create;
-
-}
-}
-
 #define YYDEBUG 1
 
 using namespace std;
@@ -924,7 +917,10 @@
 %}
 
 // parsenodes
-%destructor { release_hack( $$ ); } AbbrevForwardStep AnyKindTest Annotation AnnotationList AnnotationLiteralList AposAttrContentList opt_AposAttrContentList AposAttrValueContent ArgList GeneralizedAtomicType SimpleType AttributeTest BaseURIDecl BoundarySpaceDecl CaseClause CaseClauseList CommentTest ConstructionDecl CopyNamespacesDecl DefaultCollationDecl DefaultNamespaceDecl DirAttr DirAttributeList DirAttributeValue DirElemContentList DocumentTest ElementTest EmptyOrderDecl WindowClause ForClause ForLetWinClause FLWORClauseList ForwardAxis ForwardStep FunctionDecl FunctionDecl2 FunctionDeclSimple FunctionDeclUpdating Import ItemType KindTest LetClause LibraryModule MainModule /* Module */ ModuleDecl ModuleImport NameTest NamespaceDecl NodeComp NodeTest OccurrenceIndicator OptionDecl GroupByClause GroupSpecList GroupSpec GroupCollationSpec OrderByClause OrderCollationSpec OrderDirSpec OrderEmptySpec OrderModifier OrderSpec OrderSpecList OrderingModeDecl PITest Param ParamList PositionalVar Pragma Pragma_list PredicateList QVarInDecl QVarInDeclList QuoteAttrValueContent QuoteAttrContentList opt_QuoteAttrContentList ReverseAxis ReverseStep SIND_Decl SIND_DeclList SchemaAttributeTest SchemaElementTest SchemaImport SchemaPrefix SequenceType SequenceTypeList Setter SignList SingleType TextTest NamespaceTest TypeDeclaration TypeName TypeName_WITH_HOOK URILiteralList ValueComp CollectionDecl IndexDecl IndexKeySpec IndexKeyList IntegrityConstraintDecl CtxItemDecl CtxItemDecl2 CtxItemDecl3 CtxItemDecl4 VarDecl VarGetsDecl VarGetsDeclList VarInDecl VarInDeclList WindowVarDecl WindowVars WindowVars2 WindowVars3 FLWORWinCond VersionDecl VFO_Decl VFO_DeclList WhereClause CountClause Wildcard DecimalFormatDecl TypedFunctionTest AnyFunctionTest TypeList SwitchCaseClause SwitchCaseClauseList SwitchCaseOperandList
+%destructor { release_hack( $$ ); } AbbrevForwardStep ForwardAxis ForwardStep NodeTest ReverseAxis ReverseStep
+
+// parsenodes
+%destructor { release_hack( $$ ); } AnyKindTest Annotation AnnotationList AnnotationLiteralList AposAttrContentList opt_AposAttrContentList AposAttrValueContent ArgList GeneralizedAtomicType SimpleType AttributeTest BaseURIDecl BoundarySpaceDecl CaseClause CaseClauseList CommentTest ConstructionDecl CopyNamespacesDecl DefaultCollationDecl DefaultNamespaceDecl DirAttr DirAttributeList DirAttributeValue DirElemContentList DocumentTest ElementTest EmptyOrderDecl WindowClause ForClause ForLetWinClause FLWORClauseList FunctionDecl FunctionDecl2 FunctionDeclSimple FunctionDeclUpdating Import ItemType KindTest LetClause LibraryModule MainModule /* Module */ ModuleDecl ModuleImport NameTest NamespaceDecl NodeComp OccurrenceIndicator OptionDecl GroupByClause GroupSpecList GroupSpec GroupCollationSpec OrderByClause OrderCollationSpec OrderDirSpec OrderEmptySpec OrderModifier OrderSpec OrderSpecList OrderingModeDecl PITest Param ParamList PositionalVar Pragma Pragma_list PredicateList QVarInDecl QVarInDeclList QuoteAttrValueContent QuoteAttrContentList opt_QuoteAttrContentList SIND_Decl SIND_DeclList SchemaAttributeTest SchemaElementTest SchemaImport SchemaPrefix SequenceType SequenceTypeList Setter SignList SingleType TextTest NamespaceTest TypeDeclaration TypeName TypeName_WITH_HOOK URILiteralList ValueComp CollectionDecl IndexDecl IndexKeySpec IndexKeyList IntegrityConstraintDecl CtxItemDecl CtxItemDecl2 CtxItemDecl3 CtxItemDecl4 VarDecl VarGetsDecl VarGetsDeclList VarInDecl VarInDeclList WindowVarDecl WindowVars WindowVars2 WindowVars3 FLWORWinCond VersionDecl VFO_Decl VFO_DeclList WhereClause CountClause Wildcard DecimalFormatDecl TypedFunctionTest AnyFunctionTest TypeList SwitchCaseClause SwitchCaseClauseList SwitchCaseOperandList
 
 // parsenodes: Full-Text
 %destructor { release_hack( $$ ); } FTAnd FTAnyallOption FTBigUnit FTCaseOption FTContent FTDiacriticsOption FTDistance FTExtensionOption FTExtensionSelection FTIgnoreOption opt_FTIgnoreOption FTLanguageOption FTMatchOption FTMatchOptions opt_FTMatchOptions FTMildNot FTOptionDecl FTOr FTOrder FTPosFilter FTPrimary FTPrimaryWithOptions FTRange FTScope FTScoreVar FTSelection FTStemOption FTStopWords FTStopWordOption FTStopWordsInclExcl FTThesaurusID FTThesaurusOption FTTimes opt_FTTimes FTUnaryNot FTUnit FTWeight FTWildCardOption FTWindow FTWords FTWordsValue
@@ -932,8 +928,11 @@
 // parsenodes: JSON
 %destructor { release_hack( $$ ); } JSONObjectConstructor JSONPairList JSONArrayConstructor JSONSimpleObjectUnion JSONAccumulatorObjectUnion JSONDeleteExpr JSONInsertExpr JSONRenameExpr JSONReplaceExpr JSONAppendExpr
 
+// exprnodes: AxisStep
+%destructor { release_hack( $$ ); } AxisStep
+
 // exprnodes
-%destructor { release_hack( $$ ); } AdditiveExpr AndExpr AxisStep CDataSection CastExpr CastableExpr CommonContent ComparisonExpr CompAttrConstructor CompCommentConstructor CompDocConstructor CompElemConstructor CompPIConstructor CompNamespaceConstructor CompTextConstructor ComputedConstructor Constructor ContextItemExpr DirCommentConstructor DirElemConstructor DirElemContent DirPIConstructor DirectConstructor BracedExpr BlockExpr EnclosedStatementsAndOptionalExpr BlockStatement Statement Statements StatementsAndExpr StatementsAndOptionalExpr StatementsAndOptionalExprTop SwitchStatement TypeswitchStatement TryStatement CatchListStatement CatchStatement ApplyStatement IfStatement FLWORStatement ReturnStatement VarDeclStatement Expr ExprSingle ExprSimple ExtensionExpr FLWORExpr ReturnExpr FilterExpr FunctionCall IfExpr InstanceofExpr IntersectExceptExpr Literal MultiplicativeExpr NumericLiteral OrExpr OrderedExpr ParenthesizedExpr PathExpr Predicate PrimaryExpr QuantifiedExpr QueryBody RangeExpr RelativePathExpr StepExpr StringLiteral TreatExpr StringConcatExpr SwitchExpr TypeswitchExpr UnaryExpr UnionExpr UnorderedExpr ValidateExpr ValueExpr SimpleMapExpr VarRef TryExpr CatchListExpr CatchExpr DeleteExpr InsertExpr RenameExpr ReplaceExpr TransformExpr VarNameList VarNameDecl AssignStatement ExitStatement WhileStatement FlowCtlStatement QNAME EQNAME FUNCTION_NAME FTContainsExpr
+%destructor { release_hack( $$ ); } AdditiveExpr AndExpr CDataSection CastExpr CastableExpr CommonContent ComparisonExpr CompAttrConstructor CompCommentConstructor CompDocConstructor CompElemConstructor CompPIConstructor CompNamespaceConstructor CompTextConstructor ComputedConstructor Constructor ContextItemExpr DirCommentConstructor DirElemConstructor DirElemContent DirPIConstructor DirectConstructor BracedExpr BlockExpr EnclosedStatementsAndOptionalExpr BlockStatement Statement Statements StatementsAndExpr StatementsAndOptionalExpr StatementsAndOptionalExprTop SwitchStatement TypeswitchStatement TryStatement CatchListStatement CatchStatement ApplyStatement IfStatement FLWORStatement ReturnStatement VarDeclStatement Expr ExprSingle ExprSimple ExtensionExpr FLWORExpr ReturnExpr FilterExpr FunctionCall IfExpr InstanceofExpr IntersectExceptExpr Literal MultiplicativeExpr NumericLiteral OrExpr OrderedExpr ParenthesizedExpr PathExpr Predicate PrimaryExpr QuantifiedExpr QueryBody RangeExpr RelativePathExpr StepExpr StringLiteral TreatExpr StringConcatExpr SwitchExpr TypeswitchExpr UnaryExpr UnionExpr UnorderedExpr ValidateExpr ValueExpr SimpleMapExpr VarRef TryExpr CatchListExpr CatchExpr DeleteExpr InsertExpr RenameExpr ReplaceExpr TransformExpr VarNameList VarNameDecl AssignStatement ExitStatement WhileStatement FlowCtlStatement QNAME EQNAME FUNCTION_NAME FTContainsExpr
 
 // internal non-terminals with values
 %destructor { delete $$; } FunctionSig VarNameAndType NameTestList DecimalFormatParam DecimalFormatParamList
@@ -4110,7 +4109,8 @@
     {
       $$ = $1;
     }
-  | FilterExpr
+  |
+  FilterExpr
     {
       $$ = $1;
     }
@@ -6992,14 +6992,6 @@
 
 namespace zorba {
 
-namespace parser {
-
-const char *the_tumbling = "tumbling", *the_sliding = "sliding",
-  *the_start = "start", *the_end = "end", *the_only_end = "only end",
-  *the_declare = "declare", *the_create = "create";
-
-} // namespace parser
-
 /*
  *  The error member function registers the errors to the driver.
  */

=== modified file 'src/compiler/parser/xquery_scanner.l'
--- src/compiler/parser/xquery_scanner.l	2013-03-07 10:10:10 +0000
+++ src/compiler/parser/xquery_scanner.l	2013-03-16 01:56:23 +0000
@@ -174,7 +174,7 @@
 #define yyterminate() return token::_EOF
 
 
-std::string start_state(int);   /* forward declaration, used by YY_USER_ACTION */
+std::string start_xquery_state(int);   /* forward declaration, used by YY_USER_ACTION */
 
 /*
   The macro YY_USER_ACTION can be defined to provide an action which is
@@ -195,7 +195,7 @@
 #define YY_USER_ACTION \
 { \
   if (yy_flex_debug) { \
-    std::cerr << "<" << start_state(YY_START) << ">" << "\""<<yytext<<"\"" << std::endl; \
+    std::cerr << "<" << start_xquery_state(YY_START) << ">" << "\""<<yytext<<"\"" << std::endl; \
   } \
   \
   int last_endl = 0;\
@@ -662,7 +662,6 @@
 "entire" { return token::ENTIRE; }
 "content" { return token::CONTENT; }
 "exactly" { return token::EXACTLY; }
-"from" { return token::FROM; }
 "language" { return token::LANGUAGE; }
 "levels" { return token::LEVELS; }
 "lowercase" { return token::LOWERCASE; }
@@ -1155,8 +1154,7 @@
 
 %%
 
-
-std::string start_state(int state)
+std::string start_xquery_state(int state)
 {
   switch (state)
   {
@@ -1181,7 +1179,7 @@
     case MODE_QUOTE_ATTRIBUTE_CONTENT: return "MODE_QUOTE_ATTRIBUTE_CONTENT";
     case MODE_START_TAG: return "MODE_START_TAG";
     case MODE_XML_COMMENT: return "MODE_XML_COMMENT";
-    default: return "[zorba] Unrecognized start state. If a new state has been created, translation should be added to start_state() in xquery_scanner.l \n";
+    default: return "[zorba] Unrecognized start state. If a new state has been created, translation should be added to start_xquery_state() in xquery_scanner.l \n";
   }
 }
 

=== added file 'src/compiler/parser/zorba_parser_error.cpp'
--- src/compiler/parser/zorba_parser_error.cpp	1970-01-01 00:00:00 +0000
+++ src/compiler/parser/zorba_parser_error.cpp	2013-03-16 01:56:23 +0000
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2006-2008 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "stdafx.h"
+#include <string>
+
+#include "compiler/parser/zorba_parser_error.h"
+#include "compiler/parser/parser_helpers.h"
+
+namespace zorba
+{
+
+ZorbaParserError::ZorbaParserError(std::string _msg, Error const &code)
+  :
+  msg(_msg), err_code(code)
+{
+}
+
+ZorbaParserError::ZorbaParserError(std::string _msg, const location& aLoc, Error const &code)
+  :
+  msg(_msg), loc(parser::createQueryLoc(aLoc)), err_code(code)
+{
+}
+
+ZorbaParserError::ZorbaParserError(std::string _msg, const QueryLoc& aLoc, Error const &code)
+  :
+  msg(_msg), loc(aLoc), err_code(code)
+{
+}
+
+}

=== added file 'src/compiler/parser/zorba_parser_error.h'
--- src/compiler/parser/zorba_parser_error.h	1970-01-01 00:00:00 +0000
+++ src/compiler/parser/zorba_parser_error.h	2013-03-16 01:56:23 +0000
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2006-2008 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+#ifndef ZORBA_PARSER_ERROR_H
+#define ZORBA_PARSER_ERROR_H
+
+#include <string>
+#include "compiler/parser/query_loc.h"
+#include "compiler/parser/location.hh"
+#include "diagnostics/xquery_diagnostics.h"
+
+namespace zorba {
+
+class ZorbaParserError
+{
+public:
+  std::string msg;
+  QueryLoc loc;
+  Error const &err_code;
+
+public:
+  ZorbaParserError(std::string _msg, Error const &code = err::XPST0003);
+  ZorbaParserError(std::string _msg, const location& aLoc, Error const &code = err::XPST0003);
+  ZorbaParserError(std::string _msg, const QueryLoc& aLoc, Error const &code = err::XPST0003);
+};
+
+}
+
+#endif

=== added file 'test/rbkt/ExpQueryResults/zorba/jsoniq/nav_01.xml.res'
--- test/rbkt/ExpQueryResults/zorba/jsoniq/nav_01.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/jsoniq/nav_01.xml.res	2013-03-16 01:56:23 +0000
@@ -0,0 +1,1 @@
+bar

=== added file 'test/rbkt/ExpQueryResults/zorba/jsoniq/nav_02.xml.res'
--- test/rbkt/ExpQueryResults/zorba/jsoniq/nav_02.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/jsoniq/nav_02.xml.res	2013-03-16 01:56:23 +0000
@@ -0,0 +1,1 @@
+23

=== removed file 'test/rbkt/ExpQueryResults/zorba/jsoniq/use_case01.xml.res'
--- test/rbkt/ExpQueryResults/zorba/jsoniq/use_case01.xml.res	2013-02-07 17:24:36 +0000
+++ test/rbkt/ExpQueryResults/zorba/jsoniq/use_case01.xml.res	1970-01-01 00:00:00 +0000
@@ -1,1 +0,0 @@
-{ "name" : "Jim", "age" : 13, "gender" : "male", "friends" : [ "Sarah" ] }

=== modified file 'test/rbkt/Queries/CMakeLists.txt'
--- test/rbkt/Queries/CMakeLists.txt	2013-02-26 04:12:43 +0000
+++ test/rbkt/Queries/CMakeLists.txt	2013-03-16 01:56:23 +0000
@@ -22,7 +22,7 @@
 SET (_results_dir "${CMAKE_CURRENT_BINARY_DIR}/../QueryResults")
 
 FILE(GLOB_RECURSE TESTFILES FOLLOW_SYMLINKS
-  RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.xq")
+  RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.xq" "*.jq")
 IF(ZORBA_TEST_XQUERYX)
   FILE(GLOB_RECURSE TESTFILES_XQX FOLLOW_SYMLINKS
     RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} FOLLOW_SYMLINKS "*.xqx")

=== added file 'test/rbkt/Queries/zorba/jsoniq/nav_01.jq'
--- test/rbkt/Queries/zorba/jsoniq/nav_01.jq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/jsoniq/nav_01.jq	2013-03-16 01:56:23 +0000
@@ -0,0 +1,2 @@
+let $o := { "foo" : "bar" }
+return $o.foo

=== added file 'test/rbkt/Queries/zorba/jsoniq/nav_02.jq'
--- test/rbkt/Queries/zorba/jsoniq/nav_02.jq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/jsoniq/nav_02.jq	2013-03-16 01:56:23 +0000
@@ -0,0 +1,8 @@
+let $o := {
+  "foo" : {
+    "bar" : {
+      "42." : 23
+    }
+  }
+}
+return $o.foo.bar("42.")

=== modified file 'test/rbkt/testdriver.cpp'
--- test/rbkt/testdriver.cpp	2013-03-01 06:25:35 +0000
+++ test/rbkt/testdriver.cpp	2013-03-16 01:56:23 +0000
@@ -325,7 +325,9 @@
     lQuery = engine->createQuery(&errHandler);
     lQuery->setFileName(lQueryFile.get_path());
 
-    lQuery->compile(lQueryString.c_str(), lContext, getCompilerHints());
+    bool lJSONiqMode = lQueryFile.get_path().rfind(".jq") == lQueryFile.get_path().size() - 3;
+
+    lQuery->compile(lQueryString.c_str(), lContext, getCompilerHints(lJSONiqMode));
 
     errors = -1;
     if ( errHandler.errors() )

=== modified file 'test/rbkt/testdriver_common.cpp'
--- test/rbkt/testdriver_common.cpp	2013-02-07 17:24:36 +0000
+++ test/rbkt/testdriver_common.cpp	2013-03-16 01:56:23 +0000
@@ -166,7 +166,7 @@
 /*******************************************************************************
 
 ********************************************************************************/
-Zorba_CompilerHints getCompilerHints()
+Zorba_CompilerHints getCompilerHints(bool aJSONiqMode)
 {
   Zorba_CompilerHints lHints;
 
@@ -195,6 +195,8 @@
     lHints.for_serialization_only = true;
   }
 
+  lHints.jsoniq_mode = aJSONiqMode;
+
   return lHints;
 }
 

=== modified file 'test/rbkt/testdriver_common.h'
--- test/rbkt/testdriver_common.h	2013-02-07 17:24:36 +0000
+++ test/rbkt/testdriver_common.h	2013-03-16 01:56:23 +0000
@@ -180,7 +180,7 @@
     std::string paths,
     zorba::StaticContext_t& sctx);
 
-Zorba_CompilerHints getCompilerHints();
+Zorba_CompilerHints getCompilerHints(bool aJSONiqMode = false);
 
 zorba::Item createItem(
     DriverContext& driverCtx,


Follow ups