stage1_2/stage1_2.cc
author Mario de Sousa <msousa@fe.up.pt>
Tue, 05 Apr 2011 14:24:07 +0100
changeset 281 686fa44e3832
parent 264 6facabd9c2cd
child 279 c0453b7f99df
permissions -rwxr-xr-x
fixing exit_statement_c: produce 'break' instead of 'exit(0); (Thanks to Manuele)
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
     1
/*
264
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     3
 *
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     4
 *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     5
 *
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     6
 *  This program is free software: you can redistribute it and/or modify
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     7
 *  it under the terms of the GNU General Public License as published by
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     8
 *  the Free Software Foundation, either version 3 of the License, or
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     9
 *  (at your option) any later version.
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    10
 *
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    11
 *  This program is distributed in the hope that it will be useful,
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    14
 *  GNU General Public License for more details.
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    15
 *
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    16
 *  You should have received a copy of the GNU General Public License
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    17
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    18
 *
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    19
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    20
 * This code is made available on the understanding that it will not be
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    21
 * used in safety-critical situations without a full and competent review.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    22
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    23
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    24
/*
264
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    25
 * An IEC 61131-3 compiler.
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    26
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    27
 * Based on the
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    28
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    29
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    30
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    31
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    32
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    33
/*
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    34
 * This file contains the code that calls the stage 1 (lexical anayser) 
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    35
 * and stage 2 (syntax parser) during the first pass.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    36
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    37
139
668a54686827 added missing includes on some platform (gentoo/gcc-4.3.1)
etisserant
parents: 136
diff changeset
    38
#include <string.h>
668a54686827 added missing includes on some platform (gentoo/gcc-4.3.1)
etisserant
parents: 136
diff changeset
    39
#include <stdlib.h>
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    40
20
81a06a308b7e *** empty log message ***
mario
parents: 15
diff changeset
    41
/* file with declaration of absyntax classes... */
81a06a308b7e *** empty log message ***
mario
parents: 15
diff changeset
    42
#include "../absyntax/absyntax.hh"
81a06a308b7e *** empty log message ***
mario
parents: 15
diff changeset
    43
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    44
177
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    45
#include "stage1_2.hh"
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    46
#include "iec.y.hh"
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    47
#include "stage1_2_priv.hh"
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    48
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    49
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    50
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    51
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    52
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    53
/**************************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    54
/* The name of the file being parsed. */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    55
/**************************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    56
/* The name of the file currently being parsed...
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    57
 * Note that flex accesses and updates this global variable
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    58
 * apropriately whenever it comes across an (*#include <filename> *)
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    59
 * directive...
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    60
 * ... and bison will use it when producing error messages.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    61
 * Note that bison also sets this variable correctly to the first
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    62
 * file being parsed.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    63
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    64
const char *current_filename = NULL;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    65
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    66
177
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    67
/******************************************************/
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    68
/* whether we are suporting safe extensions           */
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    69
/* as defined in PLCopen - Technical Committee 5      */
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    70
/* Safety Software Technical Specification,           */
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    71
/* Part 1: Concepts and Function Blocks,              */
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    72
/* Version 1.0 – Official Release                     */
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    73
/******************************************************/
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    74
bool safe_extensions_ = false;
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    75
bool get_opt_safe_extensions() {return safe_extensions_;}
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    76
68
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    77
/****************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    78
/* Controlling the entry to the body_state in flex. */
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    79
/****************************************************/
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    80
static int goto_body_state__ = 0;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    81
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    82
void cmd_goto_body_state(void) {goto_body_state__ = 1;}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    83
int  get_goto_body_state(void) {return goto_body_state__;}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    84
void rst_goto_body_state(void) {goto_body_state__ = 0;}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    85
68
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    86
/*************************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    87
/* Controlling the entry to the sfc_qualifier_state in flex. */
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    88
/*************************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    89
static int goto_sfc_qualifier_state__ = 0;
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    90
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    91
void cmd_goto_sfc_qualifier_state(void) {goto_sfc_qualifier_state__ = 1;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    92
int  get_goto_sfc_qualifier_state(void) {return goto_sfc_qualifier_state__;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    93
void rst_goto_sfc_qualifier_state(void) {goto_sfc_qualifier_state__ = 0;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    94
74
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
    95
/*************************************************************/
86
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
    96
/* Controlling the entry to the sfc_priority_state in flex.  */
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
    97
