| 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 |
|
| 49 |
class CalcParser: public QParser<CalcParser, $table> |
| 50 |
{ |
| 51 |
public: |
| 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 |
|
| 69 |
void CalcParser::consumeRule(int ruleno) |
| 70 |
{ |
| 71 |
switch (ruleno) { |
| 72 |
./ |
| 73 |
|
| 74 |
Goal: Expression ; |
| 75 |
/. |
| 76 |
case $rule_number: |
| 77 |
qDebug() << "value:" << sym(1); |
| 78 |
break; |
| 79 |
./ |
| 80 |
|
| 81 |
PrimaryExpression: number ; |
| 82 |
PrimaryExpression: lparen Expression rparen ; |
| 83 |
/. |
| 84 |
case $rule_number: |
| 85 |
sym(1) = sym (2); |
| 86 |
break; |
| 87 |
./ |
| 88 |
|
| 89 |
Expression: PrimaryExpression ; |
| 90 |
|
| 91 |
Expression: Expression plus PrimaryExpression; |
| 92 |
/. |
| 93 |
case $rule_number: |
| 94 |
sym(1) += sym (3); |
| 95 |
break; |
| 96 |
./ |
| 97 |
|
| 98 |
Expression: Expression minus PrimaryExpression; |
| 99 |
/. |
| 100 |
case $rule_number: |
| 101 |
sym(1) -= sym (3); |
| 102 |
break; |
| 103 |
./ |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
/. |
| 108 |
} // switch |
| 109 |
} |
| 110 |
|
| 111 |
#include <cstdio> |
| 112 |
|
| 113 |
int main() |
| 114 |
{ |
| 115 |
CalcParser p; |
| 116 |
|
| 117 |
if (p.parse()) |
| 118 |
printf("ok\n"); |
| 119 |
} |
| 120 |
./ |