1 | // s2k - A domain specific language for streaming text extraction and transformations. |
---|
2 | // |
---|
3 | // 2012/09/08 Original definition. |
---|
4 | // 2014/03/29 Removed VOID type as a return type. |
---|
5 | // 2014/04/01 Added streamGraph. |
---|
6 | // 2014/15/04 Added foreach loop. |
---|
7 | // 2014/17/04 Added for loop. |
---|
8 | // |
---|
9 | // Tom Shermer <shermer at sfu dot ca> |
---|
10 | // Ken Herdy <ksherdy at sfu dot ca> |
---|
11 | // |
---|
12 | |
---|
13 | types { |
---|
14 | toolchain.s2k.lang.signatures.FunctionSignature, |
---|
15 | toolchain.s2k.lang.type.Type, |
---|
16 | toolchain.s2k.lang.type.PrimitiveType, |
---|
17 | toolchain.s2k.lang.type.StreamType, |
---|
18 | toolchain.s2k.semanticAnalyzer.Binding, |
---|
19 | toolchain.s2k.semanticAnalyzer.SymbolTable, |
---|
20 | } |
---|
21 | |
---|
22 | context main { |
---|
23 | |
---|
24 | recognizers { |
---|
25 | Whitespace [IGNORE], |
---|
26 | Integer [INTEGER_CONST], |
---|
27 | Identifier [IDENTIFIER], // Java identifiers, except $ character. Produces Lextant tokens for keywords. |
---|
28 | Comment [IGNORE] "#" "\n", // Support parabix2_pablo debugging. |
---|
29 | Comment [IGNORE] "//" "\n", |
---|
30 | Comment [SQ_STRING] "'" "'", |
---|
31 | Comment [IGNORE] "/*" "*/", // Comment. Comment [STRING] "/*" "*/", |
---|
32 | String [STRING], |
---|
33 | Punctuator, |
---|
34 | EndOfInput, |
---|
35 | } |
---|
36 | |
---|
37 | // t o k e n s |
---|
38 | tokens { |
---|
39 | GRAPH, |
---|
40 | FILTER, |
---|
41 | IN, |
---|
42 | OUT, |
---|
43 | INOUT, |
---|
44 | STREAM, |
---|
45 | STRUCT, |
---|
46 | INT, |
---|
47 | IF, |
---|
48 | ELSE, |
---|
49 | WHILE, |
---|
50 | FOR, |
---|
51 | BY, |
---|
52 | FOREACH, |
---|
53 | RETURN, |
---|
54 | AND "&", |
---|
55 | OR "|", |
---|
56 | NOT "~", |
---|
57 | XOR "^", |
---|
58 | MULTIPLY "*", |
---|
59 | DIVIDE "/", |
---|
60 | PLUS "+", |
---|
61 | MINUS "-", |
---|
62 | ASSIGN "=", |
---|
63 | AND_ASSIGN "&=", |
---|
64 | OR_ASSIGN "|=", |
---|
65 | XOR_ASSIGN "^=", |
---|
66 | MULTIPLY_ASSIGN "*=", |
---|
67 | DIVIDE_ASSIGN "/=", |
---|
68 | PLUS_ASSIGN "+=", |
---|
69 | MINUS_ASSIGN "-=", |
---|
70 | LANGLE "<", |
---|
71 | RANGLE ">", |
---|
72 | LCURLY "{", |
---|
73 | RCURLY "}", |
---|
74 | LROUND "(", |
---|
75 | RROUND ")", |
---|
76 | COMMA ",", |
---|
77 | TERMINATOR ";", |
---|
78 | DOT ".", |
---|
79 | DDOT "..", |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | interfaces { |
---|
84 | intValued {int value = 0;}, |
---|
85 | hasSymbolTable {SymbolTable symbolTable = null;}, |
---|
86 | hasBinding {Binding binding = null;}, |
---|
87 | hasFieldWidth {int fieldWidth = 1;}, |
---|
88 | hasSignature {FunctionSignature signature = null;}, |
---|
89 | } |
---|
90 | |
---|
91 | nodes { |
---|
92 | ASTNode {Type type = null;} [assignOperator], |
---|
93 | program {hasSymbolTable;} [], |
---|
94 | structDef {hasSymbolTable;} [], |
---|
95 | filterDef {hasSymbolTable;} [], |
---|
96 | blockStmt {hasSymbolTable;} [], |
---|
97 | identifier {hasBinding;} [], |
---|
98 | compoundIdentifier {hasBinding;} [], |
---|
99 | unaryOperator {hasSignature;} [expr5], |
---|
100 | binaryOperator {hasSignature;} [expr expr1 expr2 expr3 expr4], |
---|
101 | assign {} [assignRest], |
---|
102 | funcCall {hasSignature;} [funcCallRest], |
---|
103 | idisaFuncCall {hasSignature;} [idisaFuncCallRest], |
---|
104 | streamType {hasFieldWidth;} [], |
---|
105 | integerConstant {intValued;} [fieldWidthSpecifier], |
---|
106 | step {intValued;} [], |
---|
107 | // primitiveType {} [intType voidType], // KH: update S2K2B2k for primitiveType |
---|
108 | |
---|
109 | } |
---|
110 | |
---|
111 | grammar { |
---|
112 | |
---|
113 | program -> ( structDef | filterDef ) * graphDef ? ; |
---|
114 | |
---|
115 | // |
---|
116 | // s t r e a m s t r u c t d e f i n i t i o n |
---|
117 | // |
---|
118 | structDef -> STRUCT identifier structDefBody TERMINATOR? ; |
---|
119 | structDefBody -> LCURLY (structMember TERMINATOR)+ RCURLY ; |
---|
120 | structMember -> structMemberType identifier ; |
---|
121 | structMemberType #-> streamType ; |
---|
122 | |
---|
123 | // KH: user-defined stream functions? |
---|
124 | // |
---|
125 | // s t r e a m f u n c t i o n d e f i n i t i o n |
---|
126 | // |
---|
127 | //funcDef -> FUNCTION funcReturnType identifier LROUND funcParameterList RROUND funcBody TERMINATOR? ; |
---|
128 | //funcReturnType #-> type; |
---|
129 | //funcParameterList -> ( funcParameter (COMMA funcParameter)* ) ? ; |
---|
130 | //funcParameter -> type identifier ; |
---|
131 | |
---|
132 | // |
---|
133 | // s t r e a m f i l t e r ( p r o c e d u r e ) d e f i n i t i o n |
---|
134 | // |
---|
135 | filterDef -> FILTER identifier LROUND filterParameterList RROUND blockStmt TERMINATOR? ; |
---|
136 | filterParameterList -> ( filterParameter (COMMA filterParameter)* ) ? ; |
---|
137 | filterParameter -> filterParameterMode? type identifier ; |
---|
138 | filterParameterMode -> (IN | OUT | INOUT) ; // KH: not implemented |
---|
139 | |
---|
140 | // |
---|
141 | // s t r e a m g r a p h ( p i p e l i n e ) d e f i n i t i o n |
---|
142 | // |
---|
143 | graphDef -> GRAPH identifier LROUND graphParameterList RROUND graphBody TERMINATOR ? ; |
---|
144 | |
---|
145 | graphParameterList -> ( graphParameter (COMMA graphParameter)* ) ? ; // KH: implement as a 'modal' parameter |
---|
146 | graphParameter -> graphParameterMode? type identifier ; |
---|
147 | graphParameterMode -> (IN | OUT | INOUT) ; |
---|
148 | |
---|
149 | graphBody -> LCURLY graphVarDecls addFilterStmts RCURLY ; |
---|
150 | |
---|
151 | graphVarDecls -> ( structDecl | filterDecl ) * ; |
---|
152 | structDecl -> structType identifier TERMINATOR ; |
---|
153 | filterDecl -> filterType identifier TERMINATOR ; |
---|
154 | |
---|
155 | // graphDecl -> GRAPH identifier TERMINATOR ; |
---|
156 | |
---|
157 | addFilterStmts -> addFilterStmt * ; |
---|
158 | addFilterStmt -> identifier LROUND addFilterArgList RROUND TERMINATOR ; |
---|
159 | addFilterArgList -> (identifier (COMMA identifier) *) ? ; |
---|
160 | |
---|
161 | // |
---|
162 | // s t a t e m e n t s |
---|
163 | // |
---|
164 | stmt #-> varDecl | funcCallOrAssignStmt | returnStmt | ifStmt | whileStmt | foreachStmt | forStmt ; |
---|
165 | blockStmt -> LCURLY stmt* RCURLY ; |
---|
166 | varDecl -> type identifier (ASSIGN^ expr) ? TERMINATOR ; |
---|
167 | funcCallOrAssignStmt #-> expr (assignRest _promote_) ? TERMINATOR ; // KH: SA must validate 'expr' is a 'funcCall' |
---|
168 | assignRest -> assignOperator^! expr ; |
---|
169 | assignOperator -> ASSIGN | AND_ASSIGN | OR_ASSIGN | XOR_ASSIGN | |
---|
170 | MULTIPLY_ASSIGN | DIVIDE_ASSIGN | PLUS_ASSIGN | MINUS_ASSIGN ; |
---|
171 | returnStmt -> RETURN (expr)? TERMINATOR ; |
---|
172 | ifStmt -> IF expr blockStmt (ELSE blockStmt)? ; |
---|
173 | whileStmt -> WHILE expr blockStmt ; |
---|
174 | |
---|
175 | // KH: eliminate stream<1> type ? |
---|
176 | foreachStmt -> FOREACH indexStreamDeclList IN targetStreamDeclList blockStmt ; |
---|
177 | indexStreamDeclList #-> streamDeclList ; |
---|
178 | targetStreamDeclList #-> streamDeclList ; |
---|
179 | streamDeclList -> streamDecl ( COMMA streamDecl ) * ; |
---|
180 | streamDecl -> streamType identifier ; |
---|
181 | |
---|
182 | // KH: count controlled for loop to support reductions |
---|
183 | forStmt -> FOR identifier IN stridedRange blockStmt ; |
---|
184 | //indexTypeDecl -> indexType identifier ; // ? |
---|
185 | stridedRange -> range step ; |
---|
186 | range -> integerConstant DDOT integerConstant ; |
---|
187 | step #-> (BY integerConstant) | epsilon {@value = 1;} ; |
---|
188 | |
---|
189 | //annotationStmt -> AMPERSAND identifier LROUND annotationArgList RROUND blockStmt TERMINATOR ; |
---|
190 | //annotationArgList -> (expr (COMMA expr)*) ? |
---|
191 | |
---|
192 | // |
---|
193 | // e x p r e s s i o n s |
---|
194 | // |
---|
195 | |
---|
196 | // follows C++ operator precedence, DNE IN operator since list types DNE |
---|
197 | |
---|
198 | // expr #-> expr1 ((BY^) expr1)? _leftAssoc_ ; |
---|
199 | // expr1 #-> expr2 ((RANGE^) expr2)? _leftAssoc_ ; |
---|
200 | expr #-> expr1 ((OR^) expr)? _leftAssoc_ ; |
---|
201 | expr1 #-> expr2 ((XOR^) expr1)? _leftAssoc_ ; |
---|
202 | expr2 #-> expr3 ((AND^) expr2)? _leftAssoc_ ; |
---|
203 | expr3 #-> expr4 ((PLUS^ | MINUS^) expr3)? _leftAssoc_ ; |
---|
204 | expr4 #-> expr5 ((MULTIPLY^ | DIVIDE^) expr4)? _leftAssoc_ ; |
---|
205 | expr5 #-> ((PLUS^^ | MINUS^^)? expr6) | ((NOT^^)* expr6) ; |
---|
206 | expr6 #-> compoundIdentifier ((funcCallRest _promote_) | (idisaFuncCallRest _promote_)) ? |
---|
207 | | LROUND expr RROUND |
---|
208 | | constant ; |
---|
209 | |
---|
210 | constant #-> stringConstant | integerConstant ; |
---|
211 | integerConstant -> INTEGER_CONST {@value = @@value;} ; |
---|
212 | stringConstant -> STRING | SQ_STRING ; |
---|
213 | compoundIdentifier #-> identifier (DOT^ identifier)?; |
---|
214 | identifier -> IDENTIFIER ; |
---|
215 | funcCallRest -> LROUND^ funcCallArgList RROUND ; |
---|
216 | funcCallArgList -> (expr (COMMA expr)*) ? ; |
---|
217 | idisaFuncCallRest -> LANGLE^ fieldWidth RANGLE LROUND idisaFuncCallArgList RROUND ; |
---|
218 | idisaFuncCallArgList -> (expr (COMMA expr)*) ? ; |
---|
219 | fieldWidth -> integerConstant ; |
---|
220 | |
---|
221 | // |
---|
222 | // t y p e s |
---|
223 | // |
---|
224 | type #-> primitiveType | |
---|
225 | streamType | |
---|
226 | structType | |
---|
227 | filterType ; |
---|
228 | |
---|
229 | intType -> INT { @type = {{PrimitiveType.INTEGER}}; } ; |
---|
230 | primitiveType #-> intType ; |
---|
231 | indexType #-> intType ; |
---|
232 | |
---|
233 | streamType -> STREAM fieldWidthSpecifier! { @fieldWidth = @:value; |
---|
234 | @type = {{ StreamType.STREAM(%?) }} , @fieldWidth ; |
---|
235 | } ; |
---|
236 | fieldWidthSpecifier #-> (LANGLE integerConstant RANGLE ) | (epsilon {@value = 1;}) ; |
---|
237 | // fieldWidthSpecifier #-> (LANGLE expr RANGLE ) | (epsilon {@value = 1;}) ; |
---|
238 | |
---|
239 | structType -> STRUCT identifier ; |
---|
240 | filterType -> FILTER identifier ; |
---|
241 | } |
---|