Use locations in the variant example.

Yes, this obfuscates the point of this example, variants only.  But glr.cc
cannot work (yet?) without locations.  This change makes it easier to use
this example with glr.cc.

	* examples/variant.yy (assert): %define it.
	(locations): Request them.
	(yylex): Bind the location to the stage.
This commit is contained in:
Akim Demaille
2008-12-31 16:13:04 +01:00
parent 0623bacc05
commit 882f02ed83
2 changed files with 22 additions and 7 deletions

View File

@@ -1,3 +1,14 @@
2009-03-02 Akim Demaille <demaille@gostai.com>
Use locations in the variant example.
Yes, this obfuscates the point of this example, variants only. But glr.cc
cannot work (yet?) without locations. This change makes it easier to use
this example with glr.cc.
* examples/variant.yy (assert): %define it.
(locations): Request them.
(yylex): Bind the location to the stage.
2009-03-02 Akim Demaille <demaille@gostai.com> 2009-03-02 Akim Demaille <demaille@gostai.com>
Dub make_TOKEN as a public type interface. Dub make_TOKEN as a public type interface.

View File

@@ -1,8 +1,10 @@
%debug %debug
%skeleton "lalr1.cc" %skeleton "lalr1.cc"
%defines %defines
%define assert
%define variant %define variant
%define lex_symbol %define lex_symbol
%locations
%code requires // *.hh %code requires // *.hh
{ {
@@ -85,26 +87,28 @@ yy::parser::symbol_type
yylex () yylex ()
{ {
static int stage = -1; static int stage = -1;
switch (++stage) ++stage;
yy::parser::location_type loc(0, stage + 1, stage + 1);
switch (stage)
{ {
case 0: case 0:
return yy::parser::make_TEXT ("I have three numbers for you."); return yy::parser::make_TEXT ("I have three numbers for you.", loc);
case 1: case 1:
case 2: case 2:
case 3: case 3:
return yy::parser::make_NUMBER (stage); return yy::parser::make_NUMBER (stage, loc);
case 4: case 4:
return yy::parser::make_TEXT ("And that's all!"); return yy::parser::make_TEXT ("And that's all!", loc);
default: default:
return yy::parser::make_END_OF_FILE (); return yy::parser::make_END_OF_FILE (loc);
} }
} }
// Mandatory error function // Mandatory error function
void void
yy::parser::error (const std::string& message) yy::parser::error (const yy::parser::location_type& loc, const std::string& msg)
{ {
std::cerr << message << std::endl; std::cerr << loc << ": " << msg << std::endl;
} }
int int