Change generate_cc to generate_c
authorlbessard
Wed, 24 Oct 2007 17:39:51 +0200
changeset 70 e1f0ebd2d9ec
parent 69 41cb5b80416e
child 71 c2c867171c07
Change generate_cc to generate_c
.cvsignore
Makefile
stage4/generate_c/.cvsignore
stage4/generate_c/Makefile
stage4/generate_c/decompose_var_instance_name.cc
stage4/generate_c/function_call_iterator.cc
stage4/generate_c/function_call_param_iterator.cc
stage4/generate_c/function_param_iterator.cc
stage4/generate_c/function_type_decl.h
stage4/generate_c/generate_c.cc
stage4/generate_c/generate_c.hh
stage4/generate_c/generate_c_base.cc
stage4/generate_c/generate_c_configbody.cc
stage4/generate_c/generate_c_il.cc
stage4/generate_c/generate_c_sfc.cc
stage4/generate_c/generate_c_sfcdecl.cc
stage4/generate_c/generate_c_st.cc
stage4/generate_c/generate_c_tempvardecl.cc
stage4/generate_c/generate_c_typedecl.cc
stage4/generate_c/generate_c_vardecl.cc
stage4/generate_c/generate_location_list.cc
stage4/generate_c/get_function_type_decl.c
stage4/generate_c/il_code_gen.c
stage4/generate_c/plc.h
stage4/generate_c/search_base_type.cc
stage4/generate_c/search_constant_type.cc
stage4/generate_c/search_expression_type.cc
stage4/generate_c/search_fb_instance_decl.cc
stage4/generate_c/search_type_code.c
stage4/generate_c/search_var_instance_decl.cc
stage4/generate_c/search_varfb_instance_type.cc
stage4/generate_c/spec_init_separator.cc
stage4/generate_c/st_code_gen.c
stage4/generate_c/type_initial_value.cc
stage4/generate_c/types.h
stage4/generate_cc/.cvsignore
stage4/generate_cc/Makefile
stage4/generate_cc/decompose_var_instance_name.cc
stage4/generate_cc/function_call_iterator.cc
stage4/generate_cc/function_call_param_iterator.cc
stage4/generate_cc/function_param_iterator.cc
stage4/generate_cc/function_type_decl.h
stage4/generate_cc/generate_cc.cc
stage4/generate_cc/generate_cc.hh
stage4/generate_cc/generate_cc_base.cc
stage4/generate_cc/generate_cc_configbody.cc
stage4/generate_cc/generate_cc_il.cc
stage4/generate_cc/generate_cc_sfc.cc
stage4/generate_cc/generate_cc_sfcdecl.cc
stage4/generate_cc/generate_cc_st.cc
stage4/generate_cc/generate_cc_tempvardecl.cc
stage4/generate_cc/generate_cc_typedecl.cc
stage4/generate_cc/generate_cc_vardecl.cc
stage4/generate_cc/generate_location_list.cc
stage4/generate_cc/get_function_type_decl.c
stage4/generate_cc/il_code_gen.c
stage4/generate_cc/plc.h
stage4/generate_cc/search_base_type.cc
stage4/generate_cc/search_constant_type.cc
stage4/generate_cc/search_expression_type.cc
stage4/generate_cc/search_fb_instance_decl.cc
stage4/generate_cc/search_type_code.c
stage4/generate_cc/search_var_instance_decl.cc
stage4/generate_cc/search_varfb_instance_type.cc
stage4/generate_cc/spec_init_separator.cc
stage4/generate_cc/st_code_gen.c
stage4/generate_cc/type_initial_value.cc
stage4/generate_cc/types.h
--- a/.cvsignore	Tue Oct 23 10:35:58 2007 +0200
+++ b/.cvsignore	Wed Oct 24 17:39:51 2007 +0200
@@ -5,3 +5,4 @@
 *.c
 *.h
 iec2cc.exe
+iec2c
--- a/Makefile	Tue Oct 23 10:35:58 2007 +0200
+++ b/Makefile	Wed Oct 24 17:39:51 2007 +0200
@@ -3,25 +3,25 @@
 
 default: all
 
-all: iec2cc iec2iec
+all: iec2c iec2iec
 
 install: all
 	install -d $(INSTALL_PREDIR)/$(INSTALL_BINDIR)/ 
 	install -d $(INSTALL_PREDIR)/$(IECLIBDIR)/
