Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grammar railroad diagram #153

Open
mingodad opened this issue Nov 7, 2024 · 0 comments
Open

Grammar railroad diagram #153

mingodad opened this issue Nov 7, 2024 · 0 comments

Comments

@mingodad
Copy link

mingodad commented Nov 7, 2024

Manually converting the grammar in https://ceu-lang.github.io/ceu/out/manual/v0.30/syntax/ to an EBNF understood by https://github.com/GuntherRademacher/rr that can generate a nice navigable railroad diagram (see bellow at the top for instructions).
I needed to fix/interpret in some places where the original grammar was broken/not clear, please check it out.

//
// EBNF to be viewd at
//	(IPV6) https://www.bottlecaps.de/rr/ui
//	(IPV4) https://rr.red-dove.com/ui
//
// Copy and paste this at one url shown above in the 'Edit Grammar' tab
// then click the 'View Diagram' tab.
//
//From: https://ceu-lang.github.io/ceu/out/manual/v0.30/syntax/

Program ::= Block
Block   ::= (Stmt ';')*

Stmt ::= nothing

  /* Blocks */

      // Do ::=
      | "do" ('/'(ID_int|'_'))? ('(' (ID_int (',' ID_int) ','?)? ')')?
            Block
        "end"
      | "escape" ('/'ID_int)? Exp?

      /* pre (top level) execution */
      | "pre" "do"
            Block
        "end"

  /* Storage Entities / Declarations */

      // Dcls ::=
      | "var" ('&'|'&?')? '[' (Exp '*'?)? ']' ('/dynamic'|'/nohold')? Type ID_int ('=' Sources)?
      | "pool" '&'? '[' Exp? ']' Type ID_int ('=' Sources)?
      | "event" '&'? (Type | '(' Type (',' Type) ','? ')') ID_int ('=' Sources)?

      | "input" (Type | '(' Type (',' Type) ','? ')') ID_ext
      | "output" (Type | '(' '&'? Type ID_int? (',' '&'? Type ID_int?)* ','? ')') ID_ext
            ( "do" Block "end" )?

  /* Event Handling */

      // Await ::=
      | "await" (ID_ext | Loc) ("until" Exp)?
      | "await" (WCLOCKK|WCLOCKE)
      //
      | "await" (FOREVER | pause | resume)

      // Emit_Ext ::=
      | "emit" ID_ext ('(' ((Exp|'_') (',' (Exp|'_'))* ','?)? ')')?
      | "emit" (WCLOCKK|WCLOCKE)
      //
      | "emit" Loc ('(' ((Exp|'_') (',' (Exp|'_'))* ','?)? ')')?

      | "lock" Loc "do"
            Block
        "end"

  /* Conditional */

      | "if" Exp "then"
            Block
        ( "else/if" Exp "then"
            Block )*
        ( "else"
            Block )?
        "end"

  /* Loops */

      /* simple */
      | "loop" ('/'Exp)? "do"
            Block
        "end"

      /* numeric iterator */
      | "loop" ('/'Exp)? (ID_int|'_') "in" Range? "do"
            Block
        "end"
        // where

      /* pool iterator */
      | "loop" ('/'Exp)? (ID_int|'_') "in" Loc "do"
            Block
        "end"

      /* event iterator */
      | "every" ((Loc | '(' (Loc|'_') (',' (Loc|'_'))* ','? ')') "in")? (ID_ext|Loc|WCLOCKK|WCLOCKE) "do"
            Block
        "end"

      |  "break" ('/'ID_int)?
      |  "continue" ('/'ID_int)?

  /* Parallel Compositions */

      /* parallels */
      | ("par" | "par/and" | "par/or") "do"
            Block
        "with"
            Block
        ( "with"
            Block )*
         "end"

      /* watching */
      // Watching ::=
      | "watching" (ID_ext|Loc|WCLOCKK|WCLOCKE|Abs_Cons) (',' (ID_ext|Loc|WCLOCKK|WCLOCKE|Abs_Cons))* ',' "do"
            Block
        "end"

      /* block spawn */
      | "spawn" ('(' (ID_int (',' ID_int)* ','?)? ')')? "do"
            Block
        "end"

  /* Exceptions */

      | "throw" Exp
      | "catch" Loc (',' Loc)* ',' "do"
            Block
        "end"

  /* Pause */

      | "pause/if" (Loc|ID_ext) "do"
            Block
        "end"

  /* Asynchronous Execution */

      | "await" "async" ( '(' Var (',' Var)* ','? ')' )? "do"
            Block
        "end"

      // Thread ::=
      | "await" "async/thread" ( '(' Var (',' Var)* ','? ')' )? "do"
            Block
        "end"

      | "spawn" "async/isr" '[' Exp (',' Exp)* ','? ']' ( '(' Var (',' Var)* ','? ')' )? "do"
            Block
        "end"

      /* synchronization */
      | "atomic" "do"
            Block
        "end"

  /* C integration */

      | "native" ('/'("pure"|"const"|"nohold"|"plain"))? '(' ID_nat (',' ID_nat)* ','? ')'
      | "native" '/'("pre"|"pos") "do"
            "<code definitions in C>"
        "end"
      | "native" '/' "end"
      | '{' ("<code in C>" | '@' ('('Exp')'|Exp))* '}'     /* '@@' escapes to '@' */

      // Nat_Call ::=
      | "call"? Exp

      /* finalization */
      | "do" Stmt? Finalize
      | "var" ('&'|'&?')? Type ID_int '=' '&' (Nat_Call | Code_Call) Finalize
        // where

  /* Lua integration */

      // Lua_State ::=
      | "lua" '[' Exp? ']' "do"
            Block
        "end"
      // Lua_Stmts ::=
      | '[' '='* '['
            '{' ("<code in Lua>" | '@' ('('Exp')'|Exp))* '}'   /* '@@' escapes to '@' */
        ']' '='* ']'

  /* Abstractions */

      /* Data */

      | "data" ID_abs (as (nothing|Exp))? ( "with"
            Dcls ';' ( Dcls ';' )*
        "end" )?

      /* Code */

      // Code_Tight ::=
      | "code/tight" Mods ID_abs '(' Params ')' '->' Type

      // Code_Await ::=
      | "code/await" Mods ID_abs '(' Params ')'
                                    ( '->' '(' Params ')' )?
                                        '->' (Type | NEVER)
                                ( "throws" ID_abs (',' ID_abs)* ','? )?
        // where

      /* code implementation */
      | (Code_Tight | Code_Await) "do"
            Block
        "end"

      /* code invocation */

      // Code_Call ::=
      | "call"  Mods Abs_Cons

      // Code_Await ::=
      | "await" Mods Abs_Cons

      // Code_Spawn ::=
      | "spawn" Mods Abs_Cons ("in" Loc)?
      | "kill" Loc ( '(' Exp ')' )?

        // where

  /* Assignments */

      | (Loc | '(' (Loc|'_') (',' (Loc|'_'))* ','? ')') '=' Sources
        // where
            Sources ::= ( Do
                        | Emit_Ext
                        | Await
                        | Watching
                        | Thread
                        | Lua_Stmts
                        | Code_Await
                        | Code_Spawn
                        | Vec_Cons
                        | Data_Cons
                        | Exp
                        | '_' )
            Vec_Cons  ::= (Loc | Exp) Vec_Concat  Vec_Concat*
                       |  '[' (Exp (',' Exp)* ','?)? ']'  Vec_Concat*
                        // where
                            Vec_Concat ::= '..' (Exp | Lua_Stmts | '[' (Exp (',' Exp)* ','?)? ']')
            Data_Cons ::= ("val"|"new") Abs_Cons

  Finalize ::= "finalize" ( '(' Loc (',' Loc) ','? ')' )? "with"
                             Block
                         ( "pause" "with" Block )?
                         ( "resume" "with" Block )?
                         "end"

   Range ::= ('[' | ']')
                        ( (      Exp '->' (Exp|'_'))
                        | ((Exp|'_') '<-' Exp      ) )
                      ('[' | ']') (',' Exp)?

   Params ::= none | (Dcls (',' Dcls)* ','?)

   Mods ::= ('/' "dynamic" | '/' "static")? ('/' "recursive")?
   Abs_Cons ::= (Loc '.')? ID_abs '(' (Data_Cons|Vec_Cons|Exp|'_') (',' (Data_Cons|Vec_Cons|Exp|'_'))* ','? ')'

