CNNArch.mc4 3.53 KB
Newer Older
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
1
package de.monticore.lang.monticar;
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
2

Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
3
grammar CNNArch extends de.monticore.lang.math.Math {
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
4

Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
5
    CNNArchCompilationUnit = Architecture;
6

Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
7
    symbol scope Architecture = "architecture"
8 9
                                name:Name&
                                ( "(" (ArchitectureParameter || ",")* ")" )? "{"
10
                                declarations:ArchDeclaration*
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
11
                                body:ArchBody "}";
12

Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
13
    interface ArchDeclaration;
14
    interface ArchitectureElement;
15 16
    interface Variable;
    ast Variable = method String getName(){};
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
17

18 19 20 21 22
    IODeclaration implements ArchDeclaration = "def"
                                               (in:"input" | out:"output")
                                               type:ArchType
                                               Name&
                                               (ArrayDeclaration)?;
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
23

24 25
    ArchType implements Type = ElementType "^" Shape;

26 27 28 29 30
    Shape = "{" dimensions:(DimensionArgument || ",")* "}";

    DimensionArgument = (name:"H" ":" height:ArchSimpleExpression
                      |  name:"W" ":" width:ArchSimpleExpression
                      |  name:"C" ":" channels:ArchSimpleExpression);
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
31

32
    ArchitectureParameter implements Variable = Name& ("=" default:ArchSimpleExpression)?;
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
33

34 35
    MethodDeclaration implements ArchDeclaration = "def"
                                                   Name& "("
36
                                                   parameters:(MethodParameter || ",")* ")" "{"
37
                                                   body:ArchBody "}";
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
38

39
    MethodParameter implements Variable = Name& ("=" default:ArchSimpleExpression)?;
40

41
    scope ArchBody = elements:(ArchitectureElement || "->")*;
42

43
    IOLayer implements ArchitectureElement = Name& ("[" index:ArchSimpleExpression "]")?;
44

45 46 47 48 49
    MethodLayer implements ArchitectureElement = Name& "(" arguments:(ArchArgument || ",")* ")";

    interface ArchArgument;
    ast ArchArgument = method String getName(){}
                       method ASTArchExpression getRhs(){};
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
50

51
    ArchParameterArgument implements ArchArgument = Name "=" rhs:ArchExpression;
52

53 54
    ArchSpecialArgument implements ArchArgument = (serial:"->" | parallel:"|" | conditional:"?") "="
                                                  rhs:ArchExpression;
55
    ast ArchSpecialArgument = method public String getName(){return "";};
56

57
    ParallelLayer implements ArchitectureElement = "(" groups:ArchBody "|" groups:(ArchBody || "|")+ ")";
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
58

59
    ArrayAccessLayer implements ArchitectureElement = "[" index:ArchSimpleExpression "]";
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
60

61

62
    ArchExpression = (expression:ArchSimpleExpression | sequence:ArchValueSequence);
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
63

64 65 66
    interface ArchValueSequence;

    ArchParallelSequence implements ArchValueSequence = "[" parallelValues:(ArchSerialSequence || "|")* "]";
67 68 69

    ArchSerialSequence = serialValues:(ArchSimpleExpression || "->")+;

70
    ArchValueRange implements ArchValueSequence = start:ArchSimpleExpression
71
                                                  (serial:"->" | parallel:"|")
72 73 74
                                                  ".."
                                                  (serial2:"->" | parallel2:"|")
                                                  end:ArchSimpleExpression;
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
75 76


77 78
    ArchSimpleExpression = (arithmeticExpression:MathArithmeticExpression
                         | booleanExpression:MathBooleanExpression
79 80
                         | tupleExpression:TupleExpression
                         | string:StringLiteral);
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
81

82
    TupleExpression implements MathExpression = "(" expressions:MathExpression "," expressions:(MathExpression || ",")* ")";
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
83

Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
84
}