Appendix B EBNF Grammar for QuLog/TeleoR

(* The EBNF grammar for qulog/teleor *)

(*
Notation used as in:
   https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form

We assume the following non-terminals that group tokens. All other
tokens in the grammar are given as strings.

atom:  the allowed atoms of qulog
string:  double-quoted strings
var: the variables of qulog
int: integers
float: floating point numbers

*)

anynum = int | float; 

(* ------ query ------ *)

query =
    (
       ( int, "of", var_seq, "::", basic_query ) |
       ( int, "of", basic_query ) |
       ( int, "of", "exists", var_seq, conditions ) |
       ( var_seq, "::", basic_query ) |
       ( "exists", var_seq, conditions ) |
       basic_query 
     );

basic_query =
    ( "prolog" ) |
    ( "bs" ) |
    ( "logging", atom ) |  
    ( "logging", "(", atom , ")" ) |  
    ( "logging", atom, "@", atom ) |  
    ( "logging", "(", atom, "@", atom , ")" ) |
    ( "unlog" ) |
    ( "types", {atom_seq} ) |
    ( "stypes", {atom_seq} ) |
    ( "show", {atom_seq} ) |
    ( "watch", {atom_seq} ) |
    ( "watch", "(", {atom_seq}, ")" ) |
    ( "unwatch", {atom_seq} ) |
    ( "unwatch", "(", {atom_seq}, ")" ) |
    ( "watched" ) |
    ( "answers", int ) |
    ( "consult", atom ) |
    ( "consult", "(", atom, ")" ) |
    ( "pconsult", atom ) |
    ( "pconsult", "(", atom, ")" ) |
    ( "[", atom, "]" ) |
    conditions |
    action_seq;
    
    

(* ------ program item ------ *)
(* Note: a program file is parsed one program item at a time *)

program_item =
    ( "?-", term ) |
    enum_definition |
    macro_definition |
    declaration |
    ( compound_term, rel_rule_body ) |
    ( compound_term, act_rule_body ) |
    ( compound_term, fun_rule_body ) |
    ( simple_compound, tel_procedure_body );

(* ------ type definitions ------ *)

enum_definition = "def", definition_head, "::=", enum_type {string};

macro_definition = "def", definition_head, "==", macro_type {string};

(* either atom type or polymorphic type with args *)
definition_head =  atom |  ( atom, bracketed_var_seq ); 

(* ------ types ------ *)

enum_type =
      ( int, "..", int ) | 
      ( atom, "|", atom, {"|", atom} ) |
      ( string, "|", string , {"|", string} ) |
      ( anynum, "|", anynum, {"|", anynum} ) |
      ( simple_compound, {"|", simple_compound} ) |
      ( "(", enum_type, ")" ) ;
      
macro_type =
       ( atom_or_simple_compound,  "||", 
          atom_or_simple_compound,  {"||", atom_or_simple_compound} ) |
      macro_type_expression;

macro_type_expression =
    simple_type_compound |
    atom |
     ( "(", type_expression, ",", type_expression, 
             {",", type_expression}, ")" ) |  
     code_type_expression;
     
type_expression =
     code_type_expression |
     var |
     simple_type_compound |
     atom |
     ( "(", type_expression, ")" ) |
     ( "(", type_expression, ",", type_expression,
         {",", type_expression}, ")" );

code_type_expression =
     fun_type_expression |
     dyn_type_expression |
     rel_type_expression |
     act_type_expression |
     tel_type_expression;

fun_type_expression = 
    ( "fun", bracketed_type_seq, "->",  type_expression ) ;

rel_type_expression = 
    ( "rel", bracketed_annotated_type_seq);

dyn_type_expression = 
    ( "dyn", bracketed_type_seq);

act_type_expression = 
    ( "act", bracketed_annotated_type_seq);

tel_type_expression = 
    ( "tel", bracketed_type_seq);
   
bracketed_type_seq =
    ( "(", ")" ) |
    ( "(", type_expression, { ",", type_expression}, ")" );

mode_annotation = "!" | "?" | "??" | "@";

bracketed_annotated_type_seq =
    ( "(", ")" ) |
    ( "(", annotated_type_expression,
        { ",", annotated_type_expression}, ")" );

annotated_type_expression =
    mode_annotation |
    ( mode_annotation , inner_annotated_type_expression ) |
    inner_annotated_type_expression;

inner_annotated_type_expression =
   type_expression |
   ( atom, bracketed_annotated_type_seq);

(* ------ declarations ------ *)

