mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-22 10:43:02 +00:00
Extract calc++ from the documentation.
* doc/bison.texinfo (Calc++): Add the extraction marks. * examples/extexi: New, from the aborted GNU Programming 2E. Separate the different paragraph of a file with empty lines. * examples/Makefile: Use it to extract the whole calc++ example.
This commit is contained in:
@@ -13,3 +13,23 @@ clean:
|
||||
calc++-parser.cc calc++-parser.hh \
|
||||
calc++-scanner.cc \
|
||||
calc++
|
||||
|
||||
## ------------ ##
|
||||
## Extracting. ##
|
||||
## ------------ ##
|
||||
|
||||
EXTRACTED = \
|
||||
calc++-driver.hh calc++-driver.cc \
|
||||
calc++-parser.yy \
|
||||
calc++-scanner.ll \
|
||||
calc++.cc
|
||||
|
||||
doc = ../../doc/bison.texinfo
|
||||
extexi = gawk -f ../extexi
|
||||
|
||||
RECURSIVE_TARGETS += extract
|
||||
|
||||
$(EXTRACTED): $(doc) ../extexi
|
||||
$(extexi) $(doc) -- $(EXTRACTED)
|
||||
|
||||
extract extract-am: $(EXTRACTED)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#line 7140 "../../doc/bison.texinfo"
|
||||
#include "calc++-driver.hh"
|
||||
#include "calc++-parser.hh"
|
||||
|
||||
calcxx_driver::calcxx_driver ()
|
||||
: trace_scanning (false),
|
||||
trace_parsing (false)
|
||||
: trace_scanning (false), trace_parsing (false)
|
||||
{
|
||||
variables["one"] = 1;
|
||||
variables["two"] = 2;
|
||||
|
||||
@@ -1,64 +1,47 @@
|
||||
#line 7036 "../../doc/bison.texinfo"
|
||||
#ifndef CALCXX_DRIVER_HH
|
||||
# define CALCXX_DRIVER_HH
|
||||
# include <string>
|
||||
# include <map>
|
||||
|
||||
/// Forward declarations.
|
||||
#line 7051 "../../doc/bison.texinfo"
|
||||
// Forward declarations.
|
||||
union YYSTYPE;
|
||||
|
||||
namespace yy
|
||||
{
|
||||
class calcxx_parser;
|
||||
class location;
|
||||
class calcxx_parser;
|
||||
}
|
||||
|
||||
class calcxx_driver;
|
||||
|
||||
#line 7069 "../../doc/bison.texinfo"
|
||||
// Announce to Flex the prototype we want for lexing function, ...
|
||||
# define YY_DECL \
|
||||
# define YY_DECL \
|
||||
int yylex (YYSTYPE* yylval, yy::location* yylloc, calcxx_driver& driver)
|
||||
// ... and declare it for the parser's sake.
|
||||
YY_DECL;
|
||||
|
||||
/// Conducting the whole scanning and parsing of Calc++.
|
||||
#line 7082 "../../doc/bison.texinfo"
|
||||
// Conducting the whole scanning and parsing of Calc++.
|
||||
class calcxx_driver
|
||||
{
|
||||
public:
|
||||
calcxx_driver ();
|
||||
virtual ~calcxx_driver ();
|
||||
|
||||
/// The variables.
|
||||
std::map<std::string, int> variables;
|
||||
|
||||
/// \name Handling the scanner.
|
||||
/// \{
|
||||
/// Open \a file for scanning.
|
||||
void scan_begin ();
|
||||
/// End scanning, clean up memory.
|
||||
void scan_end ();
|
||||
/// Whether to enable scanner traces.
|
||||
bool trace_scanning;
|
||||
/// \}
|
||||
|
||||
/// \name Handling the parser.
|
||||
/// \{
|
||||
/// Parse the file \a f.
|
||||
void parse (const std::string& f);
|
||||
/// The file being parsed.
|
||||
std::string file;
|
||||
/// Whether to enable parsing traces.
|
||||
bool trace_parsing;
|
||||
/// \}
|
||||
|
||||
/// The result.
|
||||
int result;
|
||||
|
||||
/// \name Error handling.
|
||||
/// \{
|
||||
/// Register a located error.
|
||||
#line 7101 "../../doc/bison.texinfo"
|
||||
// Handling the scanner.
|
||||
void scan_begin ();
|
||||
void scan_end ();
|
||||
bool trace_scanning;
|
||||
#line 7112 "../../doc/bison.texinfo"
|
||||
// Handling the parser.
|
||||
void parse (const std::string& f);
|
||||
std::string file;
|
||||
bool trace_parsing;
|
||||
#line 7126 "../../doc/bison.texinfo"
|
||||
// Error handling.
|
||||
void error (const yy::location& l, const std::string& m);
|
||||
/// Register an error.
|
||||
void error (const std::string& m);
|
||||
/// \}
|
||||
};
|
||||
#endif
|
||||
#endif // ! CALCXX_DRIVER_HH
|
||||
|
||||
@@ -1,48 +1,44 @@
|
||||
%skeleton "lalr1.cc" /* -*- C++ -*- */
|
||||
#line 7188 "../../doc/bison.texinfo"
|
||||
%skeleton "lalr1.cc" /* -*- C++ -*- */
|
||||
%define "parser_class_name" "calcxx_parser"
|
||||
%defines
|
||||
%{
|
||||
# include <string>
|
||||
# include "calc++-driver.hh"
|
||||
%}
|
||||
|
||||
%error-verbose
|
||||
|
||||
#line 7204 "../../doc/bison.texinfo"
|
||||
// The parsing context.
|
||||
%parse-param { calcxx_driver& driver }
|
||||
%lex-param { calcxx_driver& driver }
|
||||
|
||||
#line 7217 "../../doc/bison.texinfo"
|
||||
%locations
|
||||
%initial-action
|
||||
{
|
||||
// Initialize the initial location.
|
||||
@$.begin.filename = @$.end.filename = &driver.file;
|
||||
};
|
||||
|
||||
// Define yydebug.
|
||||
#line 7231 "../../doc/bison.texinfo"
|
||||
%debug
|
||||
|
||||
%error-verbose
|
||||
#line 7241 "../../doc/bison.texinfo"
|
||||
// Symbols.
|
||||
|
||||
%union
|
||||
{
|
||||
/// Value of a numeric literal.
|
||||
int ival;
|
||||
/// Name of a variable.
|
||||
std::string *sval;
|
||||
};
|
||||
|
||||
#line 7258 "../../doc/bison.texinfo"
|
||||
%token YYEOF 0 "end of file"
|
||||
%token TOKEN_ASSIGN ":="
|
||||
%token <sval> TOKEN_IDENTIFIER "identifier"
|
||||
%token <ival> TOKEN_NUMBER "number"
|
||||
%type <ival> exp "expression"
|
||||
|
||||
#line 7271 "../../doc/bison.texinfo"
|
||||
%printer { debug_stream () << *$$; } "identifier"
|
||||
%destructor { delete $$; } "identifier"
|
||||
|
||||
%printer { debug_stream () << $$; } "number" "expression"
|
||||
|
||||
#line 7282 "../../doc/bison.texinfo"
|
||||
%%
|
||||
%start unit;
|
||||
unit: assignments exp { driver.result = $2; };
|
||||
@@ -61,8 +57,10 @@ exp: exp '+' exp { $$ = $1 + $3; }
|
||||
| TOKEN_IDENTIFIER { $$ = driver.variables[*$1]; }
|
||||
| TOKEN_NUMBER { $$ = $1; };
|
||||
%%
|
||||
#line 7308 "../../doc/bison.texinfo"
|
||||
void
|
||||
yy::calcxx_parser::error (const location& l, const std::string& m)
|
||||
yy::calcxx_parser::error (const yy::calcxx_parser::location_type& l,
|
||||
const std::string& m)
|
||||
{
|
||||
driver.error (l, m);
|
||||
}
|
||||
|
||||
@@ -1,33 +1,28 @@
|
||||
%{ /* -*- C++ -*- */
|
||||
|
||||
%{ /* -*- C++ -*- */
|
||||
# include <string>
|
||||
# include <cerrno>
|
||||
# include "calc++-driver.hh"
|
||||
# include "calc++-parser.hh"
|
||||
%}
|
||||
|
||||
%option noyywrap nounput debug batch
|
||||
%option noyywrap nounput batch debug
|
||||
|
||||
id [a-zA-Z][a-zA-Z_0-9]*
|
||||
int [0-9]+
|
||||
blank [ \t]
|
||||
|
||||
%%
|
||||
|
||||
%{
|
||||
# define YY_USER_ACTION yylloc->columns (yyleng);
|
||||
yylloc->step ();
|
||||
# define YY_USER_ACTION yylloc->columns (yyleng);
|
||||
%}
|
||||
{blank}+ yylloc->step ();
|
||||
[\n]+ yylloc->lines (yyleng); yylloc->step ();
|
||||
|
||||
|
||||
[-+*/] return yytext[0];
|
||||
":=" return TOKEN_ASSIGN;
|
||||
{int} yylval->ival = atoi (yytext); return TOKEN_NUMBER;
|
||||
{id} yylval->sval = new std::string (yytext); return TOKEN_IDENTIFIER;
|
||||
. driver.error (*yylloc, "invalid character");
|
||||
|
||||
%%
|
||||
|
||||
void
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#line 7414 "../../doc/bison.texinfo"
|
||||
#include <iostream>
|
||||
#include "calc++-driver.hh"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user