| 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 QLALR 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 |
#include "lambda.h" |
| 30 |
|
| 31 |
#include <cstdio> |
| 32 |
#include <cstdlib> |
| 33 |
#include <cstring> |
| 34 |
#include "parser_table_p.h" |
| 35 |
|
| 36 |
class Parser: protected parser_table |
| 37 |
{ |
| 38 |
public: |
| 39 |
union Value { |
| 40 |
int ival; |
| 41 |
// ### more... |
| 42 |
}; |
| 43 |
|
| 44 |
public: |
| 45 |
Parser(); |
| 46 |
~Parser(); |
| 47 |
|
| 48 |
bool parse(); |
| 49 |
|
| 50 |
protected: |
| 51 |
inline void reallocateStack(); |
| 52 |
|
| 53 |
inline Value &sym(int index) |
| 54 |
{ return sym_stack [tos + index - 1]; } |
| 55 |
|
| 56 |
int nextToken(); |
| 57 |
void consumeRule(int ruleno); |
| 58 |
|
| 59 |
protected: |
| 60 |
int tos; |
| 61 |
int stack_size; |
| 62 |
Value *sym_stack; |
| 63 |
int *state_stack; |
| 64 |
int current_char; |
| 65 |
unsigned in_tag: 1; |
| 66 |
}; |
| 67 |
|
| 68 |
inline void Parser::reallocateStack() |
| 69 |
{ |
| 70 |
if (! stack_size) |
| 71 |
stack_size = 128; |
| 72 |
else |
| 73 |
stack_size <<= 1; |
| 74 |
|
| 75 |
sym_stack = reinterpret_cast<Value*> (::realloc(sym_stack, stack_size * sizeof(Value))); |
| 76 |
state_stack = reinterpret_cast<int*> (::realloc(state_stack, stack_size * sizeof(int))); |
| 77 |
} |
| 78 |
|
| 79 |
Parser::Parser(): |
| 80 |
tos(0), |
| 81 |
stack_size(0), |
| 82 |
sym_stack(0), |
| 83 |
state_stack(0) |
| 84 |
{ |
| 85 |
} |
| 86 |
|
| 87 |
Parser::~Parser() |
| 88 |
{ |
| 89 |
if (stack_size) { |
| 90 |
::free(sym_stack); |
| 91 |
::free(state_stack); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
bool Parser::parse() |
| 96 |
{ |
| 97 |
const int INITIAL_STATE = 0; |
| 98 |
|
| 99 |
current_char = 0; |
| 100 |
in_tag = 0; |
| 101 |
|
| 102 |
int yytoken = -1; |
| 103 |
reallocateStack(); |
| 104 |
|
| 105 |
tos = 0; |
| 106 |
state_stack[++tos] = INITIAL_STATE; |
| 107 |
|
| 108 |
while (true) |
| 109 |
{ |
| 110 |
if (yytoken == -1 && - TERMINAL_COUNT != action_index [state_stack [tos]]) |
| 111 |
yytoken = nextToken(); |
| 112 |
|
| 113 |
int act = t_action (state_stack [tos], yytoken); |
| 114 |
|
| 115 |
if (act == ACCEPT_STATE) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
else if (act > 0) |
| 120 |
{ |
| 121 |
if (++tos == stack_size) |
| 122 |
reallocateStack(); |
| 123 |
|
| 124 |
sym_stack [tos].ival = current_char; // ### save the token value here |
| 125 |
state_stack [tos] = act; |
| 126 |
yytoken = -1; |
| 127 |
} |
| 128 |
|
| 129 |
else if (act < 0) |
| 130 |
{ |
| 131 |
int r = - act - 1; |
| 132 |
|
| 133 |
tos -= rhs [r]; |
| 134 |
act = state_stack [tos++]; |
| 135 |
consumeRule (r); |
| 136 |
state_stack [tos] = nt_action (act, lhs [r] - TERMINAL_COUNT); |
| 137 |
} |
| 138 |
|
| 139 |
else |
| 140 |
break; |
| 141 |
} |
| 142 |
|
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
int Parser::nextToken() |
| 148 |
{ |
| 149 |
static int tokens[] = { ID, ID, ID, EOF_SYMBOL }; |
| 150 |
static int *tk = tokens; |
| 151 |
|
| 152 |
return *tk++; |
| 153 |
} |
| 154 |
|
| 155 |
void Parser::consumeRule(int ruleno) |
| 156 |
{ |
| 157 |
switch (ruleno) { |
| 158 |
case Symbol: |
| 159 |
printf("symbol\n"); |
| 160 |
break; |
| 161 |
case SubExpression: |
| 162 |
printf("sub-expr\n"); |
| 163 |
break; |
| 164 |
case Appl: |
| 165 |
printf("appl\n"); |
| 166 |
break; |
| 167 |
case Abstr: |
| 168 |
printf("abstr\n"); |
| 169 |
break; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
///////////////////////////// |
| 174 |
// entry point |
| 175 |
///////////////////////////// |
| 176 |
int main() |
| 177 |
{ |
| 178 |
Parser parser; |
| 179 |
|
| 180 |
if (parser.parse()) |
| 181 |
printf ("OK\n"); |
| 182 |
else |
| 183 |
printf ("KO\n"); |
| 184 |
} |
| 185 |
|
| 186 |
|