WACCParser.g4 6.59 KB
Newer Older
1
parser grammar WACCParser;
2
3

options {
4
  tokenVocab=WACCLexer;
5
6
}

7
8
9
program     : import_file* BEGIN declaration stat END EOF;
library     : import_file* declaration;  // content of library
declaration : (struct | func)*;   // group of struct function declarations
xx1219's avatar
xx1219 committed
10
struct      : STRUCT IDENT IS OPEN_CURLY_BRACKET param_list? CLOSE_CURLY_BRACKET;
11
12
13
14
func        : type IDENT OPEN_PARENTHESES param_list? CLOSE_PARENTHESES IS stat END;
param_list  : param (COMMA param )* ;
param       : type IDENT;

15
import_file : IMPORT FILE_NAME;
16
17


18
19
20
21
22
23
24
25
26
stat : skp                            #StatSkipStat    // This visitor will be replaced by #SkipStat
     | declare                        #StatDeclareStat // This visitor will be replaced by #DeclareStat
     | assign                         #StatAssignStat  // This visitor will be replaced by #AssignStat
     | read                           #StatReadStat    // This visitor will be replaced by #ReadStat
     | free                           #StatFreeStat    // This visitor will be replaced by #FreeStat
     | RETURN expr                    #ReturnStat      
     | exit                           #StatExitStat    // This visitor will be replaced by #ExitStat
     | print                          #StatPrintStat   // This visitor will be replaced by #PrintStat
     | println                        #StatPrintlnStat // This visitor will be replaced by #PrintlnStat
27
28
     | BREAK                          #BreakStat
     | CONTINUE                       #ContinueStat
Tom Zhao's avatar
Tom Zhao committed
29
     | IF expr THEN stat (ELSE stat)? FI #IfStat
30
     | FOR OPEN_PARENTHESES
31
32
33
       for_stat SEMICOLON
       expr SEMICOLON
       for_stat
34
       CLOSE_PARENTHESES DO stat DONE #ForStat
35
36
     | SWITCH expr DO (CASE expr stat)*
       DEFAULT stat DONE              #SwitchStat
37
38
39
40
     | DO stat WHILE expr             #DoWhileStat
     | WHILE expr DO stat DONE        #WhileStat
     | BEGIN stat END                 #ScopeStat
     | stat SEMICOLON stat            #SeqStat
41
42
     ;

xx1219's avatar
xx1219 committed
43
44
45
46
assign_lhs : IDENT        #Ident
           | array_elem   #LHSArrayElem // This visitor will be replaced by visitArray_elem()
           | pair_elem    #LHSPairElem  // This visitor will be replaced by visitors in pair_elem
           | struct_elem  #LHSStructElem // This visitor will be replaced by visitors in struct_elem
47
           ;
Tom Zhao's avatar
Tom Zhao committed
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
for_stat : skp                  #ForStatSkp     // This visitor will be replaced by #SkipStat
     | declare                  #ForStatDeclare // This visitor will be replaced by #DeclareStat
     | assign                   #ForStatAssign  // This visitor will be replaced by #AssignStat
     | read                     #ForStatRead    // This visitor will be replaced by #ReadStat
     | free                     #ForStatFree    // This visitor will be replaced by #FreeStat
     | exit                     #ForStatExit    // This visitor will be replaced by #ExitStat
     | print                    #ForStatPrint   // This visitor will be replaced by #PrintStat
     | println                  #ForStatPrintln // This visitor will be replaced by #PrintlnStat
     | for_stat COMMA for_stat  #ForStatSeq
     ;

skp     : SKP                          #SkipStat;
declare : type IDENT ASSIGN assign_rhs #DeclareStat;
assign  : assign_lhs ASSIGN assign_rhs #AssignStat;
read    : READ assign_lhs              #ReadStat;
free    : FREE expr                    #FreeStat;
exit    : EXIT expr                    #ExitStat;
print   : PRINT expr                   #PrintStat;
println : PRINTLN expr                 #PrintlnStat;
68

xx1219's avatar
xx1219 committed
69
70
71
72
73
assign_rhs : expr                                                          #ExprNode     // This visitor will be replaced by the general visit()
           | array_liter                                                   #ArrayLiteral // This visitor will be replaced by visitArray_liter()
           | NEWPAIR OPEN_PARENTHESES expr COMMA expr  CLOSE_PARENTHESES   #NewPair
           | pair_elem                                                     #RHSPairElem  // This visitor will be replaced by visitPair_elem()
           | CALL IDENT OPEN_PARENTHESES arg_list? CLOSE_PARENTHESES       #FunctionCall
