Mixing declarative & imperative statements in behavioral blocks

Hello,

I’m currently working on a project involving source-to-source translation from a DSL (defined in C++) to Dezyne. I’m currently stuck at a point where the code I generate does not throw a syntax error but fail validation. Here’s the Dezyne interface below:

interface file_11799190314668254898{
out void demo_wrapper_2();
in void demo_wrapper_1();

enum State {A,B};
subint int {0..5};

behavior {   
    State state = State.A;
    bool y = false;
    int x = 0;
    // this works
    on demo_wrapper_1: { 
    [x<3] {
        state = State.B;
        y = true;
        }
    [x>=3] state = State.A;
    demo_wrapper_2;
}
}
}

The error I get is as follows:

file_11799190314668254898.dzn:19:5: error: declarative statement expected

I’ve read in the reference that only one imperative statement is allowed per combination of guard and on statements. However, the code provided above can be verified when demo_wrapper_2 out event is removed.

Could anyone help with the semantic details?

Hi Yigit,

It looks like you are missing some {…}. For the guard [x<3] the braces are there; for the guard [x>=3] they are missing. It should read:

[x>=3] { 
    state = State.A;
    demo_wrapper_2;
}

I also would advice to add some proper indentations; with indentations the missing braces would stand out more.

Hope this helps,

Paul Hoogendijk, Verum.

Hi Paul,

The lack of braces is intentional as I’m trying to signify that the action will take place regardless of the guard statements. My confusion arises from the following: In the reference manual, under lexical analysis, it is stated that

on: "on" trigger ("," trigger)* ":" statement
statement: declarative_statement | imperative_statement | illegal
declarative_statement: guard | on | "{" declarative_statement "}"
imperative_statement: action | assign | call | if | reply | return | variable 
| "{" (imperative_statement)* "}" | empty_statement

unless i’m missing something, an on block should only accept a single declarative statement and should not accept a mix of imperative and declarative statements in a single block. In the fix you suggest, under on demo_wrapper_1:, there exist two declarative statements. So I would like to clarify my questions:
1 - Can we mix imperative and declarative statements under a single statement ?
2 - How are multiple declarative statements under on demo_wrapper_1: allowed ?
3 - If what I’m doing is not syntactically valid, is there a way of signifying that an action occurs regardless of the guard conditions?

Thank you for your help,
Yigit Kagan

Hi,

Mixing declarative and imperative statements is not allowed. And yes, indeed, one statement is only allowed after a “on”; but that statement can be a compound statement ("{…}”) which can consist of many statements; and that compound should be either a declarative compound or a imperative compound.

(Note that the “fix” I proposed, does contain two imperative statements; not declarative as you mentioned)

Also, it is not allowed to have a imperative statement without an “on” statement in front of it. (Which is what you want as I understand now). It looks to me you want to have some “entry/exit” actions as known for classical statemachine. Those can be modeled in Dezyne using functions:

Instead of writing

state = <next-state>

make a function, e.g., “goto” and call that function:

goto(<next-state>)

with function “goto”:

void goto(State new_state) {
  <exit actions for leaving state 'state'>
  state = new_state;
  <entry actions for entering state 'new_state'>
}

Hope this helps,

Paul Hoogendijk, Verum.

Hi Paul,

Thanks for the help. Almost all my questions have been clarified. The only problem left in my mind is that I was referring to the following structure when I talked about multiple declarative statements:

on demo_wrapper_1: {
    [x < 3]{
        ...
    }
    [x >= 3]{
        ...
    }
}

Aren’t two guards counted as two declarative statements?

Thanks for the help,
Yigit Kagan

Right. There is compound around these declarative statements; and that compound counts as 1 declarative statement.