Fault-lang / Fault

a language for building system dynamic models
https://fault.tech
MIT License
164 stars 6 forks source link

Grammar railroad diagram #27

Open mingodad opened 1 year ago

mingodad commented 1 year ago

Using some online tools like https://www.bottlecaps.de/rr/ui and https://www.bottlecaps.de/convert/ we can have a nice navigable railroad diagram.

Copy and paste the EBNF shown bellow on https://www.bottlecaps.de/rr/ui on the tab Edit Grammar the click on the tab View Diagram to see/download a navigable railroad diagram.

/* converted on Thu Mar 9, 2023, 07:28 (UTC+01) by antlr_4-to-w3c v0.62 which is Copyright (c) 2011-2022 by Gunther Rademacher <grd@gmx.net> */

sysSpec  ::= sysClause importDecl* globalDecl* componentDecl* ( assertion | assumption )? startBlock? forStmt?
sysClause
         ::= 'system' IDENT ';'
globalDecl
         ::= 'global' IDENT '=' operand ';'
componentDecl
         ::= 'component' IDENT '=' 'states' '{' ( comProperties ',' )* '}' ';'
startBlock
         ::= 'start' '{' ( startPair ',' )* '}' ';'
startPair
         ::= IDENT ':' IDENT
spec     ::= specClause declaration* forStmt?
specClause
         ::= 'spec' IDENT ';'
importDecl
         ::= 'import' ( importSpec | '(' importSpec* ')' ) ';'
importSpec
         ::= ( '.' | IDENT )? importPath ','?
importPath
         ::= string_
declaration
         ::= constDecl
           | structDecl
           | assertion
           | assumption
comparison
         ::= '=='
           | '!='
           | '<'
           | '<='
           | '>'
           | '>='
constDecl
         ::= 'const' ( constSpec | '(' constSpec* ')' ) ';'
constSpec
         ::= identList ( '=' constants )?
identList
         ::= operandName ( ',' operandName )*
constants
         ::= numeric
           | string_
           | bool_
           | solvable
           | 'nil'
expressionList
         ::= expression ( ',' expression )*
structDecl
         ::= 'def' IDENT '=' structType ';'
structType
         ::= ( 'flow' | 'stock' ) '{' ( sfProperties ',' )* '}'
sfProperties
         ::= IDENT ':' functionLit
           | structProperties
comProperties
         ::= IDENT ':' stateLit
           | structProperties
structProperties
         ::= IDENT ( ':' ( numeric | string_ | bool_ | operandName | prefix | solvable ) )?
initDecl ::= 'init' operand ';'
block    ::= '{' statementList? '}'
statementList
         ::= statement+
statement
         ::= constDecl
           | initDecl
           | simpleStmt ';'
           | ( 'if' ( simpleStmt ';' )? expression ( block 'else' 'if' ( simpleStmt ';' )? expression )* ( block 'else' )? )? block
simpleStmt
         ::= expression
           | incDecStmt
           | assignment
           | ';'
incDecStmt
         ::= expression ( '++' | '--' )
stateChange
         ::= ( 'advance' '(' paramCall | 'stay' '(' ) ')'
           | stateChange ( '&&' | '||' ) stateChange
accessHistory
         ::= operandName ( '[' expression ']' )+
assertion
         ::= 'assert' invariant temporal? ';'
assumption
         ::= 'assume' invariant temporal? ';'
temporal ::= 'eventually'
           | 'always'
           | 'eventually-always'
           | ( 'nmt' | 'nft' ) integer
invariant
         ::= ( 'when' expression 'then' )? expression
assignment
         ::= expressionList ( ( '+' | '-' | '^' | '*' | '/' | '%' | '<<' | '>>' | '&' | '&^' )? '=' | '->' | '<-' ) expressionList
forStmt  ::= 'for' rounds 'run' runBlock ';'?
rounds   ::= integer
paramCall
         ::= ( IDENT | 'this' ) ( '.' IDENT )+
stateBlock
         ::= '{' stateStep* '}'