/*************************************************************/
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
    98
static int goto_sfc_priority_state__ = 0;
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
    99
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   100
void cmd_goto_sfc_priority_state(void) {goto_sfc_priority_state__ = 1;}
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   101
int  get_goto_sfc_priority_state(void) {return goto_sfc_priority_state__;}
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   102
void rst_goto_sfc_priority_state(void) {goto_sfc_priority_state__ = 0;}
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   103
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   104
/*************************************************************/
74
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   105
/* Controlling the entry to the sfc_qualifier_state in flex. */
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   106
/*************************************************************/
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   107
static int goto_task_init_state__ = 0;
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   108
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   109
void cmd_goto_task_init_state(void) {goto_task_init_state__ = 1;}
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   110
int  get_goto_task_init_state(void) {return goto_task_init_state__;}
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   111
void rst_goto_task_init_state(void) {goto_task_init_state__ = 0;}
68
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   112
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   113
/****************************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   114
/* Returning to state in flex previously pushed onto the stack. */
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   115
/****************************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   116
static int pop_state__ = 0;
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   117
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   118
void cmd_pop_state(void) {pop_state__ = 1;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   119
int  get_pop_state(void) {return pop_state__;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   120
void rst_pop_state(void) {pop_state__ = 0;}
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   121
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   122
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   123
/*********************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   124
/* The global symbol tables...   */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   125
/*********************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   126
/* NOTE: only accessed indirectly by the lexical parser (flex)
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   127
 *       through the function get_identifier_token()
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   128
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   129
/* NOTE: BOGUS_TOKEN_ID is defined in the bison generated file iec.y.hh.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   130
 *       We need this constant defined before we can declare the symbol tables.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   131
 *       However, we cannot #include "iec.y.hh" in this file (stage1_2_priv.hh) directly
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   132
 *       because of the way bison ver. 3.2 is copying all declarations in the prologue
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   133
 *       of iec.y to the iec.y.hh file (including an #include stage1_2_priv.hh).
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   134
 *       So, if we were to include "iec.y.hh" here, we would get a circular include.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   135
 *       All this means that whoever includes this file (stage1_2_priv.hh) will need
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   136
 *       to take care to first inlcude iec.y.hh !!
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   137
 */ 
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   138
/* A symbol table to store all the library elements */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   139
/* e.g.: <function_name , function_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   140
 *       <fb_name , fb_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   141
 *       <type_name , type_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   142
 *       <program_name , program_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   143
 *       <configuration_name , configuration_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   144
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   145
/* static */ symtable_c<int, BOGUS_TOKEN_ID> library_element_symtable;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   146
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   147
/* A symbol table to store the declared variables of
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   148
 * the function currently being parsed...
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   149
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   150
/* static */ symtable_c<int, BOGUS_TOKEN_ID> variable_name_symtable;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   151
175
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   152
/* A symbol table to store the declared direct variables of
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   153
 * the function currently being parsed...
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   154
 */
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   155
/* static */ symtable_c<int, BOGUS_TOKEN_ID> direct_variable_symtable;
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   156
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   157
/* Function only called from within flex!
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   158
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   159
 * search for a symbol in either of the two symbol tables
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   160
 * declared above, and return the token id of the first
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   161
 * symbol found.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   162
 * Searches first in the variables, and only if not found
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   163
 * does it continue searching in the library elements
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   164
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   165
int get_identifier_token(const char *identifier_str) {
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   166
//  std::cout << "get_identifier_token(" << identifier_str << "): \n";
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   167
  int token_id;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   168
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   169
  if ((token_id = variable_name_symtable.find_value(identifier_str)) == variable_name_symtable.end_value())
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   170
    if ((token_id = library_element_symtable.find_value(identifier_str)) == library_element_symtable.end_value())
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   171
      return identifier_token;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   172
  return token_id;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   173
}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   174
175
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   175
/* Function only called from within flex!
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   176
 *
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   177
 * search for a symbol in direct variables symbol table
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   178
 * declared above, and return the token id of the first
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   179
 * symbol found.
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   180
 */
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   181
int get_direct_variable_token(const char *direct_variable_str) {
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   182
  int token_id;
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   183
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   184
  if ((token_id = direct_variable_symtable.find_value(direct_variable_str)) == direct_variable_symtable.end_value())
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   185
    return direct_variable_token;
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   186
  return token_id;
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   187
}
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   188
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   189
/************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   190
/* Utility Functions... */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   191
/************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   192
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   193
/*
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   194
 * Join two strings together. Allocate space with malloc(3).
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   195
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   196
char *strdup2(const char *a, const char *b) {
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   197
  char *res = (char *)malloc(strlen(a) + strlen(b) + 1);
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   198
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   199
  if (!res)
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   200
    return NULL;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   201
  return strcat(strcpy(res, a), b);  /* safe, actually */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   202
}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   203
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   204
/*
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   205
 * Join three strings together. Allocate space with malloc(3).
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   206
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   207
char *strdup3(const char *a, const char *b, const char *c) {
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   208
  char *res = (char *)malloc(strlen(a) + strlen(b) + strlen(c) + 1);
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   209
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   210
  if (!res)
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   211
    return NULL;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   212
  return strcat(strcat(strcpy(res, a), b), c);  /* safe, actually */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   213
}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   214
136
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   215
/*************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   216
/* Tracking Functions... */
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   217
/*************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   218
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   219
extern tracking_t* current_tracking;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   220
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   221
/*--------------------------------------------------------------------
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   222
 * GetNextChar
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   223
 * 
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   224
 * reads a character from input for flex
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   225
 *------------------------------------------------------------------*/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   226
