-
Notifications
You must be signed in to change notification settings - Fork 165
/
java.tex
159 lines (156 loc) · 3.7 KB
/
java.tex
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
\begin{figure}
\begin{boxedminipage}[t]{\textwidth}
\begin{minipage}[l]{0.5\textwidth}
\normalsize
\textbf{A. Abstract Syntax}
\scriptsize
\begin{verbatim}
public class PROGRAM {
public ListEXP listexp_;
public PROGRAM(ListEXP p1)
{ listexp_ = p1; }
}
public abstract class EXP {}
public class EAnd extends EXP {
public EXP exp_1, exp_2;
public EAnd(EXP p1, EXP p2)
{ exp_1 = p1; exp_2 = p2; }
}
public class EOr extends EXP {
public EXP exp_1, exp_2;
public EOr(EXP p1, EXP p2)
{ exp_1 = p1; exp_2 = p2; }
}
public class ETrue extends EXP {
public ETrue() { }
}
public class EFalse extends EXP {
public EFalse() { }
}
public class EVar extends EXP {
public String ident_;
public EVar(String p1)
{ ident_ = p1; }
}
public class ListEXP {
public EXP exp_;
public ListEXP listexp_;
public ListEXP(EXP p1, ListEXP p2)
{ exp_ = p1; listexp_ = p2; }
}
\end{verbatim}
\normalsize
\textbf{B. CUP Parser}
\scriptsize
\begin{verbatim}
terminal _SYMB_0; // ||
terminal _SYMB_1; // &&
terminal _SYMB_2; // ;
terminal _SYMB_3; // (
terminal _SYMB_4; // )
terminal _SYMB_5; // false
terminal _SYMB_6; // true
terminal String _IDENT_;
PROGRAM ::= ListEXP:p_1 {:
if (p_1 != null) p_1 = p_1.reverse();
RESULT = new Absyn.PROGRAM(p_1); :}
;
EXP ::= EXP:p_1 _SYMB_0 EXP1:p_3 {:
RESULT = new Absyn.EOr(p_1, p_3); :}
| EXP1:p_1 {: RESULT = (p_1); :}
;
EXP1 ::= EXP1:p_1 _SYMB_1 EXP2:p_3 {:
RESULT = new Absyn.EAnd(p_1, p_3); :}
| EXP2:p_1 {: RESULT = (p_1); :}
;
EXP2 ::= _SYMB_6 {:
RESULT = new Absyn.ETrue(); :}
| _SYMB_5 {:
RESULT = new Absyn.EFalse(); :}
| _IDENT_:p_1 {:
RESULT = new Absyn.EVar(p_1); :}
\end{verbatim}
\normalsize
\end{minipage}
\hfill
\begin{minipage}[r]{0.5\textwidth}
\normalsize
\textbf{CUP Parser (continued)}
\scriptsize
\begin{verbatim}
| _SYMB_3 EXP:p_2 _SYMB_4 {:
RESULT = (p_2); :}
;
ListEXP ::= /*empty*/{: RESULT = null; :}
| ListEXP:p_1 EXP:p_2 _SYMB_2 {:
RESULT = new Absyn.ListEXP(p_2, p_1); :}
;
\end{verbatim}
\normalsize
\textbf{C. Pretty Printer}
\scriptsize
\begin{verbatim}
private static void
pp(Absyn.EXP exp, int _i_) {
if (exp instanceof Absyn.EOr) {
Absyn.EOr eor = (Absyn.EOr) exp;
if (_i_ > 0) render(_L_PAREN);
pp(eor.exp_1, 0);
render("||");
pp(eor.exp_2, 1);
if (_i_ > 0) render(_R_PAREN);
}
if (exp instanceof Absyn.EAnd) {
Absyn.EAnd eand = (Absyn.EAnd) exp;
if (_i_ > 1) render(_L_PAREN);
pp(eand.exp_1, 1);
render("&&");
...
\end{verbatim}
\normalsize
\textbf{D. Abstract Syntax Viewer}
\scriptsize
\begin{verbatim}
private static void sh(Absyn.EXP exp)
{
if (exp instanceof Absyn.EOr) {
Absyn.EOr eor = (Absyn.EOr) exp;
render("(");
render("EOr");
sh(eor.exp_1);
sh(eor.exp_2);
render(")");
}
if (exp instanceof Absyn.EAnd) {
Absyn.EAnd eand = (Absyn.EAnd) exp;
render("(");
render("EAnd");
...
\end{verbatim}
\normalsize
\textbf{E. Visitor Design Pattern}
\scriptsize
\begin{verbatim}
public void visitEOr(Absyn.EOr eor) {
/* Code For EOr Goes Here */
eor.exp_1.accept(this);
eor.exp_2.accept(this);
}
public void visitEAnd(Absyn.EAnd eand) {
/* Code For EAnd Goes Here */
...
public void
visitListEXP(Absyn.ListEXP listexp) {
while(listexp!= null) {
/* Code For ListEXP Goes Here */
listexp.listexp_.accept(this);
listexp = listexp.listexp_;
...
\end{verbatim}
\normalsize
\hfill
\end{minipage}
\end{boxedminipage}
\caption{Java source code fragments generated from Figure \ref{fig:source}}
\label{fig:java}
\end{figure}