coreyp1 / CTang

0 stars 0 forks source link

Grammar railroad diagram #1

Open mingodad opened 3 months ago

mingodad commented 3 months ago

Using some online tools like https://github.com/GuntherRademacher/rr and https://github.com/GuntherRademacher/ebnf-convert we can have a nice navigable railroad diagram.

Copy and paste the EBNF shown bellow on (IPV4) https://rr.red-dove.com/ui on the tab Edit Grammar the click on the tab View Diagram to see/download a navigable railroad diagram.

//
// 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 of the urls shown above in the 'Edit Grammar' tab
// then click the 'View Diagram' tab.
//

program::=
      expression
    | statements

functionDeclarationArguments::=
      /*%empty*/
    | IDENTIFIER
    | functionDeclarationArguments COMMA IDENTIFIER

expressionList::=
      /*%empty*/
    | expression
    | expressionList COMMA expression

mapList::=
      IDENTIFIER COLON expression
    | mapList COMMA IDENTIFIER COLON expression
    | mapList COMMA

statements::=
      statement
    | statements statement

statement::=
      closedStatement
    | openStatement

closedStatement::=
      IF LPAREN expression RPAREN closedStatement ELSE closedStatement
    | WHILE LPAREN expression RPAREN closedStatement
    | DO statement WHILE LPAREN expression RPAREN SEMICOLON
    | FOR LPAREN optionalExpression SEMICOLON optionalExpression SEMICOLON optionalExpression RPAREN closedStatement
    | FOR LPAREN IDENTIFIER COLON expression RPAREN closedStatement
    | FUNCTION IDENTIFIER LPAREN functionDeclarationArguments RPAREN codeBlock
    | codeBlock
    | RETURN SEMICOLON
    | RETURN expression SEMICOLON
    | BREAK SEMICOLON
    | CONTINUE SEMICOLON
    | expression SEMICOLON
    | TEMPLATESTRING
    | QUICKPRINTBEGINANDSTRING expression QUICKPRINTEND
    | QUICKPRINTBEGIN expression QUICKPRINTEND
    | USE IDENTIFIER SEMICOLON
    | USE libraryExpression AS IDENTIFIER SEMICOLON
    | GLOBAL IDENTIFIER SEMICOLON
    | GLOBAL IDENTIFIER ASSIGN expression SEMICOLON

libraryExpression::=
      IDENTIFIER
    | libraryExpression PERIOD IDENTIFIER

openStatement::=
      IF LPAREN expression RPAREN statement
    | IF LPAREN expression RPAREN closedStatement ELSE openStatement
    | WHILE LPAREN expression RPAREN openStatement
    | FOR LPAREN optionalExpression SEMICOLON optionalExpression SEMICOLON optionalExpression RPAREN openStatement
    | FOR LPAREN IDENTIFIER COLON expression RPAREN openStatement

optionalExpression::=
      /*%empty*/
    | expression

slice::=
      expression LBRACKET optionalExpression COLON optionalExpression COLON optionalExpression RBRACKET
    | expression LBRACKET optionalExpression COLON optionalExpression RBRACKET

codeBlock::=
      LBRACE RBRACE
    | LBRACE statements RBRACE

expression::=
      NULL_
    | IDENTIFIER
    | INTEGER
    | FLOAT
    | BOOLEAN
    | STRING
    | expression ASSIGN expression
    | expression PLUS expression
    | expression MINUS expression
    | expression MULTIPLY expression
    | expression DIVIDE expression
    | expression MODULO expression
    | MINUS expression
    | EXCLAMATIONPOINT expression
    | expression LESSTHAN expression
    | expression LESSTHANEQUAL expression
    | expression GREATERTHAN expression
    | expression GREATERTHANEQUAL expression
    | expression EQUALCOMPARE expression
    | expression NOTEQUAL expression
    | expression AND expression
    | expression OR expression
    | slice
    | LPAREN expression RPAREN
    | expression AS INTEGER
    | expression AS FLOAT
    | expression AS BOOLEAN
    | expression AS STRING
    | PRINT LPAREN expression RPAREN
    | expression PERIOD IDENTIFIER
    | LBRACKET expressionList RBRACKET
    | LBRACE COLON RBRACE
    | LBRACE mapList RBRACE
    | expression LBRACKET expression RBRACKET
    | expression LPAREN expressionList RPAREN
    | expression QUESTIONMARK expression COLON expression

//Tokens

AND ::= "&&"
AS ::= "as"
ASSIGN ::= "="
BOOLEAN ::= "true"|"false"
BREAK ::= "break"
COLON ::= ":"
COMMA ::= ","
CONTINUE ::= "continue"
DIVIDE ::= "/"
DO ::= "do"
ELSE ::= "else"
EQUALCOMPARE ::= "=="
EXCLAMATIONPOINT ::= "!"
FOR ::= "for"
FUNCTION ::= "function"
GLOBAL ::= "global"
GREATERTHAN ::= ">"
GREATERTHANEQUAL ::= ">="
IF ::= "if"
LBRACE ::= "{"
LBRACKET ::= "["
LESSTHAN ::= "<"
LESSTHANEQUAL ::= "<="
LPAREN ::= "("
MINUS ::= "-"
MODULO ::= "%"
MULTIPLY ::= "*"
NOTEQUAL ::= "!="
NULL_ ::= "null"
OR ::= "||"
PERIOD ::= "."
PLUS ::= "+"
PRINT ::= "print"
QUESTIONMARK ::= "?"
RBRACE ::= "}"
RBRACKET ::= "]"
RETURN ::= "return"
RPAREN ::= ")"
SEMICOLON ::= ";"
USE ::= "use"
WHILE ::= "while"
coreyp1 commented 2 months ago

Wow! That's a nice tool! Thank you for the work! I want to make sure that I like my quick print syntax, but I will add this soon!

Also, since you found the project, I went ahead and added a README.md so that it makes a bit more sense.

mingodad commented 2 months ago

Thank you for reply ! I've just added the Tang parser/lexer to https://mingodad.github.io/parsertl-playground/playground/ an Yacc/Lex compatible online editor/tester/interpreter (select Tang parser (partially working) from Examples then click Parse to see a parse tree for the content in Input source).