Skip to content

Commit

Permalink
add %conflict to the grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
reb-ddm committed Feb 10, 2025
1 parent 2c3be70 commit af58808
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions core/KaSa_rep/frontend/prepreprocess.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1008,4 +1008,5 @@ let translate_compil parameters error
Ast.tokens = compil.Ast.tokens;
Ast.volumes = compil.Ast.volumes;
Ast.guard_param_values = compil.Ast.guard_param_values;
Ast.conflicts = compil.Ast.conflicts;
} )
27 changes: 26 additions & 1 deletion core/grammar/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ type ('agent, 'agent_id, 'pattern, 'mixture, 'id, 'rule) instruction =
* string LKappa.guard option
* 'rule Loc.annoted)
| GUARD_PARAM of (string Loc.annoted * bool)
| CONFLICT of (string Loc.annoted * string Loc.annoted * string Loc.annoted)

type ('pattern, 'mixture, 'id, 'rule) command =
| RUN of ('pattern, 'id) Alg_expr.bool Loc.annoted
Expand All @@ -192,6 +193,8 @@ type ('agent, 'agent_sig, 'pattern, 'mixture, 'id, 'rule) compil = {
volumes: (string * float * string) list;
guard_param_values: (string Loc.annoted * bool) list;
(** The guard parameters that have a defined value (true or false).*)
conflicts: (string Loc.annoted * string Loc.annoted * string Loc.annoted) list;
(** A conflict (A, s1, s2) states that there might be a conflict between the two sites s1, s2 of the agent A.*)
}

type parsing_compil = (agent, agent_sig, mixture, mixture, string, rule) compil
Expand Down Expand Up @@ -234,6 +237,7 @@ let empty_compil =
tokens = [];
volumes = [];
guard_param_values = [];
conflicts = [];
}

(*
Expand Down Expand Up @@ -1122,7 +1126,7 @@ let print_perturbation f ((alarm, cond, modif, rep), _) =
rep

let print_parsing_compil_kappa f c =
Format.fprintf f "@[<v>%a@,@,%a@,%a@,@,%a@,@,%a@,%a@,@,%a@,@,%a@,%a@,@]@."
Format.fprintf f "@[<v>%a@,@,%a@,%a@,@,%a@,@,%a@,%a@,@,%a@,@,%a@,%a@,%a@,@]@."
(Pp.list Pp.space print_configuration)
c.configurations
(Pp.list Pp.space (fun f a ->
Expand Down Expand Up @@ -1153,6 +1157,9 @@ let print_parsing_compil_kappa f c =
(Pp.list Pp.space (fun f ((s, _), b) ->
Format.fprintf f "%%guard_param: %s -> %b" s b))
c.guard_param_values
(Pp.list Pp.space (fun f ((a, _), (s1, _), (s2, _)) ->
Format.fprintf f "%%conflict: %s %s %s" a s1 s2))
c.conflicts

let arrow_notation_to_yojson filenames f_mix f_var r =
JsonUtil.smart_assoc
Expand Down Expand Up @@ -1738,6 +1745,13 @@ let compil_to_json c =
(Loc.string_annoted_to_json ~filenames)
JsonUtil.of_bool)
c.guard_param_values );
( "conflicts",
JsonUtil.of_list
(JsonUtil.of_triple
(Loc.string_annoted_to_json ~filenames)
(Loc.string_annoted_to_json ~filenames)
(Loc.string_annoted_to_json ~filenames))
c.conflicts );
]

let compil_of_json = function
Expand Down Expand Up @@ -1843,6 +1857,17 @@ let compil_of_json = function
(JsonUtil.exn_msg_cant_import_from_json
"AST guard_param_values boolean value")))
(List.assoc "guard_param_values" l);
conflicts =
JsonUtil.to_list
~error_msg:
(JsonUtil.exn_msg_cant_import_from_json "AST conflicts sig")
(JsonUtil.to_triple
~error_msg:
(JsonUtil.exn_msg_cant_import_from_json "AST conflicts sig")
(Loc.string_annoted_of_json ~filenames)
(Loc.string_annoted_of_json ~filenames)
(Loc.string_annoted_of_json ~filenames))
(List.assoc "conflicts" l);
}
with Not_found ->
raise (Yojson.Basic.Util.Type_error ("Incorrect AST", x)))
Expand Down
2 changes: 2 additions & 0 deletions core/grammar/ast.mli
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ type ('agent, 'agent_sig, 'pattern, 'mixture, 'id, 'rule) instruction =
* string LKappa.guard option
* 'rule Loc.annoted)
| GUARD_PARAM of (string Loc.annoted * bool)
| CONFLICT of (string Loc.annoted * string Loc.annoted * string Loc.annoted)

