-
Notifications
You must be signed in to change notification settings - Fork 0
/
TInf.g4
94 lines (83 loc) · 2.67 KB
/
TInf.g4
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
grammar TInf;
// Given language constructs
entry: fctDef*;
stmtLst: (stmt | ifStmt | whileLoop | doWhileLoop | forLoop | switchStmt)*;
stmt: (varDecl | assignStmt) SEMICOLON;
type: TYPE_INT | TYPE_DOUBLE | TYPE_STRING | TYPE_BOOL | TYPE_EMPTY;
printBuiltinCall: PRINT LPAREN logicalExpr RPAREN;
// If statement (team 1)
ifStmt: IF LPAREN logicalExpr RPAREN LBRACE stmtLst RBRACE afterIf?;
afterIf: elsePre elsePost;
elsePre: ELSE;
elsePost: ifStmt | else;
else: LBRACE stmtLst RBRACE;
// While loop (team 2)
whileLoop: WHILE LPAREN logicalExpr RPAREN LBRACE stmtLst RBRACE;
// Do-While loop (team 3)
doWhileLoop: DO LBRACE stmtLst RBRACE WHILE LPAREN logicalExpr RPAREN SEMICOLON;
// For loop (team 4)
forLoop: FOR LPAREN varDecl SEMICOLON logicalExpr SEMICOLON assignStmt RPAREN LBRACE stmtLst RBRACE;
// Switch statement (team 5)
switchStmt: SWITCH LPAREN logicalExpr RPAREN LBRACE case* default? RBRACE;
case: (CASE (INT_LIT | DOUBLE_LIT | STRING_LIT) COLON stmtLst);
default: DEFAULT COLON stmtLst;
// Function definition / call (team 6)
fctDef: FUNC type IDENTIFIER LPAREN paramLst? RPAREN logic CNUF;
paramLst: param (COMMA param)*;
param: type IDENTIFIER;
logic: stmtLst RETURN logicalExpr? SEMICOLON;
fctCall: CALL IDENTIFIER LPAREN callParams? RPAREN;
callParams: logicalExpr (COMMA logicalExpr)*;
// Variable declaration / assignment (team 7)
varDecl: type IDENTIFIER (ASSIGN logicalExpr)?;
assignStmt: (IDENTIFIER ASSIGN)? logicalExpr;
// Expression loop (team 8)
logicalExpr: compareExpr ((LOGICAL_AND | LOGICAL_OR) compareExpr)*;
compareExpr: additiveExpr ((EQUAL | NOT_EQUAL) additiveExpr)?;
additiveExpr: multiplicativeExpr ((PLUS | MINUS) multiplicativeExpr)*;
multiplicativeExpr: prefixExpr ((MUL | DIV) prefixExpr)*;
prefixExpr: (PLUS | MINUS)? atomicExpr;
atomicExpr: INT_LIT | DOUBLE_LIT | STRING_LIT | TRUE | FALSE | IDENTIFIER | fctCall | printBuiltinCall | LPAREN logicalExpr RPAREN;
// Terminals
TYPE_INT: 'int';
TYPE_DOUBLE: 'double';
TYPE_STRING: 'string';
TYPE_BOOL: 'bool';
TYPE_EMPTY: 'empty';
IF: 'if';
ELSE: 'else';
WHILE: 'while';
DO: 'do';
FOR: 'for';
FUNC: 'func';
CNUF: 'cnuf';
RETURN: 'return';
SWITCH: 'switch';
CASE: 'case';
DEFAULT: 'default';
CALL: 'call';
PRINT: 'print';
TRUE: 'true';
FALSE: 'false';
LBRACE: '{';
RBRACE: '}';
LPAREN: '(';
RPAREN: ')';
COMMA: ',';
COLON: ':';
PLUS: '+';
MINUS: '-';
MUL: '*';
DIV: '/';
EQUAL: '==';
NOT_EQUAL: '!=';
LOGICAL_AND: '&&';
LOGICAL_OR: '||';
SEMICOLON: ';';
ASSIGN: '=';
IDENTIFIER: [a-z_][a-zA-Z0-9_]*;
INT_LIT: [1-9][0-9]* | '0';
DOUBLE_LIT: [0-9]*[.][0-9]+;
STRING_LIT: '"' (~["\\\r\n])* '"';
// Skipped tokens
WS: [ \t\r\n]+ -> skip;