Debian Codegraph @

examples/c++/calc++/parser.yy

raw
1 /* Parser for calc++. -*- C++ -*-
2
3 Copyright (C) 2005-2015, 2018-2021 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19
20 %skeleton "lalr1.cc" // -*- C++ -*-
21 %require "3.8.2"
22 %header
23
24 %define api.token.raw
25
26 %define api.token.constructor
27 %define api.value.type variant
28 %define parse.assert
29
30 %code requires {
31 # include <string>
32 class driver;
33 }
34
35 // The parsing context.
36 %param { driver& drv }
37
38 %locations
39
40 %define parse.trace
41 %define parse.error detailed
42 %define parse.lac full
43
44 %code {
45 # include "driver.hh"
46 }
47
48 %define api.token.prefix {TOK_}
49 %token
50 ASSIGN ":="
51 MINUS "-"
52 PLUS "+"
53 STAR "*"
54 SLASH "/"
55 LPAREN "("
56 RPAREN ")"
57 ;
58
59 %token <std::string> IDENTIFIER "identifier"
60 %token <int> NUMBER "number"
61 %nterm <int> exp
62
63 %printer { yyo << $$; } <*>;
64
65 %%
66 %start unit;
67 unit: assignments exp { drv.result = $2; };
68
69 assignments:
70 %empty {}
71 | assignments assignment {};
72
73 assignment:
74 "identifier" ":=" exp { drv.variables[$1] = $3; };
75
76 %left "+" "-";
77 %left "*" "/";
78 exp:
79 "number"
80 | "identifier" { $$ = drv.variables[$1]; }
81 | exp "+" exp { $$ = $1 + $3; }
82 | exp "-" exp { $$ = $1 - $3; }
83 | exp "*" exp { $$ = $1 * $3; }
84 | exp "/" exp { $$ = $1 / $3; }
85 | "(" exp ")" { $$ = $2; }
86 %%
87
88 void
89 yy::parser::error (const location_type& l, const std::string& m)
90 {
91 std::cerr << l << ": " << m << '\n';
92 }