type ('pattern, 'mixture, 'id, 'rule) command =
| RUN of ('pattern, 'id) Alg_expr.bool Loc.annoted
Expand All @@ -169,6 +170,7 @@ type ('agent, 'agent_sig, 'pattern, 'mixture, 'id, 'rule) compil = {
tokens: string Loc.annoted list;
volumes: (string * float * string) list;
guard_param_values: (string Loc.annoted * bool) list;
conflicts: (string Loc.annoted * string Loc.annoted * string Loc.annoted) list;
}

type parsing_compil = (agent, agent_sig, mixture, mixture, string, rule) compil
Expand Down
4 changes: 3 additions & 1 deletion core/grammar/cst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ let append_to_ast_compil rev_instr compil =
{
r with
Ast.guard_param_values = (params_sig, b) :: r.Ast.guard_param_values;
})
}
| Ast.CONFLICT (agent, site1, site2) ->
{ r with Ast.conflicts = (agent, site1, site2) :: r.Ast.conflicts })
compil (List.rev rev_instr)
3 changes: 3 additions & 0 deletions core/grammar/kappaParser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ start_rule:
| Ast.GUARD_PARAM (param_name,b) ->
{r with
Ast.guard_param_values = (param_name,b)::r.Ast.guard_param_values}
| Ast.CONFLICT (a,s1,s2) ->
{r with
Ast.conflicts = (a,s1,s2)::r.Ast.conflicts}
}
| error
{raise (ExceptionDefn.Syntax_Error (add_pos "Syntax error"))}
Expand Down
1 change: 1 addition & 0 deletions core/grammar/klexer4.mll
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ rule token = parse
| "def" -> CONFIG
| "token" -> TOKEN
| "guard_param" -> GUARD_PARAM
| "conflict" -> CONFLICT
| _ as s -> raise (ExceptionDefn.Syntax_Error
("Unknown directive: "^s,
Loc.of_pos (Lexing.lexeme_start_p lexbuf)
Expand Down
3 changes: 2 additions & 1 deletion core/grammar/kparser4.mly
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
%token SHARP UNDERSCORE PIPE RAR LRAR LAR EMAX TMAX CPUTIME TIME EVENT NULL_EVENT
%token COLON NEWLINE BACKSLASH SIGNATURE TOKEN INIT OBS PLOT PERT CONFIG APPLY
%token DELETE INTRO SNAPSHOT STOP FLUX TRACK ASSIGN PRINTF PLOTENTRY SPECIES_OF
%token DO REPEAT ALARM RUN LET GUARD_PARAM SHARP_OP_BRA IF
%token DO REPEAT ALARM RUN LET GUARD_PARAM SHARP_OP_BRA IF CONFLICT
%token <int> INT
%token <float> FLOAT
%token <string> ID LABEL STRING
Expand Down Expand Up @@ -828,6 +828,7 @@ an algebraic expression is expected")) }
| CONFIG annoted STRING annoted value_list
{ add (Ast.CONFIG (($3,rhs_pos 3),$5)) }
| GUARD_PARAM annoted ID annoted boolean annoted { add (Ast.GUARD_PARAM (($3,rhs_pos 3), $5)) }
| CONFLICT annoted ID annoted ID annoted ID annoted { add (Ast.CONFLICT (($3,rhs_pos 3), ($5,rhs_pos 5), ($7,rhs_pos 7))) }
;

model_body:
Expand Down
1 change: 1 addition & 0 deletions core/grammar/lKappa_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2766,5 +2766,6 @@ let compil_of_ast ~warning ~debug_mode ~syntax_version ~var_overwrite ast_compil
signatures = ast_compil.signatures;
configurations = ast_compil.configurations;
guard_param_values = ast_compil.guard_param_values;
conflicts = ast_compil.conflicts;
};
}

0 comments on commit af58808

Please sign in to comment.