mario@15: /* mario@15: * (c) 2007 Mario de Sousa mario@15: * mario@15: * Offered to the public under the terms of the GNU General Public License mario@15: * as published by the Free Software Foundation; either version 2 of the mario@15: * License, or (at your option) any later version. mario@15: * mario@15: * This program is distributed in the hope that it will be useful, but mario@15: * WITHOUT ANY WARRANTY; without even the implied warranty of mario@15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General mario@15: * Public License for more details. mario@15: * mario@15: * This code is made available on the understanding that it will not be mario@15: * used in safety-critical situations without a full and competent review. mario@15: */ mario@15: mario@15: /* mario@15: * An IEC 61131-3 IL and ST compiler. mario@15: * mario@15: * Based on the mario@15: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) mario@15: * mario@15: */ mario@15: mario@15: mario@15: /* mario@15: * This file contains the code that calls the stage 1 (lexical anayser) mario@15: * and stage 2 (syntax parser) during the first pass. mario@15: */ mario@15: mario@15: mario@20: /* file with declaration of absyntax classes... */ mario@20: #include "../absyntax/absyntax.hh" mario@20: mario@15: mario@15: // #include "stage1_2.hh" mario@15: #include "iec.y.hh" mario@15: #include "stage1_2_priv.hh" mario@15: mario@15: mario@15: mario@15: mario@15: mario@15: /**************************************/ mario@15: /* The name of the file being parsed. */ mario@15: /**************************************/ mario@15: /* The name of the file currently being parsed... mario@15: * Note that flex accesses and updates this global variable mario@15: * apropriately whenever it comes across an (*#include *) mario@15: * directive... mario@15: * ... and bison will use it when producing error messages. mario@15: * Note that bison also sets this variable correctly to the first mario@15: * file being parsed. mario@15: */ mario@15: const char *current_filename = NULL; mario@15: mario@15: mario@15: mario@15: mario@15: /*****************************************************/ mario@15: /* Controlling the entry to the st_il_state in flex. */ mario@15: /*****************************************************/ mario@15: mario@15: static int goto_body_state__ = 0; mario@15: mario@15: void cmd_goto_body_state(void) {goto_body_state__ = 1;} mario@15: int get_goto_body_state(void) {return goto_body_state__;} mario@15: void rst_goto_body_state(void) {goto_body_state__ = 0;} mario@15: mario@15: mario@15: mario@15: /*********************************/ mario@15: /* The global symbol tables... */ mario@15: /*********************************/ mario@15: /* NOTE: only accessed indirectly by the lexical parser (flex) mario@15: * through the function get_identifier_token() mario@15: */ mario@15: /* NOTE: BOGUS_TOKEN_ID is defined in the bison generated file iec.y.hh. mario@15: * We need this constant defined before we can declare the symbol tables. mario@15: * However, we cannot #include "iec.y.hh" in this file (stage1_2_priv.hh) directly mario@15: * because of the way bison ver. 3.2 is copying all declarations in the prologue mario@15: * of iec.y to the iec.y.hh file (including an #include stage1_2_priv.hh). mario@15: * So, if we were to include "iec.y.hh" here, we would get a circular include. mario@15: * All this means that whoever includes this file (stage1_2_priv.hh) will need mario@15: * to take care to first inlcude iec.y.hh !! mario@15: */ mario@15: /* A symbol table to store all the library elements */ mario@15: /* e.g.: mario@15: * mario@15: * mario@15: * mario@15: * mario@15: */ mario@15: /* static */ symtable_c library_element_symtable; mario@15: mario@15: /* A symbol table to store the declared variables of mario@15: * the function currently being parsed... mario@15: */ mario@15: /* static */ symtable_c variable_name_symtable; mario@15: mario@15: mario@15: /* Function only called from within flex! mario@15: * mario@15: * search for a symbol in either of the two symbol tables mario@15: * declared above, and return the token id of the first mario@15: * symbol found. mario@15: * Searches first in the variables, and only if not found mario@15: * does it continue searching in the library elements mario@15: */ mario@15: int get_identifier_token(const char *identifier_str) { mario@15: // std::cout << "get_identifier_token(" << identifier_str << "): \n"; mario@15: int token_id; mario@15: mario@15: if ((token_id = variable_name_symtable.find_value(identifier_str)) == variable_name_symtable.end_value()) mario@15: if ((token_id = library_element_symtable.find_value(identifier_str)) == library_element_symtable.end_value()) mario@15: return identifier_token; mario@15: return token_id; mario@15: } mario@15: mario@15: mario@15: /************************/ mario@15: /* Utility Functions... */ mario@15: /************************/ mario@15: mario@15: /* mario@15: * Join two strings together. Allocate space with malloc(3). mario@15: */ mario@15: char *strdup2(const char *a, const char *b) { mario@15: char *res = (char *)malloc(strlen(a) + strlen(b) + 1); mario@15: mario@15: if (!res) mario@15: return NULL; mario@15: return strcat(strcpy(res, a), b); /* safe, actually */ mario@15: } mario@15: mario@15: /* mario@15: * Join three strings together. Allocate space with malloc(3). mario@15: */ mario@15: char *strdup3(const char *a, const char *b, const char *c) { mario@15: char *res = (char *)malloc(strlen(a) + strlen(b) + strlen(c) + 1); mario@15: mario@15: if (!res) mario@15: return NULL; mario@15: return strcat(strcat(strcpy(res, a), b), c); /* safe, actually */ mario@15: } mario@15: mario@15: mario@15: mario@15: mario@15: mario@15: mario@15: /***********************************************************************/ mario@15: /***********************************************************************/ mario@15: /***********************************************************************/ mario@15: /***********************************************************************/ mario@15: /***********************************************************************/ mario@15: /***********************************************************************/ mario@15: /***********************************************************************/ mario@15: /***********************************************************************/ mario@15: /***********************************************************************/ mario@15: /***********************************************************************/ mario@15: mario@15: mario@15: int stage1_2__(const char *filename, const char *includedir, symbol_c **tree_root_ref); mario@15: mario@15: mario@15: int stage1_2(const char *filename, const char *includedir, symbol_c **tree_root_ref) { mario@15: return stage1_2__(filename, includedir, tree_root_ref); mario@15: } mario@15: