Skip to content

Commit f9b3606

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 f9b3606

File tree

9 files changed

+413
-4
lines changed

9 files changed

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

0 commit comments

Comments
 (0)