CNNArch.mc4 7.55 KB
Newer Older
Bernhard Rumpe's avatar
BR    
Bernhard Rumpe committed
1
/* (c) https://github.com/MontiCore/monticore */
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
2
package de.monticore.lang.monticar;
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
3

4
grammar CNNArch extends de.monticore.CommonExpressions, de.monticore.lang.Math, de.monticore.lang.monticar.Common2 {
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
5

6
7
8
9
10
11
12
13
14
15
16

    /* =================================*/
    /* ========== PRODUCTIONS ==========*/
    /* =================================*/

    /* ========== Declarations =========*/

    /**
        The complete file.
        Use nonterminal Architecture for embedding in another language (e.g. EmbeddedMontiArc)
    */
17
    symbol scope CNNArchCompilationUnit = "architecture"
18
                                          name:Name
19
20
21
22
                                          ( "(" (ArchitectureParameter || ",")* ")" )? "{"
                                          ioDeclarations:IODeclaration*
                                          Architecture
                                          "}";
23

24
    LayerDeclaration =  "def"
25
                       Name "("
26
                       parameters:(LayerParameter || ",")* ")" "{"
27
                       body:Stream  "}";
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
28

29
    IODeclaration =  "def"
30
31
                    (in:"input" | out:"output")
                    type:ArchType
32
                    Name
33
34
                    (ArrayDeclaration)?;

Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
35

36
37
38
39
40
41
    /* ============== Type =============*/

    /**
         Similar to EmbeddedMontiArc port types.
         ArchType and Shape are not used if the Architecture is integrated into EmbeddedMontiArc
    */
42
    ArchType = ElementType "^" Shape;
43

44
    Shape = "{" dimensions:(ArchSimpleExpression || ",")* "}";
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
45
46


47
    /* ========= Architecture  =========*/
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
48

49
50
51
52
    /**
        Defines the architecture of the neural network.
        This NT is used for integration in EmbeddedMontiArc.
        @attribute methodDeclaration*
53
                 A list of new layers which can be used in the architecture.
54
55
56
        @attribute body
                 The architecture of the neural network.
    */
Christian Fuß's avatar
Christian Fuß committed
57
    Architecture = methodDeclaration:LayerDeclaration*
58
                   instructions:(Instruction || ";")+ ";";
59

60
    Instruction = (LayerVariableDeclaration | NetworkInstruction);
61

62
    LayerVariableDeclaration = "layer" Layer Name;
63

64
65
66
    interface NetworkInstruction;

    StreamInstruction implements NetworkInstruction = body:Stream;
67

68
    UnrollInstruction implements NetworkInstruction = "timed" "<" timeParameter:TimeParameter ">"
69
70
71
                                     Name "(" arguments:(ArchArgument || ",")* ")"
                                     "{" body:Stream "}";

72
73
    Stream = elements:(ArchitectureElement || "->")+;

74
75
    interface ArchitectureElement;

76
    Variable implements ArchitectureElement =  Name ("." (member:"output" | member:Name))? ("[" index:ArchSimpleExpression "]")?;
77
78

    Constant implements ArchitectureElement = ArchSimpleExpression;
79

80
    Layer implements ArchitectureElement =  Name "(" arguments:(ArchArgument || ",")* ")";
81

82

83
    ParallelBlock implements ArchitectureElement =  "("
84
85
                                                   groups:Stream  "|"
                                                   groups:(Stream || "|")+ ")";
86

87
    ArrayAccessLayer implements ArchitectureElement =  "[" index:ArchSimpleExpression "]";
88
89
90
91


    /* ====== Variables/Arguments ======*/

92
    interface ArchParameter;
93

94
    ArchitectureParameter implements ArchParameter =  Name ("=" default:ArchSimpleExpression)? ;
95

96
    LayerParameter implements ArchParameter =  Name ("=" default:ArchSimpleExpression)? ;
97

98
99
    TimeParameter implements ArchParameter =  Name ("=" default:ArchSimpleExpression)? ;

100
    interface ArchArgument;
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
101

102
    ArchParameterArgument implements ArchArgument =  Name "=" rhs:ArchExpression ;
103

104
105
    ArchSpecialArgument implements ArchArgument = (serial:"->" | parallel:"|" | conditional:"?") "="
                                                  rhs:ArchExpression ;
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
106
107


108
    /* ======= Value Expressions =======*/
109

110
111
112
    /**
        Expression used for method arguments.
    */
113
    ArchExpression = (expression:ArchSimpleExpression | sequence:ArchValueSequence);
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
114

115
    interface ArchValueSequence;
116

117
    ArchParallelSequence implements ArchValueSequence = "[" parallelValues:(ArchSerialSequence || "|")+ "]";
118

119
    ArchSerialSequence = serialValues:(ArchSimpleExpression || "->")*;
120

121
    ArchValueRange implements ArchValueSequence = "[" start:ArchSimpleExpression
122
                                                  (serial:"->" | parallel:"|")
123
124
                                                  ".."
                                                  (serial2:"->" | parallel2:"|")
125
                                                  end:ArchSimpleExpression "]";
126
    /**
127
        Expressions for parameter and variable values.
128
    */
129
130
    ArchSimpleExpression = (arithmeticExpression:ArchArithmeticExpression
                         | booleanExpression:ArchBooleanExpression
131
132
                         | tupleExpression:TupleExpression
                         | string:StringLiteral);
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
133

134
135
136
137
138
139
140
141
    interface ArchMathExpression extends Expression;
    interface ArchArithmeticExpression extends ArchMathExpression;
    interface ArchBooleanExpression extends ArchMathExpression;

    ArchSimpleArithmeticExpression implements ArchArithmeticExpression = (NumberExpression
                                             | NameExpression
                                             | MathDottedNameExpression
                                             | MathAssignmentDeclarationStatement
142
                                             | MathAssignmentStatement);
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

    ArchComplexArithmeticExpression implements ArchArithmeticExpression = leftExpression:ArchMathExpression
                                               (   operator:"*"
                                               |   operator:"/"
                                               |   operator:"%"
                                               |   operator:"^"
                                               |   operator:"+"
                                               |   operator:"-"
                                               )
                                               rightExpression:ArchMathExpression;

    TupleExpression = "(" expressions:ArchArithmeticExpression "," expressions:(ArchArithmeticExpression || ",")* ")";

    ArchSimpleBooleanExpression implements ArchBooleanExpression = (BooleanExpression
                         | BooleanNotExpression
                         | LogicalNotExpression);

    ArchComplexBooleanExpression implements ArchBooleanExpression = leftExpression:ArchMathExpression
                                                                         (   operator:"=="
                                                                         |   operator:"!="
                                                                         |   operator:"&&"
                                                                         |   operator:"||"
                                                                         )
                                                                         rightExpression:ArchMathExpression;

    ArchBracketExpression implements ArchMathExpression, ArchBooleanExpression, ArchArithmeticExpression = "(" ArchMathExpression ")";
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
169

170
171
    ArchPreMinusExpression implements ArchMathExpression, ArchBooleanExpression, ArchArithmeticExpression = "-" ArchMathExpression ;

172
173
174
175

    /* =================================*/
    /* ============ ASTRULES ===========*/
    /* =================================*/
176
    ast ArchParameter = method String getName(){};
177
178
179
180
    ast ArchSpecialArgument = method public String getName(){return "";}; //Override is necessary
    ast ArchArgument = method String getName(){}
                       method ASTArchExpression getRhs(){};

181
}