# HG changeset patch # User mario # Date 1171479421 -3600 # Node ID 0b472e25eb16e6d188d8eaa60c5ddc5b802aa7db # Parent d926ee71f228776c922d6fe5aa2498911e2db895 Adding changes to make the compiler compatible with bison ver 2.3 diff -r d926ee71f228 -r 0b472e25eb16 Makefile --- a/Makefile Mon Feb 12 14:06:59 2007 +0100 +++ b/Makefile Wed Feb 14 19:57:01 2007 +0100 @@ -38,7 +38,7 @@ LIBS = absyntax/absyntax.o absyntax/visitor.o -LIBS += stage1_2/iec.y.o stage1_2/iec.flex.o +LIBS += stage1_2/stage1_2.o stage1_2/iec.y.o stage1_2/iec.flex.o iec2cc: main.o stage4/generate_cc/generate_cc.o stage4/stage4.o $(LIBS) $(CXX) -o iec2cc main.o stage4/stage4.o stage4/generate_cc/generate_cc.o $(LIBS) diff -r d926ee71f228 -r 0b472e25eb16 absyntax/absyntax.cc --- a/absyntax/absyntax.cc Mon Feb 12 14:06:59 2007 +0100 +++ b/absyntax/absyntax.cc Wed Feb 14 19:57:01 2007 +0100 @@ -38,8 +38,6 @@ -symbol_c *tree_root = NULL; - diff -r d926ee71f228 -r 0b472e25eb16 stage1_2/Makefile --- a/stage1_2/Makefile Mon Feb 12 14:06:59 2007 +0100 +++ b/stage1_2/Makefile Wed Feb 14 19:57:01 2007 +0100 @@ -7,7 +7,8 @@ .PHONY: iec.flex -all: iec.flex.o iec.y.o +all: iec.flex.o iec.y.o stage1_2.o + clean: -rm -f *.o Makefile.depend @@ -47,3 +48,11 @@ #how to make things from other directories if they are missing ../% /%: $(MAKE) -C $(@D) $(@F) + + +Makefile.depend depend: + $(CXX) -MM -MG -I. *.cc \ + | perl -pe 's/:/ Makefile.depend:/' > Makefile.depend + +include Makefile.depend + diff -r d926ee71f228 -r 0b472e25eb16 stage1_2/iec.flex --- a/stage1_2/iec.flex Mon Feb 12 14:06:59 2007 +0100 +++ b/stage1_2/iec.flex Wed Feb 14 19:57:01 2007 +0100 @@ -117,11 +117,14 @@ */ #include "../absyntax/absyntax.hh" + /* generated by bison. * Contains the definition of the token constants, and the * token value type YYSTYPE (in our case, a 'const char *') */ #include "iec.y.hh" +#include "stage1_2_priv.hh" + /* Variable defined by the bison parser, * where the value of the tokens will be stored @@ -134,7 +137,11 @@ * apropriately whenever it comes across an (*#include *) * directive... */ +/* + NOTE: already defined in iec.y.hh extern const char *current_filename; +*/ + /* We will not be using unput() in our flex code... */ #define YY_NO_UNPUT @@ -183,19 +190,6 @@ %} -/*********************************************/ -/* Change parse state to body definitions... */ -/*********************************************/ - -%{ -static int goto_body_state__ = 0; - -void cmd_goto_body_state(void) {goto_body_state__ = 1;} -int get_goto_body_state(void) {return goto_body_state__;} -void rst_goto_body_state(void) {goto_body_state__ = 0;} -%} - - /***************************************************/ /* Forward Declaration of functions defined later. */ /***************************************************/ @@ -362,17 +356,6 @@ NULL /* must end with NULL!! */ }; - -/* - * Join two strings together. Allocate space with malloc(3). - */ -static char *strdup2(const char *a, const char *b) { - char *res = (char *)malloc(strlen(a) + strlen(b) + 1); - - if (!res) - return NULL; - return strcat(strcpy(res, a), b); /* safe, actually */ -} %} @@ -1413,12 +1396,12 @@ const char *current_filename; + + int get_identifier_token(const char *identifier_str) {return 0;} - - int main(int argc, char **argv) { FILE *in_file; diff -r d926ee71f228 -r 0b472e25eb16 stage1_2/iec.y --- a/stage1_2/iec.y Mon Feb 12 14:06:59 2007 +0100 +++ b/stage1_2/iec.y Wed Feb 14 19:57:01 2007 +0100 @@ -77,10 +77,9 @@ #include "../absyntax/absyntax.hh" /* file with declaration of token constants. Generated by bison! */ -#include "iec.y.hh" - -/* file with the declarations of symbol tables... */ -#include "../util/symtable.hh" +// #include "iec.y.hh" + +#include "stage1_2_priv.hh" /* an ugly hack!! @@ -110,45 +109,30 @@ extern void error_exit(const char *file_name, int line_no); -/*********************************/ -/* The global symbol tables... */ -/*********************************/ -/* NOTE: declared static because they are not accessed - * directly by the lexical parser (flex), but rather - * through the function get_identifier_token() - */ -/* A symbol table to store all the library elements */ -/* e.g.: - * - * - * - * - */ -static symtable_c library_element_symtable; - -/* A symbol table to store the declared variables of - * the function currently being parsed... - */ -static symtable_c variable_name_symtable; - /*************************/ /* global variables... */ /*************************/ -static symbol_c *tree_root = NULL; - -/* The name of the file currently being parsed... - * Note that flex accesses and updates this global variable - * apropriately whenever it comes across an (*#include *) - * directive... - */ -const char *current_filename = NULL; +/* NOTE: For some strange reason bison ver 2.3 is including these declarations + * in the iec.y.hh file, which is in turn included by flex. + * We cannot therefore define any variables over here, but merely declare + * their existance (otherwise we get errors when linking the code, since we + * would get a new variable defined each time iec.y.hh is included!). + * Even though the variables are declared 'extern' over here, they will in + * fact be defined towards the end of this same file (i.e. in the prologue) + */ /* A global flag used to tell the parser if overloaded funtions should be allowed. * The IEC 61131-3 standard allows overloaded funtions in the standard library, * but disallows them in user code... */ -bool allow_function_overloading = false; +extern bool allow_function_overloading; + +/* A pointer to the root of the parsing tree that will be generated + * by bison. + */ +extern symbol_c *tree_root; + /************************/ @@ -162,20 +146,11 @@ /* print an error message */ void print_err_msg(const char *filename, int lineno, const char *additional_error_msg); - -/************************/ -/* forward declarations */ -/************************/ -/* The functions declared here are defined in iec.flex... */ -void print_include_stack(void); -void cmd_goto_body_state(void); -int get_goto_body_state(void); -void rst_goto_body_state(void); - %} + %glr-parser // %expect-rr 1 @@ -206,37 +181,6 @@ /*****************************/ /* Prelimenary constructs... */ /*****************************/ -%type start - -%type any_identifier - -%token prev_declared_variable_name_token -%token prev_declared_fb_name_token -%type prev_declared_variable_name -%type prev_declared_fb_name - -%token prev_declared_simple_type_name_token -%token prev_declared_subrange_type_name_token -%token prev_declared_enumerated_type_name_token -%token prev_declared_array_type_name_token -%token prev_declared_structure_type_name_token -%token prev_declared_string_type_name_token - -%type prev_declared_simple_type_name -%type prev_declared_subrange_type_name -%type prev_declared_enumerated_type_name -%type prev_declared_array_type_name -%type prev_declared_structure_type_name -%type prev_declared_string_type_name - -%token prev_declared_derived_function_name_token -%token prev_declared_derived_function_block_name_token -%token prev_declared_program_type_name_token -%type prev_declared_derived_function_name -%type prev_declared_derived_function_block_name -%type prev_declared_program_type_name - - /* A bogus token that, in principle, flex MUST NEVER generate */ /* USE 1: * ====== @@ -256,6 +200,47 @@ %token BOGUS_TOKEN_ID + +%{ +/* The interface through which bison and flex interact. */ +/* May only be included after the definition of BOGUS_TOKEN_ID */ +#include "stage1_2_priv.hh" +%} + + +%type start + +%type any_identifier + +%token prev_declared_variable_name_token +%token prev_declared_fb_name_token +%type prev_declared_variable_name +%type prev_declared_fb_name + +%token prev_declared_simple_type_name_token +%token prev_declared_subrange_type_name_token +%token prev_declared_enumerated_type_name_token +%token prev_declared_array_type_name_token +%token prev_declared_structure_type_name_token +%token prev_declared_string_type_name_token + +%type prev_declared_simple_type_name +%type prev_declared_subrange_type_name +%type prev_declared_enumerated_type_name +%type prev_declared_array_type_name +%type prev_declared_structure_type_name +%type prev_declared_string_type_name + +%token prev_declared_derived_function_name_token +%token prev_declared_derived_function_block_name_token +%token prev_declared_program_type_name_token +%type prev_declared_derived_function_name +%type prev_declared_derived_function_block_name +%type prev_declared_program_type_name + + + + /* The pragmas... */ %token pragma_token %type pragma @@ -488,11 +473,11 @@ %token STRING %token BOOL -%token TIME -%token DATE -%token DATE_AND_TIME +// %token TIME +// %token DATE +// %token DATE_AND_TIME %token DT -%token TIME_OF_DAY +// %token TIME_OF_DAY %token TOD @@ -783,10 +768,10 @@ %token FUNCTION_BLOCK %token END_FUNCTION_BLOCK %token VAR_TEMP -%token END_VAR +// %token END_VAR %token VAR -%token NON_RETAIN -%token END_VAR +// %token NON_RETAIN +// %token END_VAR /**********************/ @@ -831,7 +816,7 @@ %type transition_name -%token ASSIGN +// %token ASSIGN %token ACTION %token END_ACTION @@ -942,12 +927,12 @@ %token END_RESOURCE %token VAR_CONFIG %token VAR_ACCESS -%token END_VAR +// %token END_VAR %token WITH -%token PROGRAM -%token RETAIN -%token NON_RETAIN -%token PRIORITY +// %token PROGRAM +// %token RETAIN +// %token NON_RETAIN +// %token PRIORITY %token SINGLE %token INTERVAL %token READ_WRITE @@ -1125,11 +1110,11 @@ /* intermediate helper symbol for primary_expression */ %type function_invocation -%token AND -%token XOR -%token OR -%token MOD -%token NOT +// %token AND +// %token XOR +// %token OR +// %token MOD +// %token NOT %token OPER_NE %token OPER_GE %token OPER_LE @@ -1148,7 +1133,7 @@ /* B 3.2.1 Assignment Statements */ /*********************************/ %type assignment_statement -%token ASSIGN /* ":=" */ +// %token ASSIGN /* ":=" */ /*****************************************/ @@ -1161,8 +1146,8 @@ /* helper symbol for fb_invocation */ %type param_assignment_list -%token ASSIGN -%token SENDTO /* "=>" */ +// %token ASSIGN +// %token SENDTO /* "=>" */ %token RETURN @@ -1189,8 +1174,8 @@ %token END_IF %token CASE -%token OF -%token ELSE +// %token OF +// %token ELSE %token END_CASE @@ -1208,14 +1193,14 @@ // %type for_list %token FOR -%token ASSIGN -%token TO +// %token ASSIGN +// %token TO %token BY %token DO %token END_FOR %token WHILE -%token DO +// %token DO %token END_WHILE %token REPEAT @@ -5178,6 +5163,21 @@ extern FILE *yyin; extern int yylineno; + + +/* A global flag used to tell the parser if overloaded funtions should be allowed. + * The IEC 61131-3 standard allows overloaded funtions in the standard library, + * but disallows them in user code... + */ +bool allow_function_overloading = false; + +/* A pointer to the root of the parsing tree that will be generated + * by bison. + */ +symbol_c *tree_root; + + + /* The following function is called automatically by bison whenever it comes across * an error. Unfortunately it calls this function before executing the code that handles * the error itself, so we cannot print out the correct line numbers of the error location @@ -5200,24 +5200,6 @@ fprintf(stderr, "%s:%d: %s\n", filename, lineno, current_error_msg); } -/* Function only called from within flex! - * - * search for a symbol in either of the two symbol tables - * declared above, and return the token id of the first - * symbol found. - * Searches first in the variables, and only if not found - * does it continue searching in the library elements - */ -int get_identifier_token(const char *identifier_str) { -// std::cout << "get_identifier_token(" << identifier_str << "): \n"; - int token_id; - - if ((token_id = variable_name_symtable.find_value(identifier_str)) == variable_name_symtable.end_value()) - if ((token_id = library_element_symtable.find_value(identifier_str)) == library_element_symtable.end_value()) - return identifier_token; - return token_id; -} - /* convert between an il_operator to a function name */ @@ -5306,31 +5288,6 @@ -/* - * Join two strings together. Allocate space with malloc(3). - */ -static char *strdup2(const char *a, const char *b) { - char *res = (char *)malloc(strlen(a) + strlen(b) + 1); - - if (!res) - return NULL; - return strcat(strcpy(res, a), b); /* safe, actually */ -} - -/* - * Join three strings together. Allocate space with malloc(3). - */ -static char *strdup3(const char *a, const char *b, const char *c) { - char *res = (char *)malloc(strlen(a) + strlen(b) + strlen(c) + 1); - - if (!res) - return NULL; - return strcat(strcat(strcpy(res, a), b), c); /* safe, actually */ -} - - - - const char *standard_function_names[] = { // 2.5.1.5.1 Type conversion functions /* @@ -5410,7 +5367,7 @@ #define LIBFILE "ieclib.txt" #define DEF_LIBFILENAME LIBDIRECTORY "/" LIBFILE -int stage1_2(const char *filename, const char *includedir, symbol_c **tree_root_ref) { +int stage1_2__(const char *filename, const char *includedir, symbol_c **tree_root_ref) { FILE *in_file = NULL, *lib_file = NULL; char *libfilename = NULL; diff -r d926ee71f228 -r 0b472e25eb16 stage1_2/stage1_2.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stage1_2/stage1_2.cc Wed Feb 14 19:57:01 2007 +0100 @@ -0,0 +1,167 @@ +/* + * (c) 2007 Mario de Sousa + * + * Offered to the public under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * This code is made available on the understanding that it will not be + * used in safety-critical situations without a full and competent review. + */ + +/* + * An IEC 61131-3 IL and ST compiler. + * + * Based on the + * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) + * + */ + + +/* + * This file contains the code that calls the stage 1 (lexical anayser) + * and stage 2 (syntax parser) during the first pass. + */ + + + +// #include "stage1_2.hh" +#include "iec.y.hh" +#include "stage1_2_priv.hh" + + + + + +/**************************************/ +/* The name of the file being parsed. */ +/**************************************/ +/* The name of the file currently being parsed... + * Note that flex accesses and updates this global variable + * apropriately whenever it comes across an (*#include *) + * directive... + * ... and bison will use it when producing error messages. + * Note that bison also sets this variable correctly to the first + * file being parsed. + */ +const char *current_filename = NULL; + + + + +/*****************************************************/ +/* Controlling the entry to the st_il_state in flex. */ +/*****************************************************/ + +static int goto_body_state__ = 0; + +void cmd_goto_body_state(void) {goto_body_state__ = 1;} +int get_goto_body_state(void) {return goto_body_state__;} +void rst_goto_body_state(void) {goto_body_state__ = 0;} + + + +/*********************************/ +/* The global symbol tables... */ +/*********************************/ +/* NOTE: only accessed indirectly by the lexical parser (flex) + * through the function get_identifier_token() + */ +/* NOTE: BOGUS_TOKEN_ID is defined in the bison generated file iec.y.hh. + * We need this constant defined before we can declare the symbol tables. + * However, we cannot #include "iec.y.hh" in this file (stage1_2_priv.hh) directly + * because of the way bison ver. 3.2 is copying all declarations in the prologue + * of iec.y to the iec.y.hh file (including an #include stage1_2_priv.hh). + * So, if we were to include "iec.y.hh" here, we would get a circular include. + * All this means that whoever includes this file (stage1_2_priv.hh) will need + * to take care to first inlcude iec.y.hh !! + */ +/* A symbol table to store all the library elements */ +/* e.g.: + * + * + * + * + */ +/* static */ symtable_c library_element_symtable; + +/* A symbol table to store the declared variables of + * the function currently being parsed... + */ +/* static */ symtable_c variable_name_symtable; + + +/* Function only called from within flex! + * + * search for a symbol in either of the two symbol tables + * declared above, and return the token id of the first + * symbol found. + * Searches first in the variables, and only if not found + * does it continue searching in the library elements + */ +int get_identifier_token(const char *identifier_str) { +// std::cout << "get_identifier_token(" << identifier_str << "): \n"; + int token_id; + + if ((token_id = variable_name_symtable.find_value(identifier_str)) == variable_name_symtable.end_value()) + if ((token_id = library_element_symtable.find_value(identifier_str)) == library_element_symtable.end_value()) + return identifier_token; + return token_id; +} + + +/************************/ +/* Utility Functions... */ +/************************/ + +/* + * Join two strings together. Allocate space with malloc(3). + */ +char *strdup2(const char *a, const char *b) { + char *res = (char *)malloc(strlen(a) + strlen(b) + 1); + + if (!res) + return NULL; + return strcat(strcpy(res, a), b); /* safe, actually */ +} + +/* + * Join three strings together. Allocate space with malloc(3). + */ +char *strdup3(const char *a, const char *b, const char *c) { + char *res = (char *)malloc(strlen(a) + strlen(b) + strlen(c) + 1); + + if (!res) + return NULL; + return strcat(strcat(strcpy(res, a), b), c); /* safe, actually */ +} + + + + + + +/***********************************************************************/ +/***********************************************************************/ +/***********************************************************************/ +/***********************************************************************/ +/***********************************************************************/ +/***********************************************************************/ +/***********************************************************************/ +/***********************************************************************/ +/***********************************************************************/ +/***********************************************************************/ + + +int stage1_2__(const char *filename, const char *includedir, symbol_c **tree_root_ref); + + +int stage1_2(const char *filename, const char *includedir, symbol_c **tree_root_ref) { + return stage1_2__(filename, includedir, tree_root_ref); +} + diff -r d926ee71f228 -r 0b472e25eb16 stage1_2/stage1_2_priv.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stage1_2/stage1_2_priv.hh Wed Feb 14 19:57:01 2007 +0100 @@ -0,0 +1,135 @@ +/* + * (c) 2007 Mario de Sousa + * + * Offered to the public under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * This code is made available on the understanding that it will not be + * used in safety-critical situations without a full and competent review. + */ + +/* + * An IEC 61131-3 IL and ST compiler. + * + * Based on the + * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) + * + */ + + +/* + * The private interface to stage1_2.cc + */ + + + +/* file with the declarations of symbol tables... */ +#include "../util/symtable.hh" + + + +/* + * This file includes the interface through which the lexical parser (stage 1 - flex) + * and the syntax analyser (stage 2 - bison) interact between themselves. + * + * This is mostly through direct access to shared global variables, however some + * of the global variables will only be accessed through some accessor functions. + * + * This file also includes some utility functions (strdupX() ) that are both used + * in the lexical and syntax analysers. + */ + + + + + +/*********************************************/ +/* print the include file stack to stderr... */ +/*********************************************/ +/* This is a service that flex provides to bison... */ +void print_include_stack(void); + + + +/**************************************/ +/* The name of the file being parsed. */ +/**************************************/ +/* The name of the file currently being parsed... + * Note that flex accesses and updates this global variable + * apropriately whenever it comes across an (*#include *) + * directive... + * ... and bison will use it when producing error messages. + * Note that bison also sets this variable correctly to the first + * file being parsed. + */ +extern const char *current_filename; + + + +/*****************************************************/ +/* Controlling the entry to the st_il_state in flex. */ +/*****************************************************/ +void cmd_goto_body_state(void); +int get_goto_body_state(void); +void rst_goto_body_state(void); + + +/*********************************/ +/* The global symbol tables... */ +/*********************************/ +/* NOTE: only accessed indirectly by the lexical parser (flex) + * through the function get_identifier_token() + */ +/* NOTE: BOGUS_TOKEN_ID is defined in the bison generated file iec.y.hh. + * We need this constant defined before we can declare the symbol tables. + * However, we cannot #include "iec.y.hh" in this file (stage1_2_priv.hh) directly + * because of the way bison ver. 3.2 is copying all declarations in the prologue + * of iec.y to the iec.y.hh file (including an #include stage1_2_priv.hh). + * So, if we were to include "iec.y.hh" here, we would get a circular include. + * All this means that whoever includes this file (stage1_2_priv.hh) will need + * to take care to first inlcude iec.y.hh !! + */ +/* A symbol table to store all the library elements */ +/* e.g.: + * + * + * + * + */ +extern symtable_c library_element_symtable; + +/* A symbol table to store the declared variables of + * the function currently being parsed... + */ +extern symtable_c variable_name_symtable; + +/* Function only called from within flex! + * + * search for a symbol in either of the two symbol tables + * declared above, and return the token id of the first + * symbol found. + * Searches first in the variables, and only if not found + * does it continue searching in the library elements + */ +int get_identifier_token(const char *identifier_str); + + + +/************************/ +/* Utility Functions... */ +/************************/ + +/* Join two strings together. Allocate space with malloc(3). */ +char *strdup2(const char *a, const char *b); + +/* Join three strings together. Allocate space with malloc(3). */ +char *strdup3(const char *a, const char *b, const char *c); + + +