ANTLR Reading blocks of values - antlr

I have been working on this for more than 2 days without success. It will be a common problem, but I can't find a solution. I did do a search!
Problem:
I have some data that I want to read in say, 5 values per line. I know how many I want to read from a value read previously. For example, 6 values to read, spread over 2 lines...
6
10 20 30 40 50
60
so after every 5 variables I want to read a new line. If there are 0 variables, I want to skip the bit to do with this, and if I want to read an exact multiple of 5 variables, then I want to avoid duplicating the NL call.
I tried this...
varblock[ Integer count ]
#init{
Integer varIndex = 0;
}
: { count > 0 }? ( dp=NUMBER { count--; varIndex++; }
{ ( varIndex \% 5 ) == 0 }? NL { varIndex = 0; }
)+ { varIndex > 0 }? => NL
|
;
But I get...
failed predicate: { ( varIndex \% 5 ) == 0 }?
It might be that I misunderstand predicates. I have several other predicates in my grammar that seem to work, but they are not of this type. There, I am trying to skip bits of the grammar depending on the version of the input file.
Thanks.

NL is just a line feed that is expected at the end of the input lines.
NL : ( '\n' | '\r' )+ ;
In other lines we read several other things such as...
"IPE270" "BS 7191 GR 355C" 0.0 0 0
and the values such as STRING, FLOAT, or NUMBER must be on those lines in expected sequences. So if we encounter a NL before we have read the requisite data values, there is a syntax error. So, perhaps the answer to your question is "Yes".
Perhaps I just (over?) simplified the example.

SOLVED:
It was a problem of brackets. I looked at the generated parser code to get a clue.
varblock[ Integer count ]
#init{
Integer index = 0;
}
: ( { count > 0 }? => ( NUMBER { count--; index++; }
| { (index \% 5) == 0 }? => NL ) )+ { index > 0 }? => NL
|
;
This reads values up to 5 per line.

Related

print n times number from 1. Need print (1 2 2 3 3 3 4)

I can't figure out how to solve the following problem: there is a number n. Output the numbers to the console in order separated by a space, but so that the next digit in the iteration is output as many times as it is a digit, and at the same time so that there are no more than n digits in the output. Сan anyone suggest the correct algorithm?
example: have n = 7, need print (1 2 2 3 3 3 4) in kotlin
what i try:
var n = 7
var count = 1
var i = 1
for (count in 1..n) {
for (i in 1..count) {
print(count)
}
}
}
var n = 11
var count = 1
var i = 1
var size = 0
// loop# for naming a loop in kotlin and inside another loop we can break or continue from outer loop
loop# for (count in 1..n) {
for (i in 1..count) {
print(count)
size++
if (size == n){
break#loop
}
}
}
You can use "#" for naming loops and if you want to break from that loop, you can use this syntax in kotlin. It worked for me.
For kotlin labeled break you can look at this reference: link
var count = 1
var n = 7
for(count in 1..n) {
print(count.toString().repeat(count))
}
count.toString() converts an integer to a string, .repeat() function repeats count times the string.
In case you need to add a space between each number, you can add the following:
print(" ")
Using generateSequence and Collections functions:
val n = 7
println(generateSequence(1) {it + 1}
.flatMap{e -> List(e){e}}
.take(n)
.joinToString(" "))
Your example is correct, You have to put a space between the printing
you can follow the code from this link
Kotlin lang code snippet
or the following code snippet
fun main() {
var n = 7
var count = 1
var i = 1
for (count in 1..n) {
for (i in 1..count) {
print(count)
print(' ')
}
}
}
For completeness, here's another approach where you write your own sequence function which produces individual values on demand (instead of creating intermediate lists)
sequence {
var digit = 1
while (true) {
for (i in 1..digit) yield(digit)
digit++
}
}.take(7)
.joinToString(" ")
.run(::print)
Not a big deal in this situation, but good to know!

