1----------------------------------------------------------------------------
2--
3-- Copyright (C) 2016 The Qt Company Ltd.
4-- Contact: https://www.qt.io/licensing/
5--
6-- This file is part of the QtCore module of the Qt Toolkit.
7--
8-- $QT_BEGIN_LICENSE:GPL-EXCEPT$
9-- Commercial License Usage
10-- Licensees holding valid commercial Qt licenses may use this file in
11-- accordance with the commercial license agreement provided with the
12-- Software or, alternatively, in accordance with the terms contained in
13-- a written agreement between you and The Qt Company. For licensing terms
14-- and conditions see https://www.qt.io/terms-conditions. For further
15-- information use the contact form at https://www.qt.io/contact-us.
16--
17-- GNU General Public License Usage
18-- Alternatively, this file may be used under the terms of the GNU
19-- General Public License version 3 as published by the Free Software
20-- Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21-- included in the packaging of this file. Please review the following
22-- information to ensure the GNU General Public License requirements will
23-- be met: https://www.gnu.org/licenses/gpl-3.0.html.
24--
25-- $QT_END_LICENSE$
26--
27----------------------------------------------------------------------------
28
29%parser calc_grammar
30%decl calc_parser.h
31%impl calc_parser.cpp
32
33%token_prefix Token_
34%token number
35%token lparen
36%token rparen
37%token plus
38%token minus
39
40%start Goal
41
42/:
43#ifndef CALC_PARSER_H
44#define CALC_PARSER_H
45
46#include "qparser.h"
47#include "calc_grammar_p.h"
48
49class CalcParser: public QParser<CalcParser, $table>
50{
51public:
52  int nextToken();
53  void consumeRule(int ruleno);
54};
55
56#endif // CALC_PARSER_H
57:/
58
59
60
61
62
63/.
64#include "calc_parser.h"
65
66#include <QtDebug>
67#include <cstdlib>
68
69void CalcParser::consumeRule(int ruleno)
70  {
71    switch (ruleno) {
72./
73
74Goal: Expression ;
75/.
76case $rule_number:
77  qDebug() << "value:" << sym(1);
78  break;
79./
80
81PrimaryExpression: number ;
82PrimaryExpression: lparen Expression rparen ;
83/.
84case $rule_number:
85  sym(1) = sym (2);
86  break;
87./
88
89Expression: PrimaryExpression ;
90
91Expression: Expression plus PrimaryExpression;
92/.
93case $rule_number:
94  sym(1) += sym (3);
95  break;
96./
97
98Expression: Expression minus PrimaryExpression;
99/.
100case $rule_number:
101  sym(1) -= sym (3);
102  break;
103./
104
105
106
107/.
108    } // switch
109}
110
111#include <cstdio>
112
113int main()
114{
115  CalcParser p;
116
117  if (p.parse())
118    printf("ok\n");
119}
120./
121