A EBNF for XQuery 3.0 Grammar with Update extensions

The EBNF in this document and in this section is aligned with the current XML Query 3.0 grammar (see [XQuery 3.0: An XML Query Language]).

[1]    Module    ::=    VersionDecl? (LibraryModule | MainModule)
[2]    VersionDecl    ::=    "xquery" (("encoding" StringLiteral) | ("version" StringLiteral ("encoding" StringLiteral)?)) Separator
[3]    MainModule    ::=    Prolog QueryBody
[4]    LibraryModule    ::=    ModuleDecl Prolog
[5]    ModuleDecl    ::=    "module" "namespace" NCName "=" URILiteral Separator
[6]    Prolog    ::=    ((DefaultNamespaceDecl | Setter | NamespaceDecl | Import) Separator)* ((ContextItemDecl | AnnotatedDecl | OptionDecl) Separator)*
[7]    Separator    ::=    ";"
[8]    Setter    ::=    BoundarySpaceDecl | DefaultCollationDecl | BaseURIDecl | ConstructionDecl | OrderingModeDecl | EmptyOrderDecl | RevalidationDecl | CopyNamespacesDecl | DecimalFormatDecl
[9]    BoundarySpaceDecl    ::=    "declare" "boundary-space" ("preserve" | "strip")
[10]    DefaultCollationDecl    ::=    "declare" "default" "collation" URILiteral
[11]    BaseURIDecl    ::=    "declare" "base-uri" URILiteral
[12]    ConstructionDecl    ::=    "declare" "construction" ("strip" | "preserve")
[13]    OrderingModeDecl    ::=    "declare" "ordering" ("ordered" | "unordered")
[14]    EmptyOrderDecl    ::=    "declare" "default" "order" "empty" ("greatest" | "least")
[15]    CopyNamespacesDecl    ::=    "declare" "copy-namespaces" PreserveMode "," InheritMode
[16]    PreserveMode    ::=    "preserve" | "no-preserve"
[17]    InheritMode    ::=    "inherit" | "no-inherit"
[18]    DecimalFormatDecl    ::=    "declare" (("decimal-format" EQName) | ("default" "decimal-format")) (DFPropertyName "=" StringLiteral)*
[19]    DFPropertyName    ::=    "decimal-separator" | "grouping-separator" | "infinity" | "minus-sign" | "NaN" | "percent" | "per-mille" | "zero-digit" | "digit" | "pattern-separator"
[20]    Import    ::=    SchemaImport | ModuleImport
[21]    SchemaImport    ::=    "import" "schema" SchemaPrefix? URILiteral ("at" URILiteral ("," URILiteral)*)?
[22]    SchemaPrefix    ::=    ("namespace" NCName "=") | ("default" "element" "namespace")
[23]    ModuleImport    ::=    "import" "module" ("namespace" NCName "=")? URILiteral ("at" URILiteral ("," URILiteral)*)?
[24]    NamespaceDecl    ::=    "declare" "namespace" NCName "=" URILiteral
[25]    DefaultNamespaceDecl    ::=    "declare" "default" ("element" | "function") "namespace" URILiteral
[26]    AnnotatedDecl    ::=    "declare" (CompatibilityAnnotation | Annotation)* (VarDecl | FunctionDecl)
[27]    CompatibilityAnnotation    ::=    "updating"
[28]    Annotation    ::=    "%" EQName ("(" Literal ("," Literal)* ")")?
[29]    VarDecl    ::=    "variable" "$" VarName TypeDeclaration? ((":=" VarValue) | ("external" (":=" VarDefaultValue)?))
[30]    VarValue    ::=    ExprSingle
[31]    VarDefaultValue    ::=    ExprSingle
[32]    ContextItemDecl    ::=    "declare" "context" "item" ("as" ItemType)? ((":=" VarValue) | ("external" (":=" VarDefaultValue)?))
[33]    FunctionDecl    ::=    "function" EQName "(" ParamList? ")" ("as" SequenceType)? (FunctionBody | "external")
[34]    ParamList    ::=    Param ("," Param)*
[35]    Param    ::=    "$" EQName TypeDeclaration?
[36]    FunctionBody    ::=    EnclosedExpr
[37]    EnclosedExpr    ::=    "{" Expr "}"
[38]    OptionDecl    ::=    "declare" "option" EQName StringLiteral
[39]    QueryBody    ::=    Expr
[40]    Expr    ::=    ExprSingle ("," ExprSingle)*
[41]    ExprSingle    ::=    FLWORExpr
| QuantifiedExpr
| SwitchExpr
| TypeswitchExpr
| IfExpr
| TryCatchExpr
| InsertExpr
| DeleteExpr
| RenameExpr
| ReplaceExpr
| UpdatingFunctionCall
| CopyModifyExpr
| OrExpr
[42]    FLWORExpr    ::=    InitialClause IntermediateClause* ReturnClause
[43]    InitialClause    ::=    ForClause | LetClause | WindowClause
[44]    IntermediateClause    ::=    InitialClause | WhereClause | GroupByClause | OrderByClause | CountClause
[45]    ForClause    ::=    "for" ForBinding ("," ForBinding)*
[46]    ForBinding    ::=    "$" VarName TypeDeclaration? AllowingEmpty? PositionalVar? "in" ExprSingle
[47]    AllowingEmpty    ::=    "allowing" "empty"
[48]    PositionalVar    ::=    "at" "$" VarName
[49]    LetClause    ::=    "let" LetBinding ("," LetBinding)*
[50]    LetBinding    ::=    "$" VarName TypeDeclaration? ":=" ExprSingle
[51]    WindowClause    ::=    "for" (TumblingWindowClause | SlidingWindowClause)
[52]    TumblingWindowClause    ::=    "tumbling" "window" "$" VarName TypeDeclaration? "in" ExprSingle WindowStartCondition WindowEndCondition?
[53]    SlidingWindowClause    ::=    "sliding" "window" "$" VarName TypeDeclaration? "in" ExprSingle WindowStartCondition WindowEndCondition
[54]    WindowStartCondition    ::=    "start" WindowVars "when" ExprSingle
[55]    WindowEndCondition    ::=    "only"? "end" WindowVars "when" ExprSingle
[56]    WindowVars    ::=    ("$" CurrentItem)? PositionalVar? ("previous" "$" PreviousItem)? ("next" "$" NextItem)?
[57]    CurrentItem    ::=    EQName
[58]    PreviousItem    ::=    EQName
[59]    NextItem    ::=    EQName
[60]    CountClause    ::=    "count" "$" VarName
[61]    WhereClause    ::=    "where" ExprSingle
[62]    GroupByClause    ::=    "group" "by" GroupingSpecList
[63]    GroupingSpecList    ::=    GroupingSpec ("," GroupingSpec)*
[64]    GroupingSpec    ::=    GroupingVariable (TypeDeclaration? ":=" ExprSingle)? ("collation" URILiteral)?
[65]    GroupingVariable    ::=    "$" VarName
[66]    OrderByClause    ::=    (("order" "by") | ("stable" "order" "by")) OrderSpecList
[67]    OrderSpecList    ::=    OrderSpec ("," OrderSpec)*
[68]    OrderSpec    ::=    ExprSingle OrderModifier
[69]    OrderModifier    ::=    ("ascending" | "descending")? ("empty" ("greatest" | "least"))? ("collation" URILiteral)?
[70]    ReturnClause    ::=    "return" ExprSingle
[71]    QuantifiedExpr    ::=    ("some" | "every") "$" VarName TypeDeclaration? "in" ExprSingle ("," "$" VarName TypeDeclaration? "in" ExprSingle)* "satisfies" ExprSingle
[72]    SwitchExpr    ::=    "switch" "(" Expr ")" SwitchCaseClause+ "default" "return" ExprSingle
[73]    SwitchCaseClause    ::=    ("case" SwitchCaseOperand)+ "return" ExprSingle
[74]    SwitchCaseOperand    ::=    ExprSingle
[75]    TypeswitchExpr    ::=    "typeswitch" "(" Expr ")" CaseClause+ "default" ("$" VarName)? "return" ExprSingle
[76]    CaseClause    ::=    "case" ("$" VarName "as")? SequenceTypeUnion "return" ExprSingle
[77]    SequenceTypeUnion    ::=    SequenceType ("|" SequenceType)*
[78]    IfExpr    ::=    "if" "(" Expr ")" "then" ExprSingle "else" ExprSingle
[79]    TryCatchExpr    ::=    TryClause CatchClause+
[80]    TryClause    ::=    "try" "{" TryTargetExpr "}"
[81]    TryTargetExpr    ::=    Expr
[82]    CatchClause    ::=    "catch" CatchErrorList "{" Expr "}"
[83]    CatchErrorList    ::=    NameTest ("|" NameTest)*
[84]    OrExpr    ::=    AndExpr ( "or" AndExpr )*
[85]    AndExpr    ::=    ComparisonExpr ( "and" ComparisonExpr )*
[86]    ComparisonExpr    ::=    StringConcatExpr ( (ValueComp
| GeneralComp
| NodeComp) StringConcatExpr )?
[87]    StringConcatExpr    ::=    RangeExpr ( "||" RangeExpr )*
[88]    RangeExpr    ::=    AdditiveExpr ( "to" AdditiveExpr )?
[89]    AdditiveExpr    ::=    MultiplicativeExpr ( ("+" | "-") MultiplicativeExpr )*
[90]    MultiplicativeExpr    ::=    UnionExpr ( ("*" | "div" | "idiv" | "mod") UnionExpr )*
[91]    UnionExpr    ::=    IntersectExceptExpr ( ("union" | "|") IntersectExceptExpr )*
[92]    IntersectExceptExpr    ::=    InstanceofExpr ( ("intersect" | "except") InstanceofExpr )*
[93]    InstanceofExpr    ::=    TreatExpr ( "instance" "of" SequenceType )?
[94]    TreatExpr    ::=    CastableExpr ( "treat" "as" SequenceType )?
[95]    CastableExpr    ::=    CastExpr ( "castable" "as" SingleType )?
[96]    CastExpr    ::=    TransformWithExpr ( "cast" "as" SingleType )?
[97]    TransformWithExpr    ::=    UnaryExpr ( "transform" "with" "{" Expr? "}" )?
[98]    UnaryExpr    ::=    ("-" | "+")* ValueExpr
[99]    ValueExpr    ::=    ValidateExpr | ExtensionExpr | SimpleMapExpr
[100]    GeneralComp    ::=    "=" | "!=" | "<" | "<=" | ">" | ">="
[101]    ValueComp    ::=    "eq" | "ne" | "lt" | "le" | "gt" | "ge"
[102]    NodeComp    ::=    "is" | "<<" | ">>"
[103]    ValidateExpr    ::=    "validate" (ValidationMode | ("type" TypeName))? "{" Expr "}"
[104]    ValidationMode    ::=    "lax" | "strict"
[105]    ExtensionExpr    ::=    Pragma+ "{" Expr? "}"
[106]    Pragma    ::=    "(#" S? EQName (S PragmaContents)? "#)"
[107]    PragmaContents    ::=    (Char* - (Char* '#)' Char*))
[108]    SimpleMapExpr    ::=    PathExpr ("!" PathExpr)*
[109]    PathExpr    ::=    ("/" RelativePathExpr?)
| ("//" RelativePathExpr)
| RelativePathExpr
[110]    RelativePathExpr    ::=    StepExpr (("/" | "//") StepExpr)*
[111]    StepExpr    ::=    PostfixExpr | AxisStep
[112]    AxisStep    ::=    (ReverseStep | ForwardStep) PredicateList
[113]    ForwardStep    ::=    (ForwardAxis NodeTest) | AbbrevForwardStep
[114]    ForwardAxis    ::=    ("child" "::")
| ("descendant" "::")
| ("attribute" "::")
| ("self" "::")
| ("descendant-or-self" "::")
| ("following-sibling" "::")
| ("following" "::")
[115]    AbbrevForwardStep    ::=    "@"? NodeTest
[116]    ReverseStep    ::=    (ReverseAxis NodeTest) | AbbrevReverseStep
[117]    ReverseAxis    ::=    ("parent" "::")
| ("ancestor" "::")
| ("preceding-sibling" "::")
| ("preceding" "::")
| ("ancestor-or-self" "::")
[118]    AbbrevReverseStep    ::=    ".."
[119]    NodeTest    ::=    KindTest | NameTest
[120]    NameTest    ::=    EQName | Wildcard
[121]    Wildcard    ::=    "*"
| (NCName ":" "*")
| ("*" ":" NCName)
| (BracedURILiteral "*")
[122]    PostfixExpr    ::=    PrimaryExpr (Predicate | ArgumentList)*
[123]    ArgumentList    ::=    "(" (Argument ("," Argument)*)? ")"
[124]    PredicateList    ::=    Predicate*
[125]    Predicate    ::=    "[" Expr "]"
[126]    PrimaryExpr    ::=    Literal
| VarRef
| ParenthesizedExpr
| ContextItemExpr
| FunctionCall
| OrderedExpr
| UnorderedExpr
| Constructor
| FunctionItemExpr
[127]    Literal    ::=    NumericLiteral | StringLiteral
[128]    NumericLiteral    ::=    IntegerLiteral | DecimalLiteral | DoubleLiteral
[129]    VarRef    ::=    "$" VarName
[130]    VarName    ::=    EQName
[131]    ParenthesizedExpr    ::=    "(" Expr? ")"
[132]    ContextItemExpr    ::=    "."
[133]    OrderedExpr    ::=    "ordered" "{" Expr "}"
[134]    UnorderedExpr    ::=    "unordered" "{" Expr "}"
[135]    FunctionCall    ::=    EQName ArgumentList
[136]    Argument    ::=    ExprSingle | ArgumentPlaceholder
[137]    ArgumentPlaceholder    ::=    "?"
[138]    Constructor    ::=    DirectConstructor
| ComputedConstructor
[139]    DirectConstructor    ::=    DirElemConstructor
| DirCommentConstructor
| DirPIConstructor
[140]    DirElemConstructor    ::=    "<" QName DirAttributeList ("/>" | (">" DirElemContent* "</" QName S? ">"))
[141]    DirAttributeList    ::=    (S (QName S? "=" S? DirAttributeValue)?)*
[142]    DirAttributeValue    ::=    ('"' (EscapeQuot | QuotAttrValueContent)* '"')
| ("'" (EscapeApos | AposAttrValueContent)* "'")
[143]    QuotAttrValueContent    ::=    QuotAttrContentChar
| CommonContent
[144]    AposAttrValueContent    ::=    AposAttrContentChar
| CommonContent
[145]    DirElemContent    ::=    DirectConstructor
| CDataSection
| CommonContent
| ElementContentChar
[146]    CommonContent    ::=    PredefinedEntityRef | CharRef | "{{" | "}}" | EnclosedExpr
[147]    DirCommentConstructor    ::=    "<!--" DirCommentContents "-->"
[148]    DirCommentContents    ::=    ((Char - '-') | ('-' (Char - '-')))*
[149]    DirPIConstructor    ::=    "<?" PITarget (S DirPIContents)? "?>"
[150]    DirPIContents    ::=    (Char* - (Char* '?>' Char*))
[151]    CDataSection    ::=    "<![CDATA[" CDataSectionContents "]]>"
[152]    CDataSectionContents    ::=    (Char* - (Char* ']]>' Char*))
[153]    ComputedConstructor    ::=    CompDocConstructor
| CompElemConstructor
| CompAttrConstructor
| CompNamespaceConstructor
| CompTextConstructor
| CompCommentConstructor
| CompPIConstructor
[154]    CompDocConstructor    ::=    "document" "{" Expr "}"
[155]    CompElemConstructor    ::=    "element" (EQName | ("{" Expr "}")) "{" ContentExpr? "}"
[156]    ContentExpr    ::=    Expr
[157]    CompAttrConstructor    ::=    "attribute" (EQName | ("{" Expr "}")) "{" Expr? "}"
[158]    CompNamespaceConstructor    ::=    "namespace" (Prefix | ("{" PrefixExpr "}")) "{" URIExpr "}"
[159]    Prefix    ::=    NCName
[160]    PrefixExpr    ::=    Expr
[161]    URIExpr    ::=    Expr
[162]    CompTextConstructor    ::=    "text" "{" Expr "}"
[163]    CompCommentConstructor    ::=    "comment" "{" Expr "}"
[164]    CompPIConstructor    ::=    "processing-instruction" (NCName | ("{" Expr "}")) "{" Expr? "}"
[165]    FunctionItemExpr    ::=    NamedFunctionRef | InlineFunctionExpr
[166]    NamedFunctionRef    ::=    EQName "#" IntegerLiteral
[167]    InlineFunctionExpr    ::=    Annotation* "function" "(" ParamList? ")" ("as" SequenceType)? FunctionBody
[168]    SingleType    ::=    SimpleTypeName "?"?
[169]    TypeDeclaration    ::=    "as" SequenceType
[170]    SequenceType    ::=    ("empty-sequence" "(" ")")
| (ItemType OccurrenceIndicator?)
[171]    OccurrenceIndicator    ::=    "?" | "*" | "+"
[172]    ItemType    ::=    KindTest | ("item" "(" ")") | FunctionTest | AtomicOrUnionType | ParenthesizedItemType
[173]    AtomicOrUnionType    ::=    EQName
[174]    KindTest    ::=    DocumentTest
| ElementTest
| AttributeTest
| SchemaElementTest
| SchemaAttributeTest
| PITest
| CommentTest
| TextTest
| NamespaceNodeTest
| AnyKindTest
[175]    AnyKindTest    ::=    "node" "(" ")"
[176]    DocumentTest    ::=    "document-node" "(" (ElementTest | SchemaElementTest)? ")"
[177]    TextTest    ::=    "text" "(" ")"
[178]    CommentTest    ::=    "comment" "(" ")"
[179]    NamespaceNodeTest    ::=    "namespace-node" "(" ")"
[180]    PITest    ::=    "processing-instruction" "(" (NCName | StringLiteral)? ")"
[181]    AttributeTest    ::=    "attribute" "(" (AttribNameOrWildcard ("," TypeName)?)? ")"
[182]    AttribNameOrWildcard    ::=    AttributeName | "*"
[183]    SchemaAttributeTest    ::=    "schema-attribute" "(" AttributeDeclaration ")"
[184]    AttributeDeclaration    ::=    AttributeName
[185]    ElementTest    ::=    "element" "(" (ElementNameOrWildcard ("," TypeName "?"?)?)? ")"
[186]    ElementNameOrWildcard    ::=    ElementName | "*"
[187]    SchemaElementTest    ::=    "schema-element" "(" ElementDeclaration ")"
[188]    ElementDeclaration    ::=    ElementName
[189]    AttributeName    ::=    EQName
[190]    ElementName    ::=    EQName
[191]    SimpleTypeName    ::=    TypeName
[192]    TypeName    ::=    EQName
[193]    FunctionTest    ::=    Annotation* (AnyFunctionTest
| TypedFunctionTest)
[194]    AnyFunctionTest    ::=    "function" "(" "*" ")"
[195]    TypedFunctionTest    ::=    "function" "(" (SequenceType ("," SequenceType)*)? ")" "as" SequenceType
[196]    ParenthesizedItemType    ::=    "(" ItemType ")"
[197]    URILiteral    ::=    StringLiteral
[198]    RevalidationDecl    ::=    "declare" "revalidation" ("strict" | "lax" | "skip")
[199]    InsertExprTargetChoice    ::=    (("as" ("first" | "last"))? "into")
| "after"
| "before"
[200]    InsertExpr    ::=    "insert" ("node" | "nodes") SourceExpr InsertExprTargetChoice TargetExpr
[201]    DeleteExpr    ::=    "delete" ("node" | "nodes") TargetExpr
[202]    ReplaceExpr    ::=    "replace" ("value" "of")? "node" TargetExpr "with" ExprSingle
[203]    RenameExpr    ::=    "rename" "node" TargetExpr "as" NewNameExpr
[204]    SourceExpr    ::=    ExprSingle
[205]    TargetExpr    ::=    ExprSingle
[206]    NewNameExpr    ::=    ExprSingle
[207]    UpdatingFunctionCall    ::=    "invoke" "updating" PrimaryExpr "(" (ExprSingle ("," ExprSingle)*)? ")"
[208]    CopyModifyExpr    ::=    "copy" "$" VarName ":=" ExprSingle ("," "$" VarName ":=" ExprSingle)* "modify" ExprSingle "return" ExprSingle
[209]    EQName    ::=    QName | URIQualifiedName

