Is there a name for a syntax used for SQL?
E.g.) When you go to this page on BOL (SQL Books OnLine),
it shows the syntax of BEGIN DIALOG CONVERSION
BEGIN DIALOG [ CONVERSATION ] #dialog_handle
FROM SERVICE initiator_service_name
TO SERVICE 'target_service_name'
[ , { 'service_broker_guid' | 'CURRENT DATABASE' } ]
[ ON CONTRACT contract_name ]
[ WITH
[ { RELATED_CONVERSATION = related_conversation_handle
| RELATED_CONVERSATION_GROUP = related_conversation_group_id } ]
[ [ , ] LIFETIME = dialog_lifetime ]
[ [ , ] ENCRYPTION = { ON | OFF } ] ]
[ ; ]
What is the name/terminology of the syntax used above?
Transact-SQL Syntax Conventions (Transact-SQL) does not mention the name of their "Syntax Conventions".
I don't know whether this notation has a specific name, but it is not BNF - Backus-Naur Form, nor is it EBNF (extended BNF, aka ISO/IEC 14977:1996). It is loosely related, but only loosely related. In particular, BNF specifies that a non-terminal name is defined as some list of values. An example would be this CREATE TABLE statement from the SQL 2003 standard:
<table definition> ::=
CREATE [ <table scope> ] TABLE <table name> <table contents source>
[ ON COMMIT <table commit action> ROWS ]
The LHS ('<table definition>') is a non-terminal that can be referenced elsewhere. The '::=' operator is the 'is defined as' operator. The terms in angle brackets '<>' are more non-terminals (sometimes the same as the LHS, though not in this example), for which there is a definition somewhere else. The parts in square brackets '[ ... ]' are optional (may either be present - once - or may be omitted altogether). The SQL standard uses the notation:
<non-terminal> { [ , <non-terminal> ] }...
to indicate repetition.
The snippet in the question is not directly BNF because it does not use the 'LHS ::= RHS' notation; it effectively assumes the LHS does not need to be named. It also not clear from the notation (as shown - it may be clearer in the original document) whether things like 'CURRENT DATABASE' and 'target_server_name' are terminals or non-terminals (most likely, the current database is a terminal and target server name is not - but BNF would make that clear).
It's an informal, slightly abbreviated form of EBNF:
(http://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form)
used in Computer Language Documents at least since the late 60's.
I agree with Liao.
There is more to it. This is used not just for SQL syntax but to describe/define grammars of various artificial languages (programming languages belonging here as well).
I've seen this sort of syntax description for a long time. If you're interested in ancient history, take a look at the TOPS-10/TOPS-20 COBOL-74 Language Manual (AA-5059B-TK).
Related
I'm defining the syntax of the Move IR. The test suite for this language includes various annotations to enable testing. I need to treat comments of this form specially:
//! new-transaction
// check: "Keep(ABORTED { code: 123,"
This file is an example arithmetic_operators_u8.mvir.
So far, I've got this working by disallowing ordinary single-line comments.
module MOVE-ANNOTATION-SYNTAX-CONCRETE
imports INT-SYNTAX
syntax #Layout ::= r"([\\ \\n\\r\\t])" // Whitespace
syntax Annotation ::= "//!" "new-transaction" [klabel(NewTransaction), symbol]
syntax Check ::= "//" "check:" "\"Keep(ABORTED { code:" Int ",\"" [klabel(CheckCode), symbol]
endmodule
module MOVE-ANNOTATION-SYNTAX-ABSTRACT
imports INT-SYNTAX
syntax Annotation ::= "#NewTransaction" [klabel(NewTransaction), symbol]
syntax Check ::= #CheckCode(Int) [klabel(CheckCode), symbol]
endmodule
I'd like to also be able to use ordinary comments.
As a first step, I was able to change the Layout to allow commits only if the begin with a ! using r"(\\/\\/[^!][^\\n\\r]*)"
I'd like to exclude all comments that start with either //! or // check: from comments. What's a good way of implementing this?
Where can I find documentation for the regular expression language that K uses?
K uses flex for its scanner, and thus for its regular expression language. As a result, you can find documentation on its regular expression language here.
You want a regular expression that expresses that comments can't start with ! or check:, but flex doesn't support negative lookahead or not patterns, so you will have to exhaustively enumerate all the cases of comments that don't start with those sequence of characters. It's a bit tedious, sadly.
For reference, here is a (simplified) regular expression drawn from the syntax of C that represents all pragmas that don't start with STDC. It should give you an idea of how to proceed:
#pragma([:space:]*)([^S].*|S|S[^T].*|ST|ST[^D].*|STD|STD[^C].*|STDC[_a-zA-Z0-9].*)?$
I've been trying to write the graphql language grammar for grammarkit and I've found myself really stuck on an ambiguity issue for quite some time now. Keywords in graphql (such as: type, implements, scalar ) can also be names of types or fields. I.E.
type type implements type {}
At first I defined these keywords as tokens in the bnf but that'd mean the case above is invalid. But if I write these keywords directly as I'm describing the rule, It results in an ambiguity in the grammar.
An example of an issue I'm seeing based on this grammar below is if you define something like this
directive #foo on Baz | Bar
scalar Foobar #cool
the PSI viewer is telling me that in the position of #cool it's expecting a DirectiveAddtlLocation, which is a rule I don't even reference in the scalar rule. Is anyone familiar with grammarkit and have encountered something like this? I'd really appreciate some insight. Thank You.
Here's an excerpt of grammar for the error example I mentioned above.
{
tokens=[
LEFT_PAREN='('
RIGHT_PAREN=')'
PIPE='|'
AT='#'
IDENTIFIER="regexp:[_A-Za-z][_0-9A-Za-z]*"
WHITE_SPACE = 'regexp:\s+'
]
}
Document ::= Definition*
Definition ::= DirectiveTypeDef | ScalarTypeDef
NamedTypeDef ::= IDENTIFIER
// I.E. #foo #bar(a: 10) #baz
DirectivesDeclSet ::= DirectiveDecl+
DirectiveDecl ::= AT TypeName
// I.E. directive #example on FIELD_DEFINITION | ARGUMENT_DEFINITION
DirectiveTypeDef ::= 'directive' AT NamedTypeDef DirectiveLocationsConditionDef
DirectiveLocationsConditionDef ::= 'on' DirectiveLocation DirectiveAddtlLocation*
DirectiveLocation ::= IDENTIFIER
DirectiveAddtlLocation ::= PIPE? DirectiveLocation
TypeName ::= IDENTIFIER
// I.E. scalar DateTime #foo
ScalarTypeDef ::= 'scalar' NamedTypeDef DirectivesDeclSet?
Once your grammar sees directive #TOKEN on IDENTIFIER, it consumes a sequence of DirectiveAddtlLocation. Each of those consists of an optional PIPE followed by an IDENTIFIER. As you note in your question, the GraphQL "keywords" are really just special cases of identifiers. So what's probably happening here is that, since you allow any token as an identifier, scalar and Foobar are both being consumed as DirectiveAddtlLocation and it's never actually getting to see a ScalarTypeDef.
# Parses the same as:
directive #foo on Bar | Baz | scalar | Foobar
#cool # <-- ?????
You can get around this by listing out the explicit set of allowed directive locations in your grammar. (You might even be able to get pretty far by just copying the grammar in Appendix B of the GraphQL spec and changing its syntax.)
DirectiveLocation ::= ExecutableDirectiveLocation | TypeSystemDirectiveLocation
ExecutableDirectiveLocation ::= 'QUERY' | 'MUTATION' | ...
TypeSystemDirectiveLocation ::= 'SCHEMA' | 'SCALAR' | ...
Now when you go to parse:
directive #foo on QUERY | MUTATION
# "scalar" is not a directive location, so the DirectiveTypeDef must end
scalar Foobar #cool
(For all that the "identifier" vs. "keyword" distinction is a little weird, I'm pretty sure the GraphQL grammar isn't actually ambiguous; in every context where a free-form identifier is allowed, there's punctuation before a "keyword" could appear again, and in cases like this one there's unambiguous lists of not-quite-keywords that don't overlap.)
I believe my syntax is correct on this one, but for some reason the Anura FFL parser is not recognizing the second identifier choice defined in my where clause. What am I missing?
def(class creature creature, class game_state game) ->commands [
if(creature.choices,
if(choice < size(player.deck), [
set(player.deck, player.deck[0:choice] + player.deck[choice+1:]),
game.crypt.spawn_cards(creature.summoner, [card]),
set(creature.effects_tracking['Buried Treasure'], card),
] where card=player.deck[choice]
) where player=game.players[creature.summoner],
choice=creature.choices[0]
),
]
It gives me this error:
formula.cpp:1067 ASSERTION FAILED: Unknown identifier 'choice' :
if(choice < size(player.deck), [
^-----^
Note: if I change it to where a=... where b=... instead of where a=... , b=... then it parses.
The comma is being interpreted as an argument separator for if() -- it's ambiguous and impossible for the parser to tell intent. You have to use parens to disambiguate it, though I just recommend using there where...where syntax, as it's much more reliable. Commas are just too open to problems like this so this syntax on where clauses is deprecated.
I was wondering about notation in the documentation of PostgreSQL.
[ ], { }, [ ... ], [ , ... ], ( ) etc...
You can see these signs from SQL query example etc.
What do these signs mean?
[square brackets] indicate optional parameters. You can include any number of these, or none.
(parentheses) are simply part of the SQL code, and does not indicate any deeper meaning in the documentation.
{curly braces} and verical lines | indicate that there are a limited amount of options to choose, and you must choose one alternative or another.
[] - There are called "square brackets"
{} - These are called "curly brackets".
() - These are simply called "brackets".
I hope I answered your question?
[ is a square bracket, ( is a round bracket, { is a curly bracket .... so I believe
Use the word left or right in front of them. The ones above are left
Round bracket also known as parenthesis
I'm using StringTemplate 4.0.8 with Java.
In the StringTemplate-4 documentation, it says that
Dictionary strings can also be templates that can refer to attributes
that will become visible via dynamic scoping of attributes once the
dictionary value has been embedded within a template.
How exactly do I do that? Can I do something like this:
output(input) ::= "The output is: <aDicitionary.input>"
aDictionary ::= [
"someKey":"someValue",
"someOtherKey":"someOtherValue",
"aCertainKey": **HERE** i want the value to be <input>,
default:"doesnt matter"
]
So that output("someKey") results in The output is: someValue
and output(aCertainKey) results in "The output is: aCertainKey". If so, how exactly would the syntax look like?
I know that I could achieve the same by just not passing an input in one case and then checking if I have an input or not. But that would result in a lot of if's on the Java side which I
To use a dynamic dictionary entry:
output(input) ::= <%The output is: <aDicitionary.(input)>%>
Use no quotes around the template and put input in parentheses to evaluate it.
To have dynamic content in a dictionary (the subject of the cited block):
aDictionary ::= [
"someKey":"someValue",
"someOtherKey":"someOtherValue",
"aCertainKey": {input from scope <inputFromScope>},
default:"doesnt matter"
]
Use braces around the keys and variable (or template) references inside. Now calling
<output(input="aCertainKey", inputFromScope="myInput")>
will output
The output is: input from scope myInput