/* Identifiers */

ID       ::= [a-zA-Z0-9_]+
ID_int   ::= ID             // ID beginning with lowercase
ID_ext   ::= ID             // ID all in uppercase, not beginning with digit
ID_abs   ::= ID ('.' ID)*    // IDs beginning with uppercase, containining at least one lowercase)
ID_field ::= ID             // ID not beginning with digit
ID_nat   ::= ID             // ID beginning with underscore
ID_type  ::= ( ID_nat | ID_abs
             | "none"
             | "bool"  | "on/off" | "yes/no"
             | "byte"
             | "r32"   | "r64"    | "real"
             | "s8"    | "s16"    | "s32"     | "s64"
             | "u8"    | "u16"    | "u32"     | "u64"
             | "int"   | "uint"   | "integer"
             | "ssize"   | "usize" )

/* Types */

Type ::= ID_type  '&&'* '?'?

/* Wall-clock values */

WCLOCKK ::= (NUM h)? (NUM min)? (NUM s)? (NUM ms)? (NUM us)?
WCLOCKE ::= '(' Exp ')' ("h"|"min"|"s"|"ms"|"us")

/* Literals */

NUM ::= [0-9] ([0-9]|[xX]|[A-F]|[a-f]|".")*  // regex
STR ::= '"' [^\"\n]* '"'                        // regex

/* Expressions */

Exp ::= NUM | STR | "null" | "true" | "false" | "on" | "off" | "yes" | "no"
     |  '(' Exp ')'
     |  Exp "<binop>" Exp
     |  "<unop>" Exp
     |  Exp (':'|'.') (ID_int|ID_nat)
     |  Exp ('?'|'!')
     |  Exp '[' Exp ']'
     |  Exp '(' ( Exp (',' Exp)* ','? )? ')'
     |  Exp "is" Type
     |  Exp "as" Type
     |  Exp "as" '/'("nohold"|"plain"|"pure")
     |  "sizeof" '(' (Type|Exp) ')'
     |  Nat_Call | Code_Call
     |  ID_int
     |  ID_nat
     |  "outer"

/* Locations */

Loc ::= Loc ("as" (Type | '/' ("nohold"|"plain"|"pure")))? ')'
     |  ('*'|'$')? Loc
     |  Loc ( '['Exp']' | (':'|'.') (ID_int|ID_nat) | '!' )*
     |  ID_int
     |  ID_nat
     |  "outer"
     |  '{' "<code in C>" '}'
     |  '(' Loc ')'

///* Operator Precedence */
//
//    /* lowest priority */
//
//    // locations
//    *     $
//    :     .     !     []
//    as
//
//    // expressions
//    is    as                                            // binops
//    or
//    and
//    !=    ==    <=    >=    <     >
//    |
//    ^
//    &
//    <<    >>
//    +     -
//    *     /     %
//    not   +     -     ~     $$    $     *     &&    &   // unops
//    :     .     !     ?     ()    []
//
//    /* highest priority */
//
///* Other */
//
//    // single-line comment
//
//    /** nested
//        /* multi-line */
//        comments **/
//
//    # preprocessor directive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant