-
Notifications
You must be signed in to change notification settings - Fork 165
/
cpp.tex
173 lines (169 loc) · 4.23 KB
/
cpp.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
160
161
162
163
164
165
166
167
168
169
170
171
172
\begin{figure}
\begin{boxedminipage}[t]{\textwidth}
\begin{minipage}[l]{0.5\textwidth}
\textbf{A. Abstract Syntax Interface}
\scriptsize
\begin{verbatim}
typedef int Integer;
typedef char Char;
typedef double Double;
typedef char* String;
typedef char* Ident;
...
class PROG : public Visitable {
public:
ListEXP *listexp_;
PROG(ListEXP *p1);
~PROG();
virtual void accept(Visitor *v);
};
class EXP : public Visitable { };
class EOr : public EXP {
public:
EXP *exp_1, *exp_2;
EOr(EXP *p1, EXP *p2);
~EOr();
virtual void accept(Visitor *v);
};
...
class EFalse : public EXP {
public:
EFalse();
~EFalse();
virtual void accept(Visitor *v);
};
class EVar : public EXP {
public:
Ident ident_;
EVar(Ident p1);
~EVar();
virtual void accept(Visitor *v);
};
class ListEXP : public Visitable {
public:
EXP *exp_;
ListEXP *listexp_;
ListEXP(EXP *p1, ListEXP *p2);
ListEXP(EXP *p);
~ListEXP();
virtual void accept(Visitor *v);
};
\end{verbatim}
\normalsize
\textbf{B. Abstract Syntax Implementation}
\scriptsize
\begin{verbatim}
PROG::PROG(ListEXP *p1) { listexp_ = p1; }
PROG::~PROG() { delete(listexp_); }
void PROG::accept(Visitor *v) { v->visitPROG(this); }
EOr::EOr(EXP *p1, EXP *p2) { exp_1 = p1; exp_2 = p2; }
EOr::~EOr() { delete(exp_1); delete(exp_2); }
void EOr::accept(Visitor *v) { v->visitEOr(this); }
...
\end{verbatim}
\normalsize
\textbf{C. Flex Lexer}
\scriptsize
\begin{verbatim}
<YYINITIAL>"||" return _SYMB_0;
<YYINITIAL>"&&" return _SYMB_1;
<YYINITIAL>";" return _SYMB_2;
<YYINITIAL>"(" return _SYMB_3;
<YYINITIAL>")" return _SYMB_4;
<YYINITIAL>"false" return _SYMB_5;
<YYINITIAL>"true" return _SYMB_6;
<YYINITIAL>{LETTER}{IDENT}*
yylval.string_ = strdup(yytext); return _IDENT_;
<YYINITIAL>[ \t\r\n\f] /* ignore white space. */;
<YYINITIAL>. return _ERROR_;
\end{verbatim}
\normalsize
\end{minipage}
\hfill
\begin{minipage}[r]{0.5\textwidth}
\textbf{D. Bison Parser}
\scriptsize
\begin{verbatim}
PROG* pPROG(FILE *inp) {
initialize_lexer(inp);
if (yyparse()) /* Failure */
return 0;
else /* Success */
return YY_RESULT_PROG_;
}
...
%token _ERROR_ // Original terminal:
%token _SYMB_0 // ||
%token _SYMB_1 // &&
%token _SYMB_2 // ;
%token _SYMB_3 // (
%token _SYMB_4 // )
%token _SYMB_5 // false
%token _SYMB_6 // true
...
PROG : ListEXP { $$ = new PROG(reverseListEXP($1));
YY_RESULT_PROG_= $$; }
;
EXP : EXP _SYMB_0 EXP1 { $$ = new EOr($1, $3);
YY_RESULT_EXP_= $$; }
| EXP1 { $$ = $1; YY_RESULT_EXP_= $$; }
;
EXP1 : EXP1 _SYMB_1 EXP2 { $$ = new EAnd($1, $3);
YY_RESULT_EXP_= $$; }
| EXP2 { $$ = $1; YY_RESULT_EXP_= $$; }
;
EXP2 : _SYMB_6 { $$ = new ETrue(); YY_RESULT_EXP_= $$; }
| _SYMB_5 { $$ = new EFalse(); YY_RESULT_EXP_= $$; }
| _IDENT_ { $$ = new EVar($1); YY_RESULT_EXP_= $$; }
| _SYMB_3 EXP _SYMB_4 { $$ = $2; YY_RESULT_EXP_= $$; }
;
ListEXP : /* empty */ { $$ = 0; YY_RESULT_ListEXP_= $$; }
| ListEXP EXP _SYMB_2 { $$ = new ListEXP($2, $1);
YY_RESULT_ListEXP_= $$; }
;
\end{verbatim}
\normalsize
\textbf{E. Pretty Printer}
\scriptsize
\begin{verbatim}
void PrintAbsyn::visitEOr(EOr* p) {
int oldi = _i_;
if (oldi > 0) render(_L_PAREN);
_i_ = 0; p->exp_1->accept(this);
render("||");
_i_ = 1; p->exp_2->accept(this);
if (oldi > 0) render(_R_PAREN);
_i_ = oldi;
}
void PrintAbsyn::visitEAnd(EAnd* p) {
int oldi = _i_;
if (oldi > 1) render(_L_PAREN);
_i_ = 1; p->exp_1->accept(this);
render("&&");
...
\end{verbatim}
\normalsize
\textbf{F. Word Counts}
\scriptsize
\begin{verbatim}
Lines Words Bytes
133 287 2206 Absyn.H
77 217 1869 Absyn.C
47 145 1314 BoolExp.l
126 347 2247 BoolExp.y
43 117 973 Makefile
35 69 536 Parser.H
176 460 3565 Printer.H
334 592 5200 Printer.C
26 65 571 Skeleton.H
75 166 1376 Skeleton.C
40 123 1218 Test.C
1112 2588 21075 total
\end{verbatim}
\normalsize
\hfill
\end{minipage}
\end{boxedminipage}
\caption{C++ source code fragments generated from Figure \ref{fig:source}}
\label{fig:cpp}
\end{figure}