\subsection{Textual syntax} \label{Textual}

In this section we present {\MathQL}.4 textual syntax using the same notation
that we adopted in \cite{GS03,Gui03}. In particular the grammatical
productions we use resemble {\BNF} with some {\POSIX} formalism:

\begin{itemize}

\item
\TT{::=} defines a grammatical production by means of a regular expression.

Regular expressions are made of the following elements
(\TT{...} is a placeholder):

\item 
\TT{`...`} represents any character in a character set;

\item 
\verb+`^ ...`+ represents any character (U+20 to U+7E) not in a character set;

\item 
\TT{"..."} represents a string to be matched verbatim;

\item
\GP{...} represents a regular expression defined by a grammatical production;

\item
\TT{... ...} represents a conjunctive regular expression;

\item
\TT{... | ...} represents a disjunctive regular expression;

\item
\TT{[ ... ]?} represents an optional regular expression;

\item
\TT{[ ... ]+} represents a regular expression to be repeated one or more times;

\item
\TT{[ ... ]*} represents a regular expression to be repeated zero or more times;

\item
\TT{[ ... ]} represents a grouped regular expression.

\end{itemize}

\begin{figure}
\begin{footnotesize} \begin{verbatim}
<dec>     ::= '0 - 9'
<num>     ::= <dec> [ <dec> ]*
<hex>     ::= <dec> | 'A - F' | 'a - f'
<escaped> ::= "u" <hex> <hex> <hex> <hex> | '"' | "\" | "^"  
<string>  ::= '"' [ "\" <escaped> "^" | '^ "\^' ]* '"'
<path>    ::= "/" | [ "/" <string> ]+ 
\end{verbatim} \end{footnotesize}
\vskip-1pc
\caption{Textual syntax of numbers, strings and paths} \label{StrTS}
\end{figure}

\begin{figure}
\begin{footnotesize}
\begin{center} \begin{tabular}{|l|l|c|}
\hline {\bf Escape sequence} & {\bf Unicode character} & {\bf Text} \\
\hline \verb+\u....^+        & U+....                  &            \\
\hline \verb+\"^+            & U+0022                  & \verb+"+   \\
\hline \verb+\\^+            & U+005C                  & \verb+\+   \\
\hline \verb+\^^+            & U+005E                  & \verb+^+   \\
\hline
\end{tabular} \end{center}
\end{footnotesize}
\vskip-1pc
\caption{Textual syntax of escaped characters} \label{EscTS}
\end{figure}

Queries and results can contain quoted constant strings with the syntax of
\figref {StrTS}%
\footnote
{Note that the first slash of the \GP{path} is not optional as in {\MathQL}.3.}
and the meaning of the escaped sequences is shown in \figref{EscTS} (where 
$ .... $ is a 4-digit placeholder).
{\MathQL} character escaping syntax aims at complying with W3C character model
for the World Wide Web \cite{W3Ca} which recommends a support for standard
Unicode characters (U+0000 to U+FFFF) and escape sequences with start/end
delimiters.
In particular {\MathQL} escape delimiters (backslash and caret) are chosen
among the \emph{unwise} characters for {\URI} references (see \cite{URI}) 
because {\URI} references are the natural content of constant strings and 
these characters should not be so frequent in them.

\begin{figure}
\begin{footnotesize} \begin{verbatim}
<alpha> ::= [ 'A - Z' | 'a - z' | `_` ]+
<id>    ::= <alpha> [ <alpha> | <dec> ]*
<svar>  ::= "$" <id>
<evar>  ::= "@" <id>
\end{verbatim}\end{footnotesize} %$
\vskip-1pc
\caption{Textual syntax of variables} \label{VarTS}
\end{figure}

Queries can also contain \emph{set} variables (production \GP{svar}) and
\emph{element} variables (production \GP{evar}) according to the syntax of
\figref{VarTS}.%
\footnote{This syntax resembles the one of programming languages identifiers.}
A set variable holds an {\av} set, {\ie} a query result, while an element
variable holds an {\av}.

\begin{figure}
\begin{footnotesize} \begin{verbatim}
<ref>       ::= [ "sub" | "super" ]?
<qualifier> ::= [ "inverse" ]? <ref> <path>
<main>      ::= [ "main" <path> ]? 
<cons>      ::= <path> [ "in" | "match" ] <query>
<istrue>    ::= [ "istrue" <cons> [ "," <cons> ]* ]?
<isfalses>  ::= [ "isfalse" <cons> [ "," <cons> ]* ]* 
<exp>       ::= <path> [ "as" <path> ]?
<sec>       ::= [ "attr" <exp> [ "," <exp> ]* ]?
<opt_args>  ::= <main> <istrue> <isfalses> <sec>
<source>    ::= [ "pattern" ]? <query>
<paths>     ::= <path> [ "," <path> ]*
<query>     ::= "(" <query> ")" | <string> | "[" <xavs> "]"
            |   "property" <qualifier> <opt_args> "of" <source>
            |   "let" <svar> "=" <query> "in" <query>
            |   <query> ";;" <query> | <svar> | <evar> 
            |   "ex" <query> | <evar> "." <path>
            |   "add" [ "distr" ]? [ <xgroups> | <evar> ] "in" <query>
            |   "for" <evar> "in" <query> [ "sup" | "inf" ] <query>
            |   "while" <query> [ "sup" | "inf" ] <query>
            |   <path> "{" [ <paths> ]? "}" "{" <queries> "}"
            |   "gen" <path> [ "{" <queries> "}" | "in" <query> ] 
<queries>   ::= [ <query> [ "," <query> ]* ]?
<xattr>     ::= <path> "=" <query>
<xgroup>    ::= "{" <xattr> [ ";" <xattr> ]* "}"
<xgroups>   ::= <xgroup> [ "," <xgroup> ]*
<xav>       ::= <string> [ "attr" <xgroups> ]?
<xavs>      ::= [ <xav> [ ";" <xav> ]* ]?
\end{verbatim} \end{footnotesize}
\vskip-1pc
\caption{Textual syntax of queries} \label{QueryTS}
\end{figure}

\begin{figure}
\begin{footnotesize} \begin{verbatim}
<attr>  ::= <path> "=" <string> | "{" <string> [ "," <string> ]* "}" 
<group> ::= "{" <attr> [ ";" <attr> ]* "}"
<av>    ::= <string> [ "attr" <group> [ "," <group> ]* ]?
<avs>   ::= [ <av> [ ";" <av> ]* ]?
\end{verbatim} \end{footnotesize}
\vskip-1pc
\caption{Textual syntax of results} \label{ResultTS}
\end{figure}

\begin{figure}
\begin{footnotesize} \begin{verbatim}
<query> ::= "empty" | "false" | "true"
        |   [ "not" | "count" | "proj" <path> "of" ] <query>
        |   <query> [ "and" | "or" | "xor" ] <query>
        |   <query> [ "sub" | "meet" | "eq" | "le" | "lt" ] <query> 
        |   <query> [ "union" | "intersect" | "diff" ] <query> 
        |   "{" <queries> "}"  
        |   "keep" [ "allbut" ]? [ <paths> "in" ]? <query> 
        |   "if" <query> "then" <query> "else" <query>
        |   "select" <evar> "from" <query> "where" <query>
\end{verbatim} \end{footnotesize}
\vskip-1pc
\caption{Textual syntax of the basic extension} \label{BasicTS}
\end{figure}

The core infrastructure of {\MathQL}.4 defines a syntax for queries 
(\figref{QueryTS}, production \GP{query}) and a syntax for results 
(\figref{ResultTS}, production \GP{avs}).
A syntax extension for the most common functions of the basic library is
also provided for the user's convenience and for backward compatibility with 
{\MathQL}.3. The syntax extension concerning the functions covered in this
paper is shown in \figref{BasicTS}.
Note that this extension makes \GP{avs} an instance of \GP{xavs}.


