LabCommParser.parser 4.03 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1
%header {:
2
3
  package se.lth.control.labcomm2014.compiler;
  import se.lth.control.labcomm2014.compiler.*;
Anders Nilsson's avatar
Anders Nilsson committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
:};
%embed {:
  public static class SourceError extends Error {
    public SourceError(String msg) {
      super(msg);
    }
  }
  class Events extends Parser.Events {
    public void syntaxError(Symbol token) {
      StringBuffer s = new StringBuffer();
      s.append(token.getLine(token.getStart()) + ", " + token.getColumn(token.getStart()) + "\n");
      s.append("  *** Syntactic error: unexpected token " + Terminals.NAMES[token.getId()]);
      throw new SourceError(s.toString());
        //super.syntaxError(token);
18
        //throw new RuntimeException(token.getLine(token.getStart()) + ", " +
Anders Nilsson's avatar
Anders Nilsson committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
	// token.getColumn(token.getStart()) + ": Syntax Error");
    }
    public void scannerError(Scanner.Exception e) {
      StringBuffer s = new StringBuffer();
      s.append(e.line + ", " + e.column + "\n");
      s.append("  *** Lexical error: " + e.getMessage());
      throw new SourceError(s.toString());
        //super.scannerError(e);
        //throw new RuntimeException("Unexpected token");
    }

  }

        {
            report = new Events(); // Use error handler in parser
        }
:};

37
38
39
Specification goal =
     /* Empty program */               {: return new Specification(); :}
  |  decl_list.l                       {: return new Specification(l); :}
Anders Nilsson's avatar
Anders Nilsson committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  ;

List decl_list =
    decl.d                          {: return new List().add(d); :}
  | decl_list.l decl.d              {: return l.add(d); :}
  ;

Decl decl =
    type_decl.t                     {: return t; :}
  | sample_decl.s	            {: return s; :}
  ;

List var_decl_list =
    var_decl.v                      {: return new List().add(v); :}
  | var_decl_list.l var_decl.v      {: return l.add(v); :}
  ;

57
58
59
60
61
62
TypeInstance type_instance =
    type.t IDENTIFIER {: return new TypeInstance(t, IDENTIFIER); :}
  | type.t IDENTIFIER dim_list.d
    {: return new TypeInstance(new ParseArrayType(t, d), IDENTIFIER); :}
  ;

Anders Nilsson's avatar
Anders Nilsson committed
63
64
Field var_decl =
    type.t IDENTIFIER SEMICOLON     {: return new Field(t, IDENTIFIER); :}
65
  | type.t IDENTIFIER dim_list.d SEMICOLON
Anders Nilsson's avatar
Anders Nilsson committed
66
67
68
    {: return new Field(new ParseArrayType(t, d), IDENTIFIER); :}
  ;

69
TypeDecl type_decl =
70
    TYPEDEF type_instance.t  SEMICOLON {: return new TypeDecl(t); :} ;
Anders Nilsson's avatar
Anders Nilsson committed
71

72
SampleDecl sample_decl =
73
    SAMPLE type_instance.t SEMICOLON {: return new SampleDecl(t); :} ;
Anders Nilsson's avatar
Anders Nilsson committed
74

75
DataType type =
Anders Nilsson's avatar
Anders Nilsson committed
76
77
78
    prim_type.p                     {: return p; :}
  | user_type.u                     {: return u; :}
  | struct_type.s                   {: return s; :}
79
  | void_type.v                     {: return v; :}
Anders Nilsson's avatar
Anders Nilsson committed
80
81
  ;

82
83
PrimType prim_type =
    BOOLEAN
Anders Nilsson's avatar
Anders Nilsson committed
84
      {: return new PrimType(BOOLEAN, ASTNode.LABCOMM_BOOLEAN); :}
85
  | BYTE
Anders Nilsson's avatar
Anders Nilsson committed
86
      {: return new PrimType(BYTE, ASTNode.LABCOMM_BYTE); :}
87
  | SHORT
Anders Nilsson's avatar
Anders Nilsson committed
88
      {: return new PrimType(SHORT, ASTNode.LABCOMM_SHORT); :}
89
  | INT
Anders Nilsson's avatar
Anders Nilsson committed
90
      {: return new PrimType(INT, ASTNode.LABCOMM_INT); :}
91
  | LONG
Anders Nilsson's avatar
Anders Nilsson committed
92
      {: return new PrimType(LONG, ASTNode.LABCOMM_LONG); :}
93
  | FLOAT
Anders Nilsson's avatar
Anders Nilsson committed
94
      {: return new PrimType(FLOAT, ASTNode.LABCOMM_FLOAT); :}
95
  | DOUBLE
Anders Nilsson's avatar
Anders Nilsson committed
96
      {: return new PrimType(DOUBLE, ASTNode.LABCOMM_DOUBLE); :}
97
  | STRING
Anders Nilsson's avatar
Anders Nilsson committed
98
      {: return new PrimType(STRING, ASTNode.LABCOMM_STRING); :}
99
100
  | SAMPLE
      {: return new PrimType(SAMPLE, ASTNode.LABCOMM_SAMPLE); :}
Anders Nilsson's avatar
Anders Nilsson committed
101
102
  ;

103
UserType user_type =
Anders Nilsson's avatar
Anders Nilsson committed
104
105
106
107
108
109
110
    IDENTIFIER                      {: return new UserType(IDENTIFIER); :}
  ;

StructType struct_type =
    STRUCT LBRACE var_decl_list.l RBRACE {: return new StructType(l); :}
  ;

111
112
VoidType void_type =
    VOID {: return new VoidType(); :}
113
114
;

Anders Nilsson's avatar
Anders Nilsson committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
List dim_list =
    dim.d                           {: return new List().add(d); :}
  | dim_list.l  dim.d               {: return l.add(d); :}
  ;

Dim dim =
    LBRACK exp_list.e RBRACK        {: return new Dim(e); :}
  ;

List exp_list =
    exp.e                           {: return new List().add(e); :}
  | exp_list.l COMMA exp.e          {: return l.add(e); :}
  ;

129
Exp exp =
Anders Nilsson's avatar
Anders Nilsson committed
130
131
132
    INTEGER_LITERAL                 {: return new IntegerLiteral(INTEGER_LITERAL); :}
  | UNDERSCORE                      {: return new VariableSize(); :}
  ;