173 lines
3.2 KiB
EBNF
173 lines
3.2 KiB
EBNF
(* ---------- Program ---------- *)
|
|
|
|
program
|
|
= { using-directive }
|
|
namespace-declaration
|
|
{ type-declaration } ;
|
|
|
|
using-directive
|
|
= "using" identifier [ "as" identifier ] ";" ;
|
|
|
|
namespace-declaration
|
|
= "namespace" identifier ";" ;
|
|
|
|
type-declaration
|
|
= struct-declaration
|
|
| class-declaration ;
|
|
|
|
(* ---------- Structs & Classes ---------- *)
|
|
|
|
struct-declaration
|
|
= "struct" identifier "{" { field-declaration } "}" ;
|
|
|
|
class-declaration
|
|
= "class" identifier "{" { class-member-declaration } "}" ;
|
|
|
|
field-declaration
|
|
= type identifier [ "=" expression ] ";" ;
|
|
|
|
class-member-declaration
|
|
= field-declaration
|
|
| function-declaration ;
|
|
|
|
function-declaration
|
|
= type identifier "(" [ parameter-list ] ")" block ;
|
|
|
|
parameter-list
|
|
= parameter { "," parameter } ;
|
|
|
|
parameter
|
|
= type identifier ;
|
|
|
|
(* ---------- Types ---------- *)
|
|
|
|
type
|
|
= "int" | "string" | "bool" | "void"
|
|
| "pointer" | "array" | "var"
|
|
| identifier ;
|
|
|
|
(* ---------- Statements ---------- *)
|
|
|
|
block
|
|
= "{" { statement } "}" ;
|
|
|
|
statement
|
|
= variable-declaration ";"
|
|
| assignment ";"
|
|
| expression ";"
|
|
| if-statement
|
|
| while-statement
|
|
| for-statement
|
|
| repeat-statement
|
|
| return-statement
|
|
| break-statement
|
|
| block
|
|
| ";" ;
|
|
|
|
variable-declaration
|
|
= type identifier "=" expression ;
|
|
|
|
assignment
|
|
= primary "=" expression ;
|
|
|
|
if-statement
|
|
= "if" "(" expression ")" statement
|
|
{ "else" "if" "(" expression ")" statement }
|
|
[ "else" statement ] ;
|
|
|
|
while-statement
|
|
= "while" "(" expression ")" statement ;
|
|
|
|
for-statement
|
|
= "for" "(" [ expression ] ";"
|
|
[ expression ] ";"
|
|
[ expression ] ")"
|
|
statement ;
|
|
|
|
repeat-statement
|
|
= "repeat" "(" expression ")" statement ;
|
|
|
|
return-statement
|
|
= "return" [ expression ] ";" ;
|
|
|
|
break-statement
|
|
= "break" ";" ;
|
|
|
|
(* ---------- Expressions ---------- *)
|
|
|
|
expression
|
|
= logical-or ;
|
|
|
|
logical-or
|
|
= logical-and { "||" logical-and } ;
|
|
|
|
logical-and
|
|
= equality { "&&" equality } ;
|
|
|
|
equality
|
|
= relational { ( "==" | "!=" | "~=" ) relational } ;
|
|
|
|
relational
|
|
= additive { ( "<" | ">" | "<=" | ">=" ) additive } ;
|
|
|
|
additive
|
|
= multiplicative { ( "+" | "-" ) multiplicative } ;
|
|
|
|
multiplicative
|
|
= unary { ( "*" | "/" | "%" ) unary } ;
|
|
|
|
unary
|
|
= ( "++" | "--" | "-" | "!" ) unary
|
|
| primary ;
|
|
|
|
primary
|
|
= literal
|
|
| identifier primary-tail
|
|
| "new" identifier [ object-initializer ]
|
|
| "(" expression ")" ;
|
|
|
|
primary-tail
|
|
= "(" [ argument-list ] ")"
|
|
| "[" expression "]"
|
|
| ε ;
|
|
|
|
argument-list
|
|
= expression { "," expression } ;
|
|
|
|
object-initializer
|
|
= "{" { field-initializer ";" } "}" ;
|
|
|
|
field-initializer
|
|
= identifier "=" expression ;
|
|
|
|
(* ---------- Literals ---------- *)
|
|
|
|
literal
|
|
= integer-literal
|
|
| string-literal
|
|
| boolean-literal
|
|
| "nil" ;
|
|
|
|
integer-literal
|
|
= digit { digit } ;
|
|
|
|
string-literal
|
|
= '"' { character } '"' ;
|
|
|
|
boolean-literal
|
|
= "true" | "false" ;
|
|
|
|
identifier
|
|
= letter { letter | digit | "_" } ;
|
|
|
|
letter
|
|
= "A"…"Z" | "a"…"z" | "_" ;
|
|
|
|
digit
|
|
= "0"…"9" ;
|
|
|
|
(* ---------- Comments (lexical, skipped) ---------- *)
|
|
|
|
comment
|
|
= "//" { character }
|
|
| "/*" { character } "*/" ; |