int GetNextChar(char *b, int maxBuffer) {
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   227
  char *p;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   228
  
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   229
  if (  current_tracking->eof  )
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   230
    return 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   231
  
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   232
  while (  current_tracking->currentChar >= current_tracking->lineLength  ) {
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   233
    current_tracking->currentChar = 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   234
    current_tracking->currentTokenStart = 1;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   235
    current_tracking->eof = false;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   236
    
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   237
    p = fgets(current_tracking->buffer, MAX_BUFFER_LENGTH, current_tracking->in_file);
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   238
    if (  p == NULL  ) {
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   239
      if (  ferror(current_tracking->in_file)  )
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   240
        return 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   241
      current_tracking->eof = true;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   242
      return 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   243
    }
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   244
    
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   245
    current_tracking->lineNumber++;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   246
    current_tracking->lineLength = strlen(current_tracking->buffer);
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   247
  }
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   248
  
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   249
  b[0] = current_tracking->buffer[current_tracking->currentChar];
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   250
  if (b[0] == ' ' || b[0] == '\t')
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   251
    current_tracking->currentTokenStart++;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   252
  current_tracking->currentChar++;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   253
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   254
  return b[0]==0?0:1;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   255
}
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   256
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   257
tracking_t* GetNewTracking(FILE* in_file) {
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   258
  tracking_t* new_env = new tracking_t;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   259
  new_env->eof = 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   260
  new_env->lineNumber = 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   261
  new_env->currentChar = 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   262
  new_env->lineLength = 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   263
  new_env->currentTokenStart = 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   264
  new_env->buffer = (char*)malloc(MAX_BUFFER_LENGTH);
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   265
  new_env->in_file = in_file;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   266
  return new_env;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   267
}
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   268
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   269
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   270
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   271
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   272
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   273
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   274
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   275
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   276
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   277
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   278
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   279
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   280
177
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   281
int stage2__(const char *filename, 
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   282
             const char *includedir,     /* Include directory, where included files will be searched for... */
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   283
             symbol_c **tree_root_ref,
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   284
             bool full_token_loc         /* error messages specify full token location */
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   285
            );
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   286
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   287
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   288
int stage1_2(const char *filename, symbol_c **tree_root_ref, stage1_2_options_t options) {
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   289
      /* NOTE: we only call stage2 (bison - syntax analysis) directly, as stage 2 will itself call stage1 (flex - lexical analysis)
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   290
       *       automatically as needed
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   291
       */
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   292
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   293
      /* NOTE: Since we do not call stage1__ (flex) directly, we cannot directly pass any parameters to that function either.
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   294
       *       In this case, we use callback functions, i.e. stage1__ (i.e. flex) will call functions defined in this file
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   295
       *       whenever it needs info/parameters coming from stage1_2().
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   296
       *       These callback functions will get their data from local (to this file) global variables...
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   297
       *       We now set those variables...
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   298
       */
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   299
  safe_extensions_ = options.safe_extensions;
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   300
  
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   301
  return stage2__(filename, options.includedir, tree_root_ref, options.full_token_loc);
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   302
}
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   303