VHDL Type Attribute - attributes

I have found these lines in mathpack.vhd:
constant TWO_AT_MINUS : REAL_VECTOR := POWER_OF_2_SERIES(
NATURAL_VECTOR'(100, 90),1.0,
MAX_ITER);
The signature of the POWER_OF_2_SERIES is in the same file:
function POWER_OF_2_SERIES (D : in NATURAL_VECTOR; INITIAL_VALUE : in REAL;
NUMBER_OF_VALUES : in NATURAL) return REAL_VECTOR is
So it's clear that NATURAL_VECTOR'(100, 90) produces a NATURAL_VECTOR, but how?I have never seen something like it and I can't find it in the LRM. Can anybody elaborate on this notation/where I can find it in the LRM?

Found it... it is a qualified expression for an aggregate (LRM 7.3.4) defined as
qualified_expression ::=
type_mark ' ( expression )
| type_mark ' aggregate
and as choices are optional in an aggregate (LRM 7.3.2)
aggregate ::=
( element_association { , element_association } )
element_association ::=
[ choices => ] expression
this is simply a one dimensional array containing the two values 100 and 90 and as
type NATURAL_VECTOR is array (NATURAL range <>) of NATURAL;
I would assume that they are in 0 => 100 and 1 => 90, modelSim thinks so, too.

Related

How to translate ABNF to LBNF

Context
I'm trying to generate a parser for BCP47 Language-Tag values, which are specified in ABNF (Augmented Backus–Naur form). I'm doing this in Haskell and would like to use the robust BNFC tool-chain, which expects LBNF (Labeled Backus–Naur form). I've searched for tooling to do this conversion automatically and could find none, so I'm basically attempting to write an LBNF for it using the ABNF as reference.
Attempted so far
I've done a lot of searching, and I think this question may be useful, but I can't get bnfc to accept any use of ε, it always spits out a syntax error at that character. For example,
Convert every option [ E ] to a fresh non-terminal X and add
X = ε | E.
-- ABNF option:
-- foo = [ E ]
-- Fresh X
Foo. Foo ::= X ;
-- add
X. X ::= ε | E ;
E. E ::= "e" ;
syntax error at line 8, column 10 due to lexer error
Giving up on that, I tried to get something even simpler working:
language = 2*ALPHA
I could not.
I've seen some BNF documentation (sorry I lost the link now) with an example for digits that looked like:
number ::= digit
number ::= number digit
This makes sense to me, so I tried the following:
LanguageISO2. Language ::= ALPHA ALPHA ;
token ALPHA ( letter ) ;
The fails to parse "en", but does parse "e n". It's clear why, but what is the right way to do what I'm intending?
I can make things kind of work by abusing token,
LanguageISO2. Language ::= ALPHA_TWO ;
token ALPHA_TWO ( letter letter ) ;
But this will quickly get out of hand as I handle 3*ALPHA and 5*8ALPHA, etc.
Specific Question
Could someone convert the following to LBNF so I can see the right approach to these things?
langtag = (language
["-" script]
["-" region]
*("-" variant))
language = (2*3ALPHA [ extlang ])
extlang = *3("-" 3ALPHA) ; reserved for future use
script = 4ALPHA ; ISO 15924 code
region = 2ALPHA ; ISO 3166 code
/ 3DIGIT ; UN M.49 code
variant = 5*8alphanum ; registered variants
/ (DIGIT 3alphanum)
alphanum = (ALPHA / DIGIT) ; letters and numbers
Thanks very much in advance.

Sum of nested values in Alloy

