util/qlalr/examples/lambda/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QLALR module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "lambda.h"
       
    43 
       
    44 #include <cstdio>
       
    45 #include <cstdlib>
       
    46 #include <cstring>
       
    47 #include "parser_table_p.h"
       
    48 
       
    49 class Parser: protected parser_table
       
    50 {
       
    51 public:
       
    52     union Value {
       
    53       int ival;
       
    54       // ### more...
       
    55     };
       
    56 
       
    57 public:
       
    58     Parser();
       
    59     ~Parser();
       
    60 
       
    61     bool parse();
       
    62 
       
    63 protected:
       
    64     inline void reallocateStack();
       
    65 
       
    66     inline Value &sym(int index)
       
    67     { return sym_stack [tos + index - 1]; }
       
    68 
       
    69     int nextToken();
       
    70     void consumeRule(int ruleno);
       
    71 
       
    72 protected:
       
    73     int tos;
       
    74     int stack_size;
       
    75     Value *sym_stack;
       
    76     int *state_stack;
       
    77     int current_char;
       
    78     unsigned in_tag: 1;
       
    79 };
       
    80 
       
    81 inline void Parser::reallocateStack()
       
    82 {
       
    83     if (! stack_size)
       
    84         stack_size = 128;
       
    85     else
       
    86         stack_size <<= 1;
       
    87 
       
    88     sym_stack = reinterpret_cast<Value*> (::realloc(sym_stack, stack_size * sizeof(Value)));
       
    89     state_stack = reinterpret_cast<int*> (::realloc(state_stack, stack_size * sizeof(int)));
       
    90 }
       
    91 
       
    92 Parser::Parser():
       
    93     tos(0),
       
    94     stack_size(0),
       
    95     sym_stack(0),
       
    96     state_stack(0)
       
    97 {
       
    98 }
       
    99 
       
   100 Parser::~Parser()
       
   101 {
       
   102     if (stack_size) {
       
   103         ::free(sym_stack);
       
   104         ::free(state_stack);
       
   105     }
       
   106 }
       
   107 
       
   108 bool Parser::parse()
       
   109 {
       
   110   const int INITIAL_STATE = 0;
       
   111 
       
   112   current_char = 0;
       
   113   in_tag = 0;
       
   114 
       
   115   int yytoken = -1;
       
   116   reallocateStack();
       
   117 
       
   118   tos = 0;
       
   119   state_stack[++tos] = INITIAL_STATE;
       
   120 
       
   121   while (true)
       
   122     {
       
   123       if (yytoken == -1 && - TERMINAL_COUNT != action_index [state_stack [tos]])
       
   124         yytoken = nextToken();
       
   125 
       
   126       int act = t_action (state_stack [tos], yytoken);
       
   127 
       
   128       if (act == ACCEPT_STATE) {
       
   129         return true;
       
   130       }
       
   131 
       
   132       else if (act > 0)
       
   133         {
       
   134           if (++tos == stack_size)
       
   135             reallocateStack();
       
   136 
       
   137           sym_stack [tos].ival = current_char; // ### save the token value here
       
   138           state_stack [tos] = act;
       
   139           yytoken = -1;
       
   140         }
       
   141 
       
   142       else if (act < 0)
       
   143         {
       
   144           int r = - act - 1;
       
   145 
       
   146           tos -= rhs [r];
       
   147           act = state_stack [tos++];
       
   148           consumeRule (r);
       
   149           state_stack [tos] = nt_action (act, lhs [r] - TERMINAL_COUNT);
       
   150         }
       
   151 
       
   152       else
       
   153         break;
       
   154     }
       
   155 
       
   156     return false;
       
   157 }
       
   158 
       
   159 
       
   160 int Parser::nextToken()
       
   161 {
       
   162     static int tokens[] = { ID, ID, ID, EOF_SYMBOL };
       
   163     static int *tk = tokens;
       
   164 
       
   165     return *tk++;
       
   166 }
       
   167 
       
   168 void Parser::consumeRule(int ruleno)
       
   169 {
       
   170     switch (ruleno) {
       
   171     case Symbol:
       
   172         printf("symbol\n");
       
   173         break;
       
   174     case SubExpression:
       
   175         printf("sub-expr\n");
       
   176         break;
       
   177     case Appl:
       
   178         printf("appl\n");
       
   179         break;
       
   180     case Abstr:
       
   181         printf("abstr\n");
       
   182         break;
       
   183     }
       
   184 }
       
   185 
       
   186 /////////////////////////////
       
   187 // entry point
       
   188 /////////////////////////////
       
   189 int main()
       
   190 {
       
   191     Parser parser;
       
   192 
       
   193     if (parser.parse())
       
   194         printf ("OK\n");
       
   195     else
       
   196         printf ("KO\n");
       
   197 }
       
   198 
       
   199