Skip to main content

Formal grammar

This mirrors the recursive-descent structure of the parser directly.

program → (NEWLINE | statement)* EOF

statement → varDecl | assignment | showStmt | ifStmt
| whileStmt | repeatStmt | functionDecl | returnStmt
| exprStmt

varDecl → "let" IDENTIFIER "be" expression NEWLINE
assignment → "set" IDENTIFIER "to" expression NEWLINE
showStmt → "show" expression NEWLINE
ifStmt → "if" expression NEWLINE block ("otherwise" NEWLINE block)?
whileStmt → "while" expression NEWLINE block
repeatStmt → "repeat" expression "times" "as" IDENTIFIER NEWLINE block
functionDecl → "define" IDENTIFIER "taking" params NEWLINE block
returnStmt → "return" expression NEWLINE
exprStmt → expression NEWLINE

block → INDENT statement+ DEDENT

expression → logicOr
logicOr → logicAnd ( "or" logicAnd )*
logicAnd → equality ( "and" equality )*
equality → comparison ( ("==" | "!=") comparison )*
comparison → term ( ("<" | "<=" | ">" | ">=") term )*
term → factor ( ("+" | "-") factor )*
factor → unary ( ("*" | "/" | "%") unary )*
unary → ("not" | "-") unary | call
call → primary ( "(" arguments? ")" )?
primary → NUMBER | STRING | "true" | "false"
| IDENTIFIER | "(" expression ")"