I'm starting with definitions similar to these below
sig Sub { vals : set Int }
sig Top { subs : set Sub }
I'd like an expression that can produce the sum of all values contained inside something of type Top. Top when written as a nested set, would be something like {{3, 4}, {7}}. The result of the nested sum in this case should be 14.
This function of course just gives the number of elements in the outer set.
fun allsum[t: Top] : one Int { #t }
I believe I need to use the built-in sum function and a set comprehension, but Alloy syntax is still somewhat arcane to me.
For this, you need a nested sum expression:
fun allsum[t: Top] : Int {
sum s: t.subs | (sum v: s.vals | v)
}
The general format is:
sum e: <set> | <expression involving e>

How can I write a grammar that matches "x by y by z of a"?

I'm designing a low-punctuation language in which I want to support the declaration of arrays using the following syntax:
512 by 512 of 255 // a 512x512 array filled with 255
100 of 0 // a 100-element array filled with 0
expr1 by expr2 by expr3 ... by exprN of exprFill
These array declarations are just one kind of expression among many.
I'm having a hard time figuring out how to write the grammar rules. I've simplified my grammar down to the simplest thing that reproduces my trouble:
grammar Dimensions;
program
: expression EOF
;
expression
: expression (BY expression)* OF expression
| INT
;
BY : 'by';
OF : 'of';
INT : [0-9]+;
WHITESPACE : [ \t\n\r]+ -> skip;
When I feed in 10 of 1, I get the parse I want:
When I feed in 20 by 10 of 1, the middle expression non-terminal slurps up the 10 of 1, leaving nothing left to match the rule's OF expression:
And I get the following warning:
line 2:0 mismatched input '<EOF>' expecting 'of'
The parse I'd like to see is
(program (expression (expression 20) by (expression 10) of (expression 1)) <EOF>)
Is there a way I can reformulate my grammar to achieve this? I feel that what I need is right-association across both BY and OF, but I don't know how to express this across two operators.
After some non-intellectual experimentation, I came up with some productions that seem to generate my desired parse:
expression
:<assoc=right> expression (BY expression)+ OF expression
|<assoc=right> expression OF expression
| INT
;
I don't know if there's a way I can express it with just one production.

Understanding MCPL function call

Analyzing a solution to the eight queens puzzle using bitwise operations written by Martin Richards, I'm having a hard time understanding basic MCPL function syntax, despite consulting the language manual.
Given below is the full program:
GET "mcpl.h"
STATIC count, all
FUN try
: ?, =all, ? => count++
: ld, cols, rd => LET poss = ~(ld | cols | rd) & all
WHILE poss DO
{ LET bit = poss & -poss
poss -:= bit
try( (ld|bit)<<1, cols|bit, (rd|bit)>>1 )
}
FUN start : =>
all := 1
FOR n = 1 TO 12 DO
{ count := 0
try(0, 0, 0)
writef("There are %5d solutions to %2d-queens problem\n", count, n)
all := 2*all + 1
}
RETURN 0
What I fail to understand is the first two lines of the function try, namely, the question mark ? syntax and how parameters are passed (and handled).
The manual reads that
A question mark (?) may be used as a constant with undefined value.
as well as
Patterns are used in function definitions. [...] A question mark (?) or empty pattern will match any argument value.
What does this syntax mean for the parameters and how are ld, cols and rd given their initial values?
After finding a paper that further discusses the above and other similar algorithms, I came to find that
: ?, =all, ? => count++
essentially checks if cols == all and if so, a complete solution has been found (increase count).
ld, cols and rd are given parameter values.
What still looks odd to me is the check being executed before the parameters are used, so I might still be missing some details.

Summing over a set of util/natural numbers in alloy4

I found myself trying to sum a set of naturals. I was puzzled by the following behavior when running a simple model.
(assume the following code is in a copy of util/natural, so ord is imported)
//sums the values in a set of naturals
fun setsum[nums : set Natural] : lone Natural {
{n : Natural | #ord/prevs[n] = (sum x : nums | #ord/prevs[x])}
}
then, in a module importing my copy of util/natural:
private open mynatural as nat
let two = nat/add[nat/One, nat/One]
let three = nat/add[two, nat/One]
let four = nat/add[two, two]
let five = nat/add[four,nat/One]
pred showExpectSum10 {
some x : Natural | x in setsum[{n : Natural | nat/lt[n, five]}]
}
//run showExpectSum10 for 15 //result is 10, as expected
//run showExpectSum10 for 1 but 20 Natural //result is 10 as expected
run showExpectSum10 for 1 but 40 Natural //result is 26 somehow.
Why does changing the scope of Natural affect the result this way?
It seems you just need to disable overflows ("Options -> Forbid Overflows: Yes"), and then it should work as expected. Every time integer arithmetic is used and overflows are allowed (which is the default setting) it possible to get spurious counterexamples (i.e., invalid instances) due to the default "wraparound" semantics of arithmetic operations in Alloy.

Resources