-	install iec2cc    $(INSTALL_PREDIR)/$(INSTALL_BINDIR)/
+	install iec2c    $(INSTALL_PREDIR)/$(INSTALL_BINDIR)/
 	install lib/*.txt $(INSTALL_PREDIR)/$(IECLIBDIR)/
 #	install iec2iec   $(INSTALL_PREDIR)/$(INSTALL_BINDIR)/
 
 
 uninstall:
-	rm -f $(INSTALL_PREDIR)/$(INSTALL_BINDIR)/iec2cc
+	rm -f $(INSTALL_PREDIR)/$(INSTALL_BINDIR)/iec2c
 	rm -f $(INSTALL_PREDIR)/$(INSTALL_BINDIR)/iec2iec
 	for ff in `cd lib; ls *.txt; cd ..` do echo $$ff done
 #	for ff in `cd lib; ls *.txt; cd ..` do rm -f $(INSTALL_PREDIR)/$(IECLIBDIR)/$$ff done
 
 
 clean:
-	-rm -f iec2iec iec2cc *.o absyntax/*.o Makefile.depend
+	-rm -f iec2iec iec2c *.o absyntax/*.o Makefile.depend
 # make something everywhere (ie, in all Makefiles that have that target)
 	find . -depth -mindepth 2 -maxdepth 2 -name Makefile -printf %h\\n | xargs -i make -C{} $@
 
@@ -30,8 +30,8 @@
 LIBS  = absyntax/absyntax.o absyntax/visitor.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)
+iec2c: main.o stage4/generate_c/generate_c.o stage4/stage4.o $(LIBS)
+	$(CXX) -o iec2c main.o stage4/stage4.o stage4/generate_c/generate_c.o $(LIBS)
 
 
 iec2iec: main.o stage4/generate_iec/generate_iec.o stage4/stage4.o $(LIBS)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/.cvsignore	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,2 @@
+Makefile.depend
+test_iec_std_lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/Makefile	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,31 @@
+# include the system specific Makefile
+include ../../Makefile.$(shell uname)
+
+default: all
+
+all: generate_c.o
+
+clean:
+	-rm -f *.o */*.o Makefile.depend
+
+CXXFLAGS += -Wno-unused
+CXXFLAGS += -I. -I../* -I../../absyntax
+
+#how to make things from other directories if they are missing
+../% /%:
+	$(MAKE) -C $(@D) $(@F)
+
+test_iec_std_lib: test_iec_std_lib.c iec_std_lib.h
+	gcc -o test_iec_std_lib test_iec_std_lib.c
+	
+test: test_iec_std_lib
+
+Makefile.depend depend:
+	$(CXX) -MM -MG -I. *.cc \
+	  | perl -pe 's/:/ Makefile.depend:/' > Makefile.depend
+
+include Makefile.depend
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/decompose_var_instance_name.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,132 @@
+/*
+ * (c) 2003 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)
+ *
+ */
+
+
+/* Decomposes a variable instance name into its constituents,
+ * example:
+ *    window.points[1].coordinate.x
+ *
+ *  will succesfully return
+ *        - window
+ *        - points
+ *        - coordinate
+ *        - x
+ * on succesive calls to decompose_var_instance_name_c::next_part()
+ */
+class decompose_var_instance_name_c: null_visitor_c {
+
+  private:
+    symbol_c *variable_name;
+    symbol_c *next_variable_name;
+    symbol_c *current_recursive_variable_name;
+    symbol_c *previously_returned_variable_name;
+
+  public:
+    decompose_var_instance_name_c(symbol_c *variable_instance_name) {
+      variable_name = variable_instance_name;
+      next_variable_name = NULL;
+      current_recursive_variable_name = NULL;
+      previously_returned_variable_name = NULL;
+    }
+
+  public:
+    symbol_c *next_part(void) {
+      /* We must always start from the top!
+       * See note in the structured_variable_c visitor
+       * to understand why...
+       */
+      symbol_c *res = (symbol_c *)variable_name->accept(*this);
+      next_variable_name = current_recursive_variable_name;
+
+      if (previously_returned_variable_name == res)
+        return NULL;
+      previously_returned_variable_name = res;
+      return res;
+    }
+
+  public:
+/*********************/
+/* B 1.4 - Variables */
+/*********************/
+    void *visit(symbolic_variable_c *symbol) {return (void *)(symbol->var_name);}
+
+/********************************************/
+/* B.1.4.1   Directly Represented Variables */
+/********************************************/
+    void *visit(direct_variable_c *symbol) {return (void *)symbol;}
+
+/*************************************/
+/* B.1.4.2   Multi-element Variables */
+/*************************************/
+/*  subscripted_variable '[' subscript_list ']' */
+// SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
+    void *visit(array_variable_c *symbol) {
+      /* NOTE: the subscripted_variable may itself be a structure!,
+       * so we must recursevily visit!
+       */
+      return symbol->subscripted_variable->accept(*this);
+    }
+
+/*  record_variable '.' field_selector */
+/*  WARNING: input and/or output variables of function blocks
+ *           may be accessed as fields of a tructured variable!
+ *           Code handling a structured_variable_c must take
+ *           this into account!
+ */
+//SYM_REF2(structured_variable_c, record_variable, field_selector)
+    void *visit(structured_variable_c *symbol) {
+      /* NOTE: The following code will not work, as structured_variable_c
+       *       are grouped on the left, and not on the right!
+       *
+       *       example: window.origin.x
+       *       will result in
+       *       s1 = structured_variable_c("window, "origin");
+       *       s2 = structured_variable_c(s1, "x");
+       *       AND NOT
+       *       s1 = structured_variable_c("origin", "x");
+       *       s2 = structured_variable_c("window", s1);
+       *
+       *       as the following code assumes!!
+       *
+      current_variable_name = symbol->field_selector;
+      return symbol->record_variable->accept(*this);
+       */
+
+      /* The correct code, is therefore more complex... */
+      if (next_variable_name == symbol) {
+        /* NOTE: field_selector is always an identifier_c,
+	 * so we do not have to recursevily visit it again...
+	 * return (void *)symbol->field_selector->accept(*this);  -> NOT REQUIRED!!
+	 */
+	return (void *)symbol->field_selector;
+      }
+
+      current_recursive_variable_name = symbol;
+      return symbol->record_variable->accept(*this);
+    }
+};
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/function_call_iterator.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,150 @@
+/*
+ * (c) 2003 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)
+ *
+ */
+
+
+/*
+ * Function call parameter iterator.
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+
+//#include <stdio.h>  /* required for NULL */
+//#include <string>
+//#include <iostream>
+
+//#include "../../util/symtable.hh"
+
+//#include "generate_c.hh"
+
+
+
+
+/* given a function_body_c, iterate through each
+ * function in/out/inout parameter, returning the name
+ * of each parameter...
+ */
+class function_call_iterator_c : public iterator_visitor_c {
+  private:
+    symbol_c *start_symbol;
+    int next_fcall, fcall_count;
+    //identifier_c *current_fcall_name;
+    symbol_c *current_fcall_name;
+    //function_invocation_c *current_finvocation;
+    symbol_c *current_finvocation;
+
+  public:
+    /* initialise the iterator object.
+     * We must be given a reference to the function declaration
+     * that will be analysed...
+     */
+    function_call_iterator_c(symbol_c *symbol) {
+      this->start_symbol = symbol;
+      next_fcall = fcall_count = 0;
+      current_finvocation = NULL;
+      current_fcall_name = NULL;
+    }
+
+    /* Skip to the next function call. After object creation,
+     * the object references _before_ the first, so
+     * this function must be called once to get the object to
+     * reference the first function call...
+     *
+     * Returns the function_invocation_c!
+     */
+    //function_invocation_c *next(void) {TRACE("function_call_iterator_c::next(): called ");
+    symbol_c *next(void) {TRACE("function_call_iterator_c::next(): called ");
+      fcall_count = 0;
+      next_fcall++;
+      current_finvocation = NULL;
+      current_fcall_name = NULL;
+
+      start_symbol->accept(*this);
+      return current_finvocation;
+    }
+
+    /* Returns the name of the currently referenced function invocation */
+    identifier_c *fname(void) {
+      identifier_c *identifier = dynamic_cast<identifier_c *>(current_fcall_name);
+      if (identifier == NULL) ERROR;
+      return identifier;
+    }
+
+
+/***************************************/
+/* B.3 - Language ST (Structured Text) */
+/***************************************/
+/***********************/
+/* B 3.1 - Expressions */
+/***********************/
+  void *visit(function_invocation_c *symbol) {
+    fcall_count++;
+    if (next_fcall == fcall_count) {
+      current_finvocation = symbol;
+      current_fcall_name = symbol->function_name;
+    }
+    return NULL;
+  }
+
+
+
+/****************************************/
+/* B.2 - Language IL (Instruction List) */
+/****************************************/
+/***********************************/
+/* B 2.1 Instructions and Operands */
+/***********************************/
+
+/* | function_name [il_operand_list] */
+// SYM_REF2(il_function_call_c, function_name, il_operand_list)
+  void *visit(il_function_call_c *symbol) {
+    fcall_count++;
+    if (next_fcall == fcall_count) {
+      current_finvocation = symbol;
+      current_fcall_name = symbol->function_name;
+    }
+    return NULL;
+  }
+
+
+
+/* | function_name '(' eol_list [il_param_list] ')' */
+// SYM_REF2(il_formal_funct_call_c, function_name, il_param_list)
+  void *visit(il_formal_funct_call_c *symbol) {
+    fcall_count++;
+    if (next_fcall == fcall_count) {
+      current_finvocation = symbol;
+      current_fcall_name = symbol->function_name;
+    }
+    return NULL;
+  }
+
+
+};
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/function_call_param_iterator.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,546 @@
+/*
+ * (c) 2003 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)
+ *
+ */
+
+
+/*
+ * Function call parameter iterator.
+ * It will iterate through the formal parameters of a function call
+ * (i.e. function calls using the foo(<param1>, <param2>, ...) syntax).
+ * and/or search through the non-formal parameters of a function call
+ * (i.e. function calls using the foo(<name1> = <param1>, <name2> = <param2>, ...) syntax).
+ *
+ * Calls to function blocks and programs are also supported.
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+
+
+
+
+
+//#include <stdio.h>  /* required for NULL */
+//#include <string>
+//#include <iostream>
+
+//#include "../../util/symtable.hh"
+
+//#include "generate_c.hh"
+
+
+#include "../../absyntax/visitor.hh"
+
+
+/*
+ * Function call parameter iterator.
+ * It will iterate through the formal parameters of a function call
+ * (i.e. function calls using the foo(<param1>, <param2>, ...) syntax).
+ * and/or search through the non-formal parameters of a function call
+ * (i.e. function calls using the foo(<name1> = <param1>, <name2> = <param2>, ...) syntax).
+ *
+ * Calls to function blocks and programs are also supported.
+ *
+ * Note that calls to next() will only iterate through formal parameters,
+ * and calls to search()  will only serach through non-formal parameters.
+ */
+
+class function_call_param_iterator_c : public null_visitor_c {
+  private:
+      /* a pointer to the function call
+       * (or function block or program call!)
+       */
+    symbol_c *f_call;
+    int next_param, param_count;
+    identifier_c *search_param_name;
+
+    /* Which operation of the class was called...
+     * Search a parameter, or iterate to the next parameter.
+     */
+    typedef enum {iterate_op, search_op} operation_t;
+    operation_t current_operation;
+
+  private:
+    void *search_list(list_c *list) {
+      switch (current_operation) {
+        case iterate_op:
+          for(int i = 0; i < list->n; i++) {
+            void *res = list->elements[i]->accept(*this);
+            if (NULL != res) {
+              /* It went through the handle_parameter_assignment() function,
+               * and is therefore a parameter assignment (<param> = <value>),
+               * and not a simple expression (<value>).
+               */
+              /* we do nothing... */
+            } else {
+              param_count++;
+              if (param_count == next_param) {
+                return list->elements[i];
+              }
+            }
+          }
+          return NULL;
+          break;
+
+        case search_op:
+          for(int i = 0; i < list->n; i++) {
+            void *res = list->elements[i]->accept(*this);
+            if (res != NULL)
+              return res;
+          }
+          return NULL;
+          break;
+      } /* switch */
+      return NULL;
+    }
+
+
+
+    void *handle_parameter_assignment(symbol_c *variable_name, symbol_c *expression) {
+      switch (current_operation) {
+        case iterate_op:
+	        /* UGLY HACK -> this will be detected in the search_list() function */
+          return (void *)this; /* anything, as long as it is not NULL!! */
+          break;
+
+        case search_op:
+          identifier_c *variable_name2 = dynamic_cast<identifier_c *>(variable_name);
+          if (variable_name2 == NULL) ERROR;
+          if (strcasecmp(search_param_name->value, variable_name2->value) == 0)
+            /* FOUND! This is the same parameter!! */
+            return (void *)expression;
+          return NULL;
+          break;
+      }
+
+      ERROR;
+      return NULL;
+    }
+
+
+
+  public:
+    /* start off at the first parameter once again... */
+    void reset(void) {
+      next_param = param_count = 0;
+    }
+
+    /* initialise the iterator object.
+     * We must be given a reference to the function/program/function block call
+     * that will be analysed...
+     */
+    function_call_param_iterator_c(symbol_c *f_call) {
+      /* It is expected that f_call will reference one of the following:
+       *  program_configuration_c
+       *  function_invocation_c
+       *  fb_invocation_c
+       *  il_function_call_c
+       *  il_formal_funct_call_c
+       *  ... (have I missed any?)
+       */
+      this->f_call = f_call;
+      search_param_name = NULL;
+      reset();
+    }
+
+    /* Skip to the next parameter. After object creation,
+     * the object references on parameter _before_ the first, so
+     * this function must be called once to get the object to
+     * reference the first parameter...
+     *
+     * Returns whatever is being passed to the parameter!
+     */
+    symbol_c *next(void) {
+      param_count = 0;
+      next_param++;
+      current_operation = function_call_param_iterator_c::iterate_op;
+      void *res = f_call->accept(*this);
+      return (symbol_c *)res;
+    }
+
+    /* Search for the value passed to the parameter named <param_name>...  */
+    symbol_c *search(symbol_c *param_name) {
+      if (NULL == param_name) ERROR;
+      search_param_name = dynamic_cast<identifier_c *>(param_name);
+      if (NULL == search_param_name) ERROR;
+      current_operation = function_call_param_iterator_c::search_op;
+      void *res = f_call->accept(*this);
+      return (symbol_c *)res;
+    }
+
+
+
+
+  private:
+/********************************/
+/* B 1.7 Configuration elements */
+/********************************/
+
+/*
+CONFIGURATION configuration_name
+   optional_global_var_declarations
+   (resource_declaration_list | single_resource_declaration)
+   optional_access_declarations
+   optional_instance_specific_initializations
+END_CONFIGURATION
+*/
+/*
+SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused)
+*/
+
+/* helper symbol for configuration_declaration */
+/*
+SYM_LIST(resource_declaration_list_c)
+*/
+
+/*
+RESOURCE resource_name ON resource_type_name
+   optional_global_var_declarations
+   single_resource_declaration
+END_RESOURCE
+*/
+/*
+SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration)
+*/
+
+/* task_configuration_list program_configuration_list */
+/*
+SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list)
+*/
+
+/* helper symbol for single_resource_declaration */
+/*
+SYM_LIST(task_configuration_list_c)
+*/
+
+/* helper symbol for single_resource_declaration */
+/*
+SYM_LIST(program_configuration_list_c)
+*/
+
+/* helper symbol for
+ *  - access_path
+ *  - instance_specific_init
+ */
+/*
+SYM_LIST(any_fb_name_list_c)
+*/
+
+/*  [resource_name '.'] global_var_name ['.' structure_element_name] */
+/*
+SYM_REF4(global_var_reference_c, resource_name, global_var_name, structure_element_name, unused)
+*/
+
+/*  prev_declared_program_name '.' symbolic_variable */
+/*
+SYM_REF2(program_output_reference_c, program_name, symbolic_variable)
+*/
+
+/*  TASK task_name task_initialization */
+/*
+SYM_REF2(task_configuration_c, task_name, task_initialization)
+*/
+
+/*  '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */
+/*
+SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused)
+*/
+
+/*  PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */
+// SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused)
+    void *visit(program_configuration_c *symbol) {
+      TRACE("program_configuration_c");
+      return symbol->prog_conf_elements->accept(*this);
+    }
+
+/* prog_conf_elements ',' prog_conf_element */
+// SYM_LIST(prog_conf_elements_c)
+    void *visit(prog_conf_elements_c *symbol) {
+      TRACE("prog_conf_elements_c");
+      return search_list(symbol);
+    }
+
+/*  fb_name WITH task_name */
+/*
+SYM_REF2(fb_task_c, fb_name, task_name)
+*/
+
+/*  any_symbolic_variable ASSIGN prog_data_source */
+// SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source)
+    void *visit(prog_cnxn_assign_c *symbol) {
+      TRACE("prog_cnxn_assign_c");
+
+      /* NOTE: symbolic_variable may be something other than a symbolic_variable_c, but I (Mario)
+       *       do not understand the semantics that should be implmeneted if it is not a
+       *        symbolic_variable, so for the moment we simply give up!
+       */
+      symbolic_variable_c *symb_var = dynamic_cast<symbolic_variable_c *>(symbol->symbolic_variable);
+      if (NULL == symb_var)
+        ERROR;
+
+      return handle_parameter_assignment(symb_var->var_name, symbol->prog_data_source);
+    }
+
+/* any_symbolic_variable SENDTO data_sink */
+// SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, prog_data_source)
+    void *visit(prog_cnxn_sendto_c *symbol) {
+      TRACE("prog_cnxn_sendto_c");
+
+      /* NOTE: symbolic_variable may be something other than a symbolic_variable_c, but I (Mario)
+       *       do not understand the semantics that should be implmeneted if it is not a
+       *        symbolic_variable, so for the moment we simply give up!
+       */
+      symbolic_variable_c *symb_var = dynamic_cast<symbolic_variable_c *>(symbol->symbolic_variable);
+      if (NULL == symb_var)
+        ERROR;
+
+      return handle_parameter_assignment(symb_var->var_name, symbol->data_sink);
+    }
+
+/* VAR_CONFIG instance_specific_init_list END_VAR */
+/*
+SYM_REF2(instance_specific_initializations_c, instance_specific_init_list, unused)
+*/
+
+/* helper symbol for instance_specific_initializations */
+/*
+SYM_LIST(instance_specific_init_list_c)
+*/
+
+/* resource_name '.' program_name '.' {fb_name '.'}
+    ((variable_name [location] ':' located_var_spec_init) | (fb_name ':' fb_initialization))
+*/
+/*
+SYM_REF6(instance_specific_init_c, resource_name, program_name, any_fb_name_list, variable_name, location, initialization)
+*/
+
+/* helper symbol for instance_specific_init */
+/* function_block_type_name ':=' structure_initialization */
+/*
+SYM_REF2(fb_initialization_c, function_block_type_name, structure_initialization)
+*/
+
+
+
+
+
+
+/****************************************/
+/* B.2 - Language IL (Instruction List) */
+/****************************************/
+/***********************************/
+/* B 2.1 Instructions and Operands */
+/***********************************/
+
+/* | function_name [il_operand_list] */
+// SYM_REF2(il_function_call_c, function_name, il_operand_list)
+    void *visit(il_function_call_c *symbol) {
+      TRACE("il_function_call_c");
+      if (NULL != symbol->il_operand_list)
+        return symbol->il_operand_list->accept(*this);
+      return NULL;
+    }
+
+
+/* | function_name '(' eol_list [il_param_list] ')' */
+// SYM_REF2(il_formal_funct_call_c, function_name, il_param_list)
+    void *visit(il_formal_funct_call_c *symbol) {
+      TRACE("il_formal_funct_call_c");
+      if (NULL != symbol->il_param_list)
+        return symbol->il_param_list->accept(*this);
+      return NULL;
+    }
+
+
+/*   il_call_operator prev_declared_fb_name
+ * | il_call_operator prev_declared_fb_name '(' ')'
+ * | il_call_operator prev_declared_fb_name '(' eol_list ')'
+ * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
+ * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
+ */
+// SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list)
+    void *visit(il_fb_call_c *symbol) {
+      TRACE("il_fb_call_c");
+      /* the following should never occur. In reality the syntax parser
+       * will guarantee that they never occur, but it makes it easier to
+       * understand the remaining code :-)
+       */
+      //if ((NULL == symbol->il_operand_list) && (NULL == symbol->il_param_list)) ERROR;
+      //if ((NULL != symbol->il_operand_list) && (NULL != symbol->il_param_list)) ERROR;
+
+      if (NULL != symbol->il_operand_list)
+        return symbol->il_operand_list->accept(*this);
+      if (NULL != symbol->il_param_list)
+        return symbol->il_param_list->accept(*this);
+      return NULL;
+    }
+
+
+
+/* | il_operand_list ',' il_operand */
+// SYM_LIST(il_operand_list_c)
+    void *visit(il_operand_list_c *symbol) {
+      TRACE("il_operand_list_c");
+      return search_list(symbol);
+    }
+
+
+/* | il_initial_param_list il_param_instruction */
+// SYM_LIST(il_param_list_c)
+    void *visit(il_param_list_c *symbol) {
+      TRACE("il_param_list_c");
+      return search_list(symbol);
+    }
+
+/*  il_assign_operator il_operand
+ * | il_assign_operator '(' eol_list simple_instr_list ')'
+ */
+// SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused)
+    void *visit(il_param_assignment_c *symbol) {
+      TRACE("il_param_assignment_c");
+
+      // TODO : We do not yet handle a instruction list passed as parameter !!!
+      // since we do not yet support it, it is best to simply stop than to fail silently...
+      if (NULL != symbol->simple_instr_list) ERROR;
+
+      return handle_parameter_assignment(symbol->il_assign_operator, symbol->il_operand);
+    }
+
+/*  il_assign_out_operator variable */
+// SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable);
+    void *visit(il_param_out_assignment_c *symbol) {
+      TRACE("il_param_out_assignment_c");
+      return handle_parameter_assignment((symbol_c *)symbol->il_assign_out_operator->accept(*this), symbol->variable);
+    }
+
+
+/*******************/
+/* B 2.2 Operators */
+/*******************/
+/*| [NOT] any_identifier SENDTO */
+// SYM_REF2(il_assign_out_operator_c, option, variable_name)
+    void *visit(il_assign_out_operator_c *symbol) {
+      TRACE("il_assign_out_operator_c");
+
+      // TODO : Handle not_param !!!
+      // we do not yet support it, so it is best to simply stop than to fail silently...
+      if (NULL != symbol->option) ERROR;
+
+      return (void *)symbol->variable_name;
+    }
+
+
+
+
+/***************************************/
+/* B.3 - Language ST (Structured Text) */
+/***************************************/
+/***********************/
+/* B 3.1 - Expressions */
+/***********************/
+
+/*
+SYM_REF2(function_invocation_c, function_name, parameter_assignment_list)
+*/
+    void *visit(function_invocation_c *symbol) {
+      TRACE("function_invocation_c");
+      if ((symbol_c *)symbol == f_call && symbol->parameter_assignment_list != NULL)
+        return symbol->parameter_assignment_list->accept(*this);
+      else
+        return NULL;
+    }
+
+
+
+
+/********************/
+/* B 3.2 Statements */
+/********************/
+
+/*********************************/
+/* B 3.2.1 Assignment Statements */
+/*********************************/
+/*
+SYM_REF2(assignment_statement_c, l_exp, r_exp)
+*/
+
+/*****************************************/
+/* B 3.2.2 Subprogram Control Statements */
+/*****************************************/
+/*  RETURN */
+// SYM_REF0(return_statement_c)
+
+
+/* fb_name '(' [param_assignment_list] ')' */
+/* param_assignment_list -> may be NULL ! */
+// SYM_REF2(fb_invocation_c, fb_name, param_assignment_list)
+    void *visit(fb_invocation_c *symbol) {
+      TRACE("fb_invocation_c");
+      if (symbol->param_assignment_list != NULL)
+        return symbol->param_assignment_list->accept(*this);
+      else
+        return NULL;
+    }
+
+/* helper symbol for fb_invocation */
+/* param_assignment_list ',' param_assignment */
+// SYM_LIST(param_assignment_list_c)
+    void *visit(param_assignment_list_c *symbol) {
+      TRACE("param_assignment_list_c");
+      return search_list(symbol);
+    }
+
+/*  variable_name ASSIGN expression */
+// SYM_REF2(input_variable_param_assignment_c, variable_name, expression)
+    void *visit(input_variable_param_assignment_c *symbol) {
+      TRACE("input_variable_param_assignment_c");
+      return handle_parameter_assignment(symbol->variable_name, symbol->expression);
+    }
+
+/* [NOT] variable_name '=>' variable */
+// SYM_REF4(output_variable_param_assignment_c, not_param, variable_name, variable, unused)
+    void *visit(output_variable_param_assignment_c *symbol) {
+      TRACE("output_variable_param_assignment_c");
+      // TODO : Handle not_param !!!
+      if (NULL != symbol->not_param) ERROR;  // we do not yet support it, so it is best to simply stop than to fail silently...
+
+      return handle_parameter_assignment(symbol->variable_name, symbol->variable);
+    }
+
+/* helper CLASS for output_variable_param_assignment */
+// SYM_REF0(not_paramassign_c)
+// TODO... ???
+
+
+
+
+
+};
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/function_param_iterator.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,426 @@
+/*
+ * (c) 2003 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)
+ *
+ */
+
+
+/*
+ * Function parameter iterator.
+ * Iterate through the in/out parameters of a function declaration.
+ * Function blocks are also suported.
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+
+
+
+
+
+//#include <stdio.h>  /* required for NULL */
+//#include <string>
+//#include <iostream>
+
+//#include "../../util/symtable.hh"
+
+//#include "generate_c.hh"
+
+
+#include "../../absyntax/visitor.hh"
+
+
+/* given a function_declaration_c, iterate through each
+ * function in/out/inout parameter, returning the name
+ * of each parameter...function_param_iterator_c
+ */
+class function_param_iterator_c : public null_visitor_c {
+  public:
+    /* A type to specify the type of parameter.
+     * VAR_INPUT => direction_in
+     * VAR_OUTPUT => direction_out
+     * VAR_IN_OUT => direction_inout
+     * VAR_EXTERNAL => direction_extref
+     *
+     * Note that VAR_EXTERNAL declares variables that are in reality references
+     * to global variables. This is used only inside programs!
+     * These references to external variables must be correctly initialised to refer
+     * to the correct global variable. Note that the user may define which variable is to be
+     * referenced in a CONFIGURATION, and that different instantiations of a program
+     * may have the same external variable reference diffenrent global variables!
+     * The references must therefore be correctly initialised when the program instance
+     * is created. This may be done by the PROGRAM class constructor since the ST and IL
+     * languages do not allow the VAR_EXTERNAL reference to change at runtime
+     * for a specific instance.
+     *
+     * We therefore need to call a PROGRAM class constructor with the variables
+     * that should be refernced by the VAR_EXTERNAL variables. The direction_extref will
+     * be used to identify these parameters!
+     */
+    typedef enum {direction_in, direction_out, direction_inout, direction_extref} param_direction_t ;
+
+
+  private:
+      /* a pointer to the function_block_declaration_c
+       * or function_declaration_c currently being analysed.
+       */
+    symbol_c *f_decl;
+    int next_param, param_count;
+    identifier_c *current_param_name;
+    symbol_c *current_param_type;
+    symbol_c *current_param_default_value;
+    param_direction_t current_param_direction;
+
+  private:
+    void* handle_param_list(list_c *list) {
+      if (next_param <= param_count + list->n)
+        return list->elements[next_param - param_count - 1];
+
+      /* the desired param is not on this list... */
+      param_count += list->n;
+     return NULL;
+    }
+
+    void* handle_single_param(symbol_c *var_name) {
+      param_count++;
+      if (next_param == param_count)
+        return var_name;
+
+      /* not yet the desired param... */
+     return NULL;
+    }
+
+    void* iterate_list(list_c *list) {
+      void *res;
+      for (int i = 0; i < list->n; i++) {
+        res = list->elements[i]->accept(*this);
+	if (res != NULL)
+	  return res;
+      }
+      return NULL;
+   }
+
+
+  public:
+    /* start off at the first parameter once again... */
+    void reset(void) {
+      next_param = param_count = 0;
+      current_param_name = NULL;
+      current_param_type = current_param_default_value = NULL;
+    }
+
+    /* initialise the iterator object.
+     * We must be given a reference to the function declaration
+     * that will be analysed...
+     */
+    function_param_iterator_c(function_declaration_c *f_decl) {
+      this->f_decl = f_decl;
+      reset();
+    }
+
+    /* initialise the iterator object.
+     * We must be given a reference to the function block declaration
+     * that will be analysed...
+     */
+    function_param_iterator_c(function_block_declaration_c *fb_decl) {
+      this->f_decl = fb_decl;
+      reset();
+    }
+
+    /* initialise the iterator object.
+     * We must be given a reference to the program declaration
+     * that will be analysed...
+     */
+    function_param_iterator_c(program_declaration_c *p_decl) {
+      this->f_decl = p_decl;
+      reset();
+    }
+
+    /* Skip to the next parameter. After object creation,
+     * the object references on parameter _before_ the first, so
+     * this function must be called once to get the object to
+     * reference the first parameter...
+     *
+     * Returns the parameter's name!
+     */
+    identifier_c *next(void) {
+      void *res;
+      param_count = 0;
+      next_param++;
+      res = f_decl->accept(*this);
+      if (res == NULL)
+        return NULL;
+
+      symbol_c *sym = (symbol_c *)res;
+      identifier_c *identifier = dynamic_cast<identifier_c *>(sym);
+      if (identifier == NULL)
+        ERROR;
+
+      current_param_name = identifier;
+      return current_param_name;
+    }
+
+    /* Returns the currently referenced parameter's default value,
+     * or NULL if none is specified in the function declrataion itself.
+     */
+    symbol_c *default_value(void) {
+      return current_param_default_value;
+    }
+
+    /* Returns the currently referenced parameter's type name. */
+    symbol_c *param_type(void) {
+      return current_param_type;
+    }
+
+    /* Returns the currently referenced parameter's data passing direction.
+     * i.e. VAR_INPUT, VAR_OUTPUT or VAR_INOUT
+     */
+    param_direction_t param_direction(void) {
+      return current_param_direction;
+    }
+
+/******************************************/
+/* B 1.4.3 - Declaration & Initialisation */
+/******************************************/
+    void *visit(input_declarations_c *symbol) {
+      TRACE("input_declarations_c");
+      current_param_direction = direction_in;
+      return symbol->input_declaration_list->accept(*this);
+    }
+    void *visit(input_declaration_list_c *symbol) {TRACE("input_declaration_list_c"); return iterate_list(symbol);}
+    void *visit(edge_declaration_c *symbol) {TRACE("edge_declaration_c"); return symbol->var1_list->accept(*this);}
+
+#if 0
+/* var1_list ':' array_spec_init */
+SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)
+
+/*  var1_list ':' initialized_structure */
+SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
+
+/* name_list ':' function_block_type_name ASSIGN structure_initialization */
+/* structure_initialization -> may be NULL ! */
+SYM_REF4(fb_name_decl_c, fb_name_list, function_block_type_name, structure_initialization, unused)
+
+/* name_list ',' fb_name */
+SYM_LIST(fb_name_list_c)
+#endif
+
+    void *visit(output_declarations_c *symbol) {
+      TRACE("output_declarations_c");
+      current_param_direction = direction_out;
+      return symbol->var_init_decl_list->accept(*this);
+    }
+    void *visit(input_output_declarations_c *symbol) {
+      TRACE("input_output_declarations_c");
+      current_param_direction = direction_inout;
+      return symbol->var_declaration_list->accept(*this);
+    }
+    void *visit(var_declaration_list_c *symbol) {TRACE("var_declaration_list_c"); return iterate_list(symbol);}
+
+#if 0
+/*  var1_list ':' array_specification */
+SYM_REF2(array_var_declaration_c, var1_list, array_specification)
+
+/*  var1_list ':' structure_type_name */
+SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
+#endif
+
+/* VAR [CONSTANT] var_init_decl_list END_VAR */
+    void *visit(var_declarations_c *symbol) {TRACE("var_declarations_c"); return NULL;}
+
+#if 0
+/*  VAR RETAIN var_init_decl_list END_VAR */
+SYM_REF2(retentive_var_declarations_c, var_init_decl_list, unused)
+
+/*  VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */
+/* option -> may be NULL ! */
+SYM_REF2(located_var_declarations_c, option, located_var_decl_list)
+
+/* helper symbol for located_var_declarations */
+/* located_var_decl_list located_var_decl ';' */
+SYM_LIST(located_var_decl_list_c)
+
+/*  [variable_name] location ':' located_var_spec_init */
+/* variable_name -> may be NULL ! */
+SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused)
+#endif
+
+/*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */
+/* option -> may be NULL ! */
+// SYM_REF2(external_var_declarations_c, option, external_declaration_list)
+    void *visit(external_var_declarations_c *symbol) {
+      TRACE("external_var_declarations_c");
+      current_param_direction = direction_extref;
+      return symbol->external_declaration_list->accept(*this);
+    }
+
+/* helper symbol for external_var_declarations */
+/*| external_declaration_list external_declaration';' */
+// SYM_LIST(external_declaration_list_c)
+    void *visit(external_declaration_list_c *symbol) {TRACE("external_declaration_list_c"); return iterate_list(symbol);}
+
+/*  global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */
+//SYM_REF2(external_declaration_c, global_var_name, specification)
+    void *visit(external_declaration_c *symbol) {
+      TRACE("external_declaration_c");
+      /* It is OK to store these values in the current_param_XXX
+       * variables, because if the desired parameter is not in the
+       * variable list we will be analysing, the current_param_XXXX
+       * variables will get overwritten when we visit the next
+       * var1_init_decl_c list!
+       */
+      current_param_default_value = spec_init_sperator_c::get_init(symbol->specification);
+      current_param_type = spec_init_sperator_c::get_spec(symbol->specification);
+
+      return handle_single_param(symbol->global_var_name);
+    }
+
+
+#if 0
+/*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */
+/* option -> may be NULL ! */
+SYM_REF2(global_var_declarations_c, option, global_var_decl_list)
+
+/* helper symbol for global_var_declarations */
+/*| global_var_decl_list global_var_decl ';' */
+SYM_LIST(global_var_decl_list_c)
+
+/*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */
+/* type_specification ->may be NULL ! */
+SYM_REF2(global_var_decl_c, global_var_spec, type_specification)
+
+/*| global_var_name location */
+SYM_REF2(global_var_spec_c, global_var_name, location)
+
+/*  AT direct_variable */
+SYM_REF2(location_c, direct_variable, unused)
+
+/*| global_var_list ',' global_var_name */
+SYM_LIST(global_var_list_c)
+
+/*  var1_list ':' single_byte_string_spec */
+SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec)
+
+/*  STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */
+/* integer ->may be NULL ! */
+/* single_byte_character_string ->may be NULL ! */
+SYM_REF2(single_byte_string_spec_c, integer, single_byte_character_string)
+
+/*  var1_list ':' double_byte_string_spec */
+SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec)
+
+/*  WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */
+/* integer ->may be NULL ! */
+/* double_byte_character_string ->may be NULL ! */
+SYM_REF2(double_byte_string_spec_c, integer, double_byte_character_string)
+
+/*| VAR [RETAIN|NON_RETAIN] incompl_located_var_decl_list END_VAR */
+/* option ->may be NULL ! */
+SYM_REF2(incompl_located_var_declarations_c, option, incompl_located_var_decl_list)
+
+/* helper symbol for incompl_located_var_declarations */
+/*| incompl_located_var_decl_list incompl_located_var_decl ';' */
+SYM_LIST(incompl_located_var_decl_list_c)
+
+/*  variable_name incompl_location ':' var_spec */
+SYM_REF4(incompl_located_var_decl_c, variable_name, incompl_location, var_spec, unused)
+
+/*  AT incompl_location_token */
+SYM_TOKEN(incompl_location_c)
+#endif
+
+
+    void *visit(var1_init_decl_c *symbol) {
+      TRACE("var1_init_decl_c");
+      /* It is OK to store these values in the current_param_XXX
+       * variables, because if the desired parameter is not in the
+       * variable list we will be analysing, the current_param_XXXX
+       * variables will get overwritten when we visit the next
+       * var1_init_decl_c list!
+       */
+      current_param_default_value = spec_init_sperator_c::get_init(symbol->spec_init);
+      current_param_type = spec_init_sperator_c::get_spec(symbol->spec_init);
+
+      return symbol->var1_list->accept(*this);
+    }
+
+
+
+    void *visit(var1_list_c *symbol) {
+      TRACE("var1_list_c");
+      return handle_param_list(symbol);
+    }
+
+    void *visit(var_init_decl_list_c *symbol) {TRACE("var_init_decl_list_c"); return iterate_list(symbol);}
+
+
+/***********************/
+/* B 1.5.1 - Functions */
+/***********************/
+    void *visit(function_declaration_c *symbol) {TRACE("function_declaration_c"); return symbol->var_declarations_list->accept(*this);}
+    /* intermediate helper symbol for function_declaration */
+    void *visit(var_declarations_list_c *symbol) {TRACE("var_declarations_list_c"); return iterate_list(symbol);}
+    void *visit(function_var_decls_c *symbol) {TRACE("function_var_decls_c"); /* ignore */ return NULL;}
+
+
+/*****************************/
+/* B 1.5.2 - Function Blocks */
+/*****************************/
+/*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
+    void *visit(function_block_declaration_c *symbol) {TRACE("function_block_declaration_c"); return symbol->var_declarations->accept(*this);}
+
+/* intermediate helper symbol for function_declaration */
+/*  { io_var_declarations | other_var_declarations }   */
+/*
+ * NOTE: we re-use the var_declarations_list_c
+ */
+
+/*  VAR_TEMP temp_var_decl_list END_VAR */
+    void *visit(temp_var_decls_c *symbol) {TRACE("temp_var_decls_c"); /* ignore */ return NULL;}
+    void *visit(temp_var_decls_list_c *symbol) {TRACE("temp_var_decls_list_c"); /* ignore */ return NULL;}
+
+/*  VAR NON_RETAIN var_init_decl_list END_VAR */
+    void *visit(non_retentive_var_decls_c *symbol) {TRACE("non_retentive_var_decls_c"); /* ignore */ return NULL;}
+
+
+/**********************/
+/* B 1.5.3 - Programs */
+/**********************/
+/*  PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */
+// SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused)
+    void *visit(program_declaration_c *symbol) {TRACE("program_declaration_c"); return symbol->var_declarations->accept(*this);}
+
+/* intermediate helper symbol for program_declaration_c */
+/*  { io_var_declarations | other_var_declarations }   */
+/*
+ * NOTE: we re-use the var_declarations_list_c
+ */
+
+};
+
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/function_type_decl.h	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,453 @@
+/*
+ * (c) 2003 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)
+ *
+ */
+ 
+ 
+/****
+ * IEC 61131-3 standard function lib
+ * generated code, do not edit by hand
+ */
+typedef enum {
+    function_bool_to_sint,
+    function_bool_to_int,
+    function_bool_to_dint,
+    function_bool_to_lint,
+    function_bool_to_usint,
+    function_bool_to_uint,
+    function_bool_to_udint,
+    function_bool_to_ulint,
+    function_bool_to_real,
+    function_bool_to_lreal,
+    function_bool_to_time,
+    function_bool_to_date,
+    function_bool_to_tod,
+    function_bool_to_dt,
+    function_bool_to_string,
+    function_bool_to_byte,
+    function_bool_to_word,
+    function_bool_to_dword,
+    function_bool_to_lword,
+    function_sint_to_bool,
+    function_sint_to_int,
+    function_sint_to_dint,
+    function_sint_to_lint,
+    function_sint_to_usint,
+    function_sint_to_uint,
+    function_sint_to_udint,
+    function_sint_to_ulint,
+    function_sint_to_real,
+    function_sint_to_lreal,
+    function_sint_to_time,
+    function_sint_to_date,
+    function_sint_to_tod,
+    function_sint_to_dt,
+    function_sint_to_string,
+    function_sint_to_byte,
+    function_sint_to_word,
+    function_sint_to_dword,
+    function_sint_to_lword,
+    function_int_to_bool,
+    function_int_to_sint,
+    function_int_to_dint,
+    function_int_to_lint,
+    function_int_to_usint,
+    function_int_to_uint,
+    function_int_to_udint,
+    function_int_to_ulint,
+    function_int_to_real,
+    function_int_to_lreal,
+    function_int_to_time,
+    function_int_to_date,
+    function_int_to_tod,
+    function_int_to_dt,
+    function_int_to_string,
+    function_int_to_byte,
+    function_int_to_word,
+    function_int_to_dword,
+    function_int_to_lword,
+    function_dint_to_bool,
+    function_dint_to_sint,
+    function_dint_to_int,
+    function_dint_to_lint,
+    function_dint_to_usint,
+    function_dint_to_uint,
+    function_dint_to_udint,
+    function_dint_to_ulint,
+    function_dint_to_real,
+    function_dint_to_lreal,
+    function_dint_to_time,
+    function_dint_to_date,
+    function_dint_to_tod,
+    function_dint_to_dt,
+    function_dint_to_string,
+    function_dint_to_byte,
+    function_dint_to_word,
+    function_dint_to_dword,
+    function_dint_to_lword,
+    function_lint_to_bool,
+    function_lint_to_sint,
+    function_lint_to_int,
+    function_lint_to_dint,
+    function_lint_to_usint,
+    function_lint_to_uint,
+    function_lint_to_udint,
+    function_lint_to_ulint,
+    function_lint_to_real,
+    function_lint_to_lreal,
+    function_lint_to_time,
+    function_lint_to_date,
+    function_lint_to_tod,
+    function_lint_to_dt,
+    function_lint_to_string,
+    function_lint_to_byte,
+    function_lint_to_word,
+    function_lint_to_dword,
+    function_lint_to_lword,
+    function_usint_to_bool,
+    function_usint_to_sint,
+    function_usint_to_int,
+    function_usint_to_dint,
+    function_usint_to_lint,
+    function_usint_to_uint,
+    function_usint_to_udint,
+    function_usint_to_ulint,
+    function_usint_to_real,
+    function_usint_to_lreal,
+    function_usint_to_time,
+    function_usint_to_date,
+    function_usint_to_tod,
+    function_usint_to_dt,
+    function_usint_to_string,
+    function_usint_to_byte,
+    function_usint_to_word,
+    function_usint_to_dword,
+    function_usint_to_lword,
+    function_uint_to_bool,
+    function_uint_to_sint,
+    function_uint_to_int,
+    function_uint_to_dint,
+    function_uint_to_lint,
+    function_uint_to_usint,
+    function_uint_to_udint,
+    function_uint_to_ulint,
+    function_uint_to_real,
+    function_uint_to_lreal,
+    function_uint_to_time,
+    function_uint_to_date,
+    function_uint_to_tod,
+    function_uint_to_dt,
+    function_uint_to_string,
+    function_uint_to_byte,
+    function_uint_to_word,
+    function_uint_to_dword,
+    function_uint_to_lword,
+    function_udint_to_bool,
+    function_udint_to_sint,
+    function_udint_to_int,
+    function_udint_to_dint,
+    function_udint_to_lint,
+    function_udint_to_usint,
+    function_udint_to_uint,
+    function_udint_to_ulint,
+    function_udint_to_real,
+    function_udint_to_lreal,
+    function_udint_to_time,
+    function_udint_to_date,
+    function_udint_to_tod,
+    function_udint_to_dt,
+    function_udint_to_string,
+    function_udint_to_byte,
+    function_udint_to_word,
+    function_udint_to_dword,
+    function_udint_to_lword,
+    function_ulint_to_bool,
+    function_ulint_to_sint,
+    function_ulint_to_int,
+    function_ulint_to_dint,
+    function_ulint_to_lint,
+    function_ulint_to_usint,
+    function_ulint_to_uint,
+    function_ulint_to_udint,
+    function_ulint_to_real,
+    function_ulint_to_lreal,
+    function_ulint_to_time,
+    function_ulint_to_date,
+    function_ulint_to_tod,
+    function_ulint_to_dt,
+    function_ulint_to_string,
+    function_ulint_to_byte,
+    function_ulint_to_word,
+    function_ulint_to_dword,
+    function_ulint_to_lword,
+    function_real_to_bool,
+    function_real_to_sint,
+    function_real_to_int,
+    function_real_to_dint,
+    function_real_to_lint,
+    function_real_to_usint,
+    function_real_to_uint,
+    function_real_to_udint,
+    function_real_to_ulint,
+    function_real_to_lreal,
+    function_real_to_time,
+    function_real_to_date,
+    function_real_to_tod,
+    function_real_to_dt,
+    function_real_to_string,
+    function_real_to_byte,
+    function_real_to_word,
+    function_real_to_dword,
+    function_real_to_lword,
+    function_lreal_to_bool,
+    function_lreal_to_sint,
+    function_lreal_to_int,
+    function_lreal_to_dint,
+    function_lreal_to_lint,
+    function_lreal_to_usint,
+    function_lreal_to_uint,
+    function_lreal_to_udint,
+    function_lreal_to_ulint,
+    function_lreal_to_real,
+    function_lreal_to_time,
+    function_lreal_to_date,
+    function_lreal_to_tod,
+    function_lreal_to_dt,
+    function_lreal_to_string,
+    function_lreal_to_byte,
+    function_lreal_to_word,
+    function_lreal_to_dword,
+    function_lreal_to_lword,
+    function_time_to_sint,
+    function_time_to_int,
+    function_time_to_dint,
+    function_time_to_lint,
+    function_time_to_usint,
+    function_time_to_uint,
+    function_time_to_udint,
+    function_time_to_ulint,
+    function_time_to_real,
+    function_time_to_lreal,
+    function_time_to_string,
+    function_time_to_byte,
+    function_time_to_word,
+    function_time_to_dword,
+    function_time_to_lword,
+    function_date_to_sint,
+    function_date_to_int,
+    function_date_to_dint,
+    function_date_to_lint,
+    function_date_to_usint,
+    function_date_to_uint,
+    function_date_to_udint,
+    function_date_to_ulint,
+    function_date_to_real,
+    function_date_to_lreal,
+    function_date_to_string,
+    function_date_to_byte,
+    function_date_to_word,
+    function_date_to_dword,
+    function_date_to_lword,
+    function_tod_to_sint,
+    function_tod_to_int,
+    function_tod_to_dint,
+    function_tod_to_lint,
+    function_tod_to_usint,
+    function_tod_to_uint,
+    function_tod_to_udint,
+    function_tod_to_ulint,
+    function_tod_to_real,
+    function_tod_to_lreal,
+    function_tod_to_string,
+    function_tod_to_byte,
+    function_tod_to_word,
+    function_tod_to_dword,
+    function_tod_to_lword,
+    function_dt_to_sint,
+    function_dt_to_int,
+    function_dt_to_dint,
+    function_dt_to_lint,
+    function_dt_to_usint,
+    function_dt_to_uint,
+    function_dt_to_udint,
+    function_dt_to_ulint,
+    function_dt_to_real,
+    function_dt_to_lreal,
+    function_dt_to_string,
+    function_dt_to_byte,
+    function_dt_to_word,
+    function_dt_to_dword,
+    function_dt_to_lword,
+    function_string_to_bool,
+    function_string_to_sint,
+    function_string_to_int,
+    function_string_to_dint,
+    function_string_to_lint,
+    function_string_to_usint,
+    function_string_to_uint,
+    function_string_to_udint,
+    function_string_to_ulint,
+    function_string_to_real,
+    function_string_to_lreal,
+    function_string_to_time,
+    function_string_to_date,
+    function_string_to_tod,
+    function_string_to_dt,
+    function_string_to_byte,
+    function_string_to_word,
+    function_string_to_dword,
+    function_string_to_lword,
+    function_byte_to_bool,
+    function_byte_to_sint,
+    function_byte_to_int,
+    function_byte_to_dint,
+    function_byte_to_lint,
+    function_byte_to_usint,
+    function_byte_to_uint,
+    function_byte_to_udint,
+    function_byte_to_ulint,
+    function_byte_to_real,
+    function_byte_to_lreal,
+    function_byte_to_time,
+    function_byte_to_date,
+    function_byte_to_tod,
+    function_byte_to_dt,
+    function_byte_to_string,
+    function_byte_to_word,
+    function_byte_to_dword,
+    function_byte_to_lword,
+    function_word_to_bool,
+    function_word_to_sint,
+    function_word_to_int,
+    function_word_to_dint,
+    function_word_to_lint,
+    function_word_to_usint,
+    function_word_to_uint,
+    function_word_to_udint,
+    function_word_to_ulint,
+    function_word_to_real,
+    function_word_to_lreal,
+    function_word_to_time,
+    function_word_to_date,
+    function_word_to_tod,
+    function_word_to_dt,
+    function_word_to_string,
+    function_word_to_byte,
+    function_word_to_dword,
+    function_word_to_lword,
+    function_dword_to_bool,
+    function_dword_to_sint,
+    function_dword_to_int,
+    function_dword_to_dint,
+    function_dword_to_lint,
+    function_dword_to_usint,
+    function_dword_to_uint,
+    function_dword_to_udint,
+    function_dword_to_ulint,
+    function_dword_to_real,
+    function_dword_to_lreal,
+    function_dword_to_time,
+    function_dword_to_date,
+    function_dword_to_tod,
+    function_dword_to_dt,
+    function_dword_to_string,
+    function_dword_to_byte,
+    function_dword_to_word,
+    function_dword_to_lword,
+    function_lword_to_bool,
+    function_lword_to_sint,
+    function_lword_to_int,
+    function_lword_to_dint,
+    function_lword_to_lint,
+    function_lword_to_usint,
+    function_lword_to_uint,
+    function_lword_to_udint,
+    function_lword_to_ulint,
+    function_lword_to_real,
+    function_lword_to_lreal,
+    function_lword_to_time,
+    function_lword_to_date,
+    function_lword_to_tod,
+    function_lword_to_dt,
+    function_lword_to_string,
+    function_lword_to_byte,
+    function_lword_to_word,
+    function_lword_to_dword,
+    function_trunc,
+    function_bcd_to_usint,
+    function_bcd_to_uint,
+    function_bcd_to_udint,
+    function_bcd_to_ulint,
+    function_usint_to_bcd,
+    function_uint_to_bcd,
+    function_udint_to_bcd,
+    function_ulint_to_bcd,
+    function_date_and_time_to_time_of_day,
+    function_date_and_time_to_date,
+    function_abs,
+    function_sqrt,
+    function_ln,
+    function_log,
+    function_exp,
+    function_sin,
+    function_cos,
+    function_tan,
+    function_asin,
+    function_acos,
+    function_atan,
+    function_add,
+    function_mul,
+    function_sub,
+    function_div,
+    function_mod,
+    function_expt,
+    function_move,
+    function_shl,
+    function_shr,
+    function_ror,
+    function_rol,
+    function_and,
+    function_or,
+    function_xor,
+    function_not,
+    function_sel,
+    function_max,
+    function_min,
+    function_limit,
+    function_mux,
+    function_gt,
+    function_ge,
+    function_eq,
+    function_lt,
+    function_le,
+    function_ne,
+    function_len,
+    function_left,
+    function_right,
+    function_mid,
+    function_concat,
+    function_insert,
+    function_delete,
+    function_replace,
+    function_find,
+    function_none
+} function_type_t;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/generate_c.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,1573 @@
+/*
+ * (c) 2003 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 is one of the versions available for the 4th stage.
+ *
+ * This 4th stage generates a c++ source program equivalent
+ * to the IL and ST code.
+ */
+
+
+
+
+
+
+// #include <stdio.h>  /* required for NULL */
+#include <string>
+#include <iostream>
+#include <sstream>
+#include <typeinfo>
+
+#include "../../util/symtable.hh"
+#include "../../util/dsymtable.hh"
+#include "../../absyntax/visitor.hh"
+
+#include "../stage4.hh"
+
+
+
+
+
+
+
+
+//#define DEBUG
+#ifdef DEBUG
+#define TRACE(classname) printf("\n____%s____\n",classname);
+#else
+#define TRACE(classname)
+#endif
+
+
+
+#define ERROR error_exit(__FILE__,__LINE__)
+/* function defined in main.cc */
+extern void error_exit(const char *file_name, int line_no);
+
+
+
+
+
+
+
+/* A symbol table with all globally declared functions... */
+function_declaration_c null_symbol1(NULL,NULL,NULL,NULL);
+dsymtable_c<function_declaration_c *, &null_symbol1> function_symtable;
+
+/* A symbol table with all globally declared functions block types... */
+function_block_declaration_c null_symbol2(NULL,NULL,NULL);
+symtable_c<function_block_declaration_c *, &null_symbol2> function_block_type_symtable;
+
+/* A symbol table with all globally declared program types... */
+program_declaration_c null_symbol3(NULL,NULL,NULL);
+symtable_c<program_declaration_c *, &null_symbol3> program_type_symtable;
+
+/* A symbol table with all user declared type definitions... */
+/* Note that function block types and program types have their
+ * own symbol tables, so do not get placed in this symbol table!
+ */
+symbol_c null_symbol4;
+symtable_c<symbol_c *, &null_symbol4> type_symtable;
+
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+
+/* returns 0 if the names are equal!! */
+/* NOTE: it must ignore case!! */
+static int compare_identifiers(symbol_c *ident1, symbol_c *ident2) {
+
+  token_c *name1 = dynamic_cast<token_c *>(ident1);
+  token_c *name2 = dynamic_cast<token_c *>(ident2);
+  
+  if ((name1 == NULL) || (name2 == NULL))
+    /* invalid identifiers... */
+    return -1;
+
+  if (strcasecmp(name1->value, name2->value) == 0)
+    return 0;
+
+  /* identifiers do not match! */
+  return 1;
+}
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+/* Unlike Programs and Configurations which get mapped onto C++ classes,
+ * Function Blocks are mapped onto a C structure containing the variables, and
+ * a C function containing the code in the FB's body. This is to allow direct allocation
+ * of a FB variable (which is really an instance of the C data structure) to
+ * a member of a union variable (note that classes with constructors cannot
+ * be mebers of a union), which is done in IL when loading a FB onto IL's
+ * default variable.
+ *
+ * So as not to clash the names of the C data structure and the C function,
+ * the C structure is given a name identical to that of the FB name, whereas
+ * the name of the function is the FB name with a constant string appended.
+ * The value of that constant string which is appended is defined in the following
+ * constant.
+ * In order not to clash with any variable in the IL and ST source codem the
+ * following constant should contain a double underscore, which is not allowed
+ * in IL and ST.
+ *
+ * e.g.:  FUNTION_BLOCK TEST
+ * is mapped onto a TEST data structure, and a TEST_body__ function.
+ */
+
+#define FB_FUNCTION_SUFFIX "_body__"
+
+/* Idem as body, but for initializer FB function */
+#define FB_INIT_SUFFIX "_init__"
+
+/* Idem as body, but for run CONFIG and RESOURCE function */
+#define FB_RUN_SUFFIX "_run__"
+
+/* The FB body function is passed as the only parameter a pointer to the FB data
+ * structure instance. The name of this parameter is given by the following constant.
+ * In order not to clash with any variable in the IL and ST source codem the
+ * following constant should contain a double underscore, which is not allowed
+ * in IL and ST.
+ *
+ * e.g.: the body of FUNTION_BLOCK TEST
+ * is mapped onto the C function
+ *  TEST_body__(TEST *data__)
+ */
+
+#define FB_FUNCTION_PARAM "data__"
+
+
+#define SFC_STEP_ACTION_PREFIX "__SFC_"
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+
+#include "spec_init_separator.cc"
+#include "function_param_iterator.cc"
+#include "function_call_iterator.cc"
+#include "function_call_param_iterator.cc"
+#include "type_initial_value.cc"
+#include "search_fb_instance_decl.cc"
+#include "search_base_type.cc"
+#include "search_var_instance_decl.cc"
+#include "decompose_var_instance_name.cc"
+#include "search_varfb_instance_type.cc"
+#include "search_constant_type.cc"
+#include "search_expression_type.cc"
+
+#include "generate_c_base.cc"
+#include "generate_c_typedecl.cc"
+#include "generate_c_sfcdecl.cc"
+#include "generate_c_vardecl.cc"
+#include "generate_c_configbody.cc"
+#include "generate_location_list.cc"
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+/* Generate a name for a temporary variable.
+ * Each new name generated is appended a different number,
+ * starting off from 0.
+ * After calling reset(), the names will start off again from 0.
+ */
+#define VAR_LEADER "__"
+#define TEMP_VAR VAR_LEADER "TMP_"
+#define SOURCE_VAR VAR_LEADER "SRC_"
+
+#include "generate_c_tempvardecl.cc"
+
+#include "generate_c_st.cc"
+#include "generate_c_il.cc"
+
+#include "generate_c.hh"
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+#define MILLISECOND 1000000
+#define SECOND 1000 * MILLISECOND
+
+/* A helper class that knows how to generate code for both the IL and ST languages... */
+class calculate_time_c: public iterator_visitor_c {
+  private:
+    unsigned long time;
+    float current_value;
+  
+  public:
+    calculate_time_c(void){time = 0;};
+    
+    unsigned long get_time(void) {return time;};
+
+    void *get_integer_value(token_c *token) {
+      std::string str = "";
+      for (unsigned int i = 0; i < strlen(token->value); i++)
+        if (token->value[i] != '_')
+          str += token->value[i];
+      current_value = atof(str.c_str());
+      return NULL;
+    }
+
+    void *get_float_value(token_c *token) {
+      current_value = atof(token->value);
+      return NULL;
+    }
+
+/******************************/
+/* B 1.2.1 - Numeric Literals */
+/******************************/
+
+    void *visit(integer_c *symbol) {return get_integer_value(symbol);}
+    
+/************************/
+/* B 1.2.3.1 - Duration */
+/************************/
+  
+    /* SYM_REF2(duration_c, neg, interval) */
+    void *visit(duration_c *symbol) {
+      if (symbol->neg != NULL)
+        ERROR;
+      symbol->interval->accept(*this);
+      return NULL;
+    }
+    
+    /* SYM_TOKEN(fixed_point_c) */
+    void *visit(fixed_point_c *symbol) {return get_float_value(symbol);}
+    
+    /* SYM_REF2(days_c, days, hours) */
+    void *visit(days_c *symbol) {
+      if (symbol->hours)
+        symbol->hours->accept(*this);
+      symbol->days->accept(*this);
+      time += (unsigned long)(current_value * 24 * 3600 * SECOND);
+      return NULL;
+    }
+    
+    /* SYM_REF2(hours_c, hours, minutes) */
+    void *visit(hours_c *symbol) {
+      if (symbol->minutes)
+        symbol->minutes->accept(*this);
+      symbol->hours->accept(*this);
+      time += (unsigned long)(current_value * 3600 * SECOND);
+      return NULL;
+    }
+    
+    /* SYM_REF2(minutes_c, minutes, seconds) */
+    void *visit(minutes_c *symbol) {
+      if (symbol->seconds)
+        symbol->seconds->accept(*this);
+      symbol->minutes->accept(*this);
+      time += (unsigned long)(current_value * 60 * SECOND);
+      return NULL;
+    }
+    
+    /* SYM_REF2(seconds_c, seconds, milliseconds) */
+    void *visit(seconds_c *symbol) {
+      if (symbol->milliseconds)
+        symbol->milliseconds->accept(*this);
+      symbol->seconds->accept(*this);
+      time += (unsigned long)(current_value * SECOND);
+      return NULL;
+    }
+    
+    /* SYM_REF2(milliseconds_c, milliseconds, unused) */
+    void *visit(milliseconds_c *symbol) {
+      symbol->milliseconds->accept(*this);
+      time += (unsigned long)(current_value * MILLISECOND);
+      return NULL;
+    }
+};
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+class calculate_common_ticktime_c: public iterator_visitor_c {
+  private:
+    unsigned long common_ticktime;
+    
+  public:
+    calculate_common_ticktime_c(void){common_ticktime = 0;}
+    
+    unsigned long euclide(unsigned long a, unsigned long b) {
+      unsigned long c = a % b;
+      if (c == 0)
+        return b;
+      else
+        return euclide(b, c);
+    }
+    
+    void update_ticktime(unsigned long time) {
+      if (common_ticktime == 0)
+        common_ticktime = time;
+      else if (time > common_ticktime)
+        common_ticktime = euclide(time, common_ticktime);
+      else
+        common_ticktime = euclide(common_ticktime, time);
+    }
+
+    unsigned long get_ticktime(void) {
+      return common_ticktime;
+    }
+
+/*  TASK task_name task_initialization */
+//SYM_REF2(task_configuration_c, task_name, task_initialization)  
+    void *visit(task_initialization_c *symbol) {
+      calculate_time_c calculate_time;
+      unsigned long time = 0;
+      if (symbol->interval_data_source != NULL) {
+        symbol->interval_data_source->accept(calculate_time);
+        time = calculate_time.get_time();
+      }
+      if (time > 0)
+        update_ticktime(time);
+      return NULL;
+    }
+};    
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+/* A helper class that knows how to generate code for both the IL and ST languages... */
+class generate_c_SFC_IL_ST_c: public null_visitor_c {
+  private:
+    stage4out_c *s4o_ptr;
+    symbol_c *scope;
+    const char *variable_prefix;
+
+  public:
+    generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL);
+    /*********************************************/
+    /* B.1.6  Sequential function chart elements */
+    /*********************************************/
+    
+    /*| sequential_function_chart sfc_network*/
+    void *visit(sequential_function_chart_c * symbol);
+    
+    /****************************************/
+    /* B.2 - Language IL (Instruction List) */
+    /****************************************/
+    
+    /***********************************/
+    /* B 2.1 Instructions and Operands */
+    /***********************************/
+    /*| instruction_list il_instruction */
+    void *visit(instruction_list_c *symbol);
+    
+    /* Remainder implemented in generate_c_il_c... */
+    
+    /***************************************/
+    /* B.3 - Language ST (Structured Text) */
+    /***************************************/
+    /***********************/
+    /* B 3.1 - Expressions */
+    /***********************/
+    /* Implemented in generate_c_st_c */
+    
+    /********************/
+    /* B 3.2 Statements */
+    /********************/
+    void *visit(statement_list_c *symbol);
+
+/* Remainder implemented in generate_c_st_c... */
+};
+
+#include "generate_c_sfc.cc"
+
+generate_c_SFC_IL_ST_c::generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix) {
+  if (NULL == scope) ERROR;
+  this->s4o_ptr = s4o_ptr;
+  this->scope = scope;
+  this->variable_prefix = variable_prefix;
+}
+
+void *generate_c_SFC_IL_ST_c::visit(sequential_function_chart_c * symbol) {
+  generate_c_sfc_c generate_c_sfc(s4o_ptr, scope, variable_prefix);
+  generate_c_sfc.generate(symbol);
+  return NULL;
+}
+
+void *generate_c_SFC_IL_ST_c::visit(instruction_list_c *symbol) {
+  generate_c_il_c generate_c_il(s4o_ptr, scope, variable_prefix);
+  generate_c_il.generate(symbol);
+  return NULL;
+}
+
+void *generate_c_SFC_IL_ST_c::visit(statement_list_c *symbol) {
+  generate_c_st_c generate_c_st(s4o_ptr, scope, variable_prefix);
+  generate_c_st.generate(symbol);
+  return NULL;
+}
+
+
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+
+class generate_c_pous_c: public generate_c_typedecl_c {
+
+  public:
+    generate_c_pous_c(stage4out_c *s4o_ptr)
+      : generate_c_typedecl_c(s4o_ptr) {};
+    virtual ~generate_c_pous_c(void) {}
+
+
+  public:
+
+/*************************/
+/* B.1 - Common elements */
+/*************************/
+/*******************************************/
+/* B 1.1 - Letters, digits and identifiers */
+/*******************************************/
+  /* done in base class(es) */
+
+/*********************/
+/* B 1.2 - Constants */
+/*********************/
+  /* originally empty... */
+
+/******************************/
+/* B 1.2.1 - Numeric Literals */
+/******************************/
+  /* done in base class(es) */
+
+/*******************************/
+/* B.1.2.2   Character Strings */
+/*******************************/
+  /* done in base class(es) */
+
+/***************************/
+/* B 1.2.3 - Time Literals */
+/***************************/
+/************************/
+/* B 1.2.3.1 - Duration */
+/************************/
+  /* done in base class(es) */
+
+/************************************/
+/* B 1.2.3.2 - Time of day and Date */
+/************************************/
+  /* done in base class(es) */
+
+/**********************/
+/* B.1.3 - Data types */
+/**********************/
+/***********************************/
+/* B 1.3.1 - Elementary Data Types */
+/***********************************/
+  /* done in base class(es) */
+
+/********************************/
+/* B.1.3.2 - Generic data types */
+/********************************/
+  /* originally empty... */
+
+/********************************/
+/* B 1.3.3 - Derived data types */
+/********************************/
+  /* done in base class(es) */
+
+/*********************/
+/* B 1.4 - Variables */
+/*********************/
+  /* done in base class(es) */
+
+/********************************************/
+/* B.1.4.1   Directly Represented Variables */
+/********************************************/
+  /* done in base class(es) */
+
+/*************************************/
+/* B.1.4.2   Multi-element Variables */
+/*************************************/
+  /* done in base class(es) */
+
+/******************************************/
+/* B 1.4.3 - Declaration & Initialisation */
+/******************************************/
+  /* done in base class(es) */
+
+/**************************************/
+/* B.1.5 - Program organization units */
+/**************************************/
+/***********************/
+/* B 1.5.1 - Functions */
+/***********************/
+
+public:
+/*   FUNCTION derived_function_name ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */
+/* | FUNCTION derived_function_name ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */
+void *visit(function_declaration_c *symbol) {
+  generate_c_vardecl_c *vardecl;
+  TRACE("function_declaration_c");
+
+  /* start off by adding this declaration to the global
+   * function declaration symbol table...
+   */
+  function_symtable.insert(symbol->derived_function_name, symbol);
+
+  /* (A) Function declaration... */
+  /* (A.1) Function return type */
+  s4o.print("// FUNCTION\n");
+  symbol->type_name->accept(*this); /* return type */
+  s4o.print(" ");
+  /* (A.2) Function name */
+  symbol->derived_function_name->accept(*this);
+  s4o.print("(");
+
+  /* (A.3) Function parameters */
+  s4o.indent_right();
+  vardecl = new generate_c_vardecl_c(&s4o,
+  				      generate_c_vardecl_c::finterface_vf,
+  				      generate_c_vardecl_c::input_vt |
+				      generate_c_vardecl_c::output_vt |
+				      generate_c_vardecl_c::inoutput_vt);
+  vardecl->print(symbol->var_declarations_list);
+  delete vardecl;
+  s4o.indent_left();
+
+  s4o.print(")\n" + s4o.indent_spaces + "{\n");
+
+  /* (B) Function local variable declaration */
+    /* (B.1) Variables declared in ST source code */
+  s4o.indent_right();
+  vardecl = new generate_c_vardecl_c(&s4o, generate_c_vardecl_c::localinit_vf, generate_c_vardecl_c::private_vt);
+  vardecl->print(symbol->var_declarations_list);
+  delete vardecl;
+
+    /* (B.2) Temporary variable for function's return value */
+  /* It will have the same name as the function itself! */
+  s4o.print(s4o.indent_spaces);
+  symbol->type_name->accept(*this); /* return type */
+  s4o.print(" ");
+  symbol->derived_function_name->accept(*this);
+  s4o.print(" = ");
+  {
+    /* get the default value of this variable's type */
+    symbol_c *default_value = (symbol_c *)symbol->type_name->accept(*type_initial_value_c::instance());
+    if (default_value == NULL) ERROR;
+    default_value->accept(*this);
+  }
+  s4o.print(";\n\n");
+
+  /* (C) Function body */
+  generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol);
+  symbol->function_body->accept(generate_c_code);
+  s4o.print(s4o.indent_spaces + "return ");
+  symbol->derived_function_name->accept(*this);
+  s4o.print(";\n");
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces + "}\n\n\n");
+
+  return NULL;
+}
+
+
+/* The remaining var_declarations_list_c, function_var_decls_c
+ * and var2_init_decl_list_c are handled in the generate_c_vardecl_c class
+ */
+
+
+/*****************************/
+/* B 1.5.2 - Function Blocks */
+/*****************************/
+public:
+/*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
+//SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused)
+void *visit(function_block_declaration_c *symbol) {
+  generate_c_vardecl_c *vardecl;
+  generate_c_sfcdecl_c *sfcdecl;
+  TRACE("function_block_declaration_c");
+
+  /* start off by adding this declaration to the global
+   * function block declaration symbol table...
+   */
+  function_block_type_symtable.insert(symbol->fblock_name, symbol);
+  
+  /* (A) Function Block data structure declaration... */
+  /* (A.1) Data structure declaration */
+  s4o.print("// FUNCTION_BLOCK ");
+  symbol->fblock_name->accept(*this);
+  s4o.print("\n// Data part\n");
+  s4o.print("typedef struct {\n");
+  s4o.indent_right();
+  /* (A.2) Public variables: i.e. the function parameters... */
+  s4o.print(s4o.indent_spaces + "// FB Interface - IN, OUT, IN_OUT variables\n");
+  vardecl = new generate_c_vardecl_c(&s4o,
+  				      generate_c_vardecl_c::local_vf,
+  				      generate_c_vardecl_c::input_vt |
+  				      generate_c_vardecl_c::output_vt |
+  				      generate_c_vardecl_c::inoutput_vt);
+  vardecl->print(symbol->var_declarations);
+  delete vardecl;
+  s4o.print("\n");
+  /* (A.3) Private internal variables */
+  s4o.print(s4o.indent_spaces + "// FB private variables - TEMP, private and located variables\n");
+  vardecl = new generate_c_vardecl_c(&s4o,
+  				      generate_c_vardecl_c::local_vf,
+				        generate_c_vardecl_c::temp_vt |
+  				      generate_c_vardecl_c::private_vt |
+  				      generate_c_vardecl_c::located_vt |
+  				      generate_c_vardecl_c::external_vt);
+  vardecl->print(symbol->var_declarations);
+  delete vardecl;
+  /* (A.4) Generate private internal variables for SFC */
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::sfcdecl_sd);
+  sfcdecl->print(symbol->fblock_body);
+  delete sfcdecl;
+  s4o.print("\n");
+
+  /* (A.5) Function Block data structure type name. */
+  s4o.indent_left();
+  s4o.print("} ");
+  symbol->fblock_name->accept(*this);
+  s4o.print(";\n\n");
+
+
+  /* (B) Constructor */
+  /* (B.1) Constructor name... */
+  s4o.print(s4o.indent_spaces + "void ");
+  symbol->fblock_name->accept(*this);
+  s4o.print(FB_INIT_SUFFIX);
+  s4o.print("(");
+
+  /* first and only parameter is a pointer to the data */
+  symbol->fblock_name->accept(*this);
+  s4o.print(" *");
+  s4o.print(FB_FUNCTION_PARAM);
+  s4o.print(") {\n");
+  s4o.indent_right();
+
+  /* (B.2) Member initializations... */
+  s4o.print(s4o.indent_spaces);
+  vardecl = new generate_c_vardecl_c(&s4o,
+  				      generate_c_vardecl_c::constructorinit_vf,
+  				      generate_c_vardecl_c::input_vt |
+  				      generate_c_vardecl_c::output_vt |
+  				      generate_c_vardecl_c::inoutput_vt |
+  				      generate_c_vardecl_c::private_vt |
+				        generate_c_vardecl_c::located_vt |
+				        generate_c_vardecl_c::external_vt);
+  vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->");
+  delete vardecl;
+  s4o.print("\n");
+  /* (B.3) Generate private internal variables for SFC */
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::sfcinit_sd);
+  sfcdecl->print(symbol->fblock_body, FB_FUNCTION_PARAM"->");
+  delete sfcdecl;
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces + "}\n\n");
+
+  
+  /* (C) Function with FB body */
+  /* (C.1) Step definitions */
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepdef_sd);
+  sfcdecl->print(symbol->fblock_body);
+  delete sfcdecl;
+  
+  /* (C.2) Action definitions */
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actiondef_sd);
+  sfcdecl->print(symbol->fblock_body);
+  delete sfcdecl;
+  
+  /* (C.3) Function declaration */
+  s4o.print("// Code part\n");
+  /* function interface */
+  s4o.print("void ");
+  symbol->fblock_name->accept(*this);
+  s4o.print(FB_FUNCTION_SUFFIX);
+  s4o.print("(");
+  /* first and only parameter is a pointer to the data */
+  symbol->fblock_name->accept(*this);
+  s4o.print(" *");
+  s4o.print(FB_FUNCTION_PARAM);
+  s4o.print(") {\n");
+  s4o.indent_right();
+
+  /* (C.4) Initialize TEMP variables */
+  /* function body */
+  s4o.print(s4o.indent_spaces + "// Initialise TEMP variables\n");
+  vardecl = new generate_c_vardecl_c(&s4o,
+  				      generate_c_vardecl_c::init_vf,
+				      generate_c_vardecl_c::temp_vt);
+  vardecl->print(symbol->var_declarations, NULL,  FB_FUNCTION_PARAM"->");
+  delete vardecl;
+  s4o.print("\n");
+
+  /* (C.5) Function code */
+  generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol, FB_FUNCTION_PARAM"->");
+  symbol->fblock_body->accept(generate_c_code);
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces + "} // ");
+  symbol->fblock_name->accept(*this);
+  s4o.print(FB_FUNCTION_SUFFIX);
+  s4o.print(s4o.indent_spaces + "() \n\n");
+
+  /* (C.6) Step undefinitions */
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepundef_sd);
+  sfcdecl->print(symbol->fblock_body);
+  delete sfcdecl;
+  
+  /* (C.7) Action undefinitions */
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actionundef_sd);
+  sfcdecl->print(symbol->fblock_body);
+  delete sfcdecl;
+
+  s4o.indent_left();
+  s4o.print("\n\n\n\n");
+
+  return NULL;
+}
+
+
+/* The remaining temp_var_decls_c, temp_var_decls_list_c
+ * and non_retentive_var_decls_c are handled in the generate_c_vardecl_c class
+ */
+
+
+/**********************/
+/* B 1.5.3 - Programs */
+/**********************/
+
+
+
+public:
+/*  PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */
+//SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused)
+void *visit(program_declaration_c *symbol) {
+  generate_c_vardecl_c *vardecl;
+  generate_c_sfcdecl_c *sfcdecl;
+  TRACE("program_declaration_c");
+
+  /* start off by adding this declaration to the global
+   * program declaration symbol table...
+   */
+  program_type_symtable.insert(symbol->program_type_name, symbol);
+
+  /* (A) Program data structure declaration... */
+  /* (A.1) Data structure declaration */
+  s4o.print("// PROGRAM ");
+  symbol->program_type_name->accept(*this);
+  s4o.print("\n// Data part\n");
+  s4o.print("typedef struct {\n");
+  s4o.indent_right();
+
+  /* (A.2) Public variables: i.e. the program parameters... */
+  s4o.print(s4o.indent_spaces + "// PROGRAM Interface - IN, OUT, IN_OUT variables\n");
+  vardecl = new generate_c_vardecl_c(&s4o,
+  				      generate_c_vardecl_c::local_vf,
+  				      generate_c_vardecl_c::input_vt |
+  				      generate_c_vardecl_c::output_vt |
+  				      generate_c_vardecl_c::inoutput_vt);
+  vardecl->print(symbol->var_declarations);
+  delete vardecl;
+  s4o.print("\n");
+  /* (A.3) Private internal variables */
+  s4o.print(s4o.indent_spaces + "// PROGRAM private variables - TEMP, private and located variables\n");
+  vardecl = new generate_c_vardecl_c(&s4o,
+                generate_c_vardecl_c::local_vf,
+                generate_c_vardecl_c::temp_vt |
+                generate_c_vardecl_c::private_vt |
+                generate_c_vardecl_c::located_vt |
+                generate_c_vardecl_c::external_vt);
+  vardecl->print(symbol->var_declarations);
+  delete vardecl;
+  /* (A.4) Generate private internal variables for SFC */
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::sfcdecl_sd);
+  sfcdecl->print(symbol->function_block_body);
+  delete sfcdecl;
+  
+  /* (A.5) Program data structure type name. */
+  s4o.indent_left();
+  s4o.print("} ");
+  symbol->program_type_name->accept(*this);
+  s4o.print(";\n\n");
+
+  /* (B) Constructor */
+  /* (B.1) Constructor name... */
+  s4o.print(s4o.indent_spaces + "void ");
+  symbol->program_type_name->accept(*this);
+  s4o.print(FB_INIT_SUFFIX);
+  s4o.print("(");
+
+  /* first and only parameter is a pointer to the data */
+  symbol->program_type_name->accept(*this);
+  s4o.print(" *");
+  s4o.print(FB_FUNCTION_PARAM);
+  s4o.print(") {\n");
+  s4o.indent_right();
+
+  /* (B.2) Member initializations... */
+  s4o.print(s4o.indent_spaces);
+  vardecl = new generate_c_vardecl_c(&s4o,
+  				      generate_c_vardecl_c::constructorinit_vf,
+  				      generate_c_vardecl_c::input_vt |
+  				      generate_c_vardecl_c::output_vt |
+  				      generate_c_vardecl_c::inoutput_vt |
+  				      generate_c_vardecl_c::private_vt |
+  				      generate_c_vardecl_c::located_vt |
+				      generate_c_vardecl_c::external_vt);
+  vardecl->print(symbol->var_declarations, NULL,  FB_FUNCTION_PARAM"->");
+  delete vardecl;
+  s4o.print("\n");
+  /* (B.3) Generate private internal variables for SFC */
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::sfcinit_sd);
+  sfcdecl->print(symbol->function_block_body,FB_FUNCTION_PARAM"->");
+  delete sfcdecl;
+
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces + "}\n\n");
+
+  /* (C) Function with PROGRAM body */
+  /* (C.1) Step definitions */
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepdef_sd);
+  sfcdecl->print(symbol->function_block_body);
+  delete sfcdecl;
+  
+  /* (C.2) Action definitions */
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actiondef_sd);
+  sfcdecl->print(symbol->function_block_body);
+  delete sfcdecl;
+
+  /* (C.3) Function declaration */
+  s4o.print("// Code part\n");
+  /* function interface */
+  s4o.print("void ");
+  symbol->program_type_name->accept(*this);
+  s4o.print(FB_FUNCTION_SUFFIX);
+  s4o.print("(");
+  /* first and only parameter is a pointer to the data */
+  symbol->program_type_name->accept(*this);
+  s4o.print(" *");
+  s4o.print(FB_FUNCTION_PARAM);
+  s4o.print(") {\n");
+  s4o.indent_right();
+
+  /* (C.4) Initialize TEMP variables */
+  /* function body */
+  s4o.print(s4o.indent_spaces + "// Initialise TEMP variables\n");
+  vardecl = new generate_c_vardecl_c(&s4o,
+  				      generate_c_vardecl_c::init_vf,
+				      generate_c_vardecl_c::temp_vt);
+  vardecl->print(symbol->var_declarations, NULL,  FB_FUNCTION_PARAM"->");
+  delete vardecl;
+  s4o.print("\n");
+
+  /* (C.5) Function code */
+  generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol, FB_FUNCTION_PARAM"->");
+  symbol->function_block_body->accept(generate_c_code);
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces + "} // ");
+  symbol->program_type_name->accept(*this);
+  s4o.print(FB_FUNCTION_SUFFIX);
+  s4o.print(s4o.indent_spaces + "() \n\n");
+
+  /* (C.6) Step undefinitions */
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepundef_sd);
+  sfcdecl->print(symbol->function_block_body);
+  delete sfcdecl;
+  
+  /* (C.7) Action undefinitions */
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actionundef_sd);
+  sfcdecl->print(symbol->function_block_body);
+  delete sfcdecl;
+  
+  s4o.indent_left();
+  s4o.print("\n\n\n\n");
+
+  return NULL;
+}
+
+}; /* generate_c_pous_c */
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+class generate_c_config_c: public generate_c_typedecl_c {
+
+    public:
+    generate_c_config_c(stage4out_c *s4o_ptr)
+      : generate_c_typedecl_c(s4o_ptr) {};
+    virtual ~generate_c_config_c(void) {}
+
+    typedef enum {
+      initprotos_dt,
+      initdeclare_dt,
+      runprotos_dt,
+      rundeclare_dt
+    } declaretype_t;
+
+    declaretype_t wanted_declaretype;
+
+/********************************/
+/* B 1.7 Configuration elements */
+/********************************/
+
+
+public:
+/*
+CONFIGURATION configuration_name
+   optional_global_var_declarations
+   (resource_declaration_list | single_resource_declaration)
+   optional_access_declarations
+   optional_instance_specific_initializations
+END_CONFIGURATION
+*/
+/*
+SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused)
+*/
+void *visit(configuration_declaration_c *symbol) {
+  generate_c_vardecl_c *vardecl;
+  
+  /* Insert the header... */
+  s4o.print("/*******************************************/\n");
+  s4o.print("/*     FILE GENERATED BY iec2c             */\n");
+  s4o.print("/* Editing this file is not recommended... */\n");
+  s4o.print("/*******************************************/\n\n");
+  s4o.print("#include \"iec_std_lib.h\"\n\n");
+  
+  /* (A) configuration declaration... */
+  /* (A.1) configuration name in comment */
+  s4o.print("// CONFIGURATION ");
+  symbol->configuration_name->accept(*this);
+  s4o.print("\n");
+  
+  /* (A.2) Global variables */
+  vardecl = new generate_c_vardecl_c(&s4o,
+  				      generate_c_vardecl_c::local_vf,
+  				      generate_c_vardecl_c::global_vt);
+  vardecl->print(symbol);
+  delete vardecl;
+  s4o.print("\n");
+
+  /* (B) Initialisation Function */
+  /* (B.1) Ressources initialisation protos... */
+  wanted_declaretype = initprotos_dt;
+  symbol->resource_declarations->accept(*this);
+  s4o.print("\n");
+  
+  /* (B.2) Initialisation function name... */
+  s4o.print(s4o.indent_spaces + "void config");
+  s4o.print(FB_INIT_SUFFIX);
+  s4o.print("(void) {\n");
+  s4o.indent_right();
+  
+  /* (B.3) Global variables initializations... */
+  s4o.print(s4o.indent_spaces);
+  vardecl = new generate_c_vardecl_c(&s4o,
+  				      generate_c_vardecl_c::constructorinit_vf,
+  				      generate_c_vardecl_c::global_vt);
+  vardecl->print(symbol);
+  delete vardecl;
+  s4o.print("\n");
+  
+  /* (B.3) Resources initializations... */
+  wanted_declaretype = initdeclare_dt;
+  symbol->resource_declarations->accept(*this);
+  
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces + "}\n\n");
+
+
+  /* (C) Run Function*/
+  /* (C.1) Resources run functions protos... */
+  wanted_declaretype = runprotos_dt;
+  symbol->resource_declarations->accept(*this);
+  s4o.print("\n");
+
+  /* (C.2) Run function name... */
+  s4o.print(s4o.indent_spaces + "void config");
+  s4o.print(FB_RUN_SUFFIX);
+  s4o.print("(int tick) {\n");
+  s4o.indent_right();
+
+  /* (C.3) Resources initializations... */
+  wanted_declaretype = rundeclare_dt;
+  symbol->resource_declarations->accept(*this);
+
+  /* (C.3) Close Public Function body */
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces + "}\n");
+
+  return NULL;
+}
+
+void *visit(resource_declaration_c *symbol) {
+  if (wanted_declaretype == initprotos_dt || wanted_declaretype == runprotos_dt) {
+    s4o.print(s4o.indent_spaces + "void ");
+    symbol->resource_name->accept(*this);
+    if (wanted_declaretype == initprotos_dt) {
+      s4o.print(FB_INIT_SUFFIX);
+      s4o.print("(void);\n");
+    }
+    else {
+      s4o.print(FB_RUN_SUFFIX);
+      s4o.print("(int tick);\n");
+    }
+  }
+  if (wanted_declaretype == initdeclare_dt || wanted_declaretype == rundeclare_dt) {
+    s4o.print(s4o.indent_spaces);
+    symbol->resource_name->accept(*this);
+    if (wanted_declaretype == initdeclare_dt) {
+      s4o.print(FB_INIT_SUFFIX);
+      s4o.print("();\n");
+    }
+    else {
+      s4o.print(FB_RUN_SUFFIX);
+      s4o.print("(tick);\n");
+    }
+  }
+  return NULL;
+}
+
+void *visit(single_resource_declaration_c *symbol) {
+  if (wanted_declaretype == initprotos_dt || wanted_declaretype == runprotos_dt) {
+    s4o.print(s4o.indent_spaces + "void RESOURCE");
+    if (wanted_declaretype == initprotos_dt) {
+      s4o.print(FB_INIT_SUFFIX);
+      s4o.print("(void);\n");
+    }
+    else {
+      s4o.print(FB_RUN_SUFFIX);
+      s4o.print("(int tick);\n");
+    }
+  }
+  if (wanted_declaretype == initdeclare_dt || wanted_declaretype == rundeclare_dt) {
+    s4o.print(s4o.indent_spaces + "RESOURCE");
+    if (wanted_declaretype == initdeclare_dt) {
+      s4o.print(FB_INIT_SUFFIX);
+      s4o.print("();\n");
+    }
+    else {
+      s4o.print(FB_RUN_SUFFIX);
+      s4o.print("(tick);\n");
+    }
+  }
+  return NULL;
+}
+
+};
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+
+class generate_c_resources_c: public generate_c_typedecl_c {
+
+  search_var_instance_decl_c *search_config_instance;
+  search_var_instance_decl_c *search_resource_instance;
+
+  private:
+    /* The name of the resource curretnly being processed... */
+    symbol_c *current_resource_name;
+    symbol_c *current_global_vars;
+
+  public:
+    generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long time)
+      : generate_c_typedecl_c(s4o_ptr) {
+      search_config_instance = new search_var_instance_decl_c(config_scope);
+      search_resource_instance = new search_var_instance_decl_c(resource_scope);
+      common_ticktime = time;
+      current_resource_name = NULL;
+      current_global_vars = NULL;
+    };
+    virtual ~generate_c_resources_c(void) {
+      delete search_config_instance;
+      delete search_resource_instance;
+    }
+
+    typedef enum {
+      declare_dt,
+      init_dt,
+      run_dt
+    } declaretype_t;
+
+    declaretype_t wanted_declaretype;
+
+    unsigned long common_ticktime;
+    
+    const char *current_program_name;
+
+    typedef enum {
+      assign_at,
+      send_at
+    } assigntype_t;
+
+    assigntype_t wanted_assigntype;
+
+/********************************/
+/* B 1.7 Configuration elements */
+/********************************/
+
+/*
+RESOURCE resource_name ON resource_type_name
+   optional_global_var_declarations
+   single_resource_declaration
+END_RESOURCE
+*/
+// SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration)
+    void *visit(resource_declaration_c *symbol) {
+      current_resource_name = symbol->resource_name;
+      current_global_vars = symbol->global_var_declarations;
+      
+      symbol->resource_declaration->accept(*this);
+      
+      current_resource_name = NULL;
+      current_global_vars = NULL;
+      return NULL;
+    }
+
+/* task_configuration_list program_configuration_list */
+// SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list)
+    void *visit(single_resource_declaration_c *symbol) {
+    	bool single_resource = current_resource_name == NULL;
+      if (single_resource)
+        current_resource_name = new identifier_c("RESOURCE");
+      generate_c_vardecl_c *vardecl;
+      
+      /* Insert the header... */
+      s4o.print("/*******************************************/\n");
+      s4o.print("/*     FILE GENERATED BY iec2c             */\n");
+      s4o.print("/* Editing this file is not recommended... */\n");
+      s4o.print("/*******************************************/\n\n");
+      s4o.print("#include \"iec_std_lib.h\"\n\n");
+      
+      /* (A) resource declaration... */
+      /* (A.1) resource name in comment */
+      s4o.print("// RESOURCE ");
+      current_resource_name->accept(*this);
+      s4o.print("\n");
+       
+      /* (A.2) Global variables... */
+      if (current_global_vars != NULL) {
+        vardecl = new generate_c_vardecl_c(&s4o,
+                      generate_c_vardecl_c::local_vf,
+                      generate_c_vardecl_c::global_vt);
+        vardecl->print(current_global_vars);
+        delete vardecl;
+      }
+      s4o.print("\n");
+      
+      /* (A.3) POUs inclusion */
+      s4o.print("#include \"POUS.c\"\n\n");
+      
+      /* (A.4) Resource programs declaration... */
+      wanted_declaretype = declare_dt;
+      symbol->program_configuration_list->accept(*this);
+      s4o.print("\n");
+      
+      /* (B) resource initialisation function... */
+      /* (B.1) initialisation function name... */
+      s4o.print("void ");
+      current_resource_name->accept(*this);
+      s4o.print(FB_INIT_SUFFIX);
+      s4o.print("(void) {\n");
+      s4o.indent_right();
+      
+      /* (B.2) Global variables initialisations... */
+      if (current_global_vars != NULL) {
+        s4o.print(s4o.indent_spaces);
+        vardecl = new generate_c_vardecl_c(&s4o,
+                      generate_c_vardecl_c::constructorinit_vf,
+                      generate_c_vardecl_c::global_vt);
+        vardecl->print(current_global_vars);
+        delete vardecl;
+      }
+      s4o.print("\n");
+      
+      /* (B.3) Resource programs initialisations... */
+      wanted_declaretype = init_dt;
+      symbol->program_configuration_list->accept(*this);
+      
+      s4o.indent_left();
+      s4o.print("}\n\n");
+      
+      /* (C) Resource run function... */
+      /* (C.1) Run function name... */
+      s4o.print("void ");
+      current_resource_name->accept(*this);
+      s4o.print(FB_RUN_SUFFIX);
+      s4o.print("(int tick) {\n");
+      s4o.indent_right();
+      
+      /* (C.2) Task management... */
+      symbol->task_configuration_list->accept(*this);
+      
+      /* (C.3) Program run declaration... */
+      wanted_declaretype = run_dt;
+      symbol->program_configuration_list->accept(*this);
+      
+      s4o.indent_left();
+      s4o.print("}\n\n");
+      
+      if (single_resource) {
+        delete current_resource_name;
+        current_resource_name = NULL;
+      }
+      return NULL;
+    }
+    
+/*  PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */
+//SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused)
+    void *visit(program_configuration_c *symbol) {
+      if (wanted_declaretype == declare_dt) {
+        s4o.print(s4o.indent_spaces);
+        symbol->program_type_name->accept(*this);
+        s4o.print(" ");
+        symbol->program_name->accept(*this);
+        s4o.print(";\n");
+      }
+      if (wanted_declaretype == init_dt) {
+        s4o.print(s4o.indent_spaces);
+        symbol->program_type_name->accept(*this);
+        s4o.print(FB_INIT_SUFFIX);
+        s4o.print("(&");
+        symbol->program_name->accept(*this);
+        s4o.print(");\n");
+      }
+      if (wanted_declaretype == run_dt) {
+        current_program_name = ((identifier_c*)(symbol->program_name))->value;
+        if (symbol->task_name != NULL) {
+          s4o.print(s4o.indent_spaces);
+          s4o.print("if (!");
+          symbol->task_name->accept(*this);
+          s4o.print(") {\n");
+          s4o.indent_right(); 
+        }
+        
+        wanted_assigntype = assign_at;
+        if (symbol->prog_conf_elements != NULL)
+          symbol->prog_conf_elements->accept(*this);
+        
+        s4o.print(s4o.indent_spaces);
+        symbol->program_type_name->accept(*this);
+        s4o.print(FB_FUNCTION_SUFFIX);
+        s4o.print("(&");
+        symbol->program_name->accept(*this);
+        s4o.print(");\n");
+        
+        wanted_assigntype = send_at;
+        if (symbol->prog_conf_elements != NULL)
+          symbol->prog_conf_elements->accept(*this);
+        
+        if (symbol->task_name != NULL) {
+          s4o.indent_left();
+          s4o.print(s4o.indent_spaces + "}\n");
+        }
+      }
+      return NULL;
+    }
+    
+/*  TASK task_name task_initialization */
+//SYM_REF2(task_configuration_c, task_name, task_initialization)
+    void *visit(task_configuration_c *symbol) {
+      s4o.print(s4o.indent_spaces + "int ");
+      symbol->task_name->accept(*this);
+      s4o.print(" = ");
+      symbol->task_initialization->accept(*this);
+      s4o.print(";\n");
+      return NULL;
+    }
+    
+/*  '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */
+//SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused)
+    void *visit(task_initialization_c *symbol) {
+      if (symbol->interval_data_source != NULL) {
+        calculate_time_c calculate_time;
+        symbol->interval_data_source->accept(calculate_time);
+        unsigned long time = calculate_time.get_time();
+        if (time != 0) {
+          s4o.print("tick % ");
+          s4o.print_integer((int)(time / common_ticktime));
+        }
+        else
+          s4o.print("1");
+      }
+      else
+        s4o.print("1");
+      return NULL;
+    }
+
+/*  any_symbolic_variable ASSIGN prog_data_source */
+//SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source)
+    void *visit(prog_cnxn_assign_c *symbol) {
+      if (wanted_assigntype == assign_at) {
+        symbol_c *var_decl;
+        unsigned int vartype = 0;
+        symbol_c *current_var_reference = ((global_var_reference_c *)(symbol->prog_data_source))->global_var_name;
+        var_decl = search_resource_instance->get_decl(current_var_reference);
+        if (var_decl == NULL) {
+          var_decl = search_config_instance->get_decl(current_var_reference);
+          if (var_decl == NULL)
+            ERROR;
+          else
+            vartype = search_config_instance->get_vartype();
+        }
+        else
+          vartype = search_resource_instance->get_vartype();
+        
+        s4o.print(s4o.indent_spaces + "{extern ");
+        var_decl->accept(*this);
+        s4o.print(" ");
+        symbol->prog_data_source->accept(*this);
+        s4o.print("; ");
+        s4o.print(current_program_name);
+        s4o.print(".");
+        symbol->symbolic_variable->accept(*this);
+        s4o.print(" = ");
+        if (vartype || search_var_instance_decl_c::global_vt)
+          s4o.print("*");
+        symbol->prog_data_source->accept(*this);
+        s4o.print(";}\n");
+      }
+      return NULL;
+    }
+
+/* any_symbolic_variable SENDTO data_sink */
+//SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, data_sink)
+    void *visit(prog_cnxn_sendto_c *symbol) {
+      if (wanted_assigntype == send_at) {
+        symbol_c *var_decl;
+        unsigned int vartype = 0;
+        symbol_c *current_var_reference = ((global_var_reference_c *)(symbol->data_sink))->global_var_name;
+        var_decl = search_resource_instance->get_decl(current_var_reference);
+        if (var_decl == NULL) {
+          var_decl = search_config_instance->get_decl(current_var_reference);
+          if (var_decl == NULL)
+            ERROR;
+          else
+            vartype = search_config_instance->get_vartype();
+        }
+        else
+          vartype = search_resource_instance->get_vartype();
+        
+        s4o.print(s4o.indent_spaces);
+        s4o.print(s4o.indent_spaces + "{extern ");
+        var_decl->accept(*this);
+        s4o.print(" ");
+        symbol->data_sink->accept(*this);
+        s4o.print("; ");
+        if (vartype || search_var_instance_decl_c::global_vt)
+          s4o.print("*");
+        symbol->data_sink->accept(*this);
+        s4o.print(" = ");
+        s4o.print(current_program_name);
+        s4o.print(".");
+        symbol->symbolic_variable->accept(*this);
+        s4o.print("};\n");
+      }
+      return NULL;
+    }
+
+};
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+class generate_c_c: public iterator_visitor_c {
+  protected:
+    stage4out_c &s4o;
+    stage4out_c pous_s4o;
+    stage4out_c located_variables_s4o;
+    generate_c_pous_c generate_c_pous;
+
+    symbol_c *current_configuration;
+
+    const char *current_name;
+    const char *current_builddir;
+
+    unsigned long common_ticktime;
+
+  public:
+    generate_c_c(stage4out_c *s4o_ptr, const char *builddir): 
+            s4o(*s4o_ptr),
+            pous_s4o(builddir, "POUS", "c"),
+            located_variables_s4o(builddir, "LOCATED_VARIABLES","h"),
+            generate_c_pous(&pous_s4o) {
+      current_builddir = builddir;
+      current_configuration = NULL;
+    }
+            
+    ~generate_c_c(void) {}
+
+/***************************/
+/* B 0 - Programming Model */
+/***************************/
+    void *visit(library_c *symbol) {
+      generate_location_list_c generate_location_list(&located_variables_s4o);
+      symbol->accept(generate_location_list);
+      
+      for(int i = 0; i < symbol->n; i++) {
+        symbol->elements[i]->accept(*this);
+      }
+      return NULL;
+    }
+
+/*************************/
+/* B.1 - Common elements */
+/*************************/
+/*******************************************/
+/* B 1.1 - Letters, digits and identifiers */
+/*******************************************/
+    void *visit(identifier_c *symbol) {
+    	current_name = symbol->value;
+    	return NULL;
+    }
+
+/**************************************/
+/* B.1.5 - Program organization units */
+/**************************************/
+/***********************/
+/* B 1.5.1 - Functions */
+/***********************/
+    void *visit(function_declaration_c *symbol) {
+    	symbol->accept(generate_c_pous);
+    	return NULL;
+    }
+    
+/*****************************/
+/* B 1.5.2 - Function Blocks */
+/*****************************/
+    void *visit(function_block_declaration_c *symbol) {
+    	symbol->accept(generate_c_pous);
+    	return NULL;
+    }
+    
+/**********************/
+/* B 1.5.3 - Programs */
+/**********************/    
+    void *visit(program_declaration_c *symbol) {
+    	symbol->accept(generate_c_pous);
+    	return NULL;
+    }
+    
+
+/********************************/
+/* B 1.7 Configuration elements */
+/********************************/
+    void *visit(configuration_declaration_c *symbol) {
+  	  static int configuration_count = 0;
+  
+      if (configuration_count++) {
+        /* the first configuration is the one we will use!! */
+        ERROR;
+      }
+      
+      current_configuration = symbol;
+      
+      calculate_common_ticktime_c calculate_common_ticktime;
+      symbol->accept(calculate_common_ticktime);
+      common_ticktime = calculate_common_ticktime.get_ticktime();
+      
+      symbol->configuration_name->accept(*this);
+      stage4out_c config_s4o(current_builddir, current_name, "c");
+      generate_c_config_c generate_c_config(&config_s4o);
+      symbol->accept(generate_c_config);
+        
+      config_s4o.print("int common_ticktime__ = ");
+      config_s4o.print_integer((int)(common_ticktime / 1000000));
+      config_s4o.print("; /*ms*/\n");
+      
+      symbol->resource_declarations->accept(*this);
+
+      current_configuration = NULL;
+      
+      return NULL;
+    }
+
+    void *visit(resource_declaration_c *symbol) {
+    	symbol->resource_name->accept(*this);
+    	stage4out_c resources_s4o(current_builddir, current_name, "c");
+      generate_c_resources_c generate_c_resources(&resources_s4o, current_configuration, symbol, common_ticktime);
+    	symbol->accept(generate_c_resources);
+    	return NULL;
+    }
+
+    void *visit(single_resource_declaration_c *symbol) {
+    	stage4out_c resources_s4o(current_builddir, "RESOURCE", "c");
+      generate_c_resources_c generate_c_resources(&resources_s4o, current_configuration, symbol, common_ticktime);
+    	symbol->accept(generate_c_resources);
+    	return NULL;
+    }
+    
+};
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+
+
+
+visitor_c *new_code_generator(stage4out_c *s4o, const char *builddir)  {return new generate_c_c(s4o, builddir);}
+void delete_code_generator(visitor_c *code_generator) {delete code_generator;}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/generate_c.hh	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,56 @@
+/*
+ * (c) 2003 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, ST and SFC compiler.
+ *
+ * Based on the
+ * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
+ *
+ */
+
+
+/*
+ * This is one of the versions available for the 4th stage.
+ *
+ * This 4th stage generates a c source program equivalent
+ * to the IL, ST and SFC code.
+ */
+
+
+/*
+ * generate_c.HH
+ */
+
+
+#ifndef _generate_c_HH
+#define _generate_c_HH
+
+
+
+#include <string>
+#include "../../absyntax/absyntax.hh"
+#include "../../absyntax/visitor.hh"
+
+
+
+
+
+
+
+
+#endif /*  _generate_c_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/generate_c_base.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,668 @@
+/*
+ * (c) 2003 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)
+ *
+ */
+
+
+/*
+ * Conversion of basic abstract syntax constructs.
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+
+
+
+
+
+//#include <stdio.h>  /* required for NULL */
+//#include <string>
+//#include <iostream>
+
+//#include "../../util/symtable.hh"
+
+//#include "generate_c.hh"
+
+
+
+
+
+
+
+
+
+
+
+
+
+class generate_c_base_c: public iterator_visitor_c {
+
+  protected:
+    stage4out_c &s4o;
+
+  private:
+    /* Unlike programs that are mapped onto C++ classes, Function Blocks are mapped onto a data structure type
+     * and a separate function conatining the code. This function is passed a pointer to an instance of the data
+     * structure. This means that the code inside the functions must insert a pointer to the data structure whenever
+     * it wishes to access a Function Block variable.
+     * The variable_prefix_ variable will contain the correct string which needs to be prefixed to all variable accesses.
+     * This string is set with the set_variable_prefix() member function.
+     */
+    const char *variable_prefix_;
+
+
+
+  public:
+    generate_c_base_c(stage4out_c *s4o_ptr): s4o(*s4o_ptr) {variable_prefix_ = NULL;}
+    ~generate_c_base_c(void) {}
+
+    void set_variable_prefix(const char *variable_prefix) {variable_prefix_ = variable_prefix;}
+    void print_variable_prefix(void) {
+      if (variable_prefix_ != NULL)
+        s4o.print(variable_prefix_);
+    }
+
+    void *print_token(token_c *token, int offset = 0) {
+      return s4o.printupper((token->value)+offset);
+    }
+
+    void *print_literal(symbol_c *type, symbol_c *value) {
+      s4o.print("__");
+      type->accept(*this);
+      s4o.print("_LITERAL(");
+      value->accept(*this);
+      s4o.print(")");
+      return NULL;
+    }
+
+    void *print_striped_token(token_c *token, int offset = 0) {
+      std::string str = "";
+      for (unsigned int i = offset; i < strlen(token->value); i++)
+        if (token->value[i] != '_')
+          str += token->value[i];
+      return s4o.printupper(str);
+    }
+
+    void *print_striped_binary_token(token_c *token, unsigned int offset = 0) {
+      /* convert the binary value to hexadecimal format... */
+      unsigned char value, bit_mult;
+      unsigned int i;
+      int total_bits;
+      char str[2] = {'A', '\0'};  /* since the s4o object is not prepared to print out one character at a time... */
+
+      s4o.print("0x");
+
+      total_bits = 0;
+      for (i = offset; i < strlen(token->value); i++)
+        if (token->value[i] != '_')
+	  total_bits++;
+
+      value = 0;
+      bit_mult = (unsigned char)1 << (((total_bits+3)%4)+1);
+      for (i = offset; i < strlen(token->value); i++) {
+        if (token->value[i] != '_') {
+	  bit_mult /= 2;
+	  value += bit_mult * ((token->value[i] == '0')? 0:1);
+	  if (bit_mult == 1) {
+	    str[0] = (value <= 9)? (char)'0' + value : (char)'A' + value;
+	    s4o.print(str);
+            bit_mult = 0x10;
+            value = 0;
+	  }
+	}
+      }
+
+      return NULL;
+    }
+
+    void *print_list(list_c *list,
+		     std::string pre_elem_str = "",
+		     std::string inter_elem_str = "",
+		     std::string post_elem_str = "",
+		     visitor_c *visitor = NULL) {
+      if (visitor == NULL) visitor = this;
+
+      if (list->n > 0) {
+//std::cout << "generate_c_base_c::print_list(n = " << list->n << ")   000\n";
+        s4o.print(pre_elem_str);
+        list->elements[0]->accept(*visitor);
+      }
+
+      for(int i = 1; i < list->n; i++) {
+//std::cout << "generate_c_base_c::print_list   " << i << "\n";
+        s4o.print(inter_elem_str);
+        list->elements[i]->accept(*visitor);
+      }
+
+      if (list->n > 0)
+        s4o.print(post_elem_str);
+
+      return NULL;
+    }
+
+
+    void *print_binary_expression(symbol_c *l_exp,
+				  symbol_c *r_exp,
+				  const char *operation) {
+      s4o.print("(");
+      l_exp->accept(*this);
+      s4o.print(operation);
+      r_exp->accept(*this);
+      s4o.print(")");
+      return NULL;
+    }
+
+    void *print_unary_expression(symbol_c *exp,
+				 const char *operation) {
+      s4o.print(operation);
+      s4o.print("(");
+      exp->accept(*this);
+      s4o.print(")");
+      return NULL;
+    }
+
+    void *print_binary_function(const char *function,
+          symbol_c *l_exp,
+				  symbol_c *r_exp) {
+      s4o.print(function);
+      s4o.print("(");
+      l_exp->accept(*this);
+      s4o.print(", ");
+      r_exp->accept(*this);
+      s4o.print(")");
+      return NULL;
+   	}
+
+    void *print_compare_function(const char *function,
+          symbol_c *compare_type,
+          symbol_c *l_exp,
+          symbol_c *r_exp) {
+      s4o.print(function);
+      compare_type->accept(*this);
+      s4o.print("(2, ");
+      l_exp->accept(*this);
+      s4o.print(", ");
+      r_exp->accept(*this);
+      s4o.print(")");
+      return NULL;
+    }
+
+/***************************/
+/* 2.1.6 - Pragmas */
+/***************************/
+    /* Do not use print_token() as it will change everything into uppercase */
+    void *visit(pragma_c *symbol) {return s4o.print(symbol->value);}
+
+
+/***************************/
+/* B 0 - Programming Model */
+/***************************/
+  /* leave for derived classes... */
+
+
+
+/*************************/
+/* B.1 - Common elements */
+/*************************/
+/*******************************************/
+/* B 1.1 - Letters, digits and identifiers */
+/*******************************************/
+    void *visit(identifier_c *symbol) {return print_token(symbol);}
+
+/*********************/
+/* B 1.2 - Constants */
+/*********************/
+  /* originally empty... */
+
+/******************************/
+/* B 1.2.1 - Numeric Literals */
+/******************************/
+    void *visit(real_c *symbol) {return print_striped_token(symbol);}
+    void *visit(integer_c *symbol) {return print_striped_token(symbol);}
+    void *visit(binary_integer_c *symbol) {return print_striped_binary_token(symbol, 2);}
+    void *visit(octal_integer_c *symbol) {s4o.print("0"); return print_striped_token(symbol, 2);}
+    void *visit(hex_integer_c *symbol) {s4o.print("0x"); return print_striped_token(symbol, 3);}
+
+    void *visit(numeric_literal_c *symbol) {return print_literal(symbol->type, symbol->value);}
+    void *visit(integer_literal_c *symbol) {return print_literal(symbol->type, symbol->value);}
+    void *visit(real_literal_c *symbol) {return print_literal(symbol->type, symbol->value);}
+    void *visit(bit_string_literal_c *symbol) {return print_literal(symbol->type, symbol->value);}
+    void *visit(boolean_literal_c *symbol) {return print_literal(symbol->type, symbol->value);}
+
+    /* helper class for boolean_literal_c */
+    void *visit(boolean_true_c *symbol) {s4o.print("TRUE"); return NULL;}
+    void *visit(boolean_false_c *symbol) {s4o.print("FALSE"); return NULL;}
+
+/*******************************/
+/* B.1.2.2   Character Strings */
+/*******************************/
+    void *visit(double_byte_character_string_c *symbol) {
+      // TO DO ...
+      ERROR;
+      return print_token(symbol);
+    }
+
+    void *visit(single_byte_character_string_c *symbol) {
+      std::string str = "";
+      unsigned int count = 0; 
+      str += '"';
+      /* we ignore the first and last bytes, they will be the character ' */
+      for (unsigned int i = 1; i < strlen(symbol->value) - 1; i++) {
+        char c = symbol->value[i];
+        if ((c == '\\') || (c == '"'))
+          {str += '\\'; str += c; count ++; continue;}
+        if (c != '$')
+          {str += c; count++; continue;}
+        /* this should be safe, since the code has passed the syntax parser!! */
+        c = symbol->value[++i];
+        switch (c) {
+          case '$':
+          case '\'':
+            {str += c; count++; continue;}
+          case 'L':
+          case 'l':
+            {str += "\x0A"; /* LF */; count++; continue;}
+          case 'N':
+          case 'n':
+            {str += "\\x0A"; /* NL */; count++; continue;}
+          case 'P':
+          case 'p':
+            {str += "\\f"; /* FF */; count++; continue;}
+          case 'R':
+          case 'r':
+            {str += "\\r"; /* CR */; count++; continue;}
+          case 'T':
+          case 't':
+            {str += "\\t"; /* tab */; count++; continue;}
+          default: {
+            if (isxdigit(c)) {
+              /* this should be safe, since the code has passed the syntax parser!! */
+	      char c2 = symbol->value[++i];
+	      if (isxdigit(c2)) {
+	        str += '\\'; str += 'x'; str += c; str += c2;
+	        count++; continue;
+	      }
+	    }
+          }
+          /* otherwise we have an invalid string!! */
+          /* This should not have got through the syntax parser! */
+          ERROR;
+        } /* switch() */
+      } /* for() */
+
+      str += '"';
+      s4o.print("(STRING){");
+      s4o.print_integer(count); 
+      s4o.print(",");
+      s4o.print(str);
+      s4o.print("}");
+      return NULL;
+    }
+
+
+/***************************/
+/* B 1.2.3 - Time Literals */
+/***************************/
+
+/************************/
+/* B 1.2.3.1 - Duration */
+/************************/
+/* The following output is actually the parameters to the constructor of the TIME class! */
+
+/* SYM_REF0(neg_time_c) */
+void *visit(neg_time_c *symbol) {s4o.print("-1"); /* negative time value */; return NULL;}
+
+
+/* SYM_REF2(duration_c, neg, interval) */
+void *visit(duration_c *symbol) {
+  TRACE("duration_c");
+  s4o.print("__time_to_timespec(");
+  if (NULL == symbol->neg)
+    s4o.print("1");  /* positive time value */
+  else
+    symbol->neg->accept(*this);  /* this will print '-1'   :-) */
+
+  s4o.print(", ");
+
+  symbol->interval->accept(*this);
+  if (typeid(*symbol->interval) == typeid(hours_c)) {s4o.print(", 0");}
+  if (typeid(*symbol->interval) == typeid(minutes_c)) {s4o.print(", 0, 0");}
+  if (typeid(*symbol->interval) == typeid(seconds_c)) {s4o.print(", 0, 0, 0");}
+  if (typeid(*symbol->interval) == typeid(milliseconds_c)) {s4o.print(", 0, 0, 0, 0");}
+  s4o.print(")");
+  return NULL;
+}
+
+
+/* SYM_TOKEN(fixed_point_c) */
+void *visit(fixed_point_c *symbol) {return print_token(symbol);}
+
+
+/* SYM_REF2(days_c, days, hours) */
+void *visit(days_c *symbol) {
+  TRACE("days_c");
+  if (NULL == symbol->hours)
+    s4o.print("0, 0, 0, 0");  /* milliseconds, seconds, minutes, hours */
+  else
+    symbol->hours->accept(*this);
+
+  s4o.print(", ");
+
+  symbol->days->accept(*this);
+  return NULL;
+}
+
+
+/* SYM_REF2(hours_c, hours, minutes) */
+void *visit(hours_c *symbol) {
+  TRACE("hours_c");
+  if (NULL == symbol->minutes)
+    s4o.print("0, 0, 0");  /* milliseconds, seconds, minutes */
+  else
+    symbol->minutes->accept(*this);
+
+  s4o.print(", ");
+
+  symbol->hours->accept(*this);
+  return NULL;
+}
+
+
+/* SYM_REF2(minutes_c, minutes, seconds) */
+void *visit(minutes_c *symbol) {
+  TRACE("minutes_c");
+  if (NULL == symbol->seconds)
+    s4o.print("0, 0");  /* milliseconds, seconds */
+  else
+    symbol->seconds->accept(*this);
+
+  s4o.print(", ");
+
+  symbol->minutes->accept(*this);
+  return NULL;
+}
+
+
+/* SYM_REF2(seconds_c, seconds, milliseconds) */
+void *visit(seconds_c *symbol) {
+  TRACE("seconds_c");
+  if (NULL == symbol->milliseconds)
+    s4o.print("0");  /* milliseconds */
+  else
+    symbol->milliseconds->accept(*this);
+
+  s4o.print(", ");
+
+  symbol->seconds->accept(*this);
+  return NULL;
+}
+
+
+/* SYM_REF2(milliseconds_c, milliseconds, unused) */
+void *visit(milliseconds_c *symbol) {
+  TRACE("milliseconds_c");
+  symbol->milliseconds->accept(*this);
+  return NULL;
+}
+
+/************************************/
+/* B 1.2.3.2 - Time of day and Date */
+/************************************/
+
+/* SYM_REF2(time_of_day_c, daytime, unused) */
+void *visit(time_of_day_c *symbol) {
+  TRACE("time_of_day_c");
+  s4o.print("__tod_to_timespec(");
+  symbol->daytime->accept(*this);
+  s4o.print(")");
+  return NULL;
+}
+
+
+/* SYM_REF4(daytime_c, day_hour, day_minute, day_second, unused) */
+void *visit(daytime_c *symbol) {
+  TRACE("daytime_c");
+  symbol->day_second->accept(*this);
+  s4o.print(", ");
+  symbol->day_minute->accept(*this);
+  s4o.print(", ");
+  symbol->day_hour->accept(*this);
+  return NULL;
+}
+
+
+/* SYM_REF2(date_c, date_literal, unused) */
+void *visit(date_c *symbol) {
+  TRACE("date_c");
+  s4o.print("__date_to_timespec(");
+  symbol->date_literal->accept(*this);
+  s4o.print(")");
+  return NULL;
+}
+
+
+/* SYM_REF4(date_literal_c, year, month, day, unused) */
+void *visit(date_literal_c *symbol) {
+  TRACE("date_literal_c");
+  symbol->day->accept(*this);
+  s4o.print(", ");
+  symbol->month->accept(*this);
+  s4o.print(", ");
+  symbol->year->accept(*this);
+  return NULL;
+}
+
+
+/* SYM_REF2(date_and_time_c, date_literal, daytime) */
+void *visit(date_and_time_c *symbol) {
+  TRACE("date_and_time_c");
+  s4o.print("__dt_to_timespec(");
+  symbol->daytime->accept(*this);
+  s4o.print(", ");
+  symbol->date_literal->accept(*this);
+  s4o.print(")");
+  return NULL;
+}
+
+
+/**********************/
+/* B.1.3 - Data types */
+/**********************/
+/***********************************/
+/* B 1.3.1 - Elementary Data Types */
+/***********************************/
+    void *visit(time_type_name_c *symbol) {s4o.print("TIME"); return NULL;}
+    void *visit(bool_type_name_c *symbol) {s4o.print("BOOL"); return NULL;}
+    void *visit(sint_type_name_c *symbol) {s4o.print("SINT"); return NULL;}
+    void *visit(int_type_name_c *symbol) {s4o.print("INT"); return NULL;}
+    void *visit(dint_type_name_c *symbol) {s4o.print("DINT"); return NULL;}
+    void *visit(lint_type_name_c *symbol) {s4o.print("LINT"); return NULL;}
+    void *visit(usint_type_name_c *symbol) {s4o.print("USINT"); return NULL;}
+    void *visit(uint_type_name_c *symbol) {s4o.print("UINT"); return NULL;}
+    void *visit(udint_type_name_c *symbol) {s4o.print("UDINT"); return NULL;}
+    void *visit(ulint_type_name_c *symbol) {s4o.print("ULINT"); return NULL;}
+    void *visit(real_type_name_c *symbol) {s4o.print("REAL"); return NULL;}
+    void *visit(lreal_type_name_c *symbol) {s4o.print("LREAL"); return NULL;}
+    void *visit(date_type_name_c *symbol) {s4o.print("DATE"); return NULL;}
+    void *visit(tod_type_name_c *symbol) {s4o.print("TOD"); return NULL;}
+    void *visit(dt_type_name_c *symbol) {s4o.print("DT"); return NULL;}
+    void *visit(byte_type_name_c *symbol) {s4o.print("BYTE"); return NULL;}
+    void *visit(word_type_name_c *symbol) {s4o.print("WORD"); return NULL;}
+    void *visit(lword_type_name_c *symbol) {s4o.print("LWORD"); return NULL;}
+    void *visit(dword_type_name_c *symbol) {s4o.print("DWORD"); return NULL;}
+    void *visit(string_type_name_c *symbol) {s4o.print("STRING"); return NULL;}
+    void *visit(wstring_type_name_c *symbol) {s4o.print("WSTRING"); return NULL;}
+
+/********************************/
+/* B.1.3.2 - Generic data types */
+/********************************/
+  /* originally empty... */
+
+/********************************/
+/* B 1.3.3 - Derived data types */
+/********************************/
+  /* leave for derived classes... */
+
+/*********************/
+/* B 1.4 - Variables */
+/*********************/
+void *visit(symbolic_variable_c *symbol) {
+  TRACE("symbolic_variable_c");
+  this->print_variable_prefix();
+  symbol->var_name->accept(*this);
+  return NULL;
+}
+
+/********************************************/
+/* B.1.4.1   Directly Represented Variables */
+/********************************************/
+void *visit(direct_variable_c *symbol) {
+  TRACE("direct_variable_c");
+  /* Do not use print_token() as it will change everything into uppercase */
+  return s4o.printlocation(symbol->value);
+}
+
+
+
+/*************************************/
+/* B.1.4.2   Multi-element Variables */
+/*************************************/
+#if 0
+/*  subscripted_variable '[' subscript_list ']' */
+SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
+
+/* subscript_list ',' subscript */
+SYM_LIST(subscript_list_c)
+#endif
+
+/*  record_variable '.' field_selector */
+/*  WARNING: input and/or output variables of function blocks
+ *           may be accessed as fields of a structured variable!
+ *           Code handling a structured_variable_c must take
+ *           this into account!
+ */
+// SYM_REF2(structured_variable_c, record_variable, field_selector)
+void *visit(structured_variable_c *symbol) {
+  TRACE("structured_variable_c");
+
+  symbol->record_variable->accept(*this);
+  s4o.print(".");
+  symbol->field_selector->accept(*this);
+  return NULL;
+}
+
+/******************************************/
+/* B 1.4.3 - Declaration & Initialisation */
+/******************************************/
+  /* leave for derived classes... */
+
+/**************************************/
+/* B.1.5 - Program organization units */
+/**************************************/
+/***********************/
+/* B 1.5.1 - Functions */
+/***********************/
+  /* leave for derived classes... */
+
+/*****************************/
+/* B 1.5.2 - Function Blocks */
+/*****************************/
+  /* leave for derived classes... */
+
+/**********************/
+/* B 1.5.3 - Programs */
+/**********************/
+  /* leave for derived classes... */
+
+/*********************************************/
+/* B.1.6  Sequential function chart elements */
+/*********************************************/
+
+/********************************/
+/* B 1.7 Configuration elements */
+/********************************/
+  /* leave for derived classes... */
+
+/****************************************/
+/* B.2 - Language IL (Instruction List) */
+/****************************************/
+/***********************************/
+/* B 2.1 Instructions and Operands */
+/***********************************/
+  /* leave for derived classes... */
+
+/*******************/
+/* B 2.2 Operators */
+/*******************/
+  /* leave for derived classes... */
+
+
+/***************************************/
+/* B.3 - Language ST (Structured Text) */
+/***************************************/
+/***********************/
+/* B 3.1 - Expressions */
+/***********************/
+  /* leave for derived classes... */
+
+/********************/
+/* B 3.2 Statements */
+/********************/
+  /* leave for derived classes... */
+
+/*********************************/
+/* B 3.2.1 Assignment Statements */
+/*********************************/
+  /* leave for derived classes... */
+
+/*****************************************/
+/* B 3.2.2 Subprogram Control Statements */
+/*****************************************/
+  /* leave for derived classes... */
+
+/********************************/
+/* B 3.2.3 Selection Statements */
+/********************************/
+  /* leave for derived classes... */
+
+/********************************/
+/* B 3.2.4 Iteration Statements */
+/********************************/
+  /* leave for derived classes... */
+
+}; /* class generate_c_basic_c */
+
+
+
+
+
+
+
+
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/generate_c_configbody.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,211 @@
+/*
+ * (c) 2003 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)
+ *
+ */
+
+
+/*
+ * Build the body of the main function (currently named run() )
+ * of the class onto which is mapped a specific CONFIGURATION.
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+
+
+
+
+
+// #include <stdio.h>  /* required for NULL */
+/*
+#include <string>
+#include <iostream>
+
+#include "../../util/symtable.hh"
+
+#include "../stage4.hh"
+*/
+
+
+
+
+
+class generate_c_configbody_c: public generate_c_base_c {
+
+  public:
+    generate_c_configbody_c(stage4out_c *s4o_ptr)
+    : generate_c_base_c(s4o_ptr) {
+      current_resource_name = NULL;
+    }
+
+    virtual ~generate_c_configbody_c(void) {}
+
+
+  private:
+    /* The name of the resource curretnly being processed... */
+    symbol_c *current_resource_name;
+
+
+/********************************/
+/* B 1.7 Configuration elements */
+/********************************/
+
+
+public:
+/*
+CONFIGURATION configuration_name
+   optional_global_var_declarations
+   (resource_declaration_list | single_resource_declaration)
+   optional_access_declarations
+   optional_instance_specific_initializations
+END_CONFIGURATION
+*/
+/*
+SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused)
+*/
+void *visit(configuration_declaration_c *symbol) {
+  TRACE("configuration_declaration_c");
+
+  symbol->resource_declarations->accept(*this);
+  return NULL;
+}
+
+
+
+/* helper symbol for configuration_declaration */
+// SYM_LIST(resource_declaration_list_c)
+void *visit(resource_declaration_list_c *symbol) {
+  TRACE("resource_declaration_list_c");
+
+  return print_list(symbol);
+}
+
+/*
+RESOURCE resource_name ON resource_type_name
+   optional_global_var_declarations
+   single_resource_declaration
+END_RESOURCE
+*/
+// SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration)
+void *visit(resource_declaration_c *symbol) {
+  TRACE("resource_declaration_c");
+
+  current_resource_name = symbol->resource_name;
+  symbol->resource_declaration->accept(*this);
+  current_resource_name = NULL;
+  return NULL;
+}
+
+
+
+/* task_configuration_list program_configuration_list */
+// SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list)
+void *visit(single_resource_declaration_c *symbol) {
+  TRACE("single_resource_declaration_c");
+
+  symbol->program_configuration_list->accept(*this);
+  return NULL;
+}
+
+
+
+/* helper symbol for single_resource_declaration */
+//SYM_LIST(task_configuration_list_c)
+
+
+/* helper symbol for single_resource_declaration */
+//SYM_LIST(program_configuration_list_c)
+void *visit(program_configuration_list_c *symbol) {
+  TRACE("program_configuration_list_c");
+
+  return print_list(symbol);
+}
+
+
+/* helper symbol for
+ *  - access_path
+ *  - instance_specific_init
+ */
+//SYM_LIST(any_fb_name_list_c)
+
+/*  [resource_name '.'] global_var_name ['.' structure_element_name] */
+//SYM_REF4(global_var_reference_c, resource_name, global_var_name, structure_element_name, unused)
+
+/*  prev_declared_program_name '.' symbolic_variable */
+//SYM_REF2(program_output_reference_c, program_name, symbolic_variable)
+
+/*  TASK task_name task_initialization */
+//SYM_REF2(task_configuration_c, task_name, task_initialization)
+
+/*  '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */
+//SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused)
+
+/*  PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */
+//SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused)
+void *visit(program_configuration_c *symbol) {
+  TRACE("program_configuration_c");
+
+  s4o.print(s4o.indent_spaces);
+  if (NULL != current_resource_name) {
+    current_resource_name->accept(*this);
+    s4o.print(".");
+  }
+  symbol->program_name->accept(*this);
+  s4o.print(".f();\n");
+  return NULL;
+}
+
+/* prog_conf_elements ',' prog_conf_element */
+//SYM_LIST(prog_conf_elements_c)
+
+/*  fb_name WITH task_name */
+//SYM_REF2(fb_task_c, fb_name, task_name)
+// TODO...
+
+/*  any_symbolic_variable ASSIGN prog_data_source */
+//SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source)
+
+/* any_symbolic_variable SENDTO data_sink */
+//SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, prog_data_source)
+
+/* VAR_CONFIG instance_specific_init_list END_VAR */
+//SYM_REF2(instance_specific_initializations_c, instance_specific_init_list, unused)
+
+/* helper symbol for instance_specific_initializations */
+//SYM_LIST(instance_specific_init_list_c)
+
+/* resource_name '.' program_name '.' {fb_name '.'}
+    ((variable_name [location] ':' located_var_spec_init) | (fb_name ':' fb_initialization))
+*/
+//SYM_REF6(instance_specific_init_c, resource_name, program_name, any_fb_name_list, variable_name, location, initialization)
+
+/* helper symbol for instance_specific_init */
+/* function_block_type_name ':=' structure_initialization */
+//SYM_REF2(fb_initialization_c, function_block_type_name, structure_initialization)
+
+
+
+}; /* generate_c_configbody_c */
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/generate_c_il.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,1465 @@
+/*
+ * (c) 2003 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)
+ *
+ */
+
+
+/*
+ * Conversion of il statements (i.e. IL code).
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+/* Returns the data type of an il_operand.
+ *
+ * Note that the il_operand may be a variable, in which case
+ * we return the type of the variable instance.
+ * The il_operand may also be a constant, in which case
+ * we return the data type of that constant.
+ *
+ * The variable instance may be a member of a structured variable,
+ * or an element in an array, or any combination of the two.
+ *
+ * The class constructor must be given the search scope
+ * (function, function block or program within which
+ * the possible il_operand variable instance was declared).
+ */
+
+class search_il_operand_type_c {
+
+  private:
+    search_varfb_instance_type_c search_varfb_instance_type;
+    search_constant_type_c search_constant_type;
+
+  public:
+    search_il_operand_type_c(symbol_c *search_scope): search_varfb_instance_type(search_scope) {}
+
+  public:
+    symbol_c *get_type(symbol_c *il_operand) {
+      symbol_c *res;
+
+      /* We first assume that it is a constant... */
+      res = search_constant_type.get_type(il_operand);
+      if (res != NULL) return res;
+
+      /* Nope, now we assume it is a variable, and determine its type... */
+      res = search_varfb_instance_type.get_type(il_operand);
+      if (NULL != res) return res;
+
+      /* not found */
+      return NULL;
+    }
+};
+
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+
+/* A new class to ouput the il default variable to c++ code
+ * We use this class, inheriting from symbol_c, so it may be used
+ * as any other symbol_c object in the intermediate parse tree,
+ * more specifically, so it can be used as any other il operand.
+ * This makes the rest of the code much easier...
+ *
+ * Nevertheless, the basic visitor class visitor_c does not know
+ * how to visit this new il_default_variable_c class, so we have
+ * to extend that too.
+ * In reality extending the basic symbols doesn't quite work out
+ * as cleanly as desired (we need to use dynamic_cast in the
+ * accept method of the il_default_variable_c), but it is cleaner
+ * than the alternative...
+ */
+class il_default_variable_c;
+
+/* This visitor class is not really required, we could place the
+ * visit() method directly in genertae_cc_il_c, but doing it in
+ * a seperate class makes the architecture more evident...
+ */
+class il_default_variable_visitor_c {
+  public:
+    virtual void *visit(il_default_variable_c *symbol) = 0;
+
+    virtual ~il_default_variable_visitor_c(void) {return;}
+};
+
+
+/* A class to print out to the resulting C++ code
+ * the IL default variable name.
+ *
+ * It includes a reference to its name,
+ * and the data type of the data currently stored
+ * in this C++ variable... This is required because the
+ * C++ variable is a union, and we must know which member
+ * of the union top reference!!
+ *
+ * Note that we also need to keep track of the data type of
+ * the value currently being stored in the default variable.
+ * This is required so we can process parenthesis,
+ *
+ * e.g. :
+ *         LD var1
+ *         AND (
+ *         LD var2
+ *         OR var3
+ *         )
+ *
+ * Note that we only execute the 'AND (' operation when we come across
+ * the ')', i.e. once we have evaluated the result of the
+ * instructions inside the parenthesis.
+ * When we do execute the 'AND (' operation, we need to know the data type
+ * of the operand, which in this case is the result of the evaluation of the
+ * instruction list inside the parenthesis. We can only know this if we
+ * keep track of the data type currently stored in the default variable!
+ *
+ * We use the current_type inside the generate_c_il::default_variable_name variable
+ * to track this!
+ */
+class il_default_variable_c: public symbol_c {
+  public:
+    symbol_c *var_name;  /* in principle, this should point to an indentifier_c */
+    symbol_c *current_type;
+
+  public:
+    il_default_variable_c(const char *var_name_str, symbol_c *current_type);
+    virtual void *accept(visitor_c &visitor);
+};
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+
+
+class generate_c_il_c: public generate_c_typedecl_c, il_default_variable_visitor_c {
+
+  private:
+    /* When compiling il code, it becomes necessary to determine the
+     * data type of il operands. To do this, we must first find the
+     * il operand's declaration, within the scope of the function block
+     * or function currently being processed.
+     * The following object does just that...
+     * This object instance will then later be called while the
+     * remaining il code is being handled.
+     */
+    //search_il_operand_type_c *search_il_operand_type;
+    search_expression_type_c *search_expression_type;
+
+    /* The initial value that should be given to the IL default variable
+     * imediately after a parenthesis is opened.
+     * This variable is only used to pass data from the
+     * il_expression_c visitor to the simple_instr_list_c visitor.
+     *
+     * e.g.:
+     *         LD var1
+     *         AND ( var2
+     *         OR var3
+     *         )
+     *
+     * In the above code sample, the line 'AND ( var2' constitutes
+     * an il_expression_c, where var2 should be loaded into the
+     * il default variable before continuing with the expression
+     * inside the parenthesis.
+     * Unfortunately, only the simple_instr_list_c may do the
+     * initial laoding of the var2 bariable following the parenthesis,
+     * so the il_expression_c visitor will have to pass 'var2' as a
+     * parameter to the simple_instr_list_c visitor.
+     * Ergo, the existance of the following parameter...!
+     */
+    symbol_c *il_default_variable_init_value;
+
+    /* Operand to the IL operation currently being processed... */
+    /* These variables are used to pass data from the
+     * il_simple_operation_c and il_expression_c visitors
+     * to the il operator visitors (i.e. LD_operator_c,
+     * LDN_operator_c, ST_operator_c, STN_operator_c, ...)
+     */
+    symbol_c *current_operand;
+    symbol_c *current_operand_type;
+
+    /* Label to which the current IL jump operation should jump to... */
+    /* This variable is used to pass data from the
+     * il_jump_operation_c visitor
+     * to the il jump operator visitors (i.e. JMP_operator_c,
+     * JMPC_operator_c, JMPCN_operator_c, ...)
+     */
+    symbol_c *jump_label;
+
+    /* The result of the comparison IL operations (GT, EQ, LT, ...)
+     * is a boolean variable.
+     * This class keeps track of the current data type stored in the
+     * il default variable. This is usually done by keeping a reference
+     * to the data type of the last operand. Nevertheless, in the case of
+     * the comparison IL operators, the data type of the result (a boolean)
+     * is not the data type of the operand. We therefore need an object
+     * of the boolean data type to keep as a reference of the current
+     * data type.
+     * The following object is it...
+     */
+    bool_type_name_c bool_type;
+
+    /* the data type of the IL default variable... */
+    #define IL_DEFVAR_T VAR_LEADER "IL_DEFVAR_T"
+    /* The name of the IL default variable... */
+    #define IL_DEFVAR   VAR_LEADER "IL_DEFVAR"
+    /* The name of the variable used to pass the result of a
+     * parenthesised instruction list to the immediately preceding
+     * scope ...
+     */
+    #define IL_DEFVAR_BACK   VAR_LEADER "IL_DEFVAR_BACK"
+    il_default_variable_c default_variable_name;
+    il_default_variable_c default_variable_back_name;
+
+    /* Some function calls in the body of functions or function blocks
+     * may leave some parameters to their default values, and
+     * ignore some output parameters of the function being called.
+     * Our conversion of ST functions to C++ does not contemplate that,
+     * i.e. each called function must get all it's input and output
+     * parameters set correctly.
+     * For input parameters we merely need to call the function with
+     * the apropriate default value, but for output parameters
+     * we must create temporary variables to hold the output value.
+     *
+     * We declare all the temporary output variables at the begining of
+     * the body of each function or function block, and use them as
+     * in function calls later on as they become necessary...
+     * Note that we cannot create these variables just before a function
+     * call, as the function call itself may be integrated within an
+     * expression, or another function call!
+     *
+     * The variables are declared in the exact same order in which they
+     * will be used later on during the function calls, which allows us
+     * to simply re-create the name that was used for the temporary variable
+     * instead of keeping it in some list.
+     * The names are recreated by the temp_var_name_factory, after reset()
+     * has been called!
+     *
+     * This function will genertae code similar to...
+     *
+     *     INT __TMP_0 = 23;
+     *     REAL __TMP_1 = 45.5;
+     *     ...
+     */
+    temp_var_name_c temp_var_name_factory;
+
+    /* When calling a function block, we must first find it's type,
+     * by searching through the declarations of the variables currently
+     * in scope.
+     * This class does just that...
+     * A new class is instantiated whenever we begin generating the code
+     * for a function block type declaration, or a program declaration.
+     * This object instance will then later be called while the
+     * function block's or the program's body is being handled.
+     *
+     * Note that functions cannot contain calls to function blocks,
+     * so we do not create an object instance when handling
+     * a function declaration.
+     */
+    search_fb_instance_decl_c *search_fb_instance_decl;
+
+
+  public:
+    generate_c_il_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
+    : generate_c_typedecl_c(s4o_ptr),
+      default_variable_name(IL_DEFVAR, NULL),
+      default_variable_back_name(IL_DEFVAR_BACK, NULL)
+    {
+      //search_il_operand_type  = new search_il_operand_type_c(scope);
+      search_expression_type = new search_expression_type_c(scope);
+      search_fb_instance_decl = new search_fb_instance_decl_c(scope);
+      current_operand = NULL;
+      current_operand_type = NULL;
+      il_default_variable_init_value = NULL;
+      this->set_variable_prefix(variable_prefix);
+    }
+
+    virtual ~generate_c_il_c(void) {
+      delete search_fb_instance_decl;
+      //delete search_il_operand_type;
+      delete search_expression_type;
+    }
+
+    void generate(instruction_list_c *il) {
+      generate_c_tempvardecl_c generate_c_tempvardecl(&s4o);
+      generate_c_tempvardecl.generate(il, &temp_var_name_factory);
+      il->accept(*this);
+    }
+
+    /* Declare the backup to the default variable, that will store the result
+     * of the IL operations executed inside a parenthesis...
+     */
+    void declare_backup_variable(void) {
+      s4o.print(s4o.indent_spaces);
+      s4o.print(IL_DEFVAR_T);
+      s4o.print(" ");
+      print_backup_variable();
+      s4o.print(";\n");
+    }
+    
+    void print_backup_variable(void) {
+      this->default_variable_back_name.accept(*this);
+    }
+
+  private:
+    /* A helper function... */
+    /*
+    bool is_bool_type(symbol_c *type_symbol) {
+      return (NULL != dynamic_cast<bool_type_name_c *>(type_symbol));
+    }
+    */
+
+    /* A helper function... */
+    void *XXX_operator(symbol_c *lo, const char *op, symbol_c *ro) {
+      if ((NULL == lo) || (NULL == ro)) ERROR;
+      if (NULL == op) ERROR;
+
+      lo->accept(*this);
+      s4o.print(op);
+      ro->accept(*this);
+      return NULL;
+    }
+
+    /* A helper function... */
+    void *XXX_function(const char *func, symbol_c *lo, symbol_c *ro) {
+      if ((NULL == lo) || (NULL == ro)) ERROR;
+      if (NULL == func) ERROR;
+
+      lo->accept(*this);
+      s4o.print(" = ");
+      s4o.print(func);
+      s4o.print("(");
+      lo->accept(*this);
+      s4o.print(", ");
+      ro->accept(*this);
+      s4o.print(")");
+      return NULL;
+    }
+
+    /* A helper function... */
+    void *XXX_CAL_operator(const char *param_name, symbol_c *fb_name) {
+      if (NULL == fb_name) ERROR;
+      symbolic_variable_c *sv = dynamic_cast<symbolic_variable_c *>(fb_name);
+      if (NULL == sv) ERROR;
+      identifier_c *id = dynamic_cast<identifier_c *>(sv->var_name);
+      if (NULL == id) ERROR;
+
+      identifier_c param(param_name);
+
+      //SYM_REF3(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list)
+      il_param_assignment_c il_param_assignment(&param, &this->default_variable_name, NULL);
+      // SYM_LIST(il_param_list_c)
+      il_param_list_c il_param_list;
+      il_param_list.add_element(&il_param_assignment);
+      CAL_operator_c CAL_operator;
+      // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list)
+      il_fb_call_c il_fb_call(&CAL_operator, id, NULL, &il_param_list);
+
+      il_fb_call.accept(*this);
+      return NULL;
+    }
+
+    /* A helper function... */
+    void *CMP_operator(symbol_c *o, const char *operation) {
+      if (NULL == o) ERROR;
+      if (NULL == this->default_variable_name.current_type) ERROR;
+
+      symbol_c *backup = this->default_variable_name.current_type;
+      this->default_variable_name.current_type = &(this->bool_type);
+      this->default_variable_name.accept(*this);
+      this->default_variable_name.current_type = backup;
+
+      s4o.print(" = ");
+      s4o.print(operation);
+      this->default_variable_name.current_type->accept(*this);
+      s4o.print("(2, ");
+      this->default_variable_name.accept(*this);
+      s4o.print(", ");
+      o->accept(*this);
+      s4o.print(")");
+
+      /* the data type resulting from this operation... */
+      this->default_variable_name.current_type = &(this->bool_type);
+      return NULL;
+    }
+
+
+    /* A helper function... */
+    void C_modifier(void) {
+      if (search_expression_type->is_bool_type(default_variable_name.current_type)) {
+        s4o.print("if (");
+        this->default_variable_name.accept(*this);
+        s4o.print(") ");
+      }
+      else {ERROR;}
+    }
+
+    /* A helper function... */
+    void CN_modifier(void) {
+      if (search_expression_type->is_bool_type(default_variable_name.current_type)) {
+        s4o.print("if (!");
+        this->default_variable_name.accept(*this);
+        s4o.print(") ");
+      }
+      else {ERROR;}
+    }
+
+
+public:
+void *visit(il_default_variable_c *symbol) {
+  //s4o.print("il_default_variable_c VISITOR!!\n");
+  symbol->var_name->accept(*this);
+  if (NULL != symbol->current_type) {
+    s4o.print(".");
+    symbol->current_type->accept(*this);
+    s4o.print("var");
+  }
+  return NULL;
+}
+
+
+private:
+
+/********************************************/
+/* B.1.4.1   Directly Represented Variables */
+/********************************************/
+// direct_variable: direct_variable_token   {$$ = new direct_variable_c($1);};
+void *visit(direct_variable_c *symbol) {
+  TRACE("direct_variable_c");
+  /* Do not use print_token() as it will change everything into uppercase */
+  if (strlen(symbol->value) == 0) ERROR;
+  s4o.print("*(");
+  this->print_variable_prefix();
+  s4o.printlocation(symbol->value + 1);
+  s4o.print(")");
+  return NULL;
+}
+
+/****************************************/
+/* B.2 - Language IL (Instruction List) */
+/****************************************/
+
+/***********************************/
+/* B 2.1 Instructions and Operands */
+/***********************************/
+
+/* please see the comment before the RET_operator_c visitor for details... */
+#define END_LABEL VAR_LEADER "end"
+
+/*| instruction_list il_instruction */
+void *visit(instruction_list_c *symbol) {
+  
+  /* Declare the backup to the default variable, that will store the result
+   * of the IL operations executed inside a parenthesis...
+   */
+  declare_backup_variable();
+  
+  /* Declare the default variable, that will store the result of the IL operations... */
+  s4o.print(s4o.indent_spaces);
+  s4o.print(IL_DEFVAR_T);
+  s4o.print(" ");
+  this->default_variable_name.accept(*this);
+  s4o.print(";\n\n");
+
+  print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n");
+
+  /* write the label marking the end of the code block */
+  /* please see the comment before the RET_operator_c visitor for details... */
+  s4o.print("\n");
+  s4o.print(s4o.indent_spaces);
+  s4o.print(END_LABEL);
+  s4o.print(":\n");
+  s4o.indent_right();
+  /* since every label must be followed by at least one statement, and
+   * only the functions will introduce the return statement after this label,
+   * function blocks written in IL would result in invalid C++ code.
+   * To work around this we introduce the equivalent of a 'nop' operation
+   * to humour the compiler...
+   */
+  s4o.print(s4o.indent_spaces);
+  s4o.print("/* to humour the compiler, we insert a nop */\n");
+  s4o.print(s4o.indent_spaces);
+  this->default_variable_name.accept(*this);
+  s4o.print(" = ");
+  this->default_variable_name.accept(*this);
+  s4o.print(";\n");
+  s4o.indent_left();
+
+  return NULL;
+}
+
+
+/* | label ':' [il_incomplete_instruction] eol_list */
+// SYM_REF2(il_instruction_c, label, il_instruction)
+void *visit(il_instruction_c *symbol) {
+  if (NULL != symbol->label) {
+    symbol->label->accept(*this);
+    s4o.print(":\n");
+    s4o.print(s4o.indent_spaces);
+  }
+  symbol->il_instruction->accept(*this);
+  return NULL;
+}
+
+/* | il_simple_operator [il_operand] */
+//SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand)
+void *visit(il_simple_operation_c *symbol) {
+  this->current_operand = symbol->il_operand;
+  if (NULL == this->current_operand) {
+    this->current_operand_type = NULL;
+  } else {
+    this->current_operand_type = search_expression_type->get_type(this->current_operand);
+    if (NULL == this->current_operand_type) ERROR;
+  }
+
+  symbol->il_simple_operator->accept(*this);
+
+  this->current_operand = NULL;
+  this->current_operand_type = NULL;
+  return NULL;
+}
+
+
+/* | function_name [il_operand_list] */
+// SYM_REF2(il_function_call_c, function_name, il_operand_list)
+void *visit(il_function_call_c *symbol) {
+  function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
+
+  if (f_decl == function_symtable.end_value()) {
+    /* should never occur. The function being called MUST be in the symtable... */
+    function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
+    if (current_function_type == function_none) ERROR;
+    
+    symbol_c *param_data_type = default_variable_name.current_type;
+    symbol_c *return_data_type = (symbol_c *)search_expression_type->compute_standard_function_il(symbol, param_data_type);
+    if (NULL == return_data_type) ERROR;
+    
+    default_variable_name.current_type = return_data_type;
+    this->default_variable_name.accept(*this);
+    default_variable_name.current_type = param_data_type;
+    s4o.print(" = ");
+    
+    function_call_param_iterator_c function_call_param_iterator(symbol);
+    
+    int nb_param = 1;
+    if (symbol->il_operand_list != NULL)
+      nb_param += ((list_c *)symbol->il_operand_list)->n;
+
+#include "il_code_gen.c"
+
+#if 0
+    for(int current_param = 0; current_param < nb_param; current_param++) {
+      symbol_c *param_value;
+      if (current_param == 0)
+        param_value = &this->default_variable_name;
+      else {
+        symbol_c *param_name = NULL;
+        switch (current_function_type) {
+          default: ERROR;
+        }
+        
+         
+        /* Get the value from a foo(<param_name> = <param_value>) style call */
+        param_value = function_call_param_iterator.search(param_name);
+        delete param_name;
+        
+        /* Get the value from a foo(<param_value>) style call */
+        if (param_value == NULL)
+          param_value = function_call_param_iterator.next();
+        
+        if (param_value == NULL) ERROR;
+      }
+      
+      switch (current_function_type) {
+        case (function_sqrt):
+          if (current_param == 0) {
+            s4o.print("sqrt(");
+            param_value->accept(*this);
+            s4o.print(")");
+          }
+          else ERROR;
+          break;
+        default: ERROR;
+      }
+    } /* for(...) */
+#endif
+
+    /* the data type returned by the function, and stored in the il default variable... */
+    default_variable_name.current_type = return_data_type;
+  }
+  else {
+    /* determine the base data type returned by the function being called... */
+    search_base_type_c search_base_type;
+    symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
+    symbol_c *param_data_type = default_variable_name.current_type;
+    if (NULL == return_data_type) ERROR;
+  
+    default_variable_name.current_type = return_data_type;
+    this->default_variable_name.accept(*this);
+    default_variable_name.current_type = param_data_type;
+    s4o.print(" = ");
+  
+    symbol->function_name->accept(*this);
+    s4o.print("(");
+  
+    /* loop through each function parameter, find the value we should pass
+     * to it, and then output the c equivalent...
+     */
+  
+    function_param_iterator_c fp_iterator(f_decl);
+    identifier_c *param_name;
+    function_call_param_iterator_c function_call_param_iterator(symbol);
+    for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+      if (i != 1)
+        s4o.print(", ");
+  
+      symbol_c *param_type = fp_iterator.param_type();
+      if (param_type == NULL) ERROR;
+  
+      function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+  
+  
+      symbol_c *param_value = NULL;
+  
+      /* if it is the first parameter, semantics specifies that we should
+       * get the value off the IL default variable!
+       */
+     if (1 == i)
+       param_value = &this->default_variable_name;
+  
+      /* Get the value from a foo(<param_name> = <param_value>) style call */
+      /* NOTE: the following line of code is not required in this case, but it doesn't
+       * harm to leave it in, as in the case of a non-formal syntax function call,
+       * it will always return NULL.
+       * We leave it in in case we later decide to merge this part of the code together
+       * with the function calling code in generate_c_st_c, which does require
+       * the following line...
+       */
+      if (param_value == NULL)
+        param_value = function_call_param_iterator.search(param_name);
+  
+      /* Get the value from a foo(<param_value>) style call */
+      if (param_value == NULL)
+        param_value = function_call_param_iterator.next();
+  
+      switch (param_direction) {
+        case function_param_iterator_c::direction_in:
+          if (param_value == NULL) {
+            /* No value given for parameter, so we must use the default... */
+            /* First check whether default value specified in function declaration...*/
+            param_value = fp_iterator.default_value();
+          }
+          if (param_value == NULL) {
+            /* If not, get the default value of this variable's type */
+            param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
+          }
+          if (param_value == NULL) ERROR;
+          param_value->accept(*this);
+          break;
+        case function_param_iterator_c::direction_out:
+        case function_param_iterator_c::direction_inout:
+          if (param_value == NULL) {
+  	  /* no parameter value given, so we pass a previously declared temporary variable. */
+            std::string *temp_var_name = temp_var_name_factory.new_name();
+            s4o.print(*temp_var_name);
+            delete temp_var_name;
+          } else {
+            param_value->accept(*this);
+          }
+          break;
+        case function_param_iterator_c::direction_extref:
+          /* TODO! */
+          ERROR;
+          break;
+      } /* switch */
+    } /* for(...) */
+  
+    s4o.print(")");
+    /* the data type returned by the function, and stored in the il default variable... */
+    default_variable_name.current_type = return_data_type;
+  }
+  
+  return NULL;
+}
+
+
+/* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
+//SYM_REF4(il_expression_c, il_expr_operator, il_operand, simple_instr_list, unused)
+void *visit(il_expression_c *symbol) {
+  /* We will be recursevely interpreting an instruction list,
+   * so we store a backup of the data type of the value currently stored
+   * in the default variable, and set the current data type to NULL
+   */
+  symbol_c *old_current_default_variable_data_type = this->default_variable_name.current_type;
+  this->default_variable_name.current_type = NULL;
+
+ /* Pass the symbol->il_operand to the simple_instr_list visitor
+  * using the il_default_variable_init_value parameter...
+  * Note that the simple_instr_list_c visitor will set this parameter
+  * to NULL as soon as it does not require it any longer,
+  * so we don't do it here again after the
+  *   symbol->simple_instr_list->accept(*this);
+  * returns...
+  */
+  this->il_default_variable_init_value = symbol->il_operand;
+
+  /* Now do the parenthesised instructions... */
+  /* NOTE: the following code line will get the variable
+   * this->default_variable_name.current_type updated!
+   */
+  symbol->simple_instr_list->accept(*this);
+
+  /* Now do the operation, using the previous result! */
+  /* NOTE: The result of the previous instruction list will be stored
+   * in a variable named IL_DEFVAR_BACK. This is done in the visitor
+   * to instruction_list_c objects...
+   */
+  this->current_operand = &(this->default_variable_back_name);
+  this->current_operand_type = this->default_variable_back_name.current_type;
+
+  this->default_variable_name.current_type = old_current_default_variable_data_type;
+  if (NULL == this->current_operand_type) ERROR;
+
+  symbol->il_expr_operator->accept(*this);
+
+  this->current_operand = NULL;
+  this->current_operand_type = NULL;
+  this->default_variable_back_name.current_type = NULL;
+  return NULL;
+}
+
+/*  il_jump_operator label */
+// SYM_REF2(il_jump_operation_c, il_jump_operator, label)
+void *visit(il_jump_operation_c *symbol) {
+ /* Pass the symbol->label to the il_jump_operation visitor
+  * using the jump_label parameter...
+  */
+  this->jump_label = symbol->label;
+  symbol->il_jump_operator->accept(*this);
+  this->jump_label = NULL;
+
+  return NULL;
+}
+
+/*   il_call_operator prev_declared_fb_name
+ * | il_call_operator prev_declared_fb_name '(' ')'
+ * | il_call_operator prev_declared_fb_name '(' eol_list ')'
+ * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
+ * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
+ */
+// SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list)
+void *visit(il_fb_call_c *symbol) {
+  symbol->il_call_operator->accept(*this);
+  s4o.print("{\n");
+  s4o.indent_right();
+  s4o.print(s4o.indent_spaces);
+
+  /* first figure out what is the name of the function block type of the function block being called... */
+  symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name);
+    /* should never occur. The function block instance MUST have been declared... */
+  if (function_block_type_name == NULL) ERROR;
+
+  /* Now find the declaration of the function block type being called... */
+  function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name);
+    /* should never occur. The function block type being called MUST be in the symtable... */
+  if (fb_decl == function_block_type_symtable.end_value()) ERROR;
+
+  /* loop through each function block parameter, find the value we should pass
+   * to it, and then output the c equivalent...
+   */
+  function_param_iterator_c fp_iterator(fb_decl);
+  identifier_c *param_name;
+  function_call_param_iterator_c function_call_param_iterator(symbol);
+  for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+    function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+
+    /* Get the value from a foo(<param_name> = <param_value>) style call */
+    symbol_c *param_value = function_call_param_iterator.search(param_name);
+
+    /* Get the value from a foo(<param_value>) style call */
+    if (param_value == NULL)
+      param_value = function_call_param_iterator.next();
+
+    /* now output the value assignment */
+    if (param_value != NULL)
+      if ((param_direction == function_param_iterator_c::direction_in) ||
+          (param_direction == function_param_iterator_c::direction_inout)) {
+        symbol->fb_name->accept(*this);
+        s4o.print(".");
+        param_name->accept(*this);
+        s4o.print(" = ");
+        param_value->accept(*this);
+        s4o.print(";\n" + s4o.indent_spaces);
+      }
+  } /* for(...) */
+
+  /* now call the function... */
+  function_block_type_name->accept(*this);
+  s4o.print(FB_FUNCTION_SUFFIX);
+  s4o.print("(&");
+  symbol->fb_name->accept(*this);
+  s4o.print(")");
+
+  /* loop through each function parameter, find the variable to which
+   * we should atribute the value of all output or inoutput parameters.
+   */
+  fp_iterator.reset();
+  function_call_param_iterator.reset();
+  for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+    function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+
+    /* Get the value from a foo(<param_name> = <param_value>) style call */
+    symbol_c *param_value = function_call_param_iterator.search(param_name);
+
+    /* Get the value from a foo(<param_value>) style call */
+    if (param_value == NULL)
+      param_value = function_call_param_iterator.next();
+
+    /* now output the value assignment */
+    if (param_value != NULL)
+      if ((param_direction == function_param_iterator_c::direction_out) ||
+          (param_direction == function_param_iterator_c::direction_inout)) {
+        s4o.print(";\n"+ s4o.indent_spaces);
+        param_value->accept(*this);
+        s4o.print(" = ");
+        symbol->fb_name->accept(*this);
+        s4o.print(".");
+        param_name->accept(*this);
+      }
+  } /* for(...) */
+
+  s4o.print(";\n");
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces);
+  s4o.print("}");
+
+  return NULL;
+}
+
+
+
+/* | function_name '(' eol_list [il_param_list] ')' */
+// SYM_REF2(il_formal_funct_call_c, function_name, il_param_list)
+void *visit(il_formal_funct_call_c *symbol) {
+  function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
+
+  if (f_decl == function_symtable.end_value())
+    /* should never occur. The function being called MUST be in the symtable... */
+    ERROR;
+
+  symbol->function_name->accept(*this);
+  s4o.print("(");
+
+  /* loop through each function parameter, find the value we should pass
+   * to it, and then output the c equivalent...
+   */
+
+  function_param_iterator_c fp_iterator(f_decl);
+  identifier_c *param_name;
+  function_call_param_iterator_c function_call_param_iterator(symbol);
+  for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+    if (i != 1)
+      s4o.print(", ");
+
+    symbol_c *param_type = fp_iterator.param_type();
+    if (param_type == NULL) ERROR;
+
+    function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+
+
+    symbol_c *param_value = NULL;
+
+    /* Get the value from a foo(<param_name> = <param_value>) style call */
+    if (param_value == NULL)
+      param_value = function_call_param_iterator.search(param_name);
+
+    /* Get the value from a foo(<param_value>) style call */
+    /* NOTE: the following line of code is not required in this case, but it doesn't
+     * harm to leave it in, as in the case of a formal syntax function call,
+     * it will always return NULL.
+     * We leave it in in case we later decide to merge this part of the code together
+     * with the function calling code in generate_c_st_c, which does require
+     * the following line...
+     */
+    if (param_value == NULL)
+      param_value = function_call_param_iterator.next();
+
+    switch (param_direction) {
+      case function_param_iterator_c::direction_in:
+        if (param_value == NULL) {
+          /* No value given for parameter, so we must use the default... */
+          /* First check whether default value specified in function declaration...*/
+          param_value = fp_iterator.default_value();
+        }
+        if (param_value == NULL) {
+          /* If not, get the default value of this variable's type */
+          param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
+        }
+        if (param_value == NULL) ERROR;
+        param_value->accept(*this);
+	break;
+      case function_param_iterator_c::direction_out:
+      case function_param_iterator_c::direction_inout:
+        if (param_value == NULL) {
+	  /* no parameter value given, so we pass a previously declared temporary variable. */
+          std::string *temp_var_name = temp_var_name_factory.new_name();
+          s4o.print(*temp_var_name);
+          delete temp_var_name;
+	} else {
+          param_value->accept(*this);
+	}
+	break;
+      case function_param_iterator_c::direction_extref:
+        /* TODO! */
+        ERROR;
+	break;
+    } /* switch */
+  } /* for(...) */
+
+  // symbol->parameter_assignment->accept(*this);
+  s4o.print(")");
+  return NULL;
+}
+
+
+/* | il_operand_list ',' il_operand */
+// SYM_LIST(il_operand_list_c)
+void *visit(il_operand_list_c *symbol) {ERROR; return NULL;} // should never get called!
+
+
+/* | simple_instr_list il_simple_instruction */
+// SYM_LIST(simple_instr_list_c)
+void *visit(simple_instr_list_c *symbol) {
+  /* A simple_instr_list_c is used to store a list of il operations
+   * being done within parenthesis...
+   *
+   * e.g.:
+   *         LD var1
+   *         AND ( var2
+   *         OR var3
+   *         OR var4
+   *         )
+   *
+   * This will be converted to C++ by defining a new scope
+   * with a new il default variable, and executing the il operands
+   * within this new scope.
+   * At the end of the scope the result, i.e. the value currently stored
+   * in the il default variable is copied to the variable used to take this
+   * value to the outside scope...
+   *
+   * The above example will result in the following C++ code:
+   * {__IL_DEFVAR_T __IL_DEFVAR_BACK;
+   *  __IL_DEFVAR_T __IL_DEFVAR;
+   *
+   *  __IL_DEFVAR.INTvar = var1;
+   *  {
+   *    __IL_DEFVAR_T __IL_DEFVAR;
+   *
+   *    __IL_DEFVAR.INTvar = var2;
+   *    __IL_DEFVAR.INTvar |= var3;
+   *    __IL_DEFVAR.INTvar |= var4;
+   *
+   *    __IL_DEFVAR_BACK = __IL_DEFVAR;
+   *  }
+   *  __IL_DEFVAR.INTvar &= __IL_DEFVAR_BACK.INTvar;
+   *
+   * }
+   *
+   *  The intial value of the il default variable (in the above
+   * example 'var2') is passed to this simple_instr_list_c visitor
+   * using the il_default_variable_init_value parameter.
+   * Since it is possible to have parenthesis inside other parenthesis
+   * recursively, we reset the il_default_variable_init_value to NULL
+   * as soon as we no longer require it, as it may be used once again
+   * in the line
+   *  print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n");
+   *
+   */
+
+  /* Declare the default variable, that will store the result of the IL operations... */
+  s4o.print("{\n");
+  s4o.indent_right();
+
+  s4o.print(s4o.indent_spaces);
+  s4o.print(IL_DEFVAR_T);
+  s4o.print(" ");
+  this->default_variable_name.accept(*this);
+  s4o.print(";\n\n");
+
+  /* Check whether we should initiliase the il default variable... */
+  if (NULL != this->il_default_variable_init_value) {
+    /* Yes, we must... */
+    /* We will do it by instatiating a LD operator, and having this
+     * same generate_c_il_c class visiting it!
+     */
+    LD_operator_c ld_oper;
+    il_simple_operation_c il_simple_oper(&ld_oper, this->il_default_variable_init_value);
+
+    s4o.print(s4o.indent_spaces);
+    il_simple_oper.accept(*this);
+    s4o.print(";\n");
+  }
+
+  /* this parameter no longer required... */
+  this->il_default_variable_init_value = NULL;
+
+  print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n");
+
+  /* copy the result in the default variable to the variable
+   * used to pass the data out to the scope enclosing
+   * the current scope!
+   *
+   * We also need to update the data type currently stored within
+   * the variable used to pass the data to the outside scope...
+   */
+  this->default_variable_back_name.current_type = this->default_variable_name.current_type;
+  s4o.print("\n");
+  s4o.print(s4o.indent_spaces);
+  this->default_variable_back_name.accept(*this);
+  s4o.print(" = ");
+  this->default_variable_name.accept(*this);
+  s4o.print(";\n");
+
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces);
+  s4o.print("}\n");
+  s4o.print(s4o.indent_spaces);
+  return NULL;
+}
+
+/* | il_initial_param_list il_param_instruction */
+// SYM_LIST(il_param_list_c)
+void *visit(il_param_list_c *symbol) {ERROR; return NULL;} // should never get called!
+
+/*  il_assign_operator il_operand
+ * | il_assign_operator '(' eol_list simple_instr_list ')'
+ */
+// SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused)
+void *visit(il_param_assignment_c *symbol) {ERROR; return NULL;} // should never get called!
+
+/*  il_assign_out_operator variable */
+// SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable);
+void *visit(il_param_out_assignment_c *symbol) {ERROR; return NULL;} // should never get called!
+
+/*******************/
+/* B 2.2 Operators */
+/*******************/
+
+void *visit(LD_operator_c *symbol)	{
+  /* the data type resulting from this operation... */
+  this->default_variable_name.current_type = this->current_operand_type;
+  XXX_operator(&(this->default_variable_name), " = ", this->current_operand);
+  return NULL;
+}
+
+void *visit(LDN_operator_c *symbol)	{
+  /* the data type resulting from this operation... */
+  this->default_variable_name.current_type = this->current_operand_type;
+  XXX_operator(&(this->default_variable_name),
+               search_expression_type->is_bool_type(this->current_operand_type)?" = !":" = ~",
+               this->current_operand);
+  return NULL;
+}
+
+void *visit(ST_operator_c *symbol)	{
+  XXX_operator(this->current_operand, " = ",&(this->default_variable_name));
+  /* the data type resulting from this operation is unchamged. */
+  return NULL;
+}
+
+void *visit(STN_operator_c *symbol)	{
+  XXX_operator(this->current_operand,
+               search_expression_type->is_bool_type(this->current_operand_type)?" = !":" = ~",
+               &(this->default_variable_name));
+  /* the data type resulting from this operation is unchamged. */
+  return NULL;
+}
+
+void *visit(NOT_operator_c *symbol)	{
+  if ((NULL != this->current_operand) || (NULL != this->current_operand_type)) ERROR;
+  XXX_operator(&(this->default_variable_name),
+               search_expression_type->is_bool_type(this->default_variable_name.current_type)?" = !":" = ~",
+               &(this->default_variable_name));
+  /* the data type resulting from this operation is unchanged. */
+  return NULL;
+}
+
+void *visit(S_operator_c *symbol)	{
+  if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR;
+
+  C_modifier();
+  this->current_operand->accept(*this);
+  s4o.print(search_expression_type->is_bool_type(this->current_operand_type)?" = true":" = 1");
+  /* the data type resulting from this operation is unchanged! */
+  return NULL;
+}
+
+void *visit(R_operator_c *symbol)	{
+  if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR;
+
+  C_modifier();
+  this->current_operand->accept(*this);
+  s4o.print(search_expression_type->is_bool_type(this->current_operand_type)?" = false":" = 0");
+  /* the data type resulting from this operation is unchanged! */
+  return NULL;
+}
+
+void *visit(S1_operator_c *symbol)	{return XXX_CAL_operator("S1", this->current_operand);}
+void *visit(R1_operator_c *symbol)	{return XXX_CAL_operator("R1", this->current_operand);}
+void *visit(CLK_operator_c *symbol)	{return XXX_CAL_operator("CLK", this->current_operand);}
+void *visit(CU_operator_c *symbol)	{return XXX_CAL_operator("CU", this->current_operand);}
+void *visit(CD_operator_c *symbol)	{return XXX_CAL_operator("CD", this->current_operand);}
+void *visit(PV_operator_c *symbol)	{return XXX_CAL_operator("PV", this->current_operand);}
+void *visit(IN_operator_c *symbol)	{return XXX_CAL_operator("IN", this->current_operand);}
+void *visit(PT_operator_c *symbol)	{return XXX_CAL_operator("PT", this->current_operand);}
+
+void *visit(AND_operator_c *symbol)	{
+  if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
+    XXX_operator(&(this->default_variable_name), " &= ", this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+  }
+  else {ERROR;}
+  return NULL;
+}
+
+void *visit(OR_operator_c *symbol)	{
+  if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
+    XXX_operator(&(this->default_variable_name), " |= ", this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+  }
+  else {ERROR;}
+  return NULL;
+}
+
+void *visit(XOR_operator_c *symbol)	{
+  if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
+    // '^' is a bit by bit exclusive OR !! Also seems to work with boolean types!
+    XXX_operator(&(this->default_variable_name), " ^= ", this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+  }
+  else {ERROR;}
+  return NULL;
+}
+
+void *visit(ANDN_operator_c *symbol)	{
+  if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
+    XXX_operator(&(this->default_variable_name),
+                 search_expression_type->is_bool_type(this->current_operand_type)?" &= !":" &= ~",
+                 this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+  }
+  else {ERROR;}
+  return NULL;
+}
+
+void *visit(ORN_operator_c *symbol)	{
+  if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
+    XXX_operator(&(this->default_variable_name),
+                 search_expression_type->is_bool_type(this->current_operand_type)?" |= !":" |= ~",
+                 this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+  }
+  else {ERROR;}
+  return NULL;
+}
+
+void *visit(XORN_operator_c *symbol)	{
+  if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
+    XXX_operator(&(this->default_variable_name),
+                 // bit by bit exclusive OR !! Also seems to work with boolean types!
+                 search_expression_type->is_bool_type(this->current_operand_type)?" ^= !":" ^= ~",
+                 this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+  }
+  else {ERROR;}
+  return NULL;
+}
+
+void *visit(ADD_operator_c *symbol)	{
+  if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_time_type(this->current_operand_type)) {
+    XXX_function("__time_add", &(this->default_variable_name), this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+    return NULL;
+  }
+  if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
+    XXX_operator(&(this->default_variable_name), " += ", this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+    return NULL;
+  }
+  ERROR;
+  return NULL;
+}
+
+void *visit(SUB_operator_c *symbol)	{
+  if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_time_type(this->current_operand_type)) {
+    XXX_function("__time_sub", &(this->default_variable_name), this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+    return NULL;
+  }
+  if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
+    XXX_operator(&(this->default_variable_name), " -= ", this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+    return NULL;
+  }
+  ERROR;
+  return NULL;
+}
+
+void *visit(MUL_operator_c *symbol)	{
+  if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_integer_type(this->current_operand_type)) {
+    XXX_function("__time_mul", &(this->default_variable_name), this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+    return NULL;
+  }
+  if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
+    XXX_operator(&(this->default_variable_name), " *= ", this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+    return NULL;
+  }
+  ERROR;
+  return NULL;
+}
+
+void *visit(DIV_operator_c *symbol)	{
+  if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
+    XXX_operator(&(this->default_variable_name), " /= ", this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+  }
+  else {ERROR;}
+  return NULL;
+}
+
+void *visit(MOD_operator_c *symbol)	{
+  if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
+    XXX_operator(&(this->default_variable_name), " %= ", this->current_operand);
+    /* the data type resulting from this operation... */
+    this->default_variable_name.current_type = this->current_operand_type;
+  }
+  else {ERROR;}
+  return NULL;
+}
+
+void *visit(GT_operator_c *symbol)	{
+  return CMP_operator(this->current_operand, "__gt_");
+}
+
+void *visit(GE_operator_c *symbol)	{
+  return CMP_operator(this->current_operand, "__ge_");
+}
+
+void *visit(EQ_operator_c *symbol)	{
+  return CMP_operator(this->current_operand, "__eq_");
+}
+
+void *visit(LT_operator_c *symbol)	{
+  return CMP_operator(this->current_operand, "__lt_");
+}
+
+void *visit(LE_operator_c *symbol)	{
+  return CMP_operator(this->current_operand, "__le_");
+}
+
+void *visit(NE_operator_c *symbol)	{
+  return CMP_operator(this->current_operand, "__ne_");
+}
+
+
+//SYM_REF0(CAL_operator_c)
+// This method will be called from within the il_fb_call_c visitor method
+void *visit(CAL_operator_c *symbol) {return NULL;}
+
+//SYM_REF0(CALC_operator_c)
+// This method will be called from within the il_fb_call_c visitor method
+void *visit(CALC_operator_c *symbol) {C_modifier(); return NULL;}
+
+//SYM_REF0(CALCN_operator_c)
+// This method will be called from within the il_fb_call_c visitor method
+void *visit(CALCN_operator_c *symbol) {CN_modifier(); return NULL;}
+
+/* NOTE: The semantics of the RET operator requires us to return a value
+ *       if the IL code is inside a function, but simply return no value if
+ *       the IL code is inside a function block or program!
+ *       Nevertheless, it is the generate_c_c class itself that
+ *       introduces the 'reaturn <value>' into the c++ code at the end
+ *       of every function. This class does not know whether the IL code
+ *       is inside a function or a function block.
+ *       We work around this by jumping to the end of the code,
+ *       that will be marked by the END_LABEL label in the
+ *       instruction_list_c visitor...
+ */
+// SYM_REF0(RET_operator_c)
+void *visit(RET_operator_c *symbol) {
+  s4o.print("goto ");s4o.print(END_LABEL);
+  return NULL;
+}
+
+// SYM_REF0(RETC_operator_c)
+void *visit(RETC_operator_c *symbol) {
+  C_modifier();
+  s4o.print("goto ");s4o.print(END_LABEL);
+  return NULL;
+}
+
+// SYM_REF0(RETCN_operator_c)
+void *visit(RETCN_operator_c *symbol) {
+  CN_modifier();
+  s4o.print("goto ");s4o.print(END_LABEL);
+  return NULL;
+}
+
+//SYM_REF0(JMP_operator_c)
+void *visit(JMP_operator_c *symbol)	{
+  if (NULL == this->jump_label) ERROR;
+
+  s4o.print("goto ");
+  this->jump_label->accept(*this);
+  /* the data type resulting from this operation is unchanged! */
+  return NULL;
+}
+
+// SYM_REF0(JMPC_operator_c)
+void *visit(JMPC_operator_c *symbol)	{
+  if (NULL == this->jump_label) ERROR;
+
+  C_modifier();
+  s4o.print("goto ");
+  this->jump_label->accept(*this);
+  /* the data type resulting from this operation is unchanged! */
+  return NULL;
+}
+
+// SYM_REF0(JMPCN_operator_c)
+void *visit(JMPCN_operator_c *symbol)	{
+  if (NULL == this->jump_label) ERROR;
+
+  CN_modifier();
+  s4o.print("goto ");
+  this->jump_label->accept(*this);
+  /* the data type resulting from this operation is unchanged! */
+  return NULL;
+}
+
+#if 0
+/*| [NOT] any_identifier SENDTO */
+SYM_REF2(il_assign_out_operator_c, option, variable_name)
+#endif
+
+}; /* generate_c_il_c */
+
+
+
+
+
+
+
+
+
+/* The implementation of the single visit() member function
+ * of il_default_variable_c.
+ * It can only come after the full declaration of
+ * generate_c_il_c. Since we define and declare
+ * generate_c_il_c simultaneously, it can only come
+ * after the definition...
+ */
+void *il_default_variable_c::accept(visitor_c &visitor) {
+  /* An ugly hack!! */
+  /* This is required because we need to over-ride the base
+   * accept(visitor_c &) method of the class symbol_c,
+   * so this method may be called through a symbol_c *
+   * reference!
+   *
+   * But, the visitor_c does not include a visitor to
+   * an il_default_variable_c, which means that we couldn't
+   * simply call visitor.visit(this);
+   *
+   * We therefore need to use the dynamic_cast hack!!
+   *
+   * Note too that we can't cast a visitor_c to a
+   * il_default_variable_visitor_c, since they are not related.
+   * Nor may the il_default_variable_visitor_c inherit from
+   * visitor_c, because then generate_c_il_c would contain
+   * two visitor_c base classes, one each through
+   * il_default_variable_visitor_c and generate_c_type_c
+   *
+   * We could use virtual inheritance of the visitor_c, but it
+   * would probably create more problems than it is worth!
+   */
+  generate_c_il_c *v;
+  v = dynamic_cast<generate_c_il_c *>(&visitor);
+  if (v == NULL) ERROR;
+
+  return v->visit(this);
+}
+
+
+
+
+il_default_variable_c::il_default_variable_c(const char *var_name_str, symbol_c *current_type) {
+  if (NULL == var_name_str) ERROR;
+  /* Note: current_type may start off with NULL */
+
+  this->var_name = new identifier_c(var_name_str);
+  if (NULL == this->var_name) ERROR;
+
+  this->current_type = current_type;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/generate_c_sfc.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,736 @@
+/*
+ * (c) 2007 Mario de Sousa, Laurent Bessard
+ *
+ * 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)
+ *
+ */
+
+
+/*
+ * Conversion of sfc networks (i.e. SFC code).
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the SFC, IL and ST
+ * code.
+ */
+
+#include <list>
+
+typedef struct
+{
+  transition_c *symbol;
+  int priority;
+  int index;
+} TRANSITION;
+
+
+
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+class generate_c_sfc_elements_c: public generate_c_base_c {
+  
+  public:
+    typedef enum {
+      transitionlist_sg,
+      transitiontest_sg,
+      stepset_sg,
+      stepreset_sg,
+      actionassociation_sg,
+      actionbody_sg
+    } sfcgeneration_t;
+
+  private:
+    generate_c_il_c *generate_c_il;
+    generate_c_st_c *generate_c_st;
+    generate_c_SFC_IL_ST_c *generate_c_code;
+    search_var_instance_decl_c *search_var_instance_decl;
+    
+    int transition_number;
+    std::list<TRANSITION> transition_list;
+    
+    symbol_c *current_step;
+    symbol_c *current_action;
+
+    sfcgeneration_t wanted_sfcgeneration;
+    
+  public:
+    generate_c_sfc_elements_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
+    : generate_c_base_c(s4o_ptr) {
+      generate_c_il = new generate_c_il_c(s4o_ptr, scope, variable_prefix);
+      generate_c_st = new generate_c_st_c(s4o_ptr, scope, variable_prefix);
+      generate_c_code = new generate_c_SFC_IL_ST_c(s4o_ptr, scope, variable_prefix);
+      search_var_instance_decl = new search_var_instance_decl_c(scope);
+      this->set_variable_prefix(variable_prefix);
+    }
+    
+    ~generate_c_sfc_elements_c(void) {
+      delete generate_c_il;
+      delete generate_c_st;
+      delete generate_c_code;
+      delete search_var_instance_decl;
+    }
+
+    void reset_transition_number(void) {transition_number = 0;}
+
+    void generate(symbol_c *symbol, sfcgeneration_t generation_type) {
+      wanted_sfcgeneration = generation_type;
+      switch (wanted_sfcgeneration) {
+        case transitiontest_sg:
+          {
+            std::list<TRANSITION>::iterator pt;
+            for(pt = transition_list.begin(); pt != transition_list.end(); pt++) {
+              transition_number = pt->index;
+              pt->symbol->accept(*this);
+            }
+          }
+          break;
+        default:
+          symbol->accept(*this);
+          break;
+      }
+    }
+
+    void print_step_argument(symbol_c *step_name, const char* argument) {
+      print_variable_prefix();
+      s4o.print("step_list[");
+      s4o.print(SFC_STEP_ACTION_PREFIX);
+      step_name->accept(*this);
+      s4o.print("].");
+      s4o.print(argument);
+    }
+
+    void print_action_argument(symbol_c *action_name, const char* argument) {
+      print_variable_prefix();
+      s4o.print("action_list[");
+      s4o.print(SFC_STEP_ACTION_PREFIX);
+      action_name->accept(*this);
+      s4o.print("].");
+      s4o.print(argument);
+    }      
+
+    void print_transition_number(void) {
+      char str[10];
+      sprintf(str, "%d", transition_number);
+      s4o.print(str);
+    }
+
+    void print_reset_step(symbol_c *step_name) {
+      s4o.print(s4o.indent_spaces);
+      print_step_argument(step_name, "state");
+      s4o.print(" = 0;\n");
+    }
+    
+    void print_set_step(symbol_c *step_name) {
+      s4o.print(s4o.indent_spaces);
+      print_step_argument(step_name, "state");
+      s4o.print(" = 1;\n" + s4o.indent_spaces);
+      print_step_argument(step_name, "elapsed_time");
+      s4o.print(" = __time_to_timespec(1, 0, 0, 0, 0, 0);\n");
+    }
+    
+    bool is_variable(symbol_c *symbol) {
+      /* we try to find the variable instance declaration, to determine its type... */
+      symbol_c *var_decl = search_var_instance_decl->get_decl(symbol);
+      
+      return var_decl != NULL;
+    }
+
+/*********************************************/
+/* B.1.6  Sequential function chart elements */
+/*********************************************/
+    
+    void *visit(initial_step_c *symbol) {
+      switch (wanted_sfcgeneration) {
+        case actionassociation_sg:
+          if (((list_c*)symbol->action_association_list)->n > 0) {
+            s4o.print(s4o.indent_spaces + "// ");
+            symbol->step_name->accept(*this);
+            s4o.print(" action associations\n");
+            current_step = symbol->step_name;
+            s4o.print(s4o.indent_spaces + "{\n");
+            s4o.indent_right();
+            s4o.print(s4o.indent_spaces + "char activated = ");
+            print_step_argument(current_step, "state");
+            s4o.print(" && !");
+            print_step_argument(current_step, "prev_state");
+            s4o.print(";\n");
+            s4o.print(s4o.indent_spaces + "char desactivated = !");
+            print_step_argument(current_step, "state");
+            s4o.print(" && ");
+            print_step_argument(current_step, "prev_state");
+            s4o.print(";\n");
+            s4o.print(s4o.indent_spaces + "char active = ");
+            print_step_argument(current_step, "state");
+            s4o.print(";\n");
+            symbol->action_association_list->accept(*this);
+            s4o.indent_left();
+            s4o.print(s4o.indent_spaces + "}\n\n");
+          }
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+    
+    void *visit(step_c *symbol) {
+      switch (wanted_sfcgeneration) {
+        case actionassociation_sg:
+          if (((list_c*)symbol->action_association_list)->n > 0) {
+            s4o.print(s4o.indent_spaces + "// ");
+            symbol->step_name->accept(*this);
+            s4o.print(" action associations\n");
+            current_step = symbol->step_name;
+            s4o.print(s4o.indent_spaces + "{\n");
+            s4o.indent_right();
+            s4o.print(s4o.indent_spaces + "char activated = ");
+            print_step_argument(current_step, "state");
+            s4o.print(" && !");
+            print_step_argument(current_step, "prev_state");
+            s4o.print(";\n");
+            s4o.print(s4o.indent_spaces + "char desactivated = !");
+            print_step_argument(current_step, "state");
+            s4o.print(" && ");
+            print_step_argument(current_step, "prev_state");
+            s4o.print(";\n");
+            s4o.print(s4o.indent_spaces + "char active = ");
+            print_step_argument(current_step, "state");
+            s4o.print(";\n");
+            symbol->action_association_list->accept(*this);
+            s4o.indent_left();
+            s4o.print(s4o.indent_spaces + "}\n\n");
+          }
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+
+    void *visit(transition_c *symbol) {
+      switch (wanted_sfcgeneration) {
+        case transitionlist_sg:
+          {
+            TRANSITION *transition;
+            transition = new TRANSITION;
+            transition->symbol = symbol;
+            transition->index = transition_number;
+            if (symbol->integer != NULL) {
+              transition->priority = atoi(((token_c *)symbol->integer)->value);
+              std::list<TRANSITION>::iterator pt = transition_list.begin();
+              while (pt != transition_list.end() && pt->priority <= transition->priority) {
+                pt++;
+              } 
+              transition_list.insert(pt, *transition);
+            } 
+            else {
+              transition->priority = 0;
+              transition_list.push_back(*transition);
+            }
+            transition_number++;
+          }
+          break;
+        case transitiontest_sg:
+          s4o.print(s4o.indent_spaces + "if (");
+          symbol->from_steps->accept(*this);
+          s4o.print(") {\n");
+          s4o.indent_right();
+          
+          // Calculate transition value
+          if (symbol->transition_condition_il != NULL) {
+            generate_c_il->declare_backup_variable();
+            s4o.print(s4o.indent_spaces);
+            symbol->transition_condition_il->accept(*generate_c_il);
+            print_variable_prefix();
+            s4o.print("transition_list[");
+            print_transition_number();
+            s4o.print("] = ");
+            generate_c_il->print_backup_variable();
+            s4o.print(";\n");
+          }
+          if (symbol->transition_condition_st != NULL) {
+            s4o.print(s4o.indent_spaces);
+            print_variable_prefix();
+            s4o.print("transition_list[");
+            print_transition_number();
+            s4o.print("] = ");
+            symbol->transition_condition_st->accept(*generate_c_st);
+            s4o.print(";\n");
+          }
+          if (symbol->integer != NULL) {
+            s4o.print(s4o.indent_spaces + "if (");
+            print_variable_prefix();
+            s4o.print("transition_list[");
+            print_transition_number();
+            s4o.print("]) {\n");
+            s4o.indent_right();
+            wanted_sfcgeneration = stepreset_sg;
+            symbol->from_steps->accept(*this);
+            wanted_sfcgeneration = transitiontest_sg;
+            s4o.indent_left();
+            s4o.print(s4o.indent_spaces + "}\n");
+          }
+          s4o.indent_left();
+          s4o.print(s4o.indent_spaces + "}\n" + s4o.indent_spaces + "else {\n");
+          s4o.indent_right();
+          s4o.print(s4o.indent_spaces);
+          print_variable_prefix();
+          s4o.print("transition_list[");
+          print_transition_number();
+          s4o.print("] = 0;\n");
+          s4o.indent_left();
+          s4o.print(s4o.indent_spaces + "}\n");
+          break;
+        case stepset_sg:
+          s4o.print(s4o.indent_spaces + "if (");
+          print_variable_prefix();
+          s4o.print("transition_list[");
+          print_transition_number();
+          s4o.print("]) {\n");
+          s4o.indent_right();
+          symbol->to_steps->accept(*this);
+          s4o.indent_left();
+          s4o.print(s4o.indent_spaces + "}\n");
+          transition_number++;
+          break;
+        case stepreset_sg:
+          if (symbol->integer == NULL) {
+            s4o.print(s4o.indent_spaces + "if (");
+            print_variable_prefix();
+            s4o.print("transition_list[");
+            print_transition_number();
+            s4o.print("]) {\n");
+            s4o.indent_right();
+            symbol->from_steps->accept(*this);
+            s4o.indent_left();
+            s4o.print(s4o.indent_spaces + "}\n");
+          }
+          transition_number++;
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+    
+    void *visit(action_c *symbol) {
+      switch (wanted_sfcgeneration) {
+        case actionbody_sg:
+          s4o.print(s4o.indent_spaces + "if(");
+          print_variable_prefix();
+          s4o.print("action_list[");
+          s4o.print(SFC_STEP_ACTION_PREFIX);
+          symbol->action_name->accept(*this);
+          s4o.print("].state) {");
+          s4o.indent_right();
+          
+          // generate action code
+          symbol->function_block_body->accept(*generate_c_code);
+          
+          s4o.indent_left();
+          s4o.print(s4o.indent_spaces + "}\n\n");
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+
+    void *visit(steps_c *symbol) {
+      if (symbol->step_name != NULL) {
+        switch (wanted_sfcgeneration) {
+          case transitiontest_sg:
+            print_step_argument(symbol->step_name, "state");
+            break;
+          case stepset_sg:
+            print_set_step(symbol->step_name);
+            break;
+          case stepreset_sg:
+            print_reset_step(symbol->step_name);
+            break;
+          default:
+            break;
+        }
+      }
+      else if (symbol->step_name_list != NULL) {
+        symbol->step_name_list->accept(*this);
+      }  
+      return NULL;
+    }
+    
+    void *visit(step_name_list_c *symbol) {
+      switch (wanted_sfcgeneration) {
+        case transitiontest_sg:
+          for(int i = 0; i < symbol->n; i++) {
+            print_step_argument(symbol->elements[i], "state");
+            if (i < symbol->n - 1) {
+              s4o.print(" && ");
+            }
+          }
+          break;
+        case stepset_sg:
+          for(int i = 0; i < symbol->n; i++) {
+            print_set_step(symbol->elements[i]);
+          }
+          break;
+        case stepreset_sg:
+          for(int i = 0; i < symbol->n; i++) {
+            print_reset_step(symbol->elements[i]);
+          }
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+
+    void *visit(action_association_list_c* symbol) {
+      switch (wanted_sfcgeneration) {
+        case actionassociation_sg:
+          print_list(symbol, "", "\n", "\n");
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+
+    void *visit(action_association_c *symbol) {
+      switch (wanted_sfcgeneration) {
+        case actionassociation_sg:
+          if (symbol->action_qualifier != NULL) {
+            current_action = symbol->action_name;
+            symbol->action_qualifier->accept(*this);
+          }
+          else {
+            s4o.print(s4o.indent_spaces + "if (");
+            print_step_argument(current_step, "state");
+            s4o.print(") {\n");
+            s4o.indent_right();
+            s4o.print(s4o.indent_spaces);
+            print_action_argument(symbol->action_name, "state");
+            s4o.print(" = 1;\n");
+            s4o.indent_left();
+            s4o.print(s4o.indent_spaces + "}");
+          }
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+
+    void *visit(action_qualifier_c *symbol) {
+      switch (wanted_sfcgeneration) {
+        case actionassociation_sg:
+          {
+            char *qualifier = (char *)symbol->action_qualifier->accept(*this);
+            s4o.print(s4o.indent_spaces + "if (");
+            if (strcmp(qualifier, "N") == 0) {
+              s4o.print("active");
+            }
+            if (strcmp(qualifier, "P") == 0 || strcmp(qualifier, "SD") == 0 || 
+                strcmp(qualifier, "DS") == 0 || strcmp(qualifier, "SL") == 0 || 
+                strcmp(qualifier, "S") == 0 || strcmp(qualifier, "R") == 0) {
+              s4o.print("activated");
+            }
+            if (strcmp(qualifier, "D") == 0 || strcmp(qualifier, "L") == 0) {
+              s4o.print("active && ");
+              print_step_argument(current_step, "elapsed_time");
+              if (strcmp(qualifier, "D") == 0) {
+                s4o.print(" >= ");
+              }
+              else {
+                s4o.print(" < ");
+              }
+              symbol->action_time->accept(*this);  
+            }
+            s4o.print(") {\n");
+            s4o.indent_right();
+            s4o.print(s4o.indent_spaces);
+            if (strcmp(qualifier, "N") == 0 || strcmp(qualifier, "P") == 0 ||
+                strcmp(qualifier, "D") == 0 || strcmp(qualifier, "L") == 0) {
+              print_action_argument(current_action, "state");
+              s4o.print(" = 1;\n");  
+            }
+            if (strcmp(qualifier, "S") == 0) {
+              if (is_variable(current_action)) {
+                print_variable_prefix();
+                current_action->accept(*this);
+              }
+              else
+                print_action_argument(current_action, "set");
+              s4o.print(" = 1;\n");
+            }
+            if (strcmp(qualifier, "R") == 0) {
+              if (is_variable(current_action)) {
+                print_variable_prefix();
+                current_action->accept(*this);
+                s4o.print(" = 0;\n");
+              }
+              else {
+                print_action_argument(current_action, "reset");
+                s4o.print(" = 1;\n");
+              }
+            }
+            if (strcmp(qualifier, "SD") == 0 || strcmp(qualifier, "DS") == 0 || 
+                strcmp(qualifier, "SL") == 0) {
+              if (strcmp(qualifier, "SL") == 0) {
+                print_action_argument(current_action, "reset_remaining_time");  
+              }
+              else {
+                print_action_argument(current_action, "set_remaining_time");
+              }
+              s4o.print(" = ");
+              symbol->action_time->accept(*this);
+              s4o.print(";\n");
+            }
+            s4o.indent_left();
+            s4o.print(s4o.indent_spaces + "}");
+            if (strcmp(qualifier, "DS") == 0) {
+              s4o.print("desactivated");
+              s4o.indent_right();
+              s4o.print(s4o.indent_spaces);
+              print_action_argument(current_action, "set_remaining_time");
+              s4o.print(" = __time_to_timespec(1, 0, 0, 0, 0, 0);\n");
+            }
+          }
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+
+    void *visit(qualifier_c *symbol) {
+      return (void *)symbol->value;
+    }
+
+    void *visit(timed_qualifier_c *symbol) {
+      return (void *)symbol->value;
+    }
+
+}; /* generate_c_sfc_actiondecl_c */
+ 
+ 
+ 
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+class generate_c_sfc_c: public generate_c_typedecl_c {
+  
+  private:
+    generate_c_sfc_elements_c *generate_c_sfc_elements;
+    
+  public:
+    generate_c_sfc_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
+    : generate_c_typedecl_c(s4o_ptr) {
+      generate_c_sfc_elements = new generate_c_sfc_elements_c(s4o_ptr, scope, variable_prefix);
+      this->set_variable_prefix(variable_prefix);
+    }
+  
+    virtual ~generate_c_sfc_c(void) {
+      delete generate_c_sfc_elements;
+    }
+
+/*********************************************/
+/* B.1.6  Sequential function chart elements */
+/*********************************************/
+
+    void *visit(sfc_network_c *symbol) {
+      generate_c_sfc_elements->generate((symbol_c *) symbol, generate_c_sfc_elements_c::transitionlist_sg);
+      s4o.print(s4o.indent_spaces +"INT i;\n\n");
+      s4o.print(s4o.indent_spaces +"BOOL transition;\n\n");
+            
+      /* generate step initilizations */
+      s4o.print(s4o.indent_spaces + "// Steps initialisation\n");
+      s4o.print(s4o.indent_spaces + "for (i = 0; i < ");
+      print_variable_prefix();
+      s4o.print("nb_steps; i++) {\n");
+      s4o.indent_right();
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("step_list[i].prev_state = ");
+      print_variable_prefix();
+      s4o.print("step_list[i].state;\n");
+      s4o.print(s4o.indent_spaces + "if (");
+      print_variable_prefix();
+      s4o.print("step_list[i].state) {\n");
+      s4o.indent_right();
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("step_list[i].elapsed_time = __time_add(");
+      print_variable_prefix();
+      s4o.print("step_list[i].elapsed_time, ");
+      print_variable_prefix();
+      s4o.print("period);\n");
+      s4o.indent_left();
+      s4o.print(s4o.indent_spaces + "}\n");
+      s4o.indent_left();
+      s4o.print(s4o.indent_spaces + "}\n");
+
+      /* generate action initilizations */
+      s4o.print(s4o.indent_spaces + "// Actions initialisation\n");
+      s4o.print(s4o.indent_spaces + "for (i = 0; i < ");
+      print_variable_prefix();
+      s4o.print("nb_actions; i++) {\n");
+      s4o.indent_right();
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].state = 0;\n");
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].set = 0;\n");
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].reset = 0;\n");
+      s4o.print(s4o.indent_spaces + "if (");
+      s4o.print("__gt_TIME(2, ");
+      print_variable_prefix();
+      s4o.print("action_list[i].set_remaining_time, __time_to_timespec(1, 0, 0, 0, 0, 0))) {\n");
+      s4o.indent_right();
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].set_remaining_time = __time_sub(");
+      print_variable_prefix();
+      s4o.print("action_list[i].set_remaining_time, ");
+      print_variable_prefix();
+      s4o.print("period);\n");
+      s4o.print(s4o.indent_spaces + "if (");
+      s4o.print("__le_TIME(2, ");
+      print_variable_prefix();
+      s4o.print("action_list[i].set_remaining_time, __time_to_timespec(1, 0, 0, 0, 0, 0))) {\n");
+      s4o.indent_right();
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].set_remaining_time = __time_to_timespec(1, 0, 0, 0, 0, 0);\n");
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].set = 1;\n");
+      s4o.indent_left();
+      s4o.print(s4o.indent_spaces + "}\n");
+      s4o.indent_left();
+      s4o.print(s4o.indent_spaces + "}\n");
+      s4o.print(s4o.indent_spaces + "if (");
+      s4o.print("__gt_TIME(2, ");
+      print_variable_prefix();
+      s4o.print("action_list[i].reset_remaining_time, __time_to_timespec(1, 0, 0, 0, 0, 0))) {\n");
+      s4o.indent_right();
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].reset_remaining_time = __time_sub(");
+      print_variable_prefix();
+      s4o.print("action_list[i].reset_remaining_time, ");
+      print_variable_prefix();
+      s4o.print("period);\n");
+      s4o.print(s4o.indent_spaces + "if (");
+      s4o.print("__le_TIME(2, ");
+      print_variable_prefix();
+      s4o.print("action_list[i].reset_remaining_time, __time_to_timespec(1, 0, 0, 0, 0, 0))) {\n");
+      s4o.indent_right();
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].reset_remaining_time = __time_to_timespec(1, 0, 0, 0, 0, 0);\n");
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].reset = 1;\n");
+      s4o.indent_left();
+      s4o.print(s4o.indent_spaces + "}\n");
+      s4o.indent_left();
+      s4o.print(s4o.indent_spaces + "}\n");
+      s4o.indent_left();
+      s4o.print(s4o.indent_spaces + "}\n\n");
+      
+      /* generate transition tests */
+      s4o.print(s4o.indent_spaces + "// Transitions fire test\n");
+      generate_c_sfc_elements->generate((symbol_c *) symbol, generate_c_sfc_elements_c::transitiontest_sg);
+      s4o.print("\n");
+      
+      /* generate transition reset steps */
+      s4o.print(s4o.indent_spaces + "// Transitions reset steps\n");
+      generate_c_sfc_elements->reset_transition_number();
+      generate_c_sfc_elements->generate((symbol_c *) symbol, generate_c_sfc_elements_c::stepreset_sg);
+      s4o.print("\n");
+      
+      /* generate transition set steps */
+      s4o.print(s4o.indent_spaces + "// Transitions set steps\n");
+      generate_c_sfc_elements->reset_transition_number();
+      generate_c_sfc_elements->generate((symbol_c *) symbol, generate_c_sfc_elements_c::stepset_sg);
+      s4o.print("\n");
+      
+       /* generate step association */
+      s4o.print(s4o.indent_spaces + "// Steps association\n");
+      generate_c_sfc_elements->generate((symbol_c *) symbol, generate_c_sfc_elements_c::actionassociation_sg);
+      s4o.print("\n");
+      
+      /* generate action state evaluation */
+      s4o.print(s4o.indent_spaces + "// Actions state evaluation\n");
+      s4o.print(s4o.indent_spaces + "for (i = 0; i < ");
+      print_variable_prefix();
+      s4o.print("nb_actions; i++) {\n");
+      s4o.indent_right();
+      s4o.print(s4o.indent_spaces + "if (");
+      print_variable_prefix();
+      s4o.print("action_list[i].set) {\n");
+      s4o.indent_right();
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].stored = 1;\n");
+      s4o.indent_left();
+      s4o.print(s4o.indent_spaces + "}\n" + s4o.indent_spaces + "if (");
+      print_variable_prefix();
+      s4o.print("action_list[i].reset) {\n");
+      s4o.indent_right();
+      s4o.print(s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].set_remaining_time = __time_to_timespec(1, 0, 0, 0, 0, 0);\n" + s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].reset_remaining_time = __time_to_timespec(1, 0, 0, 0, 0, 0);\n" + s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].stored = 0;\n");
+      s4o.indent_left();
+      s4o.print(s4o.indent_spaces + "}\n" + s4o.indent_spaces);
+      print_variable_prefix();
+      s4o.print("action_list[i].state |= ");
+      print_variable_prefix();
+      s4o.print("action_list[i].stored;\n");
+      s4o.indent_left();
+      s4o.print(s4o.indent_spaces + "}\n\n");
+      
+      /* generate action execution */
+      s4o.print(s4o.indent_spaces + "// Actions execution\n");
+      generate_c_sfc_elements->generate((symbol_c *) symbol, generate_c_sfc_elements_c::actionbody_sg);
+      s4o.print("\n");
+      
+      return NULL;
+    }
+
+    void generate(sequential_function_chart_c *sfc) {
+      sfc->accept(*this);
+    }
+
+}; /* generate_c_sfc_c */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/generate_c_sfcdecl.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,308 @@
+/*
+ * (c) 2007 Mario de Sousa, Laurent Bessard
+ *
+ * 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)
+ *
+ */
+
+
+/*
+ * Conversion of sfc networks (i.e. SFC code).
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the SFC, IL and ST
+ * code.
+ */
+
+
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+class generate_c_sfcdecl_c: protected generate_c_typedecl_c {
+  
+  public:
+      typedef enum {
+        sfcdecl_sd,
+        sfcinit_sd,
+        stepcount_sd,
+        stepdef_sd,
+        stepundef_sd,
+        actiondef_sd,
+        actionundef_sd,
+        actioncount_sd
+       } sfcdeclaration_t;
+  
+  private:
+    char step_number;
+    char action_number;
+    char transition_number;
+    
+    sfcdeclaration_t wanted_sfcdeclaration;
+    
+  public:
+    generate_c_sfcdecl_c(stage4out_c *s4o_ptr, sfcdeclaration_t sfcdeclaration)
+    : generate_c_typedecl_c(s4o_ptr) {
+      wanted_sfcdeclaration = sfcdeclaration;
+    }
+    ~generate_c_sfcdecl_c(void) {}
+    
+    void print(symbol_c *symbol, const char *variable_prefix = NULL) {
+      this->set_variable_prefix(variable_prefix);
+      
+      symbol->accept(*this);
+    }
+    
+/*********************************************/
+/* B.1.6  Sequential function chart elements */
+/*********************************************/
+    
+    void *visit(sequential_function_chart_c *symbol) {
+      step_number = 0;
+      action_number = 0;
+      transition_number = 0;
+      switch (wanted_sfcdeclaration) {
+        case sfcdecl_sd:
+          for(int i = 0; i < symbol->n; i++)
+            symbol->elements[i]->accept(*this);
+          
+          /* steps table declaration */
+          s4o.print(s4o.indent_spaces + "STEP step_list[");
+          s4o.print_integer(step_number);
+          s4o.print("];\n");
+          s4o.print(s4o.indent_spaces + "UINT nb_steps;\n");
+          
+          /* actions table declaration */
+          s4o.print(s4o.indent_spaces + "ACTION action_list[");
+          s4o.print_integer(action_number);
+          s4o.print("];\n");
+          s4o.print(s4o.indent_spaces + "UINT nb_actions;\n");
+          
+          /* transitions table declaration */
+          s4o.print(s4o.indent_spaces + "USINT transition_list[");
+          s4o.print_integer(transition_number);
+          s4o.print("];\n");
+          
+          /* period declaration */
+          s4o.print(s4o.indent_spaces + "TIME period;\n");
+          break;
+        case sfcinit_sd:
+          s4o.print(s4o.indent_spaces);
+          s4o.print("UINT i;\n");
+          
+          /* steps table count */
+          wanted_sfcdeclaration = stepcount_sd;
+          for(int i = 0; i < symbol->n; i++)
+            symbol->elements[i]->accept(*this);
+          s4o.print(s4o.indent_spaces);
+          print_variable_prefix();
+          s4o.print("nb_steps = ");
+          s4o.print_integer(step_number);
+          s4o.print(";\n");
+          step_number = 0;
+          wanted_sfcdeclaration = sfcinit_sd;
+          
+          /* steps table initialisation */
+          s4o.print(s4o.indent_spaces + "STEP temp_step = {0, 0, 0};\n");
+          s4o.print(s4o.indent_spaces + "for(i = 0; i < ");
+          print_variable_prefix();
+          s4o.print("nb_steps; i++) {\n");
+          s4o.indent_right();
+          s4o.print(s4o.indent_spaces);
+          print_variable_prefix();
+          s4o.print("step_list[i] = temp_step;\n");
+          s4o.indent_left();
+          s4o.print(s4o.indent_spaces + "}\n");
+          for(int i = 0; i < symbol->n; i++)
+            symbol->elements[i]->accept(*this);
+          
+          /* steps table count */
+          wanted_sfcdeclaration = actioncount_sd;
+          for(int i = 0; i < symbol->n; i++)
+            symbol->elements[i]->accept(*this);
+          s4o.print(s4o.indent_spaces);
+          print_variable_prefix();
+          s4o.print("nb_actions = ");
+          s4o.print_integer(action_number);
+          s4o.print(";\n");
+          action_number = 0;
+          wanted_sfcdeclaration = sfcinit_sd;
+          
+          /* actions table initialisation */
+          s4o.print(s4o.indent_spaces + "ACTION temp_action = {0, 0, 0, 0, 0, 0};\n");
+          s4o.print(s4o.indent_spaces + "for(i = 0; i < ");
+          print_variable_prefix();
+          s4o.print("nb_actions; i++) {\n");
+          s4o.indent_right();
+          s4o.print(s4o.indent_spaces);
+          print_variable_prefix();
+          s4o.print("action_list[i] = temp_action;\n");
+          
+          /* period initialisation */
+          s4o.print(s4o.indent_spaces);
+          print_variable_prefix();
+          s4o.print("period = __time_to_timespec(1, 0, 0, 0, 0, 0);\n");
+          
+          s4o.indent_left();
+          s4o.print(s4o.indent_spaces + "}\n");
+          break;
+        case stepdef_sd:
+          s4o.print("// Steps definitions\n");
+          for(int i = 0; i < symbol->n; i++)
+            symbol->elements[i]->accept(*this);
+          s4o.print("\n");
+          break;
+        case actiondef_sd:
+          s4o.print("// Actions definitions\n");
+          for(int i = 0; i < symbol->n; i++)
+            symbol->elements[i]->accept(*this);
+          s4o.print("\n");
+          break;
+        case stepundef_sd:
+          s4o.print("// Steps undefinitions\n");
+          for(int i = 0; i < symbol->n; i++)
+            symbol->elements[i]->accept(*this);
+          s4o.print("\n");
+          break;
+        case actionundef_sd:
+          s4o.print("// Actions undefinitions\n");
+          for(int i = 0; i < symbol->n; i++)
+            symbol->elements[i]->accept(*this);
+          s4o.print("\n");
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+    
+    void *visit(initial_step_c *symbol) {
+      switch (wanted_sfcdeclaration) {
+        case stepcount_sd:
+        case sfcdecl_sd:
+          step_number++;
+          break;
+        case sfcinit_sd:
+          s4o.print(s4o.indent_spaces);
+          print_variable_prefix();
+          s4o.print("step_list[");
+          s4o.print_integer(step_number);
+          s4o.print("].state = 1;\n");
+          step_number++;
+          break;
+        case stepdef_sd:
+          s4o.print("#define ");
+          s4o.print(SFC_STEP_ACTION_PREFIX);
+          symbol->step_name->accept(*this);
+          s4o.print(" ");
+          s4o.print_integer(step_number);
+          s4o.print("\n");
+          step_number++;
+          break;
+        case stepundef_sd:
+          s4o.print("#undef ");
+          s4o.print(SFC_STEP_ACTION_PREFIX);
+          symbol->step_name->accept(*this);
+          s4o.print("\n");
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+    
+    void *visit(step_c *symbol) {
+      switch (wanted_sfcdeclaration) {
+        case stepcount_sd:
+        case sfcdecl_sd:
+          step_number++;
+          break;
+        case stepdef_sd:
+          s4o.print("#define ");
+          s4o.print(SFC_STEP_ACTION_PREFIX);
+          symbol->step_name->accept(*this);
+          s4o.print(" ");
+          s4o.print_integer(step_number);
+          s4o.print("\n");
+          step_number++;
+          break;
+        case stepundef_sd:
+          s4o.print("#undef ");
+          s4o.print(SFC_STEP_ACTION_PREFIX);
+          symbol->step_name->accept(*this);
+          s4o.print("\n");
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+
+    void *visit(transition_c *symbol) {
+      switch (wanted_sfcdeclaration) {
+        case sfcdecl_sd:
+          transition_number++;
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+
+    void *visit(action_c *symbol) {
+      switch (wanted_sfcdeclaration) {
+        case actiondef_sd:
+          s4o.print("#define ");
+          s4o.print(SFC_STEP_ACTION_PREFIX);
+          symbol->action_name->accept(*this);
+          s4o.print(" ");
+          s4o.print_integer(action_number);
+          s4o.print("\n");
+          action_number++;
+          break;
+        case actionundef_sd:
+          s4o.print("#undef ");
+          s4o.print(SFC_STEP_ACTION_PREFIX);
+          symbol->action_name->accept(*this);
+          s4o.print("\n");
+          break;
+        case actioncount_sd:
+        case sfcdecl_sd:
+          action_number++;
+          break;
+        default:
+          break;
+      }
+      return NULL;
+    }
+
+    void *visit(instruction_list_c *symbol) {
+      return NULL;
+    }
+    
+    void *visit(statement_list_c *symbol) {
+      return NULL;
+    }
+
+}; /* generate_c_sfcdecl_c */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/generate_c_st.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,847 @@
+/*
+ * (c) 2003 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)
+ *
+ */
+
+
+/*
+ * Conversion of st statements (i.e. ST code).
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+
+class generate_c_st_c: public generate_c_typedecl_c {
+
+  private:
+    /* When calling a function block, we must first find it's type,
+     * by searching through the declarations of the variables currently
+     * in scope.
+     * This class does just that...
+     * A new class is instantiated whenever we begin generating the code
+     * for a function block type declaration, or a program declaration.
+     * This object instance will then later be called while the
+     * function block's or the program's body is being handled.
+     *
+     * Note that functions cannot contain calls to function blocks,
+     * so we do not create an object instance when handling
+     * a function declaration.
+     */
+    search_fb_instance_decl_c *search_fb_instance_decl;
+
+    /* When compiling st code, it becomes necessary to determine the
+     * data type of st expressions. To do this, we must first find the
+     * st operand's declaration, within the scope of the function block
+     * or function currently being processed.
+     * The following object does just that...
+     * This object instance will then later be called while the
+     * remaining st code is being handled.
+     */
+    search_expression_type_c *search_expression_type;
+
+    search_varfb_instance_type_c *search_varfb_instance_type;
+
+  public:
+    generate_c_st_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
+    : generate_c_typedecl_c(s4o_ptr) {
+      search_fb_instance_decl = new search_fb_instance_decl_c(scope);
+      search_expression_type = new search_expression_type_c(scope);
+      search_varfb_instance_type = new search_varfb_instance_type_c(scope);
+      this->set_variable_prefix(variable_prefix);
+    }
+
+    virtual ~generate_c_st_c(void) {
+      delete search_fb_instance_decl;
+      delete search_expression_type;
+      delete search_varfb_instance_type;
+    }
+
+
+  private:
+    /* Some function calls in the body of functions or function blocks
+     * may leave some parameters to their default values, and
+     * ignore some output parameters of the function being called.
+     * Our conversion of ST functions to C++ does not contemplate that,
+     * i.e. each called function must get all it's input and output
+     * parameters set correctly.
+     * For input parameters we merely need to call the function with
+     * the apropriate default value, but for output parameters
+     * we must create temporary variables to hold the output value.
+     *
+     * We declare all the temporary output variables at the begining of
+     * the body of each function or function block, and use them as
+     * in function calls later on as they become necessary...
+     * Note that we cannot create these variables just before a function
+     * call, as the function call itself may be integrated within an
+     * expression, or another function call!
+     *
+     * The variables are declared in the exact same order in which they
+     * will be used later on during the function calls, which allows us
+     * to simply re-create the name that was used for the temporary variable
+     * instead of keeping it in some list.
+     * The names are recreated by the temp_var_name_factory, after reset()
+     * has been called!
+     *
+     * This function will genertae code similar to...
+     *
+     *     INT __TMP_0 = 23;
+     *     REAL __TMP_1 = 45.5;
+     *     ...
+     */
+    temp_var_name_c temp_var_name_factory;
+
+  public:
+    void generate(statement_list_c *stl) {
+      generate_c_tempvardecl_c generate_c_tempvardecl(&s4o);
+      generate_c_tempvardecl.generate(stl, &temp_var_name_factory);
+      stl->accept(*this);
+    }
+
+  private:
+
+/*********************/
+/* B 1.4 - Variables */
+/*********************/
+void *visit(symbolic_variable_c *symbol) {
+  unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
+  if (vartype == search_var_instance_decl_c::external_vt || vartype == search_var_instance_decl_c::located_vt) {
+    s4o.print("*(");
+    generate_c_base_c::visit(symbol);
+    s4o.print(")");
+  }
+  else {
+    generate_c_base_c::visit(symbol);
+  }
+  return NULL;
+}
+
+/********************************************/
+/* B.1.4.1   Directly Represented Variables */
+/********************************************/
+// direct_variable: direct_variable_token   {$$ = new direct_variable_c($1);};
+void *visit(direct_variable_c *symbol) {
+  TRACE("direct_variable_c");
+  /* Do not use print_token() as it will change everything into uppercase */
+  if (strlen(symbol->value) == 0) ERROR;
+  s4o.print("*(");
+  this->print_variable_prefix();
+  s4o.printlocation(symbol->value + 1);
+  s4o.print(")");
+  return NULL;
+}
+
+/***************************************/
+/* B.3 - Language ST (Structured Text) */
+/***************************************/
+/***********************/
+/* B 3.1 - Expressions */
+/***********************/
+void *visit(or_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_bool_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " || ");
+  if (search_expression_type->is_binary_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " | ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(xor_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_bool_type(left_type)) {
+    s4o.print("(");
+    symbol->l_exp->accept(*this);
+    s4o.print(" && !");
+    symbol->r_exp->accept(*this);
+    s4o.print(") || (!");
+    symbol->l_exp->accept(*this);
+    s4o.print(" && ");
+    symbol->r_exp->accept(*this);
+    s4o.print(")");
+  }
+  if (search_expression_type->is_binary_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " ^ ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(and_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_bool_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " && ");
+  if (search_expression_type->is_binary_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " & ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(equ_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_time_type(left_type))
+    return print_compare_function("__eq_", left_type, symbol->l_exp, symbol->r_exp);
+  return print_binary_expression(symbol->l_exp, symbol->r_exp, " == ");
+}
+
+void *visit(notequ_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_time_type(left_type))
+    return print_compare_function("__ne_", left_type, symbol->l_exp, symbol->r_exp);
+  return print_binary_expression(symbol->l_exp, symbol->r_exp, " != ");
+}
+
+void *visit(lt_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_time_type(left_type))
+    return print_compare_function("__lt_", left_type, symbol->l_exp, symbol->r_exp);
+  return print_binary_expression(symbol->l_exp, symbol->r_exp, " < ");
+}
+
+void *visit(gt_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_time_type(left_type))
+    return print_compare_function("__gt_", left_type, symbol->l_exp, symbol->r_exp);
+  return print_binary_expression(symbol->l_exp, symbol->r_exp, " > ");
+}
+
+void *visit(le_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_time_type(left_type))
+    return print_compare_function("__le_", left_type, symbol->l_exp, symbol->r_exp);
+  return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= ");
+}
+
+void *visit(ge_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_time_type(left_type))
+    return print_compare_function("__ge_", left_type, symbol->l_exp, symbol->r_exp);
+  return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= ");
+}
+
+void *visit(add_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+	symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+	if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
+      (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
+      (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)))
+    return print_binary_function("__time_add", symbol->l_exp, symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " + ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(sub_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
+      (typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) ||
+      (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
+      (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) ||
+      (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
+      (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c)))
+    return print_binary_function("__time_sub", symbol->l_exp, symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " - ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(mul_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) ||
+      (typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type)))
+    return print_binary_function("__time_mul", symbol->l_exp, symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " * ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(div_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " / ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(mod_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) {
+    s4o.print("((");
+    symbol->r_exp->accept(*this);
+    s4o.print(" == 0)?0:");
+    print_binary_expression(symbol->l_exp, symbol->r_exp, " % ");
+    s4o.print(")");
+    return NULL;
+  }
+  ERROR;
+  return NULL;
+}
+
+/* TODO: power expression... */
+void *visit(power_expression_c *symbol) {ERROR; return print_binary_expression(symbol->l_exp, symbol->r_exp, " ** ");}
+void *visit(neg_expression_c *symbol) {return print_unary_expression(symbol->exp, " -");}
+
+void *visit(not_expression_c *symbol) {
+  symbol_c *exp_type = search_expression_type->get_type(symbol->exp);
+  if (!search_expression_type->is_binary_type(exp_type))
+     ERROR;
+  return print_unary_expression(symbol->exp, search_expression_type->is_bool_type(exp_type)?"!":"~");
+}
+
+void *visit(function_invocation_c *symbol) {
+  function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
+  
+  if (f_decl == function_symtable.end_value()) {
+    /* The function called is not in the symtable, so we test if it is a
+     * standard function defined in standard */
+    
+    function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
+    if (current_function_type == function_none) ERROR;
+    
+    symbol_c *function_return_type = search_expression_type->get_type(symbol);
+    
+    function_call_param_iterator_c function_call_param_iterator(symbol);
+    
+    int nb_param = ((list_c *)symbol->parameter_assignment_list)->n;
+
+#include "st_code_gen.c"
+
+#if 0
+    for(int current_param = 0; current_param < nb_param; current_param++) {
+      symbol_c *param_name = NULL;
+      switch (current_function_type) {
+        case function_add:
+        case function_and:
+        case function_or:
+          param_name = generate_param_name("IN%d", current_param + 1);
+          break;
+        case function_sub:
+          if (current_param < 2)
+            param_name = generate_param_name("IN%d", current_param + 1);
+          else
+            ERROR;
+          break;
+        default: ERROR;
+      }
+      
+      /* Get the value from a foo(<param_name> = <param_value>) style call */
+      symbol_c *param_value = function_call_param_iterator.search(param_name);
+      delete param_name;
+      
+      /* Get the value from a foo(<param_value>) style call */
+      if (param_value == NULL)
+        param_value = function_call_param_iterator.next();
+      
+      if (param_value == NULL) ERROR;
+      
+      switch (current_function_type) {
+        case function_add:
+          if (search_expression_type->is_time_type(function_return_type)) {
+            if (current_param == 0) {
+              s4o.print("__time_add(");
+              param_value->accept(*this);
+            }
+            else if (current_param == 1) {
+              s4o.print(", ");
+              param_value->accept(*this);
+              s4o.print(")");
+            }
+            else ERROR;
+          }
+          else {
+            if (current_param == 0)
+              s4o.print("(");
+            else
+              s4o.print(" + ");
+            param_value->accept(*this);
+            if (current_param == nb_param - 1)
+              s4o.print(")");
+          }
+          break;
+        case function_sub:
+          if (search_expression_type->is_time_type(function_return_type)) {
+            if (current_param == 0) {
+              s4o.print("__time_sub(");
+              param_value->accept(*this);
+            }
+            else if (current_param == 1) {
+              s4o.print(", ");
+              param_value->accept(*this);
+              s4o.print(")");
+            }
+            else ERROR;
+          }
+          else {
+            if (current_param == 0) {
+              s4o.print("(");
+              param_value->accept(*this);
+            }
+            else if (current_param == 1) {
+              s4o.print(" - ");
+              param_value->accept(*this);
+              s4o.print(")");
+            }
+            else ERROR;
+          }
+          break;
+        case function_and:
+          if (current_param == 0)
+            s4o.print("(");
+          else
+            if (search_expression_type->is_bool_type(function_return_type))
+              s4o.print(" && ");
+            else
+              s4o.print(" & ");
+          param_value->accept(*this);
+          if (current_param == nb_param - 1)
+            s4o.print(")");
+          break;
+        case function_or:
+          if (current_param == 0)
+            s4o.print("(");
+          else
+            if (search_expression_type->is_bool_type(function_return_type))
+              s4o.print(" || ");
+            else
+              s4o.print(" | ");
+          param_value->accept(*this);
+          if (current_param == nb_param - 1)
+            s4o.print(")");
+          break;
+        default: ERROR;
+      }
+    } /* for(...) */
+#endif
+  }
+  else {
+    /* loop through each function parameter, find the value we should pass
+     * to it, and then output the c equivalent...
+     */
+    function_param_iterator_c fp_iterator(f_decl);
+  
+    symbol->function_name->accept(*this);
+    s4o.print("(");
+    s4o.indent_right();
+  
+    identifier_c *param_name;
+    function_call_param_iterator_c function_call_param_iterator(symbol);
+    for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+      if (i != 1)
+        s4o.print(",\n"+s4o.indent_spaces);
+  
+      function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+  
+      /* Get the value from a foo(<param_name> = <param_value>) style call */
+      symbol_c *param_value = function_call_param_iterator.search(param_name);
+  
+      /* Get the value from a foo(<param_value>) style call */
+      if (param_value == NULL)
+        param_value = function_call_param_iterator.next();
+  
+      symbol_c *param_type = fp_iterator.param_type();
+      if (param_type == NULL) ERROR;
+  
+      switch (param_direction) {
+        case function_param_iterator_c::direction_in:
+          if (param_value == NULL) {
+            /* No value given for parameter, so we must use the default... */
+            /* First check whether default value specified in function declaration...*/
+            param_value = fp_iterator.default_value();
+          }
+          if (param_value == NULL) {
+            /* If not, get the default value of this variable's type */
+            param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
+          }
+          if (param_value == NULL) ERROR;
+          param_value->accept(*this);
+          break;
+        case function_param_iterator_c::direction_out:
+        case function_param_iterator_c::direction_inout:
+          if (param_value == NULL) {
+            /* no parameter value given, so we pass a previously declared temporary variable. */
+            std::string *temp_var_name = temp_var_name_factory.new_name();
+            s4o.print(*temp_var_name);
+            delete temp_var_name;
+          } else {
+            param_value->accept(*this);
+          }
+          break;
+        case function_param_iterator_c::direction_extref:
+          /* TODO! */
+          ERROR;
+          break;
+      } /* switch */
+    } /* for(...) */
+    // symbol->parameter_assignment->accept(*this);
+    s4o.print(")");
+    s4o.indent_left();
+  }
+
+  return NULL;
+}
+
+/********************/
+/* B 3.2 Statements */
+/********************/
+void *visit(statement_list_c *symbol) {
+  return print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n");
+}
+
+/*********************************/
+/* B 3.2.1 Assignment Statements */
+/*********************************/
+void *visit(assignment_statement_c *symbol) {
+  symbol->l_exp->accept(*this);
+  s4o.print(" = ");
+  symbol->r_exp->accept(*this);
+  return NULL;
+}
+
+/*****************************************/
+/* B 3.2.2 Subprogram Control Statements */
+/*****************************************/
+
+/* fb_name '(' [param_assignment_list] ')' */
+/* param_assignment_list -> may be NULL ! */
+//SYM_REF2(fb_invocation_c, fb_name, param_assignment_list)
+void *visit(fb_invocation_c *symbol) {
+  TRACE("fb_invocation_c");
+  /* first figure out what is the name of the function block type of the function block being called... */
+  symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name);
+    /* should never occur. The function block instance MUST have been declared... */
+  if (function_block_type_name == NULL) ERROR;
+
+  /* Now find the declaration of the function block type being called... */
+  function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name);
+    /* should never occur. The function block type being called MUST be in the symtable... */
+  if (fb_decl == function_block_type_symtable.end_value()) ERROR;
+
+  /* loop through each function block parameter, find the value we should pass
+   * to it, and then output the c equivalent...
+   */
+  function_param_iterator_c fp_iterator(fb_decl);
+  identifier_c *param_name;
+  function_call_param_iterator_c function_call_param_iterator(symbol);
+  for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+    function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+
+    /* Get the value from a foo(<param_name> = <param_value>) style call */
+    symbol_c *param_value = function_call_param_iterator.search(param_name);
+
+    /* Get the value from a foo(<param_value>) style call */
+    if (param_value == NULL)
+      param_value = function_call_param_iterator.next();
+
+    /* now output the value assignment */
+    if (param_value != NULL)
+      if ((param_direction == function_param_iterator_c::direction_in) ||
+          (param_direction == function_param_iterator_c::direction_inout)) {
+        print_variable_prefix();
+        symbol->fb_name->accept(*this);
+        s4o.print(".");
+        param_name->accept(*this);
+        s4o.print(" = ");
+        param_value->accept(*this);
+        s4o.print(";\n" + s4o.indent_spaces);
+      }
+  } /* for(...) */
+
+  /* now call the function... */
+  function_block_type_name->accept(*this);
+  s4o.print(FB_FUNCTION_SUFFIX);
+  s4o.print("(&");
+  print_variable_prefix();
+  symbol->fb_name->accept(*this);
+  s4o.print(")");
+
+  /* loop through each function parameter, find the variable to which
+   * we should atribute the value of all output or inoutput parameters.
+   */
+  fp_iterator.reset();
+  function_call_param_iterator.reset();
+  for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+    function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+
+    /* Get the value from a foo(<param_name> = <param_value>) style call */
+    symbol_c *param_value = function_call_param_iterator.search(param_name);
+
+    /* Get the value from a foo(<param_value>) style call */
+    if (param_value == NULL)
+      param_value = function_call_param_iterator.next();
+
+    /* now output the value assignment */
+    if (param_value != NULL)
+      if ((param_direction == function_param_iterator_c::direction_out) ||
+          (param_direction == function_param_iterator_c::direction_inout)) {
+        s4o.print(";\n"+ s4o.indent_spaces);
+        param_value->accept(*this);
+        s4o.print(" = ");
+        print_variable_prefix();
+        symbol->fb_name->accept(*this);
+        s4o.print(".");
+        param_name->accept(*this);
+      }
+  } /* for(...) */
+
+  return NULL;
+}
+
+
+
+
+/* helper symbol for fb_invocation */
+/* param_assignment_list ',' param_assignment */
+void *visit(param_assignment_list_c *symbol) {
+  TRACE("param_assignment_list_c");
+  /* this should never be called... */
+  ERROR;
+  return NULL;
+//  return print_list(symbol, "", ", ");
+}
+
+
+void *visit(input_variable_param_assignment_c *symbol) {
+  TRACE("input_variable_param_assignment_c");
+  /* this should never be called... */
+  ERROR;
+  return NULL;
+/*
+  symbol->variable_name->accept(*this);
+  s4o.print(" = ");
+  symbol->expression->accept(*this);
+  return NULL;
+*/
+}
+
+void *visit(output_variable_param_assignment_c *symbol) {
+  TRACE("output_variable_param_assignment_c");
+  /* this should never be called... */
+  ERROR;
+  return NULL;
+/*
+  s4o.print(s4o.indent_spaces);
+  if (symbol->not_param != NULL)
+    symbol->not_param->accept(*this);
+  symbol->variable_name->accept(*this);
+  s4o.print(" => ");
+  symbol->variable->accept(*this);
+  return NULL;
+*/
+}
+
+// TODO: the NOT symbol in function (block) calls...
+void *visit(not_paramassign_c *symbol) {
+  TRACE("not_paramassign_c");
+  /* this should never be called... */
+  ERROR;
+  return NULL;
+/*
+  s4o.print("NOT ");
+  return NULL;
+*/
+}
+
+
+/********************************/
+/* B 3.2.3 Selection Statements */
+/********************************/
+void *visit(if_statement_c *symbol) {
+  s4o.print("if (");
+  symbol->expression->accept(*this);
+  s4o.print(") {\n");
+  s4o.indent_right();
+  symbol->statement_list->accept(*this);
+  s4o.indent_left();
+  symbol->elseif_statement_list->accept(*this);
+
+  if (symbol->else_statement_list != NULL) {
+    s4o.print(s4o.indent_spaces); s4o.print("} else {\n");
+    s4o.indent_right();
+    symbol->else_statement_list->accept(*this);
+    s4o.indent_left();
+  }
+  s4o.print(s4o.indent_spaces); s4o.print("}");
+  return NULL;
+}
+
+/* helper symbol for if_statement */
+void *visit(elseif_statement_list_c *symbol) {return print_list(symbol);}
+
+/* helper symbol for elseif_statement_list */
+void *visit(elseif_statement_c *symbol) {
+  s4o.print(s4o.indent_spaces); s4o.print("} else if (");
+  symbol->expression->accept(*this);
+  s4o.print(") {\n");
+  s4o.indent_right();
+  symbol->statement_list->accept(*this);
+  s4o.indent_left();
+  return NULL;
+}
+
+void *visit(case_statement_c *symbol) {
+  s4o.print("switch(");
+  symbol->expression->accept(*this);
+  s4o.print(") {\n");
+  s4o.indent_right();
+  symbol->case_element_list->accept(*this);
+  if (symbol->statement_list != NULL) {
+    s4o.print(s4o.indent_spaces + "default:\n");
+    s4o.indent_right();
+    symbol->statement_list->accept(*this);
+    s4o.print(s4o.indent_spaces + "break;\n");
+    s4o.indent_left();
+  }
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces + "}");
+  return NULL;
+}
+
+/* helper symbol for case_statement */
+void *visit(case_element_list_c *symbol) {return print_list(symbol);}
+
+void *visit(case_element_c *symbol) {
+  s4o.print(s4o.indent_spaces + "case ");
+  symbol->case_list->accept(*this);
+  s4o.print(" :\n");
+  s4o.indent_right();
+  symbol->statement_list->accept(*this);
+  s4o.print(s4o.indent_spaces + "break;\n");
+  s4o.indent_left();
+  return NULL;
+}
+
+void *visit(case_list_c *symbol) {return print_list(symbol, "", ", ");}
+
+/********************************/
+/* B 3.2.4 Iteration Statements */
+/********************************/
+void *visit(for_statement_c *symbol) {
+  s4o.print("for(");
+  symbol->control_variable->accept(*this);
+  s4o.print(" = ");
+  symbol->beg_expression->accept(*this);
+  s4o.print("; ");
+  symbol->control_variable->accept(*this);
+  s4o.print(" != ");
+  symbol->end_expression->accept(*this);
+  s4o.print("; ");
+  symbol->control_variable->accept(*this);
+  if (symbol->by_expression != NULL) {
+    s4o.print(" += ");
+    symbol->by_expression->accept(*this);
+  } else {
+    s4o.print("++");
+  }
+  s4o.print(") {\n");
+  s4o.indent_right();
+  symbol->statement_list->accept(*this);
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces); s4o.print("}");
+  return NULL;
+}
+void *visit(while_statement_c *symbol) {
+  s4o.print("while (");
+  symbol->expression->accept(*this);
+  s4o.print(") {\n");
+  s4o.indent_right();
+  symbol->statement_list->accept(*this);
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces); s4o.print("}");
+  return NULL;
+}
+void *visit(repeat_statement_c *symbol) {
+  s4o.print("do {\n");
+  s4o.indent_right();
+  symbol->statement_list->accept(*this);
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces); s4o.print("} while(");
+  symbol->expression->accept(*this);
+  s4o.print(")");
+  return NULL;
+}
+void *visit(exit_statement_c *symbol) {
+  s4o.print("exit(0)");
+  return NULL;
+}
+
+
+
+}; /* generate_c_st_c */
+
+
+
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/generate_c_tempvardecl.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,179 @@
+/*
+ * (c) 2003 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)
+ *
+ */
+
+
+/*
+ * Declare temporary variables to be later used as output parameters
+ * in function calls for which not all output parameters were
+ * defined in the original (st or il) source code.
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+
+
+
+class temp_var_name_c {
+  private:
+    int counter;
+
+  public:
+    void reset(void) {counter = 0;}
+    temp_var_name_c(void) {reset();}
+
+  public:
+    std::string *new_name(void) {
+      std::string *new_str = new std::string(TEMP_VAR);
+      /* yikes!!! How to convert an int to a string elegantly???
+       * Right now I (Mario) can only think of snprintf()
+       * C++ must have a more elegant method!
+       */
+      int int_str_size = snprintf(NULL, 0, "%d", counter);
+      if (int_str_size <= 0) ERROR;
+      char *int_str = (char *)malloc(int_str_size+1);
+      if (snprintf(int_str, int_str_size+1, "%d", counter++) >= int_str_size+1) ERROR;
+      *new_str += int_str;
+      free(int_str);
+      return new_str;
+    }
+
+};
+
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+
+
+/* Some function calls in the body of functions or function blocks
+ * may leave some parameters to their default values, and
+ * ignore some output parameters of the function being called.
+ * Our conversion of ST functions to C++ does not contemplate that,
+ * i.e. each called function must get all it's input and output
+ * parameters set correctly.
+ * For input parameters we merely need to call the function with
+ * the apropriate default value, but for output parameters
+ * we must create temporary variables to hold the output value.
+ *
+ * We declare all the temporary output variables at the begining of
+ * the body of each function or function block, and use them as
+ * in function calls later on as they become necessary...
+ * Note that we cannot create these variables just before a function
+ * call, as the function call itself may be integrated within an
+ * expression, or another function call!
+ *
+ * The variables are declared in the exact same order in which they
+ * will be used later on during the function calls, which allows us
+ * to simply re-create the name that was used for the temporary variable
+ * instead of keeping it in some list.
+ * The names are recreated by the temp_var_name_factory, after reset()
+ * has been called!
+ *
+ * This function will genertae code similar to...
+ *
+ *     INT __TMP_0 = 23;
+ *     REAL __TMP_1 = 45.5;
+ *     ...
+ */
+
+class generate_c_tempvardecl_c: generate_c_typedecl_c {
+  public:
+    generate_c_tempvardecl_c(stage4out_c *s4o_ptr): generate_c_typedecl_c(s4o_ptr) {}
+
+    void generate(symbol_c *body, temp_var_name_c *temp_var_name_factory) {
+      temp_var_name_factory->reset();
+      function_call_iterator_c fcall_iterator(body);
+      for(symbol_c *finvocation = NULL; (finvocation = fcall_iterator.next()) != NULL;) {
+        /* get the name of the next function that gets called */
+        identifier_c *fcalled_name = fcall_iterator.fname();
+        /* get that function's declaration... */
+        function_declaration_c *fdecl = function_symtable.find_value(fcalled_name);
+        if (fdecl == function_symtable.end_value()) {
+          function_type_t function_type = get_function_type(fcalled_name);
+          if (function_type == function_none) ERROR;
+          return;
+        }
+        /* create iterator to iterate through each of the called function's parameters... */
+        function_param_iterator_c fp_iterator(fdecl);
+
+        /* iterate through each of the called function's parameters... */
+        identifier_c *param_name = NULL;
+        function_call_param_iterator_c function_call_param_iterator(finvocation);
+        for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+
+          function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+          if (param_direction == function_param_iterator_c::direction_in)
+            /* ignore input only parameters...
+             * we do not need to create temporary variables for these!
+             */
+            continue;
+
+          /* Get the value from a foo(<param_name> = <param_value>) style call */
+          symbol_c *param_value = function_call_param_iterator.search(param_name);
+
+          /* Get the value from a foo(<param_value>) style call */
+          if (param_value == NULL)
+            param_value = function_call_param_iterator.next();
+
+          if (param_value != NULL)
+            /* ignore output parameters to which a variable is passed...
+             * we do not need to create temporary variables for these!
+             */
+            continue;
+
+          symbol_c *param_type = fp_iterator.param_type();
+
+          /* get the parameter's default value */
+          param_value = fp_iterator.default_value();
+
+          /* If no default value specified in function declaration,
+           * get the default value of this variable's type
+           */
+          if (param_value == NULL)
+            param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
+          if (param_value == NULL) ERROR;
+
+          /* now declare a temporary variable, with the correct default value... */
+          s4o.print(s4o.indent_spaces);
+          param_type->accept(*this);
+          s4o.print(" ");
+
+          std::string *temp_var_name = temp_var_name_factory->new_name();
+          s4o.print(*temp_var_name);
+          delete temp_var_name;
+
+          s4o.print(" = ");
+          param_value->accept(*this);
+          s4o.print(";\n");
+        }
+      }
+      temp_var_name_factory->reset();
+      s4o.print("\n");
+    }
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/generate_c_typedecl.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,467 @@
+/*
+ * (c) 2003 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)
+ *
+ */
+
+
+/*
+ * Conversion of type declaration constructs.
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+
+
+
+
+
+//#include <stdio.h>  /* required for NULL */
+//#include <string>
+//#include <iostream>
+
+//#include "../../util/symtable.hh"
+
+
+
+
+
+class generate_c_typedecl_c: public generate_c_base_c {
+
+  public:
+    generate_c_typedecl_c(stage4out_c *s4o_ptr): generate_c_base_c(s4o_ptr) {}
+    ~generate_c_typedecl_c(void) {}
+
+
+/***************************/
+/* B 0 - Programming Model */
+/***************************/
+  /* leave for derived classes... */
+
+/*************************/
+/* B.1 - Common elements */
+/*************************/
+/*******************************************/
+/* B 1.1 - Letters, digits and identifiers */
+/*******************************************/
+  /* done in base class(es) */
+
+/*********************/
+/* B 1.2 - Constants */
+/*********************/
+  /* originally empty... */
+
+/******************************/
+/* B 1.2.1 - Numeric Literals */
+/******************************/
+  /* done in base class(es) */
+
+/*******************************/
+/* B.1.2.2   Character Strings */
+/*******************************/
+  /* done in base class(es) */
+
+/***************************/
+/* B 1.2.3 - Time Literals */
+/***************************/
+/************************/
+/* B 1.2.3.1 - Duration */
+/************************/
+  /* done in base class(es) */
+
+/************************************/
+/* B 1.2.3.2 - Time of day and Date */
+/************************************/
+  /* done in base class(es) */
+
+/**********************/
+/* B.1.3 - Data types */
+/**********************/
+/***********************************/
+/* B 1.3.1 - Elementary Data Types */
+/***********************************/
+  /* done in base class(es) */
+
+/********************************/
+/* B.1.3.2 - Generic data types */
+/********************************/
+  /* originally empty... */
+
+/********************************/
+/* B 1.3.3 - Derived data types */
+/********************************/
+void *visit(subrange_spec_init_c *symbol) {
+  TRACE("subrange_spec_init_c");
+  // TODO...
+  ERROR;
+  return NULL;
+}
+
+void *visit(enumerated_spec_init_c *symbol) {
+  TRACE("enumerated_spec_init_c");
+  // TODO...
+  ERROR;
+  return NULL;
+}
+
+/*  TYPE type_declaration_list END_TYPE */
+void *visit(data_type_declaration_c *symbol) {
+  TRACE("data_type_declaration_c");
+  symbol->type_declaration_list->accept(*this);
+  s4o.print("\n\n");
+  return NULL;
+}
+
+/* helper symbol for data_type_declaration */
+void *visit(type_declaration_list_c *symbol) {
+  TRACE("type_declaration_list_c");
+  return print_list(symbol);
+}
+
+/*  simple_type_name ':' simple_spec_init */
+void *visit(simple_type_declaration_c *symbol) {
+  TRACE("simple_type_declaration_c");
+  /* add this type declaration to the type symbol table... */
+  type_symtable.insert(symbol->simple_type_name, symbol->simple_spec_init);
+
+  s4o.print("typedef ");
+  symbol->simple_spec_init->accept(*this);
+  s4o.print(" ");
+  symbol->simple_type_name->accept(*this);
+  s4o.print(";\n");
+  return NULL;
+}
+
+/* simple_specification [ASSIGN constant] */
+//SYM_REF2(simple_spec_init_c, simple_specification, constant)
+// <constant> may be NULL
+void *visit(simple_spec_init_c *symbol) {
+  TRACE("simple_spec_init_c");
+  symbol->simple_specification->accept(*this);
+  return NULL;
+}
+
+#if 0
+/*  subrange_type_name ':' subrange_spec_init */
+SYM_REF2(subrange_type_declaration_c, subrange_type_name, subrange_spec_init)
+
+/* subrange_specification ASSIGN signed_integer */
+SYM_REF2(subrange_spec_init_c, subrange_specification, signed_integer)
+
+/*  integer_type_name '(' subrange')' */
+SYM_REF2(subrange_specification_c, integer_type_name, subrange)
+
+/*  signed_integer DOTDOT signed_integer */
+SYM_REF2(subrange_c, lower_limit, upper_limit)
+
+/*  enumerated_type_name ':' enumerated_spec_init */
+SYM_REF2(enumerated_type_declaration_c, enumerated_type_name, enumerated_spec_init)
+
+/* enumerated_specification ASSIGN enumerated_value */
+SYM_REF2(enumerated_spec_init_c, enumerated_specification, enumerated_value)
+
+/* helper symbol for enumerated_specification->enumerated_spec_init */
+/* enumerated_value_list ',' enumerated_value */
+SYM_LIST(enumerated_value_list_c)
+
+/* enumerated_type_name '#' identifier */
+SYM_REF2(enumerated_value_c, type, value)
+
+/*  identifier ':' array_spec_init */
+SYM_REF2(array_type_declaration_c, identifier, array_spec_init)
+
+/* array_specification [ASSIGN array_initialization} */
+/* array_initialization may be NULL ! */
+SYM_REF2(array_spec_init_c, array_specification, array_initialization)
+
+/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
+SYM_REF2(array_specification_c, array_subrange_list, non_generic_type_name)
+
+/* helper symbol for array_specification */
+/* array_subrange_list ',' subrange */
+SYM_LIST(array_subrange_list_c)
+
+/* array_initialization:  '[' array_initial_elements_list ']' */
+/* helper symbol for array_initialization */
+/* array_initial_elements_list ',' array_initial_elements */
+SYM_LIST(array_initial_elements_list_c)
+
+/* integer '(' [array_initial_element] ')' */
+/* array_initial_element may be NULL ! */
+SYM_REF2(array_initial_elements_c, integer, array_initial_element)
+#endif
+
+/*  structure_type_name ':' structure_specification */
+//SYM_REF2(structure_type_declaration_c, structure_type_name, structure_specification)
+void *visit(structure_type_declaration_c *symbol) {
+  TRACE("structure_type_declaration_c");
+  /* add this type declaration to the type symbol table... */
+  type_symtable.insert(symbol->structure_type_name, symbol->structure_specification);
+
+  s4o.print("typedef ");
+  symbol->structure_specification->accept(*this);
+  s4o.print(" ");
+  symbol->structure_type_name->accept(*this);
+  s4o.print(";\n");
+  return NULL;
+}
+
+/* structure_type_name ASSIGN structure_initialization */
+/* structure_initialization may be NULL ! */
+//SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
+void *visit(initialized_structure_c *symbol) {
+  TRACE("initialized_structure_c");
+  symbol->structure_type_name->accept(*this);
+  return NULL;
+}
+
+/* helper symbol for structure_declaration */
+/* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
+/* structure_element_declaration_list structure_element_declaration ';' */
+//SYM_LIST(structure_element_declaration_list_c)
+void *visit(structure_element_declaration_list_c *symbol) {
+  TRACE("structure_element_declaration_list_c");
+  s4o.print("struct {\n");
+  s4o.indent_right();
+  s4o.print(s4o.indent_spaces);
+
+  print_list(symbol);
+
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces);
+  s4o.print("}");
+  return NULL;
+}
+
+/*  structure_element_name ':' spec_init */
+//SYM_REF2(structure_element_declaration_c, structure_element_name, spec_init)
+void *visit(structure_element_declaration_c *symbol) {
+  TRACE("structure_element_declaration_c");
+
+  symbol->spec_init->accept(*this);
+  s4o.print(" ");
+  symbol->structure_element_name->accept(*this);
+  s4o.print(";\n");
+  s4o.print(s4o.indent_spaces);
+
+  return NULL;
+}
+
+/* helper symbol for structure_initialization */
+/* structure_initialization: '(' structure_element_initialization_list ')' */
+/* structure_element_initialization_list ',' structure_element_initialization */
+//SYM_LIST(structure_element_initialization_list_c)
+void *visit(structure_element_initialization_list_c *symbol) {
+  TRACE("structure_element_initialization_list_c");
+
+  // TODO ???
+  ERROR;
+  return NULL;
+}
+
+/*  structure_element_name ASSIGN value */
+//SYM_REF2(structure_element_initialization_c, structure_element_name, value)
+void *visit(structure_element_initialization_c *symbol) {
+  TRACE("structure_element_initialization_c");
+
+  // TODO ???
+  ERROR;
+  return NULL;
+}
+
+#if 0
+/*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
+/*
+ * NOTE:
+ * (Summary: Contrary to what is expected, the
+ *           string_type_declaration_c is not used to store
+ *           simple string type declarations that do not include
+ *           size limits.
+ *           For e.g.:
+ *             str1_type: STRING := "hello!"
+ *           will be stored in a simple_type_declaration_c
+ *           instead of a string_type_declaration_c.
+ *           The following:
+ *             str2_type: STRING [64] := "hello!"
+ *           will be stored in a sring_type_declaration_c
+ *
+ *           Read on for why this is done...
+ * End Summary)
+ *
+ * According to the spec, the valid construct
+ * TYPE new_str_type : STRING := "hello!"; END_TYPE
+ * has two possible routes to type_declaration...
+ *
+ * Route 1:
+ * type_declaration: single_element_type_declaration
+ * single_element_type_declaration: simple_type_declaration
+ * simple_type_declaration: identifier ':' simple_spec_init
+ * simple_spec_init: simple_specification ASSIGN constant
+ * (shift:  identifier <- 'new_str_type')
+ * simple_specification: elementary_type_name
+ * elementary_type_name: STRING
+ * (shift: elementary_type_name <- STRING)
+ * (reduce: simple_specification <- elementary_type_name)
+ * (shift: constant <- "hello!")
+ * (reduce: simple_spec_init: simple_specification ASSIGN constant)
+ * (reduce: ...)
+ *
+ *
+ * Route 2:
+ * type_declaration: string_type_declaration
+ * string_type_declaration: identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init
+ * (shift:  identifier <- 'new_str_type')
+ * elementary_string_type_name: STRING
+ * (shift: elementary_string_type_name <- STRING)
+ * (shift: string_type_declaration_size <-  empty )
+ * string_type_declaration_init: ASSIGN character_string
+ * (shift: character_string <- "hello!")
+ * (reduce: string_type_declaration_init <- ASSIGN character_string)
+ * (reduce: string_type_declaration <- identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init )
+ * (reduce: type_declaration <- string_type_declaration)
+ *
+ *
+ * At first glance it seems that removing route 1 would make
+ * the most sense. Unfortunately the construct 'simple_spec_init'
+ * shows up multiple times in other rules, so changing this construct
+ * would also mean changing all the rules in which it appears.
+ * I (Mario) therefore chose to remove route 2 instead. This means
+ * that the above declaration gets stored in a
+ * simple_type_declaration_c, and not in a string_type_declaration_c
+ * as would be expected!
+ */
+/*  string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
+SYM_REF4(string_type_declaration_c,	string_type_name,
+					elementary_string_type_name,
+					string_type_declaration_size,
+					string_type_declaration_init) /* may be == NULL! */
+#endif
+
+/*********************/
+/* B 1.4 - Variables */
+/*********************/
+  /* done in base class(es) */
+
+/********************************************/
+/* B.1.4.1   Directly Represented Variables */
+/********************************************/
+// direct_variable: direct_variable_token	{$$ = new direct_variable_c($1);};
+void *visit(direct_variable_c *symbol) {
+  TRACE("direct_variable_c");
+  /* Do not use print_token() as it will change everything into uppercase */
+  if (strlen(symbol->value) == 0) ERROR;
+  return s4o.printlocation(symbol->value + 1);
+}
+
+
+/*************************************/
+/* B.1.4.2   Multi-element Variables */
+/*************************************/
+  /* done in base class(es) */
+
+/******************************************/
+/* B 1.4.3 - Declaration & Initialisation */
+/******************************************/
+  /* leave for derived classes... */
+
+/**************************************/
+/* B.1.5 - Program organization units */
+/**************************************/
+/***********************/
+/* B 1.5.1 - Functions */
+/***********************/
+  /* leave for derived classes... */
+
+/*****************************/
+/* B 1.5.2 - Function Blocks */
+/*****************************/
+  /* leave for derived classes... */
+
+/**********************/
+/* B 1.5.3 - Programs */
+/**********************/
+  /* leave for derived classes... */
+
+/*********************************************/
+/* B.1.6  Sequential function chart elements */
+/*********************************************/
+
+/********************************/
+/* B 1.7 Configuration elements */
+/********************************/
+  /* leave for derived classes... */
+
+/****************************************/
+/* B.2 - Language IL (Instruction List) */
+/****************************************/
+/***********************************/
+/* B 2.1 Instructions and Operands */
+/***********************************/
+  /* leave for derived classes... */
+
+/*******************/
+/* B 2.2 Operators */
+/*******************/
+  /* leave for derived classes... */
+
+
+/***************************************/
+/* B.3 - Language ST (Structured Text) */
+/***************************************/
+/***********************/
+/* B 3.1 - Expressions */
+/***********************/
+  /* leave for derived classes... */
+
+/********************/
+/* B 3.2 Statements */
+/********************/
+  /* leave for derived classes... */
+
+/*********************************/
+/* B 3.2.1 Assignment Statements */
+/*********************************/
+  /* leave for derived classes... */
+
+/*****************************************/
+/* B 3.2.2 Subprogram Control Statements */
+/*****************************************/
+  /* leave for derived classes... */
+
+/********************************/
+/* B 3.2.3 Selection Statements */
+/********************************/
+  /* leave for derived classes... */
+
+/********************************/
+/* B 3.2.4 Iteration Statements */
+/********************************/
+  /* leave for derived classes... */
+
+
+
+
+}; /* generate_c_typedecl_c */
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/generate_c_vardecl.cc	Wed Oct 24 17:39:51 2007 +0200
@@ -0,0 +1,1702 @@
+/*
+ * (c) 2003 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)
+ *
+ */
+
+
+/*
+ * Conversion of variable declaration constructs.
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+
+
+
+//#include <stdio.h>  /* required for NULL */
+//#include <string>
+//#include <iostream>
+
+//#include "../../util/symtable.hh"
+
+
+
+
+
+
+
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+
+
+
+class generate_c_vardecl_c: protected generate_c_typedecl_c {
+
+  /* A Helper class to the main class... */
+  /* print a string, except the first time it is called */
+  /* used to print the separator "," before each variable
+   * declaration, except the first...
+   *
+   * Needs to be done in a seperate class because this is called
+   * from within various locations within the code.
+   */
+  class next_var_c {
+    private:
+      bool print_flag;
+      std::string str1, str2;
+
+      next_var_c *embedded_scope;
+
+    public:
+      next_var_c(std::string s1, std::string s2) {
+        str1 = s1;
+        str2 = s2;
+        print_flag = false;
+        embedded_scope = NULL;
+      }
+
+      std::string get(void) {
+        if (NULL != embedded_scope)
+          return embedded_scope->get();
+
+        bool old_print_flag = print_flag;
+        print_flag = true;
+        if (!old_print_flag)
+          return str1;
+        else
+          return str2;
+      }
+
+      /* Create a new next_var_c for an embedded scope.
+       * From now on, every call to get() will return the
+       * inner-most scope...!
+       */
+      void push(std::string s1, std::string s2) {
+        if (NULL != embedded_scope)
+          return embedded_scope->push(s1, s2);
+
+        embedded_scope = new next_var_c(s1, s2);
+        if (NULL == embedded_scope)
+          ERROR;
+        return;
+      }
+
+      /* Remove the inner-most scope... */
+      void pop(void) {
+        if (NULL != embedded_scope)
+          return embedded_scope->pop();
+
+        delete embedded_scope;
+        embedded_scope = NULL;
+      }
+  };
+
+  private:
+    /* used to generate the ',' separating the parameters in a function call */
+    next_var_c *nv;
+
+
+  public:
+    /* the types of variables that need to be processed... */
+    static const unsigned int none_vt	  = 0x0000;
+    static const unsigned int input_vt	  = 0x0001;  // VAR_INPUT
+    static const unsigned int output_vt	  = 0x0002;  // VAR_OUTPUT
+    static const unsigned int inoutput_vt = 0x0004;  // VAR_IN_OUT
+    static const unsigned int private_vt  = 0x0008;  // VAR
+    static const unsigned int temp_vt	  = 0x0010;  // VAR_TEMP
+    static const unsigned int external_vt = 0x0020;  // VAR_EXTERNAL
+    static const unsigned int global_vt   = 0x0040;  // VAR_GLOBAL
+						     //    Globals declared inside a resource will not be declared
+						     //    unless global_vt is acompanied by resource_vt
+    static const unsigned int located_vt  = 0x0080;  // VAR <var_name> AT <location>
+    static const unsigned int program_vt  = 0x0100;  // PROGRAM (inside a configuration!)
+						     //    Programs declared inside a resource will not be declared
+						     //    unless program_vt is acompanied by resource_vt
+
+    static const unsigned int resource_vt = 0x8000;  // RESOURCE (inside a configuration!)
+                                                     //    This, just of itself, will not print out any declarations!!
+						     //    It must be acompanied by either program_vt and/or global_vt
+
+    /* How variables should be declared: as local variables or
+     * variables within a function call interface.
+     *
+     * This will define the format of the output generated
+     * by this class.
+     *
+     * finterface_vf: function interface parameters
+     *               e.g.  f(  int a, long b, real c );
+     *                         ---------------------
+     *               This class/function will produce the
+     *               underlined code of the above examples,
+     *               and no more!!
+     *
+     * localinit_vf: local declaration. Will include variable
+     *           initialisation if it is not a located variable.
+     *           e.g.
+     *                int a = 9;
+     *                long b = 99;
+     *                real c = 99.9;
+     *
+     * local_vf: local declaration. Will NOT include variable
+     *           initialisation.
+     *           e.g.
+     *                int a;
+     *                long b;
+     *                real c;
+     *
+     * init_vf: local initialisation without declaration.
+     *           e.g.
+     *                a = 9;
+     *                b = 99;
+     *                c = 99.9;
+     *
+     * constructorinit_vf: initialising of member variables...
+     *                e.g. for a constructor...
+     *                class_name_c(void)
+     *                : a(9), b(99), c(99.9)  { // code... }
+     *                  --------------------
+     *               This class/function will produce the
+     *               underlined code of the above examples,
+     *               and no more!!
+     *
+     * globalinit_vf: initialising of static c++ variables. These
+     *                variables may have been declared as static inside
+     *                a class, in which case the scope within which they were
+     *                previously delcared must be passed as the second parameter
+     *                to the print() function.
+     *
+     *                e.g.
+     *                __plc_pt_c<INT, 8*sizeof(INT)> START_P::loc = __plc_pt_c<INT, 8*sizeof(INT)>("I2");
+     */
+    typedef enum {finterface_vf,
+                    local_vf,
+                    localstatic_vf,
+		    localinit_vf,
+		    init_vf,
+		    constructorinit_vf,
+		    globalinit_vf
+		   } varformat_t;
+
+
+  private:
+    /* variable used to store the types of variables that need to be processed... */
+    /* Only set in the constructor...! */
+    /* Will contain a set of values of generate_c_vardecl_c::XXXX_vt */
+    unsigned int wanted_vartype;
+    /* variable used to store the type of variable currently being processed... */
+    /* Will contain a single value of generate_c_vardecl_c::XXXX_vt */
+    unsigned int current_vartype;
+
+    /* How variables should be declared: as local variables or
+     * variables within a function interface...
+     */
+    /* Only set in the constructor...! */
+    varformat_t wanted_varformat;
+
+    /* The number of variables already declared. */
+    /* Used to declare 'void' in case no variables are declared in a function interface... */
+    int finterface_var_count;
+
+
+
+    /* Holds the references to the type and initial value
+     * of the variables currently being declared.
+     * Please read the comment under var1_init_decl_c for further
+     * details...
+     *
+     * We make an effort to keep these pointers pointing to NULL
+     * whenever we are outside the scope of variable declaration
+     * (i.e. when we are traversing a part of the parse tree which
+     * is not part of variable declaration) in order tio try to catch
+     * any bugs as soon as possible.
+     */
+    symbol_c *current_var_type_symbol;
+    symbol_c *current_var_init_symbol;
+    void update_type_init(symbol_c *symbol /* a spec_init_c, subrange_spec_init_c, etc... */ ) {
+      this->current_var_type_symbol = spec_init_sperator_c::get_spec(symbol);
+      this->current_var_init_symbol = spec_init_sperator_c::get_init(symbol);
+      if (NULL == this->current_var_type_symbol)
+        ERROR;
+      if (NULL == this->current_var_init_symbol) {
+        /* We try to find the data type's default value... */
+        this->current_var_init_symbol = (symbol_c *)this->current_var_type_symbol->accept(*type_initial_value_c::instance());
+      /* Note that Function Block 'data types' do not have a default value, so we cannot abort if no default value is found! */
+      /*
+      if (NULL == this->current_var_init_symbol)
+        ERROR;
+      */
+      }
+    }
+
+    void void_type_init(void) {
+      this->current_var_type_symbol = NULL;
+      this->current_var_init_symbol = NULL;
+    }
+
+    /* Only used when wanted_varformat == globalinit_vf
+     * Holds a pointer to an identifier_c, which in turns contains
+     * the identifier of the scope within which the static member was
+     * declared.
+     */
+    symbol_c *globalnamespace;
+
+    /* Actually produce the output where variables are declared... */
+    /* Note that located variables are the exception, they
+     * being declared in the located_var_decl_c visitor...
+     */
+    void *declare_variables(symbol_c *symbol, bool is_fb = false) {
+      list_c *list = dynamic_cast<list_c *>(symbol);
+      /* should NEVER EVER occur!! */
+      if (list == NULL) ERROR;
+
+      /* now to produce the c equivalent... */
+      if ((wanted_varformat == local_vf) ||
+          (wanted_varformat == init_vf) ||
+          (wanted_varformat == localinit_vf)) {
+        for(int i = 0; i < list->n; i++) {
+          s4o.print(s4o.indent_spaces);
+          if (wanted_varformat != init_vf) {
+            this->current_var_type_symbol->accept(*this);
+            s4o.print(" ");
+          }
+          print_variable_prefix();
+          list->elements[i]->accept(*this);
+          if (wanted_varformat != local_vf) {
+            if (this->current_var_init_symbol != NULL) {
+              s4o.print(" = ");
+              this->current_var_init_symbol->accept(*this);
+            }
+          }
+          s4o.print(";\n");
+        }
+      }
+
+      if (wanted_varformat == finterface_vf) {
+        for(int i = 0; i < list->n; i++) {
+          finterface_var_count++;
+          s4o.print(nv->get());
+          s4o.print("\n" + s4o.indent_spaces);
+          this->current_var_type_symbol->accept(*this);
+          if ((current_vartype & (output_vt | inoutput_vt)) != 0)
+            s4o.print(" &");
+          else
+            s4o.print(" ");
+          list->elements[i]->accept(*this);
+          /* We do not print the initial value at function declaration!
+           * It is up to the caller to pass the correct default value
+           * if none is specified in the ST source code
+           */
+          /* if (this->current_var_init_symbol != NULL) {
+               s4o.print(" = "); this->current_var_init_symbol->accept(*this);}
+           */
+        }
+      }
+
+      if (wanted_varformat == constructorinit_vf) {
+        for(int i = 0; i < list->n; i++) {
+          if (is_fb) {
+            s4o.print(nv->get());
+            this->current_var_type_symbol->accept(*this);
+            s4o.print(FB_INIT_SUFFIX);
+            s4o.print("(&");
+            this->print_variable_prefix();
+            list->elements[i]->accept(*this);
+            s4o.print(");");
+          }
+          else if (this->current_var_init_symbol != NULL) {
+            s4o.print(nv->get());
+            this->print_variable_prefix();
+            list->elements[i]->accept(*this);
+            s4o.print(" = ");
+            this->current_var_init_symbol->accept(*this);
+            s4o.print(";");
+          }
+        }
+      }
+
+      return NULL;
+    }
+
+
+
+
+  public:
+    generate_c_vardecl_c(stage4out_c *s4o_ptr, varformat_t varformat, unsigned int vartype)
+    : generate_c_typedecl_c(s4o_ptr) {
+      wanted_varformat = varformat;
+      wanted_vartype   = vartype;
+      current_vartype  = none_vt;
+      current_var_type_symbol = NULL;
+      current_var_init_symbol = NULL;
+      globalnamespace         = NULL;
+      nv = NULL;
+    }
+
+    ~generate_c_vardecl_c(void) {}
+
+
+    void print(symbol_c *symbol, symbol_c *scope = NULL, const char *variable_prefix = NULL) {
+      this->set_variable_prefix(variable_prefix);
+      if (globalinit_vf == wanted_varformat)
+        globalnamespace = scope;
+
+      finterface_var_count = 0;
+
+      switch (wanted_varformat) {
+        case constructorinit_vf: nv = new next_var_c("", "\n"+s4o.indent_spaces); break;
+        case finterface_vf:      /* fall through... */
+        case localinit_vf:       /* fall through... */
+        case local_vf:           nv = new next_var_c("", ", "); break;
+        default:                 nv = NULL;
+      } /* switch() */
+
+      symbol->accept(*this);
+
+      /* special case... */
+      if (wanted_varformat == finterface_vf)
+        if (finterface_var_count == 0)
+          s4o.print("void");
+
+      delete nv;
+      nv = NULL;
+      globalnamespace = NULL;
+    }
+
+
+  protected:
+/***************************/
+/* B 0 - Programming Model */
+/***************************/
+  /* leave for derived classes... */
+
+/*************************/
+/* B.1 - Common elements */
+/*************************/
+/*******************************************/
+/* B 1.1 - Letters, digits and identifiers */
+/*******************************************/
+  /* done in base class(es) */
+
+/*********************/
+/* B 1.2 - Constants */
+/*********************/
+  /* originally empty... */
+
+/******************************/
+/* B 1.2.1 - Numeric Literals */
+/******************************/
+  /* done in base class(es) */
+
+/*******************************/
+/* B.1.2.2   Character Strings */
+/*******************************/
+  /* done in base class(es) */
+
+/***************************/
+/* B 1.2.3 - Time Literals */
+/***************************/
+/************************/
+/* B 1.2.3.1 - Duration */
+/************************/
+  /* done in base class(es) */
+
+/************************************/
+/* B 1.2.3.2 - Time of day and Date */
+/************************************/
+  /* done in base class(es) */
+
+/**********************/
+/* B.1.3 - Data types */
+/**********************/
+/***********************************/
+/* B 1.3.1 - Elementary Data Types */
+/***********************************/
+  /* done in base class(es) */
+
+/********************************/
+/* B.1.3.2 - Generic data types */
+/********************************/
+  /* originally empty... */
+
+/********************************/
+/* B 1.3.3 - Derived data types */
+/********************************/
+  /* done in base class(es) */
+
+/*********************/
+/* B 1.4 - Variables */
+/*********************/
+  /* done in base class(es) */
+
+/********************************************/
+/* B.1.4.1   Directly Represented Variables */
+/********************************************/
+  /* done in base class(es) */
+
+/*************************************/
+/* B.1.4.2   Multi-element Variables */
+/*************************************/
+  /* done in base class(es) */
+
+/******************************************/
+/* B 1.4.3 - Declaration & Initialisation */
+/******************************************/
+void *visit(constant_option_c *symbol) {s4o.print("CONSTANT"); return NULL;}
+void *visit(retain_option_c *symbol) {s4o.print("RETAIN"); return NULL;}
+void *visit(non_retain_option_c *symbol) {s4o.print("NON_RETAIN"); return NULL;}
+
+void *visit(input_declarations_c *symbol) {
+  TRACE("input_declarations_c");
+  if ((wanted_vartype & input_vt) != 0) {
+/*
+    // TO DO ...
+    if (symbol->option != NULL)
+      symbol->option->accept(*this);
+*/
+    //s4o.indent_right();
+    current_vartype = input_vt;
+    symbol->input_declaration_list->accept(*this);
+    current_vartype = none_vt;
+    //s4o.indent_left();
+  }
+  return NULL;
+}
+
+/* helper symbol for input_declarations */
+void *visit(input_declaration_list_c *symbol) {
+  TRACE("input_declaration_list_c");
+  print_list(symbol);
+  return NULL;
+}
+
+void *visit(edge_declaration_c *symbol) {
+  TRACE("edge_declaration_c");
+  // TO DO ...
+  symbol->var1_list->accept(*this);
+  s4o.print(" : BOOL ");
+  symbol->edge->accept(*this);
+  return NULL;
+}
+
+void *visit(raising_edge_option_c *symbol) {
+  // TO DO ...
+  s4o.print("R_EDGE");
+  return NULL;
+}
+
+
+#if 0
+/* var1_list ':' array_spec_init */
+SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)
+#endif
+
+/*  var1_list ':' initialized_structure */
+// SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
+void *visit(structured_var_init_decl_c *symbol) {
+  TRACE("structured_var_init_decl_c");
+  /* Please read the comments inside the var1_init_decl_c
+   * visitor, as they apply here too.
+   */
+
+  /* Start off by setting the current_var_type_symbol and
+   * current_var_init_symbol private variables...
+   */
+  update_type_init(symbol->initialized_structure);
+
+  /* now to produce the c equivalent... */
+  symbol->var1_list->accept(*this);
+
+  /* Values no longer in scope, and therefore no longer used.
+   * Make an effort to keep them set to NULL when not in use
+   * in order to catch bugs as soon as possible...
+   */
+  void_type_init();
+
+  return NULL;
+}
+
+/* fb_name_list ':' function_block_type_name ASSIGN structure_initialization */
+/* structure_initialization -> may be NULL ! */
+void *visit(fb_name_decl_c *symbol) {
+  TRACE("fb_name_decl_c");
+  /* Please read the comments inside the var1_init_decl_c
+   * visitor, as they apply here too.
+   */
+
+  /* Start off by setting the current_var_type_symbol and
+   * current_var_init_symbol private variables...
+   */
+  update_type_init(symbol);
+
+  /* now to produce the c equivalent... */
+  symbol->fb_name_list->accept(*this);
+
+  /* Values no longer in scope, and therefore no longer used.
+   * Make an effort to keep them set to NULL when not in use
+   * in order to catch bugs as soon as possible...
+   */
+  void_type_init();
+
+  return NULL;
+}
+
+/* fb_name_list ',' fb_name */
+void *visit(fb_name_list_c *symbol) {
+  TRACE("fb_name_list_c");
+  declare_variables(symbol, true);
+  return NULL;
+}
+
+
+/* VAR_OUTPUT [RETAIN | NON_RETAIN] var_init_decl_list END_VAR */
+/* option -> may be NULL ! */
+void *visit(output_declarations_c *symbol) {
+  TRACE("output_declarations_c");
+  if ((wanted_vartype & output_vt) != 0) {
+/*
+    // TO DO ...
+    if (symbol->option != NULL)
+      symbol->option->accept(*this);
+*/
+    //s4o.indent_right();
+    current_vartype = output_vt;
+    symbol->var_init_decl_list->accept(*this);
+    current_vartype = none_vt;
+    //s4o.indent_left();
+  }
+  return NULL;
+}
+
+/*  VAR_IN_OUT var_declaration_list END_VAR */
+void *visit(input_output_declarations_c *symbol) {
+  TRACE("input_output_declarations_c");
+  if ((wanted_vartype & inoutput_vt) != 0) {
+    //s4o.indent_right();
+    current_vartype = inoutput_vt;
+    symbol->var_declaration_list->accept(*this);
+    current_vartype = none_vt;
+    //s4o.indent_left();
+  }
+  return NULL;
+}
+
+/* helper symbol for input_output_declarations */
+/* var_declaration_list var_declaration ';' */
+void *visit(var_declaration_list_c *symbol) {
+  TRACE("var_declaration_list_c");
+  print_list(symbol);
+  return NULL;
+}
+
+#if 0
+/*  var1_list ':' array_specification */
+SYM_REF2(array_var_declaration_c, var1_list, array_specification)
+#endif
+
+/*  var1_list ':' structure_type_name */
+//SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
+void *visit(structured_var_declaration_c *symbol) {
+  TRACE("structured_var_declaration_c");
+  /* Please read the comments inside the var1_init_decl_c
+   * visitor, as they apply here too.
+   */
+
+  /* Start off by setting the current_var_type_symbol and
+   * current_var_init_symbol private variables...
+   */
+  update_type_init(symbol->structure_type_name);
+
+  /* now to produce the c equivalent... */
+  symbol->var1_list->accept(*this);
+
+  /* Values no longer in scope, and therefore no longer used.
+   * Make an effort to keep them set to NULL when not in use
+   * in order to catch bugs as soon as possible...
+   */
+  void_type_init();
+
+  return NULL;
+}
+
+
+/* VAR [CONSTANT] var_init_decl_list END_VAR */
+/* option -> may be NULL ! */
+/* helper symbol for input_declarations */
+void *visit(var_declarations_c *symbol) {
+  TRACE("var_declarations_c");
+  if ((wanted_vartype & private_vt) != 0) {
+/*
+    // TO DO ...
+    if (symbol->option != NULL)
+      symbol->option->accept(*this);
+*/
+    current_vartype = private_vt;
+    symbol->var_init_decl_list->accept(*this);
+    current_vartype = none_vt;
+  }
+  return NULL;
+}
+
+/*  VAR RETAIN var_init_decl_list END_VAR */
+void *visit(retentive_var_declarations_c *symbol) {
+  TRACE("retentive_var_declarations_c");
+  if ((wanted_vartype & private_vt) != 0) {
+    current_vartype = private_vt;
+    symbol->var_init_decl_list->accept(*this);
+    current_vartype = none_vt;
+  }
+  return NULL;
+}
+
+/*  VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */
+/* option -> may be NULL ! */
+//SYM_REF2(located_var_declarations_c, option, located_var_decl_list)
+void *visit(located_var_declarations_c *symbol) {
+  TRACE("located_var_declarations_c");
+  if ((wanted_vartype & located_vt) != 0) {
+/*
+    // TO DO ...
+    if (symbol->option != NULL)
+      symbol->option->accept(*this);
+*/
+    current_vartype = located_vt;
+    symbol->located_var_decl_list->accept(*this);
+    current_vartype = none_vt;
+  }
+  return NULL;
+}
+
+/* helper symbol for located_var_declarations */
+/* located_var_decl_list located_var_decl ';' */
+//SYM_LIST(located_var_decl_list_c)
+void *visit(located_var_decl_list_c *symbol) {
+  TRACE("located_var_decl_list_c");
+  print_list(symbol);
+  return NULL;
+}
+
+
+/*  [variable_name] location ':' located_var_spec_init */
+/* variable_name -> may be NULL ! */
+//SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused)
+void *visit(located_var_decl_c *symbol) {
+  TRACE("located_var_decl_c");
+  /* Please read the comments inside the var1_init_decl_c
+   * visitor, as they apply here too.
+   */
+
+  /* Start off by setting the current_var_type_symbol and
+   * current_var_init_symbol private variables...
+   */
+  update_type_init(symbol->located_var_spec_init);
+
+  /* now to produce the c equivalent... */
+  switch(wanted_varformat) {
+    case local_vf:
+      s4o.print(s4o.indent_spaces);
+      this->current_var_type_symbol->accept(*this);
+      s4o.print(" *");
+      if (symbol->variable_name != NULL)
+        symbol->variable_name->accept(*this);
+      else
+        symbol->location->accept(*this);
+      s4o.print(";\n");
+      break;
+
+    case constructorinit_vf:
+      s4o.print(nv->get());
+      s4o.print("{extern ");
+      this->current_var_type_symbol->accept(*this);
+      s4o.print(" ");
+      symbol->location->accept(*this);
+      s4o.print("; ");
+      print_variable_prefix();
+      if (symbol->variable_name != NULL)
+        symbol->variable_name->accept(*this);
+      else
+        symbol->location->accept(*this);
+      s4o.print(" = &");
+      symbol->location->accept(*this);
+      s4o.print(";}");
+      break;
+
+    case globalinit_vf:
+      s4o.print(s4o.indent_spaces + "__plc_pt_c<");
+      this->current_var_type_symbol->accept(*this);
+      s4o.print(", 8*sizeof(");
+      this->current_var_type_symbol->accept(*this);
+      s4o.print(")> ");
+      if (this->globalnamespace != NULL) {
+        this->globalnamespace->accept(*this);
+        s4o.print("::");
+      }
+      if (symbol->variable_name != NULL)
+        symbol->variable_name->accept(*this);
+      else
+        symbol->location->accept(*this);
+
+      s4o.print(" = ");
+
+      s4o.print(s4o.indent_spaces + "__plc_pt_c<");
+      this->current_var_type_symbol->accept(*this);
+      s4o.print(", 8*sizeof(");
+      this->current_var_type_symbol->accept(*this);
+      s4o.print(")>(\"");
+      symbol->location->accept(*this);
+      s4o.print("\"");
+      if (this->current_var_init_symbol != NULL) {
+        s4o.print(", ");
+        this->current_var_init_symbol->accept(*this);
+      }
+      s4o.print(");\n");
+      break;
+
+    default:
+      ERROR;
+  } /* switch() */
+
+  /* Values no longer in scope, and therefore no longer used.
+   * Make an effort to keep them set to NULL when not in use
+   * in order to catch bugs as soon as possible...
+   */
+  void_type_init();
+
+  return NULL;
+}
+
+
+
+
+/*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */
+/* option -> may be NULL ! */
+//SYM_REF2(external_var_declarations_c, option, external_declaration_list)
+void *visit(external_var_declarations_c *symbol) {
+  TRACE("external_var_declarations_c");
+  if ((wanted_vartype & external_vt) != 0) {
+/*
+    // TODO ...
+    if (symbol->option != NULL)
+      symbol->option->accept(*this);
+*/
+    //s4o.indent_right();
+    current_vartype = external_vt;
+    symbol->external_declaration_list->accept(*this);
+    current_vartype = none_vt;
+    //s4o.indent_left();
+  }
+  return NULL;
+}
+
+/* helper symbol for external_var_declarations */
+/*| external_declaration_list external_declaration';' */
+//SYM_LIST(external_declaration_list_c)
+/* helper symbol for input_declarations */
+void *visit(external_declaration_list_c *symbol) {
+  TRACE("external_declaration_list_c");
+  print_list(symbol);
+  return NULL;
+}
+
+
+/*  global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */
+//SYM_REF2(external_declaration_c, global_var_name, specification)
+void *visit(external_declaration_c *symbol) {
+  TRACE("external_declaration_c");
+  /* Please read the comments inside the var1_init_decl_c
+   * visitor, as they apply here too.
+   */
+
+  /* Start off by setting the current_var_type_symbol and
+   * current_var_init_symbol private variables...
+   */
+  this->current_var_type_symbol = symbol->specification;
+  this->current_var_init_symbol = NULL;
+
+  /* now to produce the c equivalent... */
+  switch (wanted_varformat) {
+    case local_vf:
+    case localinit_vf:
+      s4o.print(s4o.indent_spaces);
+      this->current_var_type_symbol->accept(*this);
+      s4o.print(" *");
+      symbol->global_var_name->accept(*this);
+      if ((wanted_varformat == localinit_vf) &&
+          (this->current_var_init_symbol != NULL)) {
+        s4o.print(" = ");
+        this->current_var_init_symbol->accept(*this);
+      }
+      s4o.print(";\n");
+      break;
+
+    case constructorinit_vf:
+      s4o.print(nv->get());
+      s4o.print("{extern ");
+      this->current_var_type_symbol->accept(*this);
+      s4o.print(" *");
+      symbol->global_var_name->accept(*this);
+      s4o.print("; ");
+      print_variable_prefix();
+      symbol->global_var_name->accept(*this);
+      s4o.print(" = ");
+      symbol->global_var_name->accept(*this);
+      s4o.print(";}");
+      break;
+
+    case finterface_vf:
+      finterface_var_count++;
+      s4o.print(nv->get());
+      this->current_var_type_symbol->accept(*this);
+      s4o.print(" *");
+      symbol->global_var_name->accept(*this);
+      break;
+
+    default:
+      ERROR;
+  }
+
+  /* Values no longer in scope, and therefore no longer used.
+   * Make an effort to keep them set to NULL when not in use
+   * in order to catch bugs as soon as possible...
+   */
+  void_type_init();
+
+  return NULL;
+}
+
+
+
+/*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */
+/* option -> may be NULL ! */
+// SYM_REF2(global_var_declarations_c, option, global_var_decl_list)
+void *visit(global_var_declarations_c *symbol) {
+  TRACE("global_var_declarations_c");
+  if ((wanted_vartype & global_vt) != 0) {
+/*
+    // TODO ...
+    if (symbol->option != NULL)
+      symbol->option->accept(*this);
+*/
+    //s4o.indent_right();
+    unsigned int previous_vartype = current_vartype;
+      // previous_vartype will be either none_vt, or resource_vt
+    current_vartype = global_vt;
+    symbol->global_var_decl_list->accept(*this);
+    current_vartype = previous_vartype;
+    //s4o.indent_left();
+  }
+  return NULL;
+}
+
+/* helper symbol for global_var_declarations */
+/*| global_var_decl_list global_var_decl ';' */
+//SYM_LIST(global_var_decl_list_c)
+void *visit(global_var_decl_list_c *symbol) {
+  TRACE("global_var_decl_list_c");
+  print_list(symbol);
+  return NULL;
+}
+
+
+
+/*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */
+/* type_specification ->may be NULL ! */
+// SYM_REF2(global_var_decl_c, global_var_spec, type_specification)
+void *visit(global_var_decl_c *symbol) {
+  TRACE("global_var_decl_c");
+  /* Please read the comments inside the var1_init_decl_c
+   * visitor, as they apply here too.
+   */
+
+  /* Start off by setting the current_var_type_symbol and
+   * current_var_init_symbol private variables...
+   */
+  update_type_init(symbol->type_specification);
+
+  /* now to produce the c equivalent... */
+  symbol->global_var_spec->accept(*this);
+
+  /* Values no longer in scope, and therefore no longer used.
+   * Make an effort to keep them set to NULL when not in use
+   * in order to catch bugs as soon as possible...
+   */
+  void_type_init();
+
+  return NULL;
+}
+
+
+/*| global_var_name location */
+// SYM_REF2(global_var_spec_c, global_var_name, location)
+void *visit(global_var_spec_c *symbol) {
+  TRACE("global_var_spec_c");
+
+  /* now to produce the c equivalent... */
+  switch(wanted_varformat) {
+    case local_vf:
+    case localstatic_vf:
+      /* NOTE: located variables must be declared static, as the connection to the
+       * MatPLC point must be initialised at program startup, and not whenever
+       * a new function block is instantiated!
+       * Nevertheless, this construct never occurs inside a Function Block, but
+       * only inside a configuration. In this case, only a single instance will
+       * be created, directly at startup, so it is not necessary that the variables
+       * be declared static.
+       */
+      s4o.print(s4o.indent_spaces);
+      if (symbol->global_var_name != NULL) {
+        s4o.print("extern ");
+        this->current_var_type_symbol->accept(*this);
+        s4o.print(" ");
+        symbol->location->accept(*this);
+        s4o.print(";\n");
+        if (wanted_varformat == localstatic_vf)
+          s4o.print("static ");
+        this->current_var_type_symbol->accept(*this);
+        s4o.print(" *");
+        symbol->global_var_name->accept(*this);
+        s4o.print(" = &");
+        symbol->location->accept(*this);
+        s4o.print(";\n");
+      }
+      break;
+
+    case constructorinit_vf:
+      s4o.print(nv->get());
+      
+      if (symbol->global_var_name != NULL) {
+        s4o.print("*");
+        symbol->global_var_name->accept(*this);
+      }
+      else
+        symbol->location->accept(*this);
+      s4o.print(" = ");
+      if (this->current_var_init_symbol != NULL) {
+        this->current_var_init_symbol->accept(*this);
+      }
+      s4o.print(";");
+      break;
+
+    default:
+      ERROR;
+  } /* switch() */
+
+  return NULL;
+}
+
+
+
+/*  AT direct_variable */
+// SYM_REF2(location_c, direct_variable, unused)
+void *visit(location_c *symbol) {
+  TRACE("location_c");
+  return symbol->direct_variable->accept(*this);
+}
+
+
+/*| global_var_list ',' global_var_name */
+//SYM_LIST(global_var_list_c)
+void *visit(global_var_list_c *symbol) {
+  TRACE("global_var_list_c");
+  list_c *list = dynamic_cast<list_c *>(symbol);
+  /* should NEVER EVER occur!! */
+  if (list == NULL) ERROR;
+
+  /* now to produce the c equivalent... */
+  switch (wanted_varformat) {
+    case local_vf:
+    case localinit_vf:
+      for(int i = 0; i < list->n; i++) {
+        s4o.print(s4o.indent_spaces);
+        this->current_var_type_symbol->accept(*this);
+        s4o.print(" __");
+        list->elements[i]->accept(*this);
+        s4o.print(";\n");
+        this->current_var_type_symbol->accept(*this);
+        s4o.print(" *");
+        list->elements[i]->accept(*this);
+        s4o.print(" = &__");
+        list->elements[i]->accept(*this);
+#if 0
+        if (wanted_varformat == localinit_vf) {
+          if (this->current_var_init_symbol != NULL) {
+            s4o.print(" = ");
+            this->current_var_init_symbol->accept(*this);
+          }
+        }
+#endif
+        s4o.print(";\n");
+      }
+      break;
+
+    case constructorinit_vf:
+      if (this->current_var_init_symbol != NULL) {
+        for(int i = 0; i < list->n; i++) {
+          s4o.print(nv->get());
+
+          s4o.print("*");
+          list->elements[i]->accept(*this);
+          s4o.print(" = ");
+          this->current_var_init_symbol->accept(*this);
+          s4o.print(";");
+#if 0
+ 	  /* The following code would be for globalinit_vf !!
+	   * But it is not currently required...
+	   */
+	  s4o.print(s4o.indent_spaces + "__ext_element_c<");
+          this->current_var_type_symbol->accept(*this);
+          s4o.print("> ");
+          if (this->globalnamespace != NULL) {
+            this->globalnamespace->accept(*this);
+            s4o.print("::");
+          }
+          list->elements[i]->accept(*this);
+
+          if (this->current_var_init_symbol != NULL) {
+            s4o.print(" = ");
+            s4o.print("__ext_element_c<");
+            this->current_var_type_symbol->accept(*this);
+            s4o.print(">(");
+            this->current_var_init_symbol->accept(*this);
+            s4o.print(")");
+	  }
+          s4o.print(";\n");
+#endif
+        }
+      }
+      break;
+
+    default:
+      ERROR; /* not supported, and not needed either... */
+  }
+
+  return NULL;
+}
+
+
+#if 0
+/*  var1_list ':' single_byte_string_spec */
+SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec)
+
+/*  STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */
+/* integer ->may be NULL ! */
+/* single_byte_character_string ->may be NULL ! */
+SYM_REF2(single_byte_string_spec_c, integer, single_byte_character_string)
+
+/*  var1_list ':' double_byte_string_spec */
+SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec)
+
+/*  WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */
+/* integer ->may be NULL ! */
+/* double_byte_character_string ->may be NULL ! */
+SYM_REF2(double_byte_string_spec_c, integer, double_byte_character_string)
+
+/*| VAR [RETAIN|NON_RETAIN] incompl_located_var_decl_list END_VAR */
+/* option ->may be NULL ! */
+SYM_REF2(incompl_located_var_declarations_c, option, incompl_located_var_decl_list)
+
+/* helper symbol for incompl_located_var_declarations */
+/*| incompl_located_var_decl_list incompl_located_var_decl ';' */
+SYM_LIST(incompl_located_var_decl_list_c)
+
+/*  variable_name incompl_location ':' var_spec */
+SYM_REF4(incompl_located_var_decl_c, variable_name, incompl_location, var_spec, unused)
+
+/*  AT incompl_location_token */
+SYM_TOKEN(incompl_location_c)
+#endif
+
+
+void *visit(falling_edge_option_c *symbol) {
+  // TO DO ...
+  s4o.print("F_EDGE");
+  return NULL;
+}
+
+
+void *visit(var1_init_decl_c *symbol) {
+  TRACE("var1_init_decl_c");
+  /* We have several implementation alternatives here...
+   *
+   * The issue is that generation of c code
+   * based on the abstract syntax tree requires the reversal
+   * of the symbol order compared to st. For e.g.:
+   * (ST) a, b, c: INT := 98;
+   * (C ) int a=98, b=98, c=98;
+   * The spec_init contains the references to 'INT' and '98'.
+   * The var1_list contains the references to 'a', 'b', and 'c'.
+   * The var1_init_decl_c contains the references to spec_init and var1_list.
+   *
+   * For c code generation, the var1_init_decl_c visitor
+   * would need to access the internals of other classes
+   * (e.g. the simple_spec_init_c).
+   *
+   * We can do this using one of three methods:
+   *   1) Create the abstract syntax tree differently;
+   *   2) Access other classes from within the var1_init_decl_c;
+   *   3) Pass info between the visitors using global variables
+   *       only acessible by this class (private vars)
+   *
+   * In 1), the abstract syntax tree would be built so that
+   * var1_init_decl_c would contain direct references to
+   * var1_list_c, to the var type 'INT' and to the initialiser '98'
+   * (as per the example above).
+   *
+   * 2) would have several sub-options to obtain the references
+   * to 'INT' and '98' from within var1_init_decl_c.
+   *  In 2a), the var1_init_decl_c would use dynamic casts to determine
+   * the class of spec_init (simple_spec_init_c, subrange_spec_init_c or
+   * enumerated_spec_init_c), and from there obtain the 'INT' and '98'
+   *  In 2b) var1_init_decl_c would have one reference for each
+   * simple_spec_init_c, subrange_spec_init_c and enumerated_spec_init_c,
+   * the apropriate one being initialised by the var1_init_decl_c constructor.
+   * Note that the constructor would use dynamic casts. In essence, we
+   * would merely be changing the location of the code with the
+   * dynamic casts.
+   *  In 2c) we would use three overloaded constructors for var1_init_decl_c
+   * one each for simple_spec_init_c, etc... This implies
+   * use type specific pointers to each symbol in the bison
+   * parser, instead of simply using a symbol_c * for all symbols;
+   *
+   * In 3), we use two global but private variables with references to
+   * 'INT' and '98', that would be initiliased by the visitors to
+   * simple_spec_init_c, subrange_spec_init_c and enumerated_spec_init_c.
+   * The visitor to var1_list_c would then use the references in the global
+   * variables.
+   *
+   * I (Mario) have chosen to use 3).
+   */
+
+  /* Start off by setting the current_var_type_symbol and
+   * current_var_init_symbol private variables...
+   */
+  update_type_init(symbol->spec_init);
+
+  /* now to produce the c equivalent... */
+  symbol->var1_list->accept(*this);
+
+  /* Values no longer in scope, and therefore no longer used.
+   * Make an effort to keep them set to NULL when not in use
+   * in order to catch bugs as soon as possible...
+   */
+  void_type_init();
+
+  return NULL;
+}
+
+
+
+void *visit(var1_list_c *symbol) {
+  TRACE("var1_list_c");
+  declare_variables(symbol);
+  return NULL;
+}
+
+
+
+
+/* intermediate helper symbol for:
+ *  - non_retentive_var_decls
+ *  - output_declarations
+ */