A.1 Terminal Symbols

[210]    IntegerLiteral    ::=    Digits
[211]    DecimalLiteral    ::=    ("." Digits) | (Digits "." [0-9]*)
[212]    DoubleLiteral    ::=    (("." Digits) | (Digits ("." [0-9]*)?)) [eE] [+-]? Digits
[213]    StringLiteral    ::=    ('"' (PredefinedEntityRef | CharRef | EscapeQuot | [^"&])* '"') | ("'" (PredefinedEntityRef | CharRef | EscapeApos | [^'&])* "'")
[214]    URIQualifiedName    ::=    BracedURILiteral NCName
[215]    BracedURILiteral    ::=    "Q" "{" (PredefinedEntityRef | CharRef | [^&{}])* "}"
[216]    PredefinedEntityRef    ::=    "&" ("lt" | "gt" | "amp" | "quot" | "apos") ";"
[217]    EscapeQuot    ::=    '""'
[218]    EscapeApos    ::=    "''"
[219]    ElementContentChar    ::=    (Char - [{}<&])
[220]    QuotAttrContentChar    ::=    (Char - ["{}<&])
[221]    AposAttrContentChar    ::=    (Char - ['{}<&])
[222]    Comment    ::=    "(:" (CommentContents | Comment)* ":)"
[223]    PITarget    ::=    [http://www.w3.org/TR/REC-xml#NT-PITarget]XML
[224]    CharRef    ::=    [http://www.w3.org/TR/REC-xml#NT-CharRef]XML
[225]    QName    ::=    [http://www.w3.org/TR/REC-xml-names/#NT-QName]Names
[226]    NCName    ::=    [http://www.w3.org/TR/REC-xml-names/#NT-NCName]Names
[227]    S    ::=    [http://www.w3.org/TR/REC-xml#NT-S]XML
[228]    Char    ::=    [http://www.w3.org/TR/REC-xml#NT-Char]XML

The following symbols are used only in the definition of terminal symbols; they are not terminal symbols in the grammar of [A EBNF for XQuery 3.0 Grammar with Update extensions].

[229]    Digits    ::=    [0-9]+
[230]    CommentContents    ::=    (Char+ - (Char* ('(:' | ':)') Char*))