| 1 |
-- Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
-- SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
%parser calc_grammar |
| 5 |
%decl calc_parser.h |
| 6 |
%impl calc_parser.cpp |
| 7 |
|
| 8 |
%token_prefix Token_ |
| 9 |
%token number |
| 10 |
%token lparen |
| 11 |
%token rparen |
| 12 |
%token plus |
| 13 |
%token minus |
| 14 |
|
| 15 |
%start Goal |
| 16 |
|
| 17 |
/: |
| 18 |
#ifndef CALC_PARSER_H |
| 19 |
#define CALC_PARSER_H |
| 20 |
|
| 21 |
#include "qparser.h" |
| 22 |
#include "calc_grammar_p.h" |
| 23 |
|
| 24 |
class CalcParser: public QParser<CalcParser, $table> |
| 25 |
{ |
| 26 |
public: |
| 27 |
int nextToken(); |
| 28 |
void consumeRule(int ruleno); |
| 29 |
}; |
| 30 |
|
| 31 |
#endif // CALC_PARSER_H |
| 32 |
:/ |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
/. |
| 39 |
#include "calc_parser.h" |
| 40 |
|
| 41 |
#include <QtDebug> |
| 42 |
#include <cstdlib> |
| 43 |
|
| 44 |
void CalcParser::consumeRule(int ruleno) |
| 45 |
{ |
| 46 |
switch (ruleno) { |
| 47 |
./ |
| 48 |
|
| 49 |
Goal: Expression ; |
| 50 |
/. |
| 51 |
case $rule_number: |
| 52 |
qDebug() << "value:" << sym(1); |
| 53 |
break; |
| 54 |
./ |
| 55 |
|
| 56 |
PrimaryExpression: number ; |
| 57 |
PrimaryExpression: lparen Expression rparen ; |
| 58 |
/. |
| 59 |
case $rule_number: |
| 60 |
sym(1) = sym (2); |
| 61 |
break; |
| 62 |
./ |
| 63 |
|
| 64 |
Expression: PrimaryExpression ; |
| 65 |
|
| 66 |
Expression: Expression plus PrimaryExpression; |
| 67 |
/. |
| 68 |
case $rule_number: |
| 69 |
sym(1) += sym (3); |
| 70 |
break; |
| 71 |
./ |
| 72 |
|
| 73 |
Expression: Expression minus PrimaryExpression; |
| 74 |
/. |
| 75 |
case $rule_number: |
| 76 |
sym(1) -= sym (3); |
| 77 |
break; |
| 78 |
./ |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
/. |
| 83 |
} // switch |
| 84 |
} |
| 85 |
|
| 86 |
#include <cstdio> |
| 87 |
|
| 88 |
int main() |
| 89 |
{ |
| 90 |
CalcParser p; |
| 91 |
|
| 92 |
if (p.parse()) |
| 93 |
printf("ok\n"); |
| 94 |
} |
| 95 |
./ |