Skip to content

Commit 3a24da1

Browse files
committed
glr2.cc: add an example
Currently this example crashes on input such as "T (x) + y;". The same example with glr.c works properly. * examples/c++/glr/Makefile, examples/c++/glr/README.md, * examples/c++/glr/c++-types.test, examples/c++/glr/c++-types.yy, * examples/c++/glr/local.mk, examples/c++/local.mk: New. Based on examples/c/glr/c++-types.y.
1 parent 3add9ff commit 3a24da1

File tree

9 files changed

+410
-4
lines changed

9 files changed

+410
-4
lines changed

data/skeletons/glr2.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2092,7 +2092,7 @@ class glr_stack
20922092
{
20932093
state_set_index yynewStack = yystateStack.yysplitStack (yyk);
20942094
YY_DEBUG_STREAM << "Splitting off stack " << yynewStack.get()
2095-
<< " from " << yyk.get();
2095+
<< " from " << yyk.get() << '\n';
20962096
YYRESULTTAG yyflag =
20972097
yyglrReduce (yynewStack, *yyconflicts,
20982098
yyimmediate[*yyconflicts]);

examples/c++/calc++/local.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
## Parser generation. ##
1919
## ------------------- ##
2020

21-
%D%/parser.stamp: $(dependencies)
2221
SUFFIXES += .yy .stamp
2322
.yy.stamp:
2423
$(AM_V_YACC)rm -f $@
2524
$(AM_V_at)touch $@.tmp
2625
$(AM_V_at)$(YACCCOMPILE) -o $*.cc $<
2726
$(AM_V_at)mv -f $@.tmp $@
2827

28+
%D%/parser.stamp: $(dependencies)
2929
$(calcxx_sources_generated): %D%/parser.stamp
3030
@test -f $@ || rm -f %D%/parser.stamp
3131
@test -f $@ || $(MAKE) $(AM_MAKEFLAGS) %D%/parser.stamp

examples/c++/glr/Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This Makefile is designed to be simple and readable. It does not
2+
# aim at portability. It requires GNU Make.
3+
4+
BASE = c++-types
5+
BISON = bison
6+
XSLTPROC = xsltproc
7+
8+
all: $(BASE)
9+
10+
%.c %.h %.xml %.gv: %.y
11+
$(BISON) $(BISONFLAGS) --defines --xml --graph=$*.gv -o $*.c $<
12+
13+
$(BASE): $(BASE).o
14+
$(CC) $(CFLAGS) -o $@ $^
15+
16+
run: $(BASE)
17+
@echo "Type C++ declarations or expressions. Quit with ctrl-d."
18+
./$<
19+
20+
html: $(BASE).html
21+
%.html: %.xml
22+
$(XSLTPROC) $(XSLTPROCFLAGS) -o $@ $$($(BISON) --print-datadir)/xslt/xml2xhtml.xsl $<
23+
24+
CLEANFILES = \
25+
$(BASE) *.o $(BASE).[ch] $(BASE).output $(BASE).xml $(BASE).html $(BASE).gv
26+
27+
clean:
28+
rm -f $(CLEANFILES)

examples/c++/glr/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# glr
2+
3+
This example demonstrates the use of GLR parsers to handle (local)
4+
ambiguities in the C++ language. See the node "Merging GLR Parses" in
5+
Bison's documentation.
6+
7+
<!---
8+
Local Variables:
9+
fill-column: 76
10+
ispell-dictionary: "american"
11+
End:
12+
13+
Copyright (C) 2020 Free Software Foundation, Inc.
14+
15+
This file is part of Bison, the GNU Compiler Compiler.
16+
17+
Permission is granted to copy, distribute and/or modify this document
18+
under the terms of the GNU Free Documentation License, Version 1.3 or
19+
any later version published by the Free Software Foundation; with no
20+
Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
21+
Texts. A copy of the license is included in the "GNU Free
22+
Documentation License" file as part of this distribution.
23+
24+
--->

examples/c++/glr/c++-types.test

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#! /bin/sh
2+
3+
# Copyright (C) 2020 Free Software Foundation, Inc.
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
cat >input <<EOF
19+
z + q;
20+
21+
T x;
22+
23+
T x = y;
24+
25+
x = y;
26+
EOF
27+
run 0 "\
28+
1.0-4: +(z, q)
29+
3.0-2: <declare>(T, x)
30+
5.0-6: <init-declare>(T, x, y)
31+
7.0-4: =(x, y)"
32+
33+
exit 77
34+
35+
cat >input <<EOF
36+
T (x) + y;
37+
38+
T (x);
39+
40+
T (y) = z + q;
41+
42+
T (y y) = z + q;
43+
44+
z + q;
45+
EOF
46+
run 0 "\
47+
1.0-8: +(<cast>(x, T), y)
48+
3.0-4: <OR>(<declare>(T, x), <cast>(x, T))
49+
5.0-12: <OR>(<init-declare>(T, y, +(z, q)), =(<cast>(y, T), +(z, q)))
50+
7.0-14: <error>
51+
9.0-4: +(z, q)
52+
err: 7.5: syntax error, unexpected identifier, expecting '=' or '+' or ')'"

examples/c++/glr/c++-types.yy

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/* Simplified -*- C++ -*- Type and Expression Grammar. */
2+
3+
%glr-parser
4+
%skeleton "glr2.cc"
5+
%header
6+
%locations
7+
%debug
8+
9+
/* Nice error messages with details. */
10+
%define parse.error detailed
11+
12+
%code requires
13+
{
14+
union Node {
15+
struct {
16+
int isNterm;
17+
int parents;
18+
} nodeInfo;
19+
struct {
20+
int isNterm; /* 1 */
21+
int parents;
22+
char const *form;
23+
union Node *children[3];
24+
} nterm;
25+
struct {
26+
int isNterm; /* 0 */
27+
int parents;
28+
char *text;
29+
} term;
30+
};
31+
typedef union Node Node;
32+
}
33+
34+
%define api.value.type {Node *}
35+
36+
%code
37+
{
38+
39+
#include <cassert>
40+
#include <cctype>
41+
#include <cstdio>
42+
#include <cstdlib>
43+
#include <cstring>
44+
45+
static Node *new_nterm (char const *form, Node *child0 = nullptr, Node *child1 = nullptr, Node *child2 = nullptr);
46+
static Node *new_term (char *);
47+
static void free_node (Node *);
48+
static std::ostream& operator<< (std::ostream& o, const Node &node);
49+
static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);
50+
51+
static int yylex (YYSTYPE *lvalp, YYLTYPE *llocp);
52+
}
53+
54+
%expect-rr 1
55+
56+
%token
57+
TYPENAME "typename"
58+
ID "identifier"
59+
60+
%right '='
61+
%left '+'
62+
63+
%destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID
64+
65+
%%
66+
67+
prog : %empty
68+
| prog stmt { std::cout << @2 << ": " << *$2 << '\n'; free_node ($2); }
69+
;
70+
71+
stmt : expr ';' %merge <stmtMerge> { $$ = $1; }
72+
| decl %merge <stmtMerge>
73+
| error ';' { $$ = new_nterm ("<error>"); }
74+
| '@' { $$ = $1; YYACCEPT; }
75+
;
76+
77+
expr : ID
78+
| TYPENAME '(' expr ')'
79+
{ $$ = new_nterm ("<cast>", $3, $1); }
80+
| expr '+' expr { $$ = new_nterm ("+", $1, $3); }
81+
| expr '=' expr { $$ = new_nterm ("=", $1, $3); }
82+
;
83+
84+
decl : TYPENAME declarator ';'
85+
{ $$ = new_nterm ("<declare>", $1, $2); }
86+
| TYPENAME declarator '=' expr ';'
87+
{ $$ = new_nterm ("<init-declare>", $1,
88+
$2, $4); }
89+
;
90+
91+
declarator
92+
: ID
93+
| '(' declarator ')' { $$ = $2; }
94+
;
95+
96+
%%
97+
98+
int
99+
main (int argc, char **argv)
100+
{
101+
// Enable parse traces on option -p.
102+
if (1 < argc && strcmp (argv[1], "-p") == 0)
103+
yydebug = 1;
104+
yy::parser parser;
105+
return !!parser.parse ();
106+
}
107+
108+
109+
/* A C error reporting function. */
110+
void yy::parser::error (const location_type& l, const std::string& m)
111+
{
112+
std::cerr << l << ": " << m << '\n';
113+
}
114+
115+
int yylex (YYSTYPE *lvalp, YYLTYPE *llocp)
116+
{
117+
static int lineNum = 1;
118+
static int colNum = 0;
119+
120+
while (1)
121+
{
122+
int c;
123+
assert (!feof (stdin));
124+
c = getchar ();
125+
switch (c)
126+
{
127+
case EOF:
128+
return 0;
129+
case '\t':
130+
colNum = (colNum + 7) & ~7;
131+
break;
132+
case ' ': case '\f':
133+
colNum += 1;
134+
break;
135+
case '\n':
136+
lineNum += 1;
137+
colNum = 0;
138+
break;
139+
default:
140+
{
141+
int tok;
142+
llocp->begin.line = llocp->end.line = lineNum;
143+
llocp->begin.column = colNum;
144+
if (isalpha (c))
145+
{
146+
char buffer[256];
147+
unsigned i = 0;
148+
149+
do
150+
{
151+
buffer[i++] = static_cast<char> (c);
152+
colNum += 1;
153+
assert (i != sizeof buffer - 1);
154+
c = getchar ();
155+
}
156+
while (isalnum (c) || c == '_');
157+
158+
ungetc (c, stdin);
159+
buffer[i++] = 0;
160+
tok
161+
= isupper (static_cast <unsigned char> (buffer[0]))
162+
? yy::parser::token::TYPENAME
163+
: yy::parser::token::ID;
164+
*lvalp = new_term (strcpy (static_cast<char*> (malloc (i)), buffer));
165+
}
166+
else
167+
{
168+
colNum += 1;
169+
tok = c;
170+
*lvalp = NULL;
171+
}
172+
llocp->end.column = colNum-1;
173+
return tok;
174+
}
175+
}
176+
}
177+
}
178+
179+
static Node *
180+
new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
181+
{
182+
Node *res = new Node;
183+
res->nterm.isNterm = 1;
184+
res->nterm.parents = 0;
185+
res->nterm.form = form;
186+
res->nterm.children[0] = child0;
187+
if (child0)
188+
child0->nodeInfo.parents += 1;
189+
res->nterm.children[1] = child1;
190+
if (child1)
191+
child1->nodeInfo.parents += 1;
192+
res->nterm.children[2] = child2;
193+
if (child2)
194+
child2->nodeInfo.parents += 1;
195+
return res;
196+
}
197+
198+
static Node *
199+
new_term (char *text)
200+
{
201+
Node *res = new Node;
202+
res->term.isNterm = 0;
203+
res->term.parents = 0;
204+
res->term.text = text;
205+
return res;
206+
}
207+
208+
static void
209+
free_node (Node *node)
210+
{
211+
if (!node)
212+
return;
213+
node->nodeInfo.parents -= 1;
214+
/* Free only if 0 (last parent) or -1 (no parents). */
215+
if (node->nodeInfo.parents > 0)
216+
return;
217+
if (node->nodeInfo.isNterm == 1)
218+
{
219+
free_node (node->nterm.children[0]);
220+
free_node (node->nterm.children[1]);
221+
free_node (node->nterm.children[2]);
222+
}
223+
else
224+
free (node->term.text);
225+
delete node;
226+
}
227+
228+
static std::ostream&
229+
operator<< (std::ostream& o, const Node &node)
230+
{
231+
if (node.nodeInfo.isNterm == 1)
232+
{
233+
o << node.nterm.form;
234+
if (node.nterm.children[0])
235+
{
236+
o << '(' << *node.nterm.children[0];
237+
if (node.nterm.children[1])
238+
o << ", " << *node.nterm.children[1];
239+
if (node.nterm.children[2])
240+
o << ", " << *node.nterm.children[2];
241+
o << ")";
242+
}
243+
}
244+
else
245+
o << node.term.text;
246+
return o;
247+
}
248+
249+
250+
static YYSTYPE
251+
stmtMerge (YYSTYPE x0, YYSTYPE x1)
252+
{
253+
return new_nterm ("<OR>", x0, x1);
254+
}
255+

0 commit comments

Comments
 (0)