stateStep
         ::= ( paramCall ( '|' paramCall )* | stateChange ) ';'
           | 'if' ( simpleStmt ';' )? expression stateBlock ( 'else' 'if' ( simpleStmt ';' )? expression stateBlock )* ( 'else' stateBlock )?
runBlock ::= '{' runStep* '}'
runStep  ::= ( paramCall ( '|' paramCall )* | IDENT '=' 'new' ( paramCall | IDENT ) | simpleStmt ) ';'
           | 'if' ( simpleStmt ';' )? expression runBlock ( 'else' 'if' ( simpleStmt ';' )? expression runBlock )* ( 'else' runBlock )?
faultType
         ::= 'string'
           | 'bool'
           | 'int'
           | 'float'
           | 'natural'
           | 'uncertain'
           | 'unknown'
solvable ::= faultType '(' operand? ( ',' operand )* ')'
expression
         ::= operand
           | solvable
           | prefix
           | expression ( '**' | '*' | '/' | '%' | '<<' | '>>' | '&' | '&^' | '+' | '-' | '^' | '==' | '!=' | '<' | '<=' | '>' | '>=' | '&&' | '||' ) expression
operand  ::= 'nil'
           | numeric
           | string_
           | bool_
           | operandName
           | accessHistory
           | '(' expression ')'
operandName
         ::= ( 'new' ( IDENT '.' )? )? IDENT
           | paramCall
           | 'this'
           | 'now'
prefix   ::= ( ( '+' | '-' | '!' | '^' | '*' | '&' ) expression )?
numeric  ::= integer
           | negative
           | float_
integer  ::= DECIMAL_LIT
           | OCTAL_LIT
           | HEX_LIT
negative ::= '-' ( integer | float_ )
float_   ::= FLOAT_LIT
string_  ::= RAW_STRING_LIT
           | INTERPRETED_STRING_LIT
bool_    ::= 'true'
           | 'false'
functionLit
         ::= 'func' block
stateLit ::= 'func' stateBlock
_        ::= WS
           | COMMENT
           | TERMINATOR
           | LINE_COMMENT
          /* ws: definition */

<?TOKENS?>

IDENT    ::= LETTER ( LETTER | UNICODE_DIGIT )*
DECIMAL_LIT
         ::= [1-9] [0-9]*
OCTAL_LIT
         ::= '0' OCTAL_DIGIT*
HEX_LIT  ::= '0' [xX] HEX_DIGIT+
FLOAT_LIT
         ::= DECIMALS ( '.' DECIMALS? EXPONENT? | EXPONENT )
           | '.' DECIMALS EXPONENT?