declaration =
    ( "fun", fun_declaration, {",", fun_declaration}, [ string ] ) |
    ( "act", annotated_declaration, {",", annotated_declaration}, [string]) |
    ( "rel", annotated_declaration, {",", annotated_declaration}, [string]) |
    ( "dyn", unannotated_declaration,
             {",", unannotated_declaration}, [ string ] ) |
    ( "mrel", annotated_declaration,
             {",", annotated_declaration}, [ string ] ) |
    ( "mfun", fun_declaration, {",", fun_declaration}, [ string ] ) |
    ( ( "tel" | "start_tel" | "atomic_tel" ), unannotated_declaration,
             {",", unannotated_declaration}, [ string ] ) |
    ( "percept", unannotated_declaration,
             {",", unannotated_declaration}, [ string ] ) |
    global_num_declaration;

fun_declaration =
    unannotated_declaration, "->", type_expression;

unannotated_declaration =
    atom, (
        ( "(", ")" ) |
        ( "(", [var, ":"], type_expression, ["default", term],
           { ",",  [var, ":"], type_expression}, ")" )
        );
       
annotated_declaration =
    atom, (
        ( "(", ")" ) |
        ( "(", [var, ":"], annotated_type_expression, ["default", term],
           { ",",  [var, ":"],
                annotated_type_expression, ["default", term]}, ")" )
        );

global_num_declaration =
    ("int", atom, ":=", int) |
    ("num", atom, ":=", num);
  

(* ------ rule definitions ------ *)

rel_rule_body =
    ["::", conditions], ["<=", conditions];

act_rule_body =
    ["::", conditions], "~>", action_seq;

fun_rule_body =
    ["::", conditions], "->", term;

tel_procedure_body =
    "{", ">>>", tel_rule, { tel_rule }, "<<<", { tel_rule }, "}" |
    "{", tel_rule, { tel_rule }, "}";


(* ------ conjunction and conditions ------ *)

conditions = a_condition, { "&", a_condition };

a_condition =
    ( "forall", var_seq,
       "(", exists_conditions, "=>", exists_conditions, ")" ) |
    ( "not", exists_a_condition ) |
    ( "not", "(", exists_conditions, ")" ) |
    ( "once", a_condition ) |
    ( "once", "(", conditions, ")" ) |
    ( "(", int, "of", var_type_seq, "::",
         exists_conditions__, ")", ")" , "query_at", pedro_handle ) |
    ("(", var_type_seq, "::",
         exists_conditions__, ")", "query_at", pedro_handle ) |
    ( simple_condition, "query_at", pedro_handle ) |
    simple_condition;

exists_a_condition =
    ( "exists", var_seq, simple_condition ) |
    a_condition;

exists_conditions =
    ( "exists", var_seq, a_condition ) |  conditions;

simple_condition =
    ( "(", conditions, ")" ) |
    "true" | "false" |
    ( "type", "(", term, ",", annotated_type_expression, ")" ) |
    ( "listof", "(", term, ",", term, "::", exists_conditions, ")" ) |
    compound_term |
    ( term, "=?", qeqrhs) |
    ( term, test_op, term );

qeqrhs =
    ( qeqrhs_string_term, "++", qeqrhs_string_term,
                  {"++", qeqrhs_string_term} ) |
    ( qeqrhs_list_term, "<>", qeqrhs_list_term, {"<>", qeqrhs_list_term} );

qeqrhs_string_term =
    ( simple_string_term, "::", conditions ) |
    simple_string_term;

qeqrhs_list_term =
    ( simple_list_term, "::", conditions ) |
    simple_list_term;

(* ------ actions ------ *)

action_seq = action, {";", action};

action =
    ( "forall",  var_seq, "{", 
       exists_conditions, "~>", action_seq, "}") |
    simple_action ;

simple_action =
    ( "{", "}" ) |
    ( "{", action_seq, "}" ) |
    ( "atomic_action", simple_action ) |
    ( "case", "{", case_alt, { case_alt }, "}" ) |
    ( "wait", "(", conditions, ")" ) |
    ( "wait_case", "{", case_alt, { case_alt },
                      [ "timeout", term, "~>", action_seq ], "}" ) |
    ( "receive", "{", receive_alt, { receive_alt },
                      [ "timeout", term, "~>", action_seq ], "}" ) |
    ( "try", action_seq, "except", "{", except_alt, { except_alt }, "}" ) |
    ( "repeat", simple_action, [ "until", a_condition ] ) |
    ( "?", "(", exists_conditions, ")" ) |
    ( term, "from", agent_handle, ["::", conditions ] ) |
    ( term, "from_thread", pedro_handle, ["::", conditions ] ) |
    tr_pp_action;

case_alt =
   conditions, "~>", action_seq;


