diff options
Diffstat (limited to 'examples/c++/glr')
-rw-r--r-- | examples/c++/glr/README.md | 3 | ||||
-rw-r--r-- | examples/c++/glr/c++-types.test | 2 | ||||
-rw-r--r-- | examples/c++/glr/c++-types.yy | 43 |
3 files changed, 44 insertions, 4 deletions
diff --git a/examples/c++/glr/README.md b/examples/c++/glr/README.md index 26067c27..ed06a0c6 100644 --- a/examples/c++/glr/README.md +++ b/examples/c++/glr/README.md @@ -4,6 +4,9 @@ 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. +It uses (Bison) variants to store objects as semantic values. It also +demonstrates custom error messages in C++. + <!--- Local Variables: fill-column: 76 diff --git a/examples/c++/glr/c++-types.test b/examples/c++/glr/c++-types.test index 7440ea24..db5be8d7 100644 --- a/examples/c++/glr/c++-types.test +++ b/examples/c++/glr/c++-types.test @@ -47,4 +47,4 @@ run 0 "\ 5.0-13: <OR>(<init-declare>(T, y, +(z, q)), =(<cast>(y, T), +(z, q))) 7.0-15: <error> 9.0-5: +(z, q) -err: 7.5: syntax error, unexpected identifier, expecting = or + or )" +err: 7.5: syntax error on token identifier (expected = or + or ))" diff --git a/examples/c++/glr/c++-types.yy b/examples/c++/glr/c++-types.yy index fa8889de..a3131b05 100644 --- a/examples/c++/glr/c++-types.yy +++ b/examples/c++/glr/c++-types.yy @@ -27,8 +27,8 @@ %locations %debug -// Nice error messages with details. -%define parse.error detailed +// Custom error messages. +%define parse.error custom %code requires { @@ -44,9 +44,11 @@ #include <fstream> #include <cstring> + // Merge two semantic values. static Node stmt_merge (const Node& x0, const Node& x1); + // Fetch a token. static yy::parser::symbol_type yylex (); } @@ -100,13 +102,41 @@ declarator std::istream* input = nullptr; yy::parser::location_type loc; -// An error reporting function. + +/*---------. +| Parser. | +`---------*/ + +// Generate a custom error message. +void +yy::parser::report_syntax_error (const context& ctx) const +{ + std::cerr << ctx.location () << ": syntax error"; + if (!ctx.lookahead ().empty ()) + std::cerr << " on token " << ctx.lookahead ().name (); + { + enum { TOKENMAX = 10 }; + symbol_kind_type expected[TOKENMAX]; + int n = ctx.expected_tokens (expected, TOKENMAX); + if (0 < n) + { + for (int i = 0; i < n; ++i) + std::cerr << (i == 0 ? " (expected " : " or ") + << symbol_name (expected[i]); + std::cerr << ')'; + } + } + std::cerr << '\n'; +} + +// Report the error to the user. void yy::parser::error (const location_type& l, const std::string& m) { std::cerr << l << ": " << m << '\n'; } +// Fetch the next token. static yy::parser::symbol_type yylex () { @@ -168,12 +198,19 @@ yylex () } } +// Merge two semantic values as an AST including both alternatives. static Node stmt_merge (const Node& x0, const Node& x1) { return Nterm ("<OR>", x0, x1); } + +/*-------. +| Main. | +`-------*/ + +// Parse `file` using parser `parse`. int process (yy::parser& parse, const std::string& file) { |