\chapter{The Notation3 Syntax}
\label{notation3-syntax}
Notation3, or N3, was a syntax for RDF that evolved from a syntax used on whiteboards. Its main purpose is to be more readable, writable, and extensible than RDF/XML. The full grammar of Notation3 is at \cite{bl01n3} and \cite{bl06n3bnf}. What follows is complete enough for the examples used in this thesis.

The processing of Notation3 files is modified by some \emph{declarations}. In particular, \verb|@prefix prefix: <IRI> . | is used to declare prefixes for Qnames. \verb|@keywords| is used to change the parsing of keywords. These declarations should be at the top of the file. Anywhere else, and their behavior is not well specified.

Notation3 has \emph{keywords}. We have already seen two of these. Then entire list of keywords currently is: \verb|keywords|, \verb|is|, \verb|of|, \verb|has|, \verb|prefix|, \verb|base|, \verb|a|, \verb|forAll|, \verb|forSome|. Of these, \verb|a|, \verb|has|, \verb|is| and \verb|of| can appear just like that. The others must appear preceded by a \verb|@|. 

An IRI reference is put into \verb|<>|. Thus, \verb|<http://www.w3.org/2000/10/swap/log#semantics>| is an IRI reference RDF term. 

A Qname simply appears as \verb|prefix:localname|, like \qname{log:semantics}. If preceded by the statement
\begin{verbatim}
@prefix log: <http://www.w3.org/2000/10/swap/log#> .
\end{verbatim}
then \qname{log:semantics} is the same as \verb|<http://www.w3.org/2000/10/swap/log#semantics>|, with the two strings concatenated.
There is the empty prefix, which defaults to the current document. Thus, \qname{:} is valid, as is \qname{:a}. 

By using \verb|@keywords keyword1, keyword2, ... .|, one can define which keywords must be preceded by \verb|@|. By doing so, qnames with an empty prefix whose localname is not a keyword can appear without the \verb|:|. Thus, after \verb|@keywords a .|, \qname{:a} must be written that way, but \qname{:b} can be written as \qname{b}. This can allow for much more readable notation3 files, while allowing for new keywords to be introduced in the future.

A string can be in double quotes \verb|"This is a string"|, or triple double quotes 
\begin{verbatim}
"""This is a
multiline string
"""
\end{verbatim}. A literal can be typed by being a number that is not in quotes, which is automatically typed, or a string followed by \verb|^^dt|, where dt is a qname or IRI reference to the type. A language can be attached to a literal by appending \verb|@langtag|, where langtag is the short name for the language, like ``en''. It is an error to include both a language tag and a type. 

An RDF statement is a subject, predicate and object. In Notation3, one writes this as \verb|subject predicate object .| . Multiple statements with the same subject can be combined using a \verb|;|, while statements that differ only in the object can be combined using \verb|,|. Thus, the following are the same.
\begin{verbatim}
:foo a :Thing1,
       :Thing2;
   :says "Hi" .

:foo a :Thing1 .
:foo a :Thing2 .
:foo :says "Hi" .
\end{verbatim}

The keyword \verb|a| stands for \verb|<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>|. Similarly, \verb|=>| stands for \verb|<http://www.w3.org/2000/10/swap/log#implies|, and \verb|<=| stands for \verb|is <http://www.w3.org/2000/10/swap/log#implies of|. The keyword \verb|=| stands for \verb|<http://www.w3.org/2002/07/owl#sameAs>|.

Anonymous nodes, or blank nodes, have several representations. The first is uniquely named, using the special \verb|_| prefix in qnames. These will look like \verb|_:a16|. The second is using \verb|[]| notation. Anything within the brackets has the anonymous node as its subject. The third is using variable notation, declaring before any use of a IRI \verb|@forSome <IRI1>, foo:IRI2 . |. 

Another way yet of expressing blank nodes is using the path syntax. \verb|a!b| is the same as \verb| [ is b of a ] |. Also, \verb|a^b| is the same as \verb| [ b a ] |.

Notation3 has a shorthand for RDF linked lists. A list can be written as \verb|(elt1 elt2 ... eltn)|, as a shorthand for 
\begin{verbatim}
_:L1 rdf:first elt1;
  rdf:rest _:L2 .
_:L2 rdf:first elt2;
  rdf:rest _:L3 . 
...
_:LN rdf:first eltn;
  rdf:rest rdf:nil
\end{verbatim} 

A Notation3 graph can be quoted as a Notation3 node in a triple by putting it between \verb|{| and \verb|}|. 