receive_alt =
    ( term, "from", agent_handle, [ "::", conditions ], "~>", action_seq ) |
    ( term, "from_thread", pedro_handle, [ "::", conditions ],
           "~>", action_seq ) |
    ( "query", term, "from_thread", pedro_handle, [ "::", conditions ],
           "~>", action_seq );
           
except_alt =
    term, [ "::", conditions ], "~>", action_seq;

(* ------ TR rule ------ *)

tel_rule =
    ( tr_rule_LHS, "~+>",  tr_pp_action) |
    tr_rule_LHS, "~>", tr_rule_RHS;

tr_rule_LHS =
    conditions,
    [( "commit_while", while_commit) | ("or_while", while_commit)]; 
    

while_commit =
    conditions |
    ( "min_time", term ) |
    (conditions, "min_time", term );

tr_rule_RHS = tr_action, [ "++", simple_tr_pp_action ];

tr_action =
    ( "(", ")" ) |
    ( simple_tr_action, {",", simple_tr_action} ) |
    ( "(", simple_tr_action, {",", simple_tr_action}, ")" ) |
    ( "[", tr_timed_seq, "]" );

tr_timed_seq =
    simple_tr_action, ":", term, {",",  simple_tr_action, ":", term },
        [",", simple_tr_action];

simple_tr_action = var | compound_term;

tr_pp_action =
    simple_tr_pp_action |
    ( "{" simple_tr_pp_action { ";" simple_tr_pp_action } "}" )

simple_tr_pp_action =
     ( term, "to", agent_handle ) |
     ( term, "to_thread", pedro_handle ) |
     ( atom, global_num_op, term) |
     compound_term;

(* ------ term ------ *)

term =
    simple_term | 
    ( simple_term, "<>", simple_term, { "<>", simple_term } ) |
    ( simple_term, "++", simple_term, { "++", simple_term } ) |
    ( simple_term, set_op, simple_term, { set_op, simple_term } ) |
    ( simple_term, arith_op, simple_term, { arith_op, simple_term } );

simple_term =
    var |
    atom |
    anynum |
    string |
    compound_term |
    agent_handle | pedro_handle |
    ( "(", term, ")" ) |   
    ( tuple ) |      
    ( "$", atom ) |
    set_term |
    set_comprehension |
    list_term |
    list_comprehension |
    ( "-", simple_term ) |
    ( "#", simple_term );

set_comprehension =
   "{", simple_term, "::", exists_conditions, "}";
    
list_comprehension = 
    ( "[", simple_term, "::", exists_conditions, "]" );



(* ------ auxilary rules -------*)

atom_seq = atom, {",", atom};

var_seq = ( var, {",", var} ) | ( "(", var, {",", var}, ")" );

var_type_seq = ( var, ":", type_expression,
                   {",", var,":", type_expression} ) |
                   ( "(", var, ":", type_expression,
                   {",", var,":", type_expression}, ")" );

atom_or_simple_compound = atom | simple_compound;

simple_compound = atom, bracketed_arg_seq;

simple_type_compound = atom, bracketed_type_arg_seq;


compound_term = ( atom | var ), bracketed_arg_seq, { bracketed_arg_seq };

(* compound means ultimate functor is an atom *)

bracketed_arg_seq  = ("(", { term, "," }, ")") | ("(", arg_seq, ")");

bracketed_type_arg_seq = ("(", type_expression, {",", type_expression}, ")");

tuple = ( "(", ")" ) | ("(", term, ",", arg_seq, ")");

bracketed_var_seq  = ("(", ")") | ("(", var_seq, ")");

arg_seq = term, {",", term};

test_op =
    "=" | "==" | "\=" | "=@" | ">" | "<" | ">=" | "=<" |
    "@>" | "@<" | "@>=" | "@=<" | "in";

global_num_op =
    ":=" | "+:=" | "-:=";

simple_string_term =
    string | var | ( var, "/", ( string | var ) ) | compound_term;
    
simple_list_term =
    var | compound_term | list_term | list_comprehension ;

set_term =
    ( "{", "}" ) |
    ( "{", arg_seq, "}" );
    
list_term =
    ( "[", "]" ) |
    "[", term, list_tail;

list_tail =
    "]" |
    ( "|", term, "]" ) |
    ( ",..", "]" ) |
    ( ",..", term, "]" ) |
    ( term, ",", list_tail );

pedro_handle = (atom | var), [":", (atom | var)], ["@", (atom | var)];
agent_handle = (atom | var), ["@", (atom | var)];


set_op = "union" | "diff" | "inter";

arith_op =
    "**" | "*" | "+" | "-" | "/" | "//" | ">>" | "<<" |
    "mod" | "rem" | "/\\" | "\\/";



On This Site