How to build lazy lists with defined generators and is there a "takeWhile" alternative?

I am reading through perl6intro on lazy lists and it leaves me confused about certain things.
Take this example:
sub foo($x) {
$x**2
}
my $alist = (1,2, &foo ... ^ * > 100);
will give me (1 2 4 16 256), it will square the same number until it exceeds 100. I want this to give me (1 4 9 16 25 .. ), so instead of squaring the same number, to advance a number x by 1 (or another given "step"), foo x, and so on.
Is it possible to achieve this in this specific case?
Another question I have on lazy lists is the following:
In Haskell, there is a takeWhile function, does something similar exist in Perl6?
I want this to give me (1 4 9 16 25 .. )
The easiest way to get that sequence, would be:
my #a = (1..*).map(* ** 2); # using a Whatever-expression
my #a = (1..*).map(&foo); # using your `foo` function
...or if you prefer to write it in a way that resembles a Haskell/Python list comprehension:
my #a = ($_ ** 2 for 1..*); # using an in-line expression
my #a = (foo $_ for 1..*); # using your `foo` function
While it is possible to go out of one's way to express this sequence via the ... operator (as Brad Gilbert's answer and raiph's answer demonstrate), it doesn't really make sense, as the purpose of that operator is to generate sequences where each element is derived from the previous element(s) using a consistent rule.
Use the best tool for each job:
If a sequence is easiest to express iteratively (e.g. Fibonacci sequence):
Use the ... operator.
If a sequence is easiest to express as a closed formula (e.g. sequence of squares):
Use map or for.
Here is how you could write a Perl 6 equivalent of Haskell's takewhile.
sub take-while ( &condition, Iterable \sequence ){
my \iterator = sequence.iterator;
my \generator = gather loop {
my \value = iterator.pull-one;
last if value =:= IterationEnd or !condition(value);
take value;
}
# should propagate the laziness of the sequence
sequence.is-lazy
?? generator.lazy
!! generator
}
I should probably also show an implementation of dropwhile.
sub drop-while ( &condition, Iterable \sequence ){
my \iterator = sequence.iterator;
GATHER: my \generator = gather {
# drop initial values
loop {
my \value = iterator.pull-one;
# if the iterator is out of values, stop everything
last GATHER if value =:= IterationEnd;
unless condition(value) {
# need to take this so it doesn't get lost
take value;
# continue onto next loop
last;
}
}
# take everything else
loop {
my \value = iterator.pull-one;
last if value =:= IterationEnd;
take value
}
}
sequence.is-lazy
?? generator.lazy
!! generator
}
These are only just-get-it-working examples.
It could be argued that these are worth adding as methods to lists/iterables.
You could (but probably shouldn't) implement these with the sequence generator syntax.
sub take-while ( &condition, Iterable \sequence ){
my \iterator = sequence.iterator;
my \generator = { iterator.pull-one } …^ { !condition $_ }
sequence.is-lazy ?? generator.lazy !! generator
}
sub drop-while ( &condition, Iterable \sequence ){
my \end-condition = sequence.is-lazy ?? * !! { False };
my \iterator = sequence.iterator;
my $first;
loop {
$first := iterator.pull-one;
last if $first =:= IterationEnd;
last unless condition($first);
}
# I could have shoved the loop above into a do block
# and placed it where 「$first」 is below
$first, { iterator.pull-one } … end-condition
}
If they were added to Perl 6/Rakudo, they would likely be implemented with Iterator classes.
( I might just go and add them. )
A direct implementation of what you are asking for is something like:
do {
my $x = 0;
{ (++$x)² } …^ * > 100
}
Which can be done with state variables:
{ ( ++(state $x = 0) )² } …^ * > 100
And a state variable that isn't used outside of declaring it doesn't need a name.
( A scalar variable starts out as an undefined Any, which becomes 0 in a numeric context )
{ (++( $ ))² } …^ * > 100
{ (++$)² } …^ * > 100
If you need to initialize the anonymous state variable, you can use the defined-or operator // combined with the equal meta-operator =.
{ (++( $ //= 5))² } …^ * > 100
In some simple cases you don't have to tell the sequence generator how to calculate the next values.
In such cases the ending condition can also be simplified.
say 1,2,4 ...^ 100
# (1 2 4 8 16 32 64)
The only other time you can safely simplify the ending condition is if you know that it will stop on the value.
say 1, { $_ * 2 } ... 64;
# (1 2 4 8 16 32 64)
say 1, { $_ * 2 } ... 3;
# (1 2 4 8 16 32 64 128 256 512 ...)
I want this to give me (1 4 9 16 25 .. )
my #alist = {(++$)²} ... Inf;
say #alist[^10]; # (1 4 9 16 25 36 49 64 81 100)
The {…} is an arbitrary block of code. It is invoked for each value of a sequence when used as the LHS of the ... sequence operator.
The (…)² evaluates to the square of the expression inside the parens. (I could have written (…) ** 2 to mean the same thing.)
The ++$ returns 1, 2, 3, 4, 5, 6 … by combining a pre-increment ++ (add one) with a $ variable.
In Haskell, there is a takeWhile function, does something similar exist in Perl6?
Replace the Inf from the above sequence with the desired end condition:
my #alist = {(++$)²} ... * > 70; # stop at step that goes past 70
say #alist; # [1 4 9 16 25 36 49 64 81]
my #alist = {(++$)²} ...^ * > 70; # stop at step before step past 70
say #alist; # [1 4 9 16 25 36 49 64]
Note how the ... and ...^ variants of the sequence operator provide the two variations on the stop condition. I note in your original question you have ... ^ * > 70, not ...^ * > 70. Because the ^ in the latter is detached from the ... it has a different meaning. See Brad's comment.

Check brackets are balanced/matching

I have some MS Word documents which I have transferred the entire contents into a SQL table.
The contents contain a number of square brackets and curly brackets e.g.
[{a} as at [b],] {c,} {d,} etc
and I need to do a check to make sure that the brackets are balanced/matching, e.g. the below contents should return false:
- [{a} as at [b], {c,} {d,}
- ][{a} as at [b], {c,} {d,}
- [{a} as at [b],] {c,} }{d,
What I've done so far is extracted all the brackets and stored their info into a SQL table like below:
(paragraph number, bracket type, bracket position, bracket level)
3 [ 8 1
3 ] 18 0
3 [ 23 1
3 ] 35 0
7 [ 97 1
7 ] 109 0
7 [ 128 1
7 { 129 2
7 } 165 1
7 [ 173 2
7 ] 187 1
7 ] 189 0
7 { 192 1
7 } 214 0
7 { 216 1
7 } 255 0
7 { 257 1
7 } 285 0
7 { 291 1
7 } 326 0
7 { 489 1
7 } 654 0
I am unsure how the algorithm will work to do the check on whether the brackets are balanced in each paragraph, and give an error message when they are not.
Any advice would be appreciated!
EDIT:
Code will need to work for the following scenario too;
(paragraph number, bracket type, bracket position, bracket level)
15 [ 543 1
15 { 544 2
15 } 556 1
15 [ 560 2
15 ] 580 1
15 ] 581 0
15 [ 610 1
15 ] 624 0
15 [ 817 1
15 ] 829 0
does this have to be on sql server ?
a simple solution would be to use a general purpose language and use a stack.
Read the string character by character
if you encounter a opening brace push it to stack.
if you encounter a closing brace pop.
All brackets are matched if
after reading the paragraph completely the stack is empty.
UNLESS one of the below happens during the process
you had to pop an empty stack
the popped bracket does not match the closing bracket
its not a good idea to use regex to match brackets, they are not meant to be used like that
I'm not sure which tool you have available, but here is a tested JavaScript function which validates that all (possibly nested) square brackets and curly braces are properly matched:
function isBalanced(text) {
var re = /\[[^[\]{}]*\]|\{[^[\]{}]*\}/g;
while (text.search(re) !== -1) { text = text.replace(re, ''); }
return !(/[[\]{}]/.test(text))
}
It works by matching and removing innermost balanced pairs in an iterative manner until there are no more matching pairs left. Once this is complete, a test is made to see if any square bracket or curly braces remain. If any remain, then the function returns false, otherwise it returns true. You should be able to implement this function in just about any language.
Note that this assumes that the square and curly brace pairs are not interleaved like so: [..{..]..}
Hope this helps.
Addendum: Extended version for: (), {}, [] and <>
The above method can be easily extended to handle testing all four matching bracket types: (), {}, [] and <>, like so:
/*#!(?#!js\/g re Rev:20150530_121324)
# Innermost bracket matching pair from 4 global alternatives:
\( [^(){}[\]<>]* \) # Either g1of4. Innermost parentheses.
| \{ [^(){}[\]<>]* \} # Or g2of4. Innermost curly braces.
| \[ [^(){}[\]<>]* \] # Or g3of4. Innermost square brackets.
| \< [^(){}[\]<>]* \> # Or g4of4. Innermost angle brackets.
!#*/
function isBalanced(text) {
var re = /\([^(){}[\]<>]*\)|\{[^(){}[\]<>]*\}|\[[^(){}[\]<>]*\]|\<[^(){}[\]<>]*\>/g;
while (text.search(re) !== -1) { text = text.replace(re, ''); }
return !(/[(){}[\]<>]/.test(text));
}
Note the regex has been documented in an extended mode C comment.
Edit 20150530: Extended to handle a mix of all four matching bracket types: (), {}, [] and <>.
I agree with user mzzzzb. I've been working on a coding challenge that is somewhat similar and came up with the following solution in JavaScript:
function isBalanced(str) {
const stack = [];
const pairs = { ')': '(', ']': '[', '}': '{' };
return str.split('').reduce((res, e) => {
// if its opening, put in stack
if (['(', '{', '['].includes(e)) stack.push(e);
// if closing, compare thru stack
else if ([')', '}', ']'].includes(e)) {
if (stack.pop() !== pairs[e]) return false;
}
return res;
// stack must also be empty
}, true) && stack.length === 0;
}

simple math expression parser

I have a simple math expression parser and I want to build the AST by myself (means no ast parser). But every node can just hold two operands. So a 2+3+4 will result in a tree like this:
+
/ \
2 +
/ \
3 4
The problem is, that I am not able to get my grammer doing the recursion, here ist just the "add" part:
add returns [Expression e]
: op1=multiply { $e = $op1.e; Print.ln($op1.text); }
( '+' op2=multiply { $e = new AddOperator($op1.e, $op2.e); Print.ln($op1.e.getClass(), $op1.text, "+", $op2.e.getClass(), $op2.text); }
| '-' op2=multiply { $e = null; } // new MinusOperator
)*
;
But at the end of the day this will produce a single tree like:
+
/ \
2 4
I know where the problem is, it is because a "add" can occour never or infinitly (*) but I do not know how to solve this. I thought of something like:
"add" part:
add returns [Expression e]
: op1=multiply { $e = $op1.e; Print.ln($op1.text); }
( '+' op2=(multiply|add) { $e = new AddOperator($op1.e, $op2.e); Print.ln($op1.e.getClass(), $op1.text, "+", $op2.e.getClass(), $op2.text); }
| '-' op2=multiply { $e = null; } // new MinusOperator
)?
;
But this will give me a recoursion error. Any ideas?
I don't have the full grammar to test this solution, but consider replacing this (from the first add rule in the question):
$e = new AddOperator($op1.e, $op2.e);
With this:
$e = new AddOperator($e, $op2.e); //$e instead of $op1.e
This way each iteration over ('+' multiply)* extends e rather than replaces it.
It may require a little playing around to get it right, or you may need a temporary Expression in the rule to keep things managed. Just make sure that the last expression created by the loop is somewhere on the right-hand side of the = operator, as in $e = new XYZ($e, $rhs.e);.

Is there a way to make Objective-C support a multi-variable switch construct?

I was wondering: is there a way to make Objective-C support a multi-variable switch construct?
I mean, very often I have to deal with problems in which the solution depends on a pair of variables instead of a single one. For a long list of single variable values one can use a simple switch/case construct:
switch (var) {
case 0: ...
case 1: ...
default: ...
}
But when you have to deal with the combination of two values you often happen to do something like this:
switch (var1) {
case 0:
switch (var2) {
case 0: ...
case 1: ...
}
case 1:
switch (var2) {
case 0: ...
case 1: ...
}
...
}
And it gets more and more complicated ... What I'd really love to do is something like this:
switch (var1, var2) {
case (0,0) : ...
case (1,0) : ...
case (*,1) : ...
default: ...
}
that will result in a more simple and readable construct. Is there any solution to support such a structure? Or a slight variation of this one?
I like the answer from saphrosit but I'll make try to make it easy to understand.
Imagine the possible outcomes of your problem as squares in a grid where one edge of the edge of the grid represents values of var1 and the other edge represents the possible values of var2, then if you incrementialy counted through the squares of the of the grid you would it get something like this
|| var1 |
|| 0 | 1 | 2 | ... | j | ... | n-1 |
======++=====================================================================|
0 || 0 | 1 | 2 | ... | j | ... | n-1 |
---||---------+-----------+-----------+-----+-----------+-----+-----------|
1 || n | n+1 | n+2 | ... | n+j | ... | n+(n-1) |
---||---------+-----------+-----------+-----+-----------+-----+-----------|
2 || 2*n | 2*n+1 | 2*n+2 | ... | 2*n+j | ... | 2*n+(n-1) |
v ---||---------+-----------+-----------+-----+-----------+-----+-----------|
a || . | . | . | | . | | . |
r ---||---------+-----------+-----------+-----+-----------+-----+-----------|
2 i || i*n | i*n+1 | i*n+2 | ... | i*n+j | ... | i*n+(n-1) |
---||---------+-----------+-----------+-----+-----------+-----+-----------|
|| . | . | . | | . | | . |
----||---------+-----------+-----------+-----+-----------+-----+-----------|
m-1 || (m-1)*n | (m-1)*n+1 | (m-1)*n+2 | ... | (m-1)*n+j | ... | mn-1 | <-- (m-1)*n+(n-1) = m*n-n + (n-1) = mn-1
------||---------+-----------+-----------+-----+-----------+-----+-----------|
This would is called a row major matrix since you start by counting accross the rows. There is also a column major matrix where you start counting down first instead of across. This is how matrixes are stored in the C BLAS library so it should be very fimilar to many people.
In your case the outcome you're looking for can be addressed as var3 = var2*n + var1 you could lay this out in code as
#define N 10 // Maximum size of var1
int main() {
int var1 = 1;
int var2 = 1;
switch(var1 + var2 * N){
case 1 + 1 * N: printf("One One"); break;
case 2 + 2 * N: printf("Two Two"); break;
default:
printf("Bada Bing");
}
return 0;
}
NOTE: the code that was here earlier wouldn't have worked, this works.
In your question you mention:
"What I'd really love to do is something like this:"
switch (var1, var2) {
case (0,0) : ...
case (1,0) : ...
case (*,1) : ...
default: ...
}
If it is the case that your possible values are in the range {0..n}, there are two methods you could use.
You could construct a
multidimensional array of selectors
and then select the correct selector
using your var1, var2. (This method is more efficient due to the constructing of the selectors at compile time)
You could construct the selector
name based on the values of the
var,var2 variables.
BOTH methods are exemplified here in this code snippet.
- (void) case00 {
NSLog(#"Case ZeroZero");
}
- (void) testSelectorIdea {
NSInteger var1 = 0;
NSInteger var2 = 0;
// ----- ARRAY OF SELECTORS METHOD ----
SEL selectors[2][2] = {#selector(case00),#selector(case01), #selector(case10), #selector(case11)};
[self performSelector:selectors[var1][var2]];
// ----- SELECTOR CONSTRUCTION METHOD ----
NSString * selectorName = [NSString stringWithFormat:#"case%d%d",var1,var2];
SEL selector = NSSelectorFromString(selectorName);
[self performSelector:selector];
}
As others have said, if you find yourself wanting to do this, then you should really think about a way of structuring your data better. You seem like you're dealing with scalar types. I tend to find a little c++ goes a long way there, and you can integrate that into an objective-c project using objective-c++. That said, if you're sure you want what you say you want and you're not averse to evilness with the preprocessor, you could try something like this:
#define BEGIN_SWITCH(type,...) ({ typedef type T; T switchVars[] = { __VA_ARGS__ }; BOOL performAction;
#define CASE(...) { \
T caseVars[] = { __VA_ARGS__ }; \
performAction = YES; \
size_t count = MIN(sizeof(switchVars), sizeof(caseVars)) / sizeof(T); \
for (size_t i = 0 ; i < count / sizeof(T) ; i++) \
{ \
if (caseVars[i] != switchVars[i]) \
{ \
performAction = NO; \
break; \
} \
} \
} \
if (performAction)
#define END_SWITCH });
int main (int argc, char const *argv[])
{
id pool = [[NSAutoreleasePool alloc] init];
int a1 = 0;
int a2 = 5;
int a3 = 10;
BEGIN_SWITCH(int, a1, a2, a3)
CASE(0,5) NSLog(#"0,5");
CASE(1,2,3) NSLog(#"1,2,3");
CASE(0,5,10) NSLog(#"0,5,10");
CASE(1) NSLog(#"1");
END_SWITCH
[pool drain];
return 0;
}
It isn't quite like switch/case, as you can't stack multiple case: clauses on top of oneanother. You'd have to add in default and break somehow in any case -- maybe you could do this with some extra macros and a goto for break. Standard caveats about the prepreocessor apply: It isn't pretty as Objective-C, and will be prone to giving you all kinds of unfathomable syntax errors. But if you really want to alter the syntax of the language, then your options are either something like this or getting a job with Apple.
Not a solution, just a workaround: you may think to something like this
SInt32 var1, var2;
/*...*/
SInt64 var3 = var1<<32 + var2;
switch(var3) {
.
.
.
}
if your vars have some particular property you may use it to do some slight simplifications, i.e. if vars are < 10 then you may use
var3 = 10*var1+var2;
Are you sure that this is a good programming style to have such constructions :) ?
Switch statement is designed to speedup multiple if () else if () statements, but when you will need to compare multiple variables that optimization will go.
The one way is to use logical shifts/other operations to place multiple variables in one, and the other is to use if ((a == 0) & (b == 0)) {...} else if ((a == 0) && (b == 1))....
That wouldn't take a lot of space.
Due to the existences of the ObjC-Runtime, it should be possible to build up a switch replacement with things like the selector type SEL/#selector()/performSelector, NSSelectorFromString, NSArrays/NSDictionaries and NSIndexPathes, that is way more powerful than the c-switch statement.
But it won't reside on language level, as this Python-code isn't as-well.
How about something like:
switch ([NSString StringWithFormat: #"%d,%d", var1, var2]) {
case (#"0,0") : ...
case (#"1,0") : ...
case (#"*,1") : ...
default: ...
}
The problem would be *,1 but you could either spell those all out, or you could do a contains #",1" (I forget the right syntax atm)