LabCommParser.parser 4.15 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
%header {:
 package AST;
:};
%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);
        //throw new RuntimeException(token.getLine(token.getStart()) + ", " + 
	// 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
        }
:};

Program goal =
     /* Empty program */               {: return new Program(); :}
  |  decl_list.l                       {: return new Program(l); :}
  ;

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); :}
  ;

Field var_decl =
    type.t IDENTIFIER SEMICOLON     {: return new Field(t, IDENTIFIER); :}
  | type.t IDENTIFIER dim_list.d SEMICOLON 
    {: return new Field(new ParseArrayType(t, d), IDENTIFIER); :}
  ;

TypeDecl type_decl = 
    TYPEDEF type.t IDENTIFIER SEMICOLON {: return new TypeDecl(t, IDENTIFIER); :}
  | TYPEDEF type.t IDENTIFIER dim_list.d SEMICOLON 
    {: return new TypeDecl(new ParseArrayType(t, d), IDENTIFIER); :}
  ;

SampleDecl sample_decl = 
    SAMPLE type.t IDENTIFIER SEMICOLON 
      {: return new SampleDecl(t, IDENTIFIER); :}
  | SAMPLE type.t IDENTIFIER dim_list.d SEMICOLON 
      {: return new SampleDecl(new ParseArrayType(t, d), IDENTIFIER); :}
  | SAMPLE VOID IDENTIFIER SEMICOLON 
      {: return new SampleDecl(new VoidType(), IDENTIFIER); :}
  ;

Type type =
    prim_type.p                     {: return p; :}
  | user_type.u                     {: return u; :}
  | struct_type.s                   {: return s; :}
  ;

PrimType prim_type =      
    BOOLEAN                         
      {: return new PrimType(BOOLEAN, ASTNode.LABCOMM_BOOLEAN); :}
  | BYTE                            
      {: return new PrimType(BYTE, ASTNode.LABCOMM_BYTE); :}
  | SHORT                           
      {: return new PrimType(SHORT, ASTNode.LABCOMM_SHORT); :}
  | INT                             
      {: return new PrimType(INT, ASTNode.LABCOMM_INT); :}
  | LONG                            
      {: return new PrimType(LONG, ASTNode.LABCOMM_LONG); :}
  | FLOAT                           
      {: return new PrimType(FLOAT, ASTNode.LABCOMM_FLOAT); :}
  | DOUBLE                          
      {: return new PrimType(DOUBLE, ASTNode.LABCOMM_DOUBLE); :}
  | STRING                          
      {: return new PrimType(STRING, ASTNode.LABCOMM_STRING); :}
  ;

UserType user_type =      
    IDENTIFIER                      {: return new UserType(IDENTIFIER); :}
  ;

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

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); :}
  ;

Exp exp = 
    INTEGER_LITERAL                 {: return new IntegerLiteral(INTEGER_LITERAL); :}
  | UNDERSCORE                      {: return new VariableSize(); :}
  ;