Antlr4 order of token in lexer - .net-core

lexer grammar
DESC: D | D E S C;
.
.
.
INCREMENTOPTION: S | H | M | D;
parser grammar:
sortExpression: integer? sortFieldList Desc = DESC?;
.
.
.
incrementOption: integer INCREMENTOPTION;
in the case of input 'd' i have a problem.
each of DESC or INCREMENTOPTION token be the upper token in lexer that is matched and the other one not matched
what can i do?!

You will have to do something like this:
sortExpression. : integer? sortFieldList desc?;
incrementOption : integer incrementoption;
desc : DESC | SINGLE_D;
incrementoption : SINGLE_D | SINGLE_S_H_M;
DESC : D E S C;
SINGLE_D : D;
SINGLE_S_H_M : S | H | M;

Related

How to duplicate input into outputs with jq?

I'm trying to adapt the following snippet:
echo '{"a":{"value":"b"}, "c":{"value":"d"}}' \
| jq -r '. as $in | keys[] | [$in[.].value | tostring + " 1"] | #tsv'
b 1
d 1
to output:
b 1
b 2
d 1
d 2
The following adaptation produces the desired output:
echo '{"a":{"value":"b"}, "c":{"value":"d"}}' |
jq -r '
def addindex(start;lessthan):
range(start;lessthan) as $i | "\(.) \($i)";
. as $in
| keys[]
| $in[.].value
| addindex(1;3)'
Note that keys emits the key names after they have been sorted, whereas keys_unsorted retains the ordering.

SQLITE order by numeric and not alphabetic

When I order my database SQLITE by Classement I have this :
Classement | Nom
1 | clem
10 | caro
11 | flo
12 | raph
2 | prisc
3 | karim
4 | prout
I would like to get :
Classement | Nom
1 | clem
2 | prisc
3 | karim
4 | prout
10 | caro
11 | flo
12 | raph
Here is my code :
SELECT t.Classement
FROM tableau t
WHERE 1 = (SELECT 1 + COUNT (*) FROM tableau t2 WHERE t2.Classement < t.Classement OR ( t2.Classement == t.Classement AND t2.Nom < t.Nom ))
Can anyone help me ?
Thank you!
I guess column Classement is not an integer but character. So try this:
SELECT * FROM tableau ORDER BY cast(Classement as integer);
You get alphabetic order if the values are strings.
To change the table so that all Classement values are numbers, ensure that the column type is not a text type, and use this:
UPDATE tableau SET Classement = CAST(Classement AS NUMBER);

SPARQL count unique value combinations