RAW_STRING_LIT
         ::= '`' [^`]* '`'
INTERPRETED_STRING_LIT
         ::= '"' ( [^"\] | ESCAPED_VALUE )* '"'
WS       ::= [ #x9]+
COMMENT? ::= '/*' .* '*/'
TERMINATOR
         ::= [#xd#xa]+
LINE_COMMENT
         ::= '//' [^#xd#xa]*
ESCAPED_VALUE
         ::= '\' ( ( ( 'u' | 'U' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT ) HEX_DIGIT HEX_DIGIT | 'x' ) HEX_DIGIT HEX_DIGIT | [abfnrtv\'"] | OCTAL_DIGIT OCTAL_DIGIT OCTAL_DIGIT )
DECIMALS ::= [0-9]+
OCTAL_DIGIT
         ::= [0-7]
HEX_DIGIT
         ::= [0-9a-fA-F]
EXPONENT ::= [eE] [+#x2D]? DECIMALS
LETTER   ::= UNICODE_LETTER
           | '_'
UNICODE_DIGIT
         ::= [#x0030-#x0039#x0660-#x0669#x06F0-#x06F9#x0966-#x096F#x09E6-#x09EF#x0A66-#x0A6F#x0AE6-#x0AEF#x0B66-#x0B6F#x0BE7-#x0BEF#x0C66-#x0C6F#x0CE6-#x0CEF#x0D66-#x0D6F#x0E50-#x0E59#x0ED0-#x0ED9#x0F20-#x0F29#x1040-#x1049#x1369-#x1371#x17E0-#x17E9#x1810-#x1819#xFF10-#xFF19]
UNICODE_LETTER
         ::= [#x0041-#x005A#x0061-#x007A#x00AA#x00B5#x00BA#x00C0-#x00D6#x00D8-#x00F6#x00F8-#x021F#x0222-#x0233#x0250-#x02AD#x02B0-#x02B8#x02BB-#x02C1#x02D0-#x02D1#x02E0-#x02E4#x02EE#x037A#x0386#x0388-#x038A#x038C#x038E-#x03A1#x03A3-#x03CE#x03D0-#x03D7#x03DA-#x03F3#x0400-#x0481#x048C-#x04C4#x04C7-#x04C8#x04CB-#x04CC#x04D0-#x04F5#x04F8-#x04F9#x0531-#x0556#x0559#x0561-#x0587#x05D0-#x05EA#x05F0-#x05F2#x0621-#x063A#x0640-#x064A#x0671-#x06D3#x06D5#x06E5-#x06E6#x06FA-#x06FC#x0710#x0712-#x072C#x0780-#x07A5#x0905-#x0939#x093D#x0950#x0958-#x0961#x0985-#x098C#x098F-#x0990#x0993-#x09A8#x09AA-#x09B0#x09B2#x09B6-#x09B9#x09DC-#x09DD#x09DF-#x09E1#x09F0-#x09F1#x0A05-#x0A0A#x0A0F-#x0A10#x0A13-#x0A28#x0A2A-#x0A30#x0A32-#x0A33#x0A35-#x0A36#x0A38-#x0A39#x0A59-#x0A5C#x0A5E#x0A72-#x0A74#x0A85-#x0A8B#x0A8D#x0A8F-#x0A91#x0A93-#x0AA8#x0AAA-#x0AB0#x0AB2-#x0AB3#x0AB5-#x0AB9#x0ABD#x0AD0#x0AE0#x0B05-#x0B0C#x0B0F-#x0B10#x0B13-#x0B28#x0B2A-#x0B30#x0B32-#x0B33#x0B36-#x0B39#x0B3D#x0B5C-#x0B5D#x0B5F-#x0B61#x0B85-#x0B8A#x0B8E-#x0B90#x0B92-#x0B95#x0B99-#x0B9A#x0B9C#x0B9E-#x0B9F#x0BA3-#x0BA4#x0BA8-#x0BAA#x0BAE-#x0BB5#x0BB7-#x0BB9#x0C05-#x0C0C#x0C0E-#x0C10#x0C12-#x0C28#x0C2A-#x0C33#x0C35-#x0C39#x0C60-#x0C61#x0C85-#x0C8C#x0C8E-#x0C90#x0C92-#x0CA8#x0CAA-#x0CB3#x0CB5-#x0CB9#x0CDE#x0CE0-#x0CE1#x0D05-#x0D0C#x0D0E-#x0D10#x0D12-#x0D28#x0D2A-#x0D39#x0D60-#x0D61#x0D85-#x0D96#x0D9A-#x0DB1#x0DB3-#x0DBB#x0DBD#x0DC0-#x0DC6#x0E01-#x0E30#x0E32-#x0E33#x0E40-#x0E46#x0E81-#x0E82#x0E84#x0E87-#x0E88#x0E8A#x0E8D#x0E94-#x0E97#x0E99-#x0E9F#x0EA1-#x0EA3#x0EA5#x0EA7#x0EAA-#x0EAB#x0EAD-#x0EB0#x0EB2-#x0EB3#x0EBD-#x0EC4#x0EC6#x0EDC-#x0EDD#x0F00#x0F40-#x0F6A#x0F88-#x0F8B#x1000-#x1021#x1023-#x1027#x1029-#x102A#x1050-#x1055#x10A0-#x10C5#x10D0-#x10F6#x1100-#x1159#x115F-#x11A2#x11A8-#x11F9#x1200-#x1206#x1208-#x1246#x1248#x124A-#x124D#x1250-#x1256#x1258#x125A-#x125D#x1260-#x1286#x1288#x128A-#x128D#x1290-#x12AE#x12B0#x12B2-#x12B5#x12B8-#x12BE#x12C0#x12C2-#x12C5#x12C8-#x12CE#x12D0-#x12D6#x12D8-#x12EE#x12F0-#x130E#x1310#x1312-#x1315#x1318-#x131E#x1320-#x1346#x1348-#x135A#x13A0-#x13B0#x13B1-#x13F4#x1401-#x1676#x1681-#x169A#x16A0-#x16EA#x1780-#x17B3#x1820-#x1877#x1880-#x18A8#x1E00-#x1E9B#x1EA0-#x1EE0#x1EE1-#x1EF9#x1F00-#x1F15#x1F18-#x1F1D#x1F20-#x1F39#x1F3A-#x1F45#x1F48-#x1F4D#x1F50-#x1F57#x1F59#x1F5B#x1F5D#x1F5F-#x1F7D#x1F80-#x1FB4#x1FB6-#x1FBC#x1FBE#x1FC2-#x1FC4#x1FC6-#x1FCC#x1FD0-#x1FD3#x1FD6-#x1FDB#x1FE0-#x1FEC#x1FF2-#x1FF4#x1FF6-#x1FFC#x207F#x2102#x2107#x210A-#x2113#x2115#x2119-#x211D#x2124#x2126#x2128#x212A-#x212D#x212F-#x2131#x2133-#x2139#x2160-#x2183#x3005-#x3007#x3021-#x3029#x3031-#x3035#x3038-#x303A#x3041-#x3094#x309D-#x309E#x30A1-#x30FA#x30FC-#x30FE#x3105-#x312C#x3131-#x318E#x31A0-#x31B7#x3400#x4DB5#x4E00#x9FA5#xA000-#xA48C#xAC00#xD7A3#xF900-#xFA2D#xFB00-#xFB06#xFB13-#xFB17#xFB1D#xFB1F-#xFB28#xFB2A-#xFB36#xFB38-#xFB3C#xFB3E#xFB40-#xFB41#xFB43-#xFB44#xFB46-#xFBB1#xFBD3-#xFD3D#xFD50-#xFD8F#xFD92-#xFDC7#xFDF0-#xFDFB#xFE70-#xFE72#xFE74#xFE76-#xFEFC#xFF21-#xFF3A#xFF41-#xFF5A#xFF66-#xFFBE#xFFC2-#xFFC7#xFFCA-#xFFCF#xFFD2-#xFFD7#xFFDA-#xFFDC]
mbellotti commented 1 year ago

Oh hey these are really cool 😀

mingodad commented 11 months ago

Trying to convert this grammar to use in https://mingodad.github.io/parsertl-playground/playground/ I've noticed that probably there is a mistake in this rule:

prefix
    :
    | ('+' | '-' | '!' | '^' | '*' | '&' ) expression
    ;

Probably this is what you want/mean:

prefix
    :  ('+' | '-' | '!' | '^' | '*' | '&' ) expression  //remove the |
    ;

You do not seem to want prefix to have an empty alternative, do you ?

mingodad commented 11 months ago

Also you have a duplicated production here:

stringDecl
    : IDENT '=' string_ eos
    | IDENT '=' compoundString eos
    | IDENT '=' compoundString eos  // identical the above
    ;

Probably you want this:

stringDecl
    : IDENT '=' string_ eos
    | IDENT '=' compoundString eos
    ;
mbellotti commented 11 months ago

Oh nice catch. Probably a merge conflict that got messy.

mingodad commented 11 months ago

Also I edited https://github.com/Fault-lang/Fault/issues/27#issuecomment-1761150545 changed my mistake:

-You do seem to want prefix to have an empty alternative, do you ?
+You do not seem to want prefix to have an empty alternative, do you ?
mingodad commented 11 months ago

I've just added Fault-lang grammar to https://mingodad.github.io/parsertl-playground/playground/ (select Fault-lang parser (partailly working) from Examples then click Parse to see a parser tree for the content in Input source).

Also you can test your grammar on http://lab.antlr.org/ .