-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrammar.ebnf
32 lines (32 loc) · 1.82 KB
/
grammar.ebnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Program ::= (Class | Function | Statement)*
SimpleType ::= ('int' | 'float' | 'string' | 'char' | 'bool')
ComposedType ::= SimpleType ('[' IntLiteral ']')* | 'id'
Class ::= 'class' 'id' ('(' 'id' ')')? ':' InitBlock Function*
InitBlock ::= 'init' ':' ('let' 'id' ':' SimpleType)* Function
Function ::= 'def' 'id' '(' ('id' ':' SimpleType ( ',' 'id' ':' SimpleType)*)? ')' '->' (SimpleType | 'void') ':' Block
CreateObject ::= 'create' 'id' ':' 'id' '(' (Expr (',' Expr)*)? ')'
Statement ::= FunctionCall | MethodCall | Declaration | CreateObject | Assignment | If | While | For | Print | Read | 'return' Expr? | 'continue' | 'pass' | 'break'
Assignment ::= ('id' | 'self') ('.' 'id')? ('[' Expr ']')? ':=' Expr
Declaration ::= 'let' 'id' (',' 'id')* ':' ComposedType (':=' Expr)?
Print ::= 'print' '(' Expr ')'
Read ::= 'read' '(' ')'
FunctionCall ::= 'id' '(' (Expr (',' Expr)*)? ')'
MethodCall ::= ('id' | 'self') '.' 'id' '(' (Expr (',' Expr)*)? ')'
MemberAccess ::= ('id' | 'self') '.' 'id'
Block ::= Statement+
If ::= 'if' Expr ':' Block ('elif' Expr ':' Block)* ('else' Block)?
While ::= 'while' Expr ':' Block
For ::= 'for' 'id' (',' 'id')* ':' SimpleType ':=' Expr ':' Expr ':' (Assignment (',' Assignment)*) ':' Block
Expr ::= AndExpr 'or' Expr | AndExpr
AndExpr ::= NotExpr 'and' AndExpr | NotExpr
NotExpr ::= 'not'? CExpr
CExpr ::= FirstLevel ('==' | '!=' | '>' | '<' | '<=' | '>=') CExpr | FirstLevel
FirstLevel ::= SecondLevel ('+' | '-') FirstLevel | SecondLevel
SecondLevel ::= ThirdLevel ('*' | '/') SecondLevel | ThirdLevel
ThirdLevel ::= FourthLevel '**' ThirdLevel | FourthLevel
FourthLevel ::= ('+' | '-')? Factor
Factor ::= 'id' | FunctionCall | MethodCall | ArrayAccess | MemberAccess | '(' Expr ')' | Integer | Float | Bool | String
Integer ::= IntLiteral
Float ::= FloatLiteral
Bool ::= 'True' | 'False'
ArrayAccess ::= 'id' ('[' Expr ']')+