I have been working on a SPARQL query to find unique value combinations in my graph store. But I dont succeed.
Basically what I try to do is:
a b c
e f g
e r t
a b c
k l m
e f g
a b c
result:
a b c | 3
e f g | 2
e r t | 1
k l m | 1
Tried several constructions, with distincts, group by`s and sub queries but I dont succeed.
Last Try:
SELECT (count (*) as ?n){
SELECT DISTINCT ?value1 ?value2 ?value3 WHERE {
?instance vocab:relate ?value1 .
?instance vocab:relate ?value2 .
?instance vocab:relate ?value3 .
}
}
RDF:
<http://test.example.com/instance1>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/c> , <http://test.example.com/b> , <http://test.example.com/a> .
<http://test.example.com/instance6>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/g> , <http://test.example.com/f> , <http://test.example.com/e> .
<http://test.example.com/instance4>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/c> , <http://test.example.com/b> , <http://test.example.com/a> .
<http://test.example.com/instance2>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/g> , <http://test.example.com/f> , <http://test.example.com/e> .
<http://test.example.com/instance7>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/c> , <http://test.example.com/b> , <http://test.example.com/a> .
<http://test.example.com/instance5>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/m> , <http://test.example.com/l> , <http://test.example.com/k> .
<http://test.example.com/instance3>
a <http://test.example.com#Instance> ;
<http://vocab.example.com/relate>
<http://test.example.com/t> , <http://test.example.com/r> , <http://test.example.com/e> .
AKSW's comment is spot on: you need to add an ordering criteria to the values so that you're not considering all the different possible ways of ordering the values. Also, remember that RDF doesn't have "duplicate" triples, so
:a :p :c, :c, :d
is the same as
:a :p :c, :d
so the appropriate comparison is < as opposed to <=, since without duplicate triples, you'd never have an = case. Also, since the values are IRIs, you need to get their string values before you can compare with <, but the str function will take care of that.
prefix v: <http://vocab.example.com/>
prefix : <http://test.example.com/>
select ?a ?b ?c (count(distinct ?i) as ?count) where {
?i v:relate ?a, ?b, ?c .
filter (str(?a) < str(?b) && str(?b) < str(?c))
}
group by ?a ?b ?c
------------------------
| a | b | c | count |
========================
| :a | :b | :c | 3 |
| :e | :f | :g | 2 |
| :e | :r | :t | 1 |
| :k | :l | :m | 1 |
------------------------

What would the conversion of this from EBNF to BNF be? Also, what is the leftmost derivation?

I need to convert this from EBNF to BNF.
<statement> ::= <ident> = <expr>
<statement> ::= IF <expr> THEN <statement> [ ELSE <statement> ] END
<statement> ::= WHILE <expr> DO <statement> END
<statement> ::= BEGIN <statement> {; <statement>} END
Also, I'm stuck on this one:
E -> E+T | E-T | T
T -> T*F | T/F | F
F -> (E) | VAR | INT
VAR -> a | b | c
INT -> 0 | 1 | 2| 3 | 4| 5 | 6 | 7 | 8 | 9
After modifying the grammer to add a ^ operator, What is the leftmost derivation that your grammar assigns to the expression a^2^b*(c+1)? You may find it convenient to sketch the parse tree for this expression first, and then figure out the leftmost derivation from that.
I added G -> F^G | G and then got G 2 G b E as my answer but am not sure if that is correct.

Get the type of a variable in VBScript

How do I get the type of a variable using VBScript?
Is VarType what you need?
Returns a value indicating the subtype of a variable.
+--------------+-------+---------------------------------------------+
| Constant | Value | Description |
+--------------+-------+---------------------------------------------+
| vbEmpty | 0 | Empty (uninitialized) |
| vbNull | 1 | Null (no valid data) |
| vbInteger | 2 | Integer |
| vbLong | 3 | Long integer |
| vbSingle | 4 | Single-precision floating-point number |
| vbDouble | 5 | Double-precision floating-point number |
| vbCurrency | 6 | Currency |
| vbDate | 7 | Date |
| vbString | 8 | String |
| vbObject | 9 | Automation object |
| vbError | 10 | Error |
| vbBoolean | 11 | Boolean |
| vbVariant | 12 | Variant (used only with arrays of Variants) |
| vbDataObject | 13 | A data-access object |
| vbDecimal | 14 | Decimal Value |
| vbByte | 17 | Byte |
| vbLongLong | 20 | LongLong integer (64 bit) |
| vbArray | 8192 | Array |
+--------------+-------+---------------------------------------------+
The VarType function never returns the value for Array by itself. It
is always added to some other value to indicate an array of a
particular type. The value for Variant is only returned when it has
been added to the value for Array to indicate that the argument to the
VarType function is an array. For example, the value returned for an
array of integers is calculated as 2 + 8192, or 8194. If an object has
a default property, VarType (object) returns the type of its default
property.
If you want to get the type name of an object assigned to a variable with Set, you can use TypeName instead.
Class SomeClass
'' empty class
End Class
Dim x
Set x = New SomeClass
WScript.Echo TypeName(x) '' displays "SomeClass"
Tmdean's answer also works to get the Type Name (not an integer) of (almost) all other types of variables (per http://msdn.microsoft.com/en-us/library/ie/y58s1cs6%28v=vs.84%29.aspx)
dim i,s,a
i = 1
s = "Hello world"
a = split("Hello World"," ")
WScript.Echo TypeName(i) 'Displays "Integer"
WScript.Echo TypeName(s) 'Displays "String"
WScript.Echo TypeName(a) 'Displays "Variant()"
Dim a, b, c, d, e, f
a = 10
b = "text"
c = Split("John Doe,Jane Smith,Dick Tracy", ",")
d = 1.2
e = Null
f = True
MsgBox "'a' is " & fVarType(a) & vbNewLine & _
"'b' is " & fVarType(b) & vbNewLine & _
"'c' is " & fVarType(c) & vbNewLine & _
"'d' is " & fVarType(d) & vbNewLine & _
"'e' is " & fVarType(e) & vbNewLine & _
"'f' is " & fVarType(f) & vbNewLine & _
"'g' is " & fVarType(c(0))
Function fVarType(x)
Const ArrayCode = 8192
Dim y
y = VarType(x)
If y < ArrayCode Then
fVarType = fGetType(VarType(x))
Else
fVarType = "vbArray with " & fGetType(VarType(x)- ArrayCode) & " elements"
End If
End Function
Function fGetType(vType)
Select Case vType
Case 0 fGetType = "vbEmpty"
Case 1 fGetType = "vbNull"
Case 2 fGetType = "vbInteger"
Case 3 fGetType = "vbLong"
Case 4 fGetType = "vbSingle"
Case 5 fGetType = "vbDouble"
Case 6 fGetType = "vbCurrency"
Case 7 fGetType = "vbDate"
Case 8 fGetType = "vbString"
Case 9 fGetType = "vbObject"
Case 10 fGetType = "vbError"
Case 11 fGetType = "vbBoolean"
Case 12 fGetType = "vbVariant"
Case 13 fGetType = "vbDataObject"
Case 14 fGetType = "vbDecimal"
Case 17 fGetType = "vbByte"
Case Else fGetType = "undetected"
End Select
End Function
vartype is appropriate ...
Dim x
x=123
msgbox VarType(x)
Dim TypeDictionary
Set TypeDictionary = CreateObject("Scripting.Dictionary")
TypeDictionary.Add 0, "vbEmpty"
TypeDictionary.Add 1, "vbNull"
TypeDictionary.Add 2, "vbInteger"
TypeDictionary.Add 3, "vbLong"
TypeDictionary.Add 4, "vbSingle"
TypeDictionary.Add 5, "vbDouble"
TypeDictionary.Add 6, "vbCurrency"
TypeDictionary.Add 7, "vbDate"
TypeDictionary.Add 8, "vbString"
TypeDictionary.Add 9, "vbObject"
TypeDictionary.Add 10, "vbError"
TypeDictionary.Add 11, "vbBoolean"
TypeDictionary.Add 12, "vbVariant"
TypeDictionary.Add 13, "vbDataObject"
TypeDictionary.Add 17, "vbByte"
Public Function GetType(argument)
GetType = TypeDictionary.Item(VarType(argument))
End Function
This version invests more effort setting up the dictionary, but then looks up any type in one check(fingers crossed) rather than checking every single type every single time.
equivalent JScript code(hypothetical, since JScript has typeof, and lacks many vb types):
var TypeDictionary = {
0: 'vbEmpty',
1: 'vbNull',
2: 'vbInteger',
3: 'vbLong',
4: 'vbSingle',
5: 'vbDouble',
6: 'vbCurrency',
7: 'vbDate',
8: 'vbString',
9: 'vbObject',
10: 'vbError',
11: 'vbBoolean',
12: 'vbVariant',
13: 'vbDataObject',
17: 'vbByte'
};
var GetType = function() {
return TypeDictionary[arguments[0]];
};

Resources