-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
9 changed files
with
413 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This Makefile is designed to be simple and readable. It does not | ||
# aim at portability. It requires GNU Make. | ||
|
||
BASE = c++-types | ||
BISON = bison | ||
XSLTPROC = xsltproc | ||
|
||
all: $(BASE) | ||
|
||
%.c %.h %.xml %.gv: %.y | ||
$(BISON) $(BISONFLAGS) --defines --xml --graph=$*.gv -o $*.c $< | ||
|
||
$(BASE): $(BASE).o | ||
$(CC) $(CFLAGS) -o $@ $^ | ||
|
||
run: $(BASE) | ||
@echo "Type C++ declarations or expressions. Quit with ctrl-d." | ||
./$< | ||
|
||
html: $(BASE).html | ||
%.html: %.xml | ||
$(XSLTPROC) $(XSLTPROCFLAGS) -o $@ $$($(BISON) --print-datadir)/xslt/xml2xhtml.xsl $< | ||
|
||
CLEANFILES = \ | ||
$(BASE) *.o $(BASE).[ch] $(BASE).output $(BASE).xml $(BASE).html $(BASE).gv | ||
|
||
clean: | ||
rm -f $(CLEANFILES) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# glr | ||
|
||
This example demonstrates the use of GLR parsers to handle (local) | ||
ambiguities in the C++ language. See the node "Merging GLR Parses" in | ||
Bison's documentation. | ||
|
||
<!--- | ||
Local Variables: | ||
fill-column: 76 | ||
ispell-dictionary: "american" | ||
End: | ||
Copyright (C) 2020 Free Software Foundation, Inc. | ||
This file is part of Bison, the GNU Compiler Compiler. | ||
Permission is granted to copy, distribute and/or modify this document | ||
under the terms of the GNU Free Documentation License, Version 1.3 or | ||
any later version published by the Free Software Foundation; with no | ||
Invariant Sections, with no Front-Cover Texts, and with no Back-Cover | ||
Texts. A copy of the license is included in the "GNU Free | ||
Documentation License" file as part of this distribution. | ||
---> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#! /bin/sh | ||
|
||
# Copyright (C) 2020 Free Software Foundation, Inc. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
cat >input <<EOF | ||
z + q; | ||
T x; | ||
T x = y; | ||
x = y; | ||
EOF | ||
run 0 "\ | ||
1.0-4: +(z, q) | ||
3.0-2: <declare>(T, x) | ||
5.0-6: <init-declare>(T, x, y) | ||
7.0-4: =(x, y)" | ||
|
||
exit 77 | ||
|
||
cat >input <<EOF | ||
T (x) + y; | ||
T (x); | ||
T (y) = z + q; | ||
T (y y) = z + q; | ||
z + q; | ||
EOF | ||
run 0 "\ | ||
1.0-8: +(<cast>(x, T), y) | ||
3.0-4: <OR>(<declare>(T, x), <cast>(x, T)) | ||
5.0-12: <OR>(<init-declare>(T, y, +(z, q)), =(<cast>(y, T), +(z, q))) | ||
7.0-14: <error> | ||
9.0-4: +(z, q) | ||
err: 7.5: syntax error, unexpected identifier, expecting '=' or '+' or ')'" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
/* Simplified -*- C++ -*- Type and Expression Grammar. */ | ||
|
||
%glr-parser | ||
%skeleton "glr2.cc" | ||
%header | ||
%locations | ||
%debug | ||
|
||
/* Nice error messages with details. */ | ||
%define parse.error detailed | ||
|
||
%code requires | ||
{ | ||
union Node { | ||
struct { | ||
int isNterm; | ||
int parents; | ||
} nodeInfo; | ||
struct { | ||
int isNterm; /* 1 */ | ||
int parents; | ||
char const *form; | ||
union Node *children[3]; | ||
} nterm; | ||
struct { | ||
int isNterm; /* 0 */ | ||
int parents; | ||
char *text; | ||
} term; | ||
}; | ||
typedef union Node Node; | ||
} | ||
|
||
%define api.value.type {Node *} | ||
|
||
%code | ||
{ | ||
|
||
#include <cassert> | ||
#include <cctype> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <cstring> | ||
|
||
#if __cplusplus < 201103L | ||
# define nullptr 0 | ||
#endif | ||
|
||
static Node *new_nterm (char const *form, Node *child0 = nullptr, Node *child1 = nullptr, Node *child2 = nullptr); | ||
static Node *new_term (char *); | ||
static void free_node (Node *); | ||
static std::ostream& operator<< (std::ostream& o, const Node &node); | ||
static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1); | ||
|
||
static int yylex (YYSTYPE *lvalp, YYLTYPE *llocp); | ||
} | ||
|
||
%expect-rr 1 | ||
|
||
%token | ||
TYPENAME "typename" | ||
ID "identifier" | ||
|
||
%right '=' | ||
%left '+' | ||
|
||
%destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID | ||
|
||
%% | ||
|
||
prog : %empty | ||
| prog stmt { std::cout << @2 << ": " << *$2 << '\n'; free_node ($2); } | ||
; | ||
|
||
stmt : expr ';' %merge <stmtMerge> { $$ = $1; } | ||
| decl %merge <stmtMerge> | ||
| error ';' { $$ = new_nterm ("<error>"); } | ||
| '@' { $$ = $1; YYACCEPT; } | ||
; | ||
|
||
expr : ID | ||
| TYPENAME '(' expr ')' | ||
{ $$ = new_nterm ("<cast>", $3, $1); } | ||
| expr '+' expr { $$ = new_nterm ("+", $1, $3); } | ||
| expr '=' expr { $$ = new_nterm ("=", $1, $3); } | ||
; | ||
|
||
decl : TYPENAME declarator ';' | ||
{ $$ = new_nterm ("<declare>", $1, $2); } | ||
| TYPENAME declarator '=' expr ';' | ||
{ $$ = new_nterm ("<init-declare>", $1, | ||
$2, $4); } | ||
; | ||
|
||
declarator | ||
: ID | ||
| '(' declarator ')' { $$ = $2; } | ||
; | ||
|
||
%% | ||
|
||
int | ||
main (int argc, char **argv) | ||
{ | ||
// Enable parse traces on option -p. | ||
if (1 < argc && strcmp (argv[1], "-p") == 0) | ||
yydebug = 1; | ||
yy::parser parser; | ||
return !!parser.parse (); | ||
} | ||
|
||
|
||
/* A C error reporting function. */ | ||
void yy::parser::error (const location_type& l, const std::string& m) | ||
{ | ||
std::cerr << l << ": " << m << '\n'; | ||
} | ||
|
||
int yylex (YYSTYPE *lvalp, YYLTYPE *llocp) | ||
{ | ||
static int lineNum = 1; | ||
static int colNum = 0; | ||
|
||
while (1) | ||
{ | ||
int c; | ||
assert (!feof (stdin)); | ||
c = getchar (); | ||
switch (c) | ||
{ | ||
case EOF: | ||
return 0; | ||
case '\t': | ||
colNum = (colNum + 7) & ~7; | ||
break; | ||
case ' ': case '\f': | ||
colNum += 1; | ||
break; | ||
case '\n': | ||
lineNum += 1; | ||
colNum = 0; | ||
break; | ||
default: | ||
{ | ||
int tok; | ||
llocp->begin.line = llocp->end.line = lineNum; | ||
llocp->begin.column = colNum; | ||
if (isalpha (c)) | ||
{ | ||
char buffer[256]; | ||
unsigned i = 0; | ||
|
||
do | ||
{ | ||
buffer[i++] = static_cast<char> (c); | ||
colNum += 1; | ||
assert (i != sizeof buffer - 1); | ||
c = getchar (); | ||
} | ||
while (isalnum (c) || c == '_'); | ||
|
||
ungetc (c, stdin); | ||
buffer[i++] = 0; | ||
tok | ||
= isupper (static_cast <unsigned char> (buffer[0])) | ||
? yy::parser::token::TYPENAME | ||
: yy::parser::token::ID; | ||
*lvalp = new_term (strcpy (static_cast<char*> (malloc (i)), buffer)); | ||
} | ||
else | ||
{ | ||
colNum += 1; | ||
tok = c; | ||
*lvalp = nullptr; | ||
} | ||
llocp->end.column = colNum-1; | ||
return tok; | ||
} | ||
} | ||
} | ||
} | ||
|
||
static Node * | ||
new_nterm (char const *form, Node *child0, Node *child1, Node *child2) | ||
{ | ||
Node *res = new Node; | ||
res->nterm.isNterm = 1; | ||
res->nterm.parents = 0; | ||
res->nterm.form = form; | ||
res->nterm.children[0] = child0; | ||
if (child0) | ||
child0->nodeInfo.parents += 1; | ||
res->nterm.children[1] = child1; | ||
if (child1) | ||
child1->nodeInfo.parents += 1; | ||
res->nterm.children[2] = child2; | ||
if (child2) | ||
child2->nodeInfo.parents += 1; | ||
return res; | ||
} | ||
|
||
static Node * | ||
new_term (char *text) | ||
{ | ||
Node *res = new Node; | ||
res->term.isNterm = 0; | ||
res->term.parents = 0; | ||
res->term.text = text; | ||
return res; | ||
} | ||
|
||
static void | ||
free_node (Node *node) | ||
{ | ||
if (!node) | ||
return; | ||
node->nodeInfo.parents -= 1; | ||
/* Free only if 0 (last parent) or -1 (no parents). */ | ||
if (node->nodeInfo.parents > 0) | ||
return; | ||
if (node->nodeInfo.isNterm == 1) | ||
{ | ||
free_node (node->nterm.children[0]); | ||
free_node (node->nterm.children[1]); | ||
free_node (node->nterm.children[2]); | ||
} | ||
else | ||
free (node->term.text); | ||
delete node; | ||
} | ||
|
||
static std::ostream& | ||
operator<< (std::ostream& o, const Node &node) | ||
{ | ||
if (node.nodeInfo.isNterm == 1) | ||
{ | ||
o << node.nterm.form; | ||
if (node.nterm.children[0]) | ||
{ | ||
o << '(' << *node.nterm.children[0]; | ||
if (node.nterm.children[1]) | ||
o << ", " << *node.nterm.children[1]; | ||
if (node.nterm.children[2]) | ||
o << ", " << *node.nterm.children[2]; | ||
o << ")"; | ||
} | ||
} | ||
else | ||
o << node.term.text; | ||
return o; | ||
} | ||
|
||
|
||
static YYSTYPE | ||
stmtMerge (YYSTYPE x0, YYSTYPE x1) | ||
{ | ||
return new_nterm ("<OR>", x0, x1); | ||
} |
Oops, something went wrong.