Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- grammar language;
- compilationUnit
- : statement+
- ;
- statement
- : declaringStatement
- | nonDeclaringStatement
- ;
- declaringStatement
- : variableDeclaration
- ;
- nonDeclaringStatement
- : ifStatement
- | whileStatement
- | assignStatement
- | returnStatement
- ;
- returnStatement
- : 'return' expression?
- ;
- assignStatement
- : reference '=' expression
- ;
- variableDeclaration
- : mutability=('var' | 'let') name=Identifier ':' type '=' expression
- ;
- ifStatement
- : 'if' '(' condition=expression ')' '{' statement* '}' elseBlock?
- | 'if' '(' condition=expression ')' nonDeclaringStatement elseBlock?
- ;
- elseBlock
- : 'else' '{' statement* '}'
- | 'else' nonDeclaringStatement
- ;
- whileStatement
- : 'while' '(' condition=expression ')' '{' statement* '}'
- | 'while' '(' condition=expression ')' nonDeclaringStatement
- ;
- //====Expressions====
- expression
- : logical
- ;
- logical
- : logical op=('&&' | '||') comparison
- | comparison
- ;
- comparison
- : comparison op = ('==' | '!=' | '>' | '<' | '>=' | '<=' ) addition+
- | addition
- ;
- addition
- : addition op=('+' | '-') multiplication
- | multiplication
- ;
- multiplication
- : multiplication op=('*' | '/' | '%') unary
- | unary
- ;
- unary
- : '-' primary
- | '!' primary
- | primary
- ;
- primary
- : literal
- | '(' expression ')'
- | reference
- ;
- reference
- : Identifier
- ;
- literal
- : IntLiteral #intLiteral
- | FloatLiteral #floatLiteral
- | BoolLiteral #boolLiteral
- ;
- IntLiteral
- : '0'
- | [1-9][0-9]*
- ;
- FloatLiteral
- : [0-9]+'.'[0-9]+
- ;
- BoolLiteral
- : 'true' | 'false'
- ;
- type
- : primitiveType
- ;
- primitiveType
- : 'int'
- | 'float'
- | 'bool'
- ;
- Identifier
- : [a-zA-Z_][a-zA-Z_0-9]*
- ;
- WS: [ \t\r\n] -> skip;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement