Skip to content

Commit

Permalink
Added grammar in EBNF format (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdoucy authored Aug 24, 2024
1 parent 619b881 commit f11ef76
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 49 deletions.
48 changes: 48 additions & 0 deletions doc/grammar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Grammar

The specification of the Hudson programming language in EBNF notation is the following :

```ebnf
program = { stmt }
stmt = expr_stmt | declaration | assignment | inc_dec | return
| break | continue | print | block | while
| for | if | function
expr_stmt = expr ";"
declaration = type ident [ "=" expr ] ";"
assignment = ident ( "=" | "+=" | "+-" | "*=" | "%=" | "/+" ) expr ";"
inc_dec = ident ( "++" | "--" ) ";"
return = "return" [ expr ] ";"
break = "break" ";"
continue = "continue" ";"
print = "print" "(" [ expr ] ")" ";"
block = "{" { stmt } "}"
while = "while" "(" expr ")" ( stmt | ";" )
for = "for" "(" [ declaration | assignment ] ";" expr ";" assignment | inc_dec ")" ( smt | ";" )
if = "if" "(" expr ")" stmt [ "else" stmt ]
function = "fnc" ident fnc_signature block
expr = or
or = and { "||" and }
and = bitwise_or { "&&" bitwise_or }
bitwise_or = bitwise_xor { "|" bitwise_xor }
bitwise_xor = bitwise_and { "^" bitwise_and }
bitwise_and = equality { "&" equality }
equality = comparison { "==" | "!=" comparison }
comparison = bitshift { "<" | "<=" | ">" | ">=" bitshift }
bitshift = term { ">>" | "<<" term }
term = factor { "-" | "+" factor }
factor = unary { "/" | "*" | "%" unary }
unary = ( "+" | "-" | "!" | "~" ) unary | call
call = primary { "(" [ params ] ")" }
primary = int_lit | ident | string_lit | anonymous_fnc | grouping
grouping = "(" expr ")"
params = expr { "," expr }
anonymous_fnc = 'fnc' fnc_signature block
fnc_signature = "(" [ ident type { "," ident type } ] ")" [ type ]
type = "int" | "str" | "fnc" | fnc_signature
ident = ( letter | "_" ) { letter | digit | "_" }
int_lit = digit | { digit }
string_lit = '"' { * } '"'
letter = "A" ... " Z" | "a" ... "z"
digit = "0" ... "9"
```
49 changes: 0 additions & 49 deletions doc/operator-precedence.md

This file was deleted.

0 comments on commit f11ef76

Please sign in to comment.