]> git.saurik.com Git - bison.git/blob - examples/calc++/calc++-parser.yy
141d1da26ffe3b3f341b2faadb88e5fc7b804abb
[bison.git] / examples / calc++ / calc++-parser.yy
1 #line 7195 "../../doc/bison.texinfo"
2 %skeleton "lalr1.cc" /* -*- C++ -*- */
3 %define "parser_class_name" "calcxx_parser"
4 %defines
5 %{
6 # include <string>
7 # include "calc++-driver.hh"
8 %}
9 #line 7211 "../../doc/bison.texinfo"
10 // The parsing context.
11 %parse-param { calcxx_driver& driver }
12 %lex-param { calcxx_driver& driver }
13 #line 7224 "../../doc/bison.texinfo"
14 %locations
15 %initial-action
16 {
17 // Initialize the initial location.
18 @$.begin.filename = @$.end.filename = &driver.file;
19 };
20 #line 7238 "../../doc/bison.texinfo"
21 %debug
22 %error-verbose
23 #line 7248 "../../doc/bison.texinfo"
24 // Symbols.
25 %union
26 {
27 int ival;
28 std::string *sval;
29 };
30 #line 7265 "../../doc/bison.texinfo"
31 %token YYEOF 0 "end of file"
32 %token TOKEN_ASSIGN ":="
33 %token <sval> TOKEN_IDENTIFIER "identifier"
34 %token <ival> TOKEN_NUMBER "number"
35 %type <ival> exp "expression"
36 #line 7278 "../../doc/bison.texinfo"
37 %printer { debug_stream () << *$$; } "identifier"
38 %destructor { delete $$; } "identifier"
39
40 %printer { debug_stream () << $$; } "number" "expression"
41 #line 7289 "../../doc/bison.texinfo"
42 %%
43 %start unit;
44 unit: assignments exp { driver.result = $2; };
45
46 assignments: assignments assignment {}
47 | /* Nothing. */ {};
48
49 assignment: TOKEN_IDENTIFIER ":=" exp { driver.variables[*$1] = $3; };
50
51 %left '+' '-';
52 %left '*' '/';
53 exp: exp '+' exp { $$ = $1 + $3; }
54 | exp '-' exp { $$ = $1 - $3; }
55 | exp '*' exp { $$ = $1 * $3; }
56 | exp '/' exp { $$ = $1 / $3; }
57 | TOKEN_IDENTIFIER { $$ = driver.variables[*$1]; }
58 | TOKEN_NUMBER { $$ = $1; };
59 %%
60 #line 7315 "../../doc/bison.texinfo"
61 void
62 yy::calcxx_parser::error (const yy::calcxx_parser::location_type& l,
63 const std::string& m)
64 {
65 driver.error (l, m);
66 }