74
75
           ;

76
// argument list for functions 
77
arg_list  : expr (COMMA expr)*;
xx1219's avatar
xx1219 committed
78
79

struct_elem : IDENT (DOT IDENT)+;
80
new_struct  : NEW IDENT OPEN_PARENTHESES arg_list? CLOSE_PARENTHESES ;
xx1219's avatar
xx1219 committed
81

82
83
pair_elem : FST expr #FstExpr
          | SND expr #SndExpr
84
85
          ;

xx1219's avatar
xx1219 committed
86
87
88
89
type : base_type    #BaseType
     | array_type   #ArrayType
     | pair_type    #PairType
     | struct_type  #StructType
90
91
     ;

xx1219's avatar
xx1219 committed
92
93
94
95
base_type : INT     #IntType
          | BOOL    #BoolType
          | CHAR    #CharType
          | STRING  #StringType
96
97
          ;

98
array_type     : array_type OPEN_SQUARE_BRACKET CLOSE_SQUARE_BRACKET
99
               | base_type OPEN_SQUARE_BRACKET CLOSE_SQUARE_BRACKET
100
               | pair_type OPEN_SQUARE_BRACKET CLOSE_SQUARE_BRACKET
xx1219's avatar
xx1219 committed
101
               | struct_type OPEN_SQUARE_BRACKET CLOSE_SQUARE_BRACKET
102
               ;
103
pair_type      : PAIR OPEN_PARENTHESES pair_elem_type  COMMA pair_elem_type  CLOSE_PARENTHESES ;
xx1219's avatar
xx1219 committed
104
105
106
107
pair_elem_type : base_type   #PairElemBaseType   // This visitor will be replaced by base_type visitors
               | array_type  #PairElemArrayType  // This visitor will be replaced by array_type visitors
               | struct_type #PairElemStructType // This visitor will be replaced by struct_type visitors
               | PAIR        #PairElemPairType
108
109
               ;

xx1219's avatar
xx1219 committed
110
111
112
struct_type : IDENT ;


113
114
expr : INT_LITER      #IntExpr
     | PLUS INT_LITER #IntExpr
沈's avatar
committed
115
116
117
     | BINARY_LITER   #BinaryExpr
     | OCTAL_LITER    #OctalExpr
     | HEX_LITER      #HexExpr
118
119
120
121
122
123
     | BOOL_LITER     #BoolExpr
     | CHAR_LITER     #CharExpr
     | STR_LITER      #StrExpr
     | PAIR_LITER     #PairExpr
     | IDENT          #IdExpr
     | array_elem     #ArrayExpr
xx1219's avatar
xx1219 committed
124
125
     | struct_elem    #StructElemExpr
     | new_struct     #StructExpr
xx1219's avatar
xx1219 committed
126
     | EMPTY          #EmptyStructExpr
127
     | uop=( '-' | '!' | 'len' | 'ord' | 'chr' | '~') expr #UnopExpr
徐世桐's avatar
徐世桐 committed
128
129
     | expr bop=( '*' | '/' | '%' ) expr              #ArithmeticExpr
     | expr bop=( '+' | '-' ) expr                    #ArithmeticExpr
Tom Zhao's avatar
Tom Zhao committed
130
     | expr bop=( '<<' | '>>' ) expr                  #BitwiseExpr
131
132
     | expr bop=( '>' | '>=' | '<' | '<=' ) expr      #CmpExpr
     | expr bop=( '==' | '!=' ) expr                  #EqExpr
Tom Zhao's avatar
Tom Zhao committed
133
     | expr bop=( '&' | '|' | '^' ) expr              #BitwiseExpr
134
135
     | expr '&&' expr                                 #AndExpr
     | expr '||' expr                                 #OrExpr
136
     | OPEN_PARENTHESES expr CLOSE_PARENTHESES        #ParenExpr
137
138
139
     ;

array_elem  : IDENT (OPEN_SQUARE_BRACKET expr CLOSE_SQUARE_BRACKET)+ ;
沈's avatar
committed
140
141
array_liter : OPEN_SQUARE_BRACKET (expr (COMMA expr)*)? CLOSE_SQUARE_BRACKET ;