2

How do you declare and initialize a nested associative array in SystemVerilog?

/*
  Creating an associative array(AA) called timings such that
  each key contains an AA with a list of relevant key value pairs
*/
typedef string timingObj [string];
timingObj timings [string] = {"A": {"B" : "C"}, "X": {"Y" : "Z"} };
//string timings [timingObj] = {"A": {"B" : "C"}, "X": {"Y" : "Z"} }; //Same error

timingObj t;
$cast(t, timings["A"]); // t = {"B" : "C"}
$display("%s", timings["A"]);
$display("%s", t["B"]);

The code above results in compiler errors:

"Syntax error. Unexpected token: }. Expected tokens: ':'." "testbench.sv" 2
"Syntax error. Unexpected token: $cast[_SYSTEM_CAST]. Expected tokens: ';' , 'checker' , 'function' , 'task' , 'timeprecision' ... ." "testbench.sv" 6  6

1 Answer 1

3

Assignment patterns for associative arrays need a '{} mark in front to distinguish it from a concatenation {}. There are some cases where it is ambiguous (but not here). So write

timingObj timings [string] = '{"A": '{"B" : "C"}, "X": '{"Y" : "Z"} };
Sign up to request clarification or add additional context in comments.

6 Comments

Good thing i cleaned the code a bit, then how would you access each individual key? the $cast function returns an error, is it even required?
$cast is only needed for certain class variable assignments and when you are not sure of the variable type because of type parameterization. You should be able to write t = timings["A"];
and finally.... $display("%s", t["B"]); to access the nested variable?
timings["A"] is not a string, is an associative array of strings. Use %p instead of %s
I hate to be pedantic, t = timings["A"];$display("%p", t["B"]); displaying "C" but instead it returns a new error "Syntax error. Unexpected token: t[_IDENTIFIER] ..."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.