59 lines
1.2 KiB
ANTLR
59 lines
1.2 KiB
ANTLR
grammar xlang;
|
|
|
|
file : function+ EOF;
|
|
function : Identifier LeftParen argumentList? RightParen block;
|
|
argumentList : Identifier (Comma Identifier)*;
|
|
block : LeftBrace statement* RightBrace;
|
|
statement : Identifier Assign expr Semicolon
|
|
| If expr block (Else block)?
|
|
| While expr block
|
|
| Return expr Semicolon
|
|
| Print expr Semicolon
|
|
;
|
|
expr : sum ((Less|LessEqual|Greater|GreaterEqual|Equal|NotEqual) sum)
|
|
| sum
|
|
;
|
|
sum : sum ((Plus|Minus) term)
|
|
| term
|
|
;
|
|
term : term ((Mul|Div) factor)
|
|
| factor
|
|
;
|
|
factor : Integer
|
|
| Identifier
|
|
| Identifier LeftParen exprList? RightParen
|
|
| LeftParen sum RightParen
|
|
;
|
|
exprList : expr (Comma expr)*;
|
|
|
|
If : 'if';
|
|
Else : 'else';
|
|
While : 'while';
|
|
Return : 'return';
|
|
Print : 'print';
|
|
|
|
LeftParen : '(';
|
|
RightParen : ')';
|
|
LeftBrace : '{';
|
|
RightBrace : '}';
|
|
Assign : '=';
|
|
Less : '<';
|
|
LessEqual : '<=';
|
|
Greater : '>';
|
|
GreaterEqual : '>=';
|
|
Equal : '==';
|
|
NotEqual : '!=';
|
|
Plus : '+';
|
|
Minus : '-';
|
|
Mul : '*';
|
|
Div : '/';
|
|
Comma : ',';
|
|
Semicolon : ';';
|
|
|
|
Identifier : [_a-zA-Z][_a-zA-Z0-9]*;
|
|
Integer : [0-9]+;
|
|
|
|
Comment : '//' ~[\n]* '\n' -> skip;
|
|
Whitespace : [ \t\r\n]+ -> skip;
|
|
Unknown : .;
|