summaryrefslogtreecommitdiff
path: root/old/cocoo0c/Oberon2.atg
blob: a8097ac5b900872fe2fd48ee8c621d30f936ab34 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import java.util.HashSet;

COMPILER Oberon2

/*
Source: http://www.zel.org/aos/o2report.htm

The Programming Language Oberon-2

H. Mössenböck, N. Wirth, Institut für Computersysteme, ETH Zürich, 1992-1996

Changes:
 - Enclosed all keywords with double quotes
 - Renamed start symbol "Module" to Oberon2
 - Added production ImportListMember
   Please note that the optional part of the production is different from 
   the original. This solves a LL(1) conflict, but introduces a slightly
   different meaning: in the original grammar, the emphasis is on the
   imported module name, which might be referenced via an alias name.
   This definition emphasis the alias name, which now may be backed up
   by a module with a different name....
 - Moved the token "PROCEDURE" out of the ProcDecl and ForwardDecl
   productions into the DeclSeq production to solve a LL(1) conflict.
 - Added a new production for non terminal number
 - Added two conflict resolvers
*/

public static void main(String[] args) {
    Scanner scanner = new Scanner(args[0]);
    Parser parser = new Parser(scanner);
    parser.Parse();
    System.out.println("Done.");
}

private HashSet<String> modules = new HashSet<String>();

/* Returns true if the next ident is a module name */
private boolean isModule() {
	if (la.kind == _ident) {
	  return modules.contains(la.val);
	}
	return false;
}

/* Returns true if the current look ahead symbol is
   part of the current scanned designator. */
private boolean isDesignatorPart() {
  switch (la.kind) {
    case _dot:
    case _lbrack:
    case _arrow:
  		return true;
    case _lpar:
      /* Semantic analysis is missing! */
      /* Assumes for now a procedure invocation. */
      return false;
    default:
      return false;
  }
}

CHARACTERS
  letter    = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
  digit     = "0123456789".
  hexDigit	= "0123456789ABCDEF".
  cr        = '\r'.
  lf        = '\n'.
  tab       = '\t'.
  stringCh  = ANY - "'" - '"' - cr - lf.

TOKENS
  ident     = letter { letter | digit }.
  integer   = digit { digit }
              | digit { digit } CONTEXT ("..")
              | digit { hexDigit } "H".
  real      = digit { digit } "." { digit } [("E" | "D") ["+" | "-"] digit {digit}].
  string    = '"' { stringCh | "'" } '"' | "'" { stringCh | '"' } "'".
  character = digit { hexDigit } "X".
  
  dot       = ".".
  lbrack    = "[".
  arrow     = "^". 
  lpar      = "(".

COMMENTS FROM "(*" TO "*)" NESTED

IGNORE cr + lf + tab

PRODUCTIONS

Oberon2
  = "MODULE" ident ";" [ImportList] DeclSeq ["BEGIN" StatementSeq] "END" ident ".".

ImportList
  = "IMPORT" ImportListMember {"," ImportListMember } ";".

ImportListMember
  = ident (. modules.add(t.val); .) [":=" ident ].

DeclSeq
  = { "CONST" {ConstDecl ";" } | "TYPE" {TypeDecl ";"} | "VAR" {VarDecl ";"}} 
    { "PROCEDURE" (ProcDecl | ForwardDecl) ";"}.

ConstDecl
  = IdentDef "=" ConstExpr.

TypeDecl
  = IdentDef "=" Type.

VarDecl
  = IdentList ":" Type.

ProcDecl
  = [Receiver] IdentDef [FormalPars] ";" DeclSeq ["BEGIN" StatementSeq] "END" ident.

ForwardDecl
  = "^" [Receiver] IdentDef [FormalPars].

FormalPars
  = "(" [FPSection {";" FPSection}] ")" [":" Qualident].

FPSection
  = ["VAR"] ident {"," ident} ":" Type.

Receiver
  = "(" ["VAR"] ident ":" ident ")".

Type
  = Qualident
  | "ARRAY" [ConstExpr {"," ConstExpr}] "OF" Type 
  | "RECORD" ["("Qualident")"] FieldList {";" FieldList} "END"
  | "POINTER" "TO" Type
  | "PROCEDURE" [FormalPars]
  .

FieldList
  = [IdentList ":" Type].

StatementSeq
  = Statement {";" Statement}.

Statement
  = [ Designator (":=" Expr | ["(" [ExprList] ")"])
      | "IF" Expr "THEN" StatementSeq 
        {"ELSIF" Expr "THEN" StatementSeq} 
        ["ELSE" StatementSeq] 
        "END" 
      | "CASE" Expr "OF" Case 
        {"|" Case} 
        ["ELSE" StatementSeq] 
        "END" 
      | "WHILE" Expr "DO" StatementSeq "END" 
      | "REPEAT" StatementSeq "UNTIL" Expr 
      | "FOR" ident ":=" Expr "TO" Expr ["BY" ConstExpr] "DO" StatementSeq "END" 
      | "LOOP" StatementSeq "END"
      | "WITH" Guard "DO" StatementSeq 
        {"|" Guard "DO" StatementSeq} 
        ["ELSE" StatementSeq] 
        "END"
      | "EXIT" 
      | "RETURN" [Expr]
    ]
  .

Case
  = [CaseLabels {"," CaseLabels} ":" StatementSeq]. 

CaseLabels
  = ConstExpr [".." ConstExpr].

Guard
  = Qualident ":" Qualident.

ConstExpr
  = Expr.

Expr
  = SimpleExpr [Relation SimpleExpr].

SimpleExpr
  = ["+" | "-"] Term {AddOp Term}. 

Term
  = Factor {MulOp Factor}.
Factor
  = Designator ["(" [ExprList] ")"]
  | number 
  | character 
  | string 
  | "NIL"
  | Set
  | "(" Expr ")"
  | "~" Factor
  .

Set
  = "{" [Element {"," Element}] "}". 

Element
  = Expr [".." Expr].

Relation
  = "=" | "#" | "<" | "<=" | ">" | ">=" | "IN" | "IS".

AddOp
  = "+" | "-" | "OR".

MulOp
  = "*" | "/" | "DIV" | "MOD" | "&". 

Designator
  = Qualident { IF(isDesignatorPart()) ("." ident | "[" ExprList "]" | "^" | "(" Qualident ")") }.

ExprList
  = Expr {"," Expr}.

IdentList
  = IdentDef {"," IdentDef}.

Qualident
  = [IF(isModule()) ident "."] ident.

IdentDef
  = ident ["*" | "-"].

number
  = integer | real.

END Oberon2.