mario@15: /*
msousa@264: * matiec - a compiler for the programming languages defined in IEC 61131-3
msousa@264: *
msousa@264: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt)
Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant
msousa@264: *
msousa@264: * This program is free software: you can redistribute it and/or modify
msousa@264: * it under the terms of the GNU General Public License as published by
msousa@264: * the Free Software Foundation, either version 3 of the License, or
msousa@264: * (at your option) any later version.
msousa@264: *
msousa@264: * This program is distributed in the hope that it will be useful,
msousa@264: * but WITHOUT ANY WARRANTY; without even the implied warranty of
msousa@264: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
msousa@264: * GNU General Public License for more details.
msousa@264: *
msousa@264: * You should have received a copy of the GNU General Public License
msousa@264: * along with this program. If not, see .
msousa@264: *
mario@15: *
mario@15: * This code is made available on the understanding that it will not be
mario@15: * used in safety-critical situations without a full and competent review.
mario@15: */
mario@15:
mario@15: /*
msousa@264: * An IEC 61131-3 compiler.
mario@15: *
mario@15: * Based on the
mario@15: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
mario@15: *
mario@15: */
mario@15:
mario@15:
mario@15: /*
mario@15: * This file contains the code that calls the stage 1 (lexical anayser)
mario@15: * and stage 2 (syntax parser) during the first pass.
mario@15: */
mario@15:
etisserant@139: #include
etisserant@139: #include
mario@15:
mario@20: /* file with declaration of absyntax classes... */
mario@20: #include "../absyntax/absyntax.hh"
mario@20:
mario@15:
mario@177: #include "stage1_2.hh"
msousa@354: #include "iec_bison.h"
mario@15: #include "stage1_2_priv.hh"
mario@15:
mario@15:
mario@15:
mario@15:
mario@15:
mario@15: /**************************************/
mario@15: /* The name of the file being parsed. */
mario@15: /**************************************/
mario@15: /* The name of the file currently being parsed...
mario@15: * Note that flex accesses and updates this global variable
mario@15: * apropriately whenever it comes across an (*#include *)
mario@15: * directive...
mario@15: * ... and bison will use it when producing error messages.
mario@15: * Note that bison also sets this variable correctly to the first
mario@15: * file being parsed.
mario@15: */
mario@15: const char *current_filename = NULL;
mario@15:
mario@15:
mario@177: /******************************************************/
mario@177: /* whether we are suporting safe extensions */
mario@177: /* as defined in PLCopen - Technical Committee 5 */
mario@177: /* Safety Software Technical Specification, */
mario@177: /* Part 1: Concepts and Function Blocks, */
mario@177: /* Version 1.0 – Official Release */
mario@177: /******************************************************/
mario@177: bool safe_extensions_ = false;
mario@177: bool get_opt_safe_extensions() {return safe_extensions_;}
mario@15:
mario@68: /****************************************************/
mario@68: /* Controlling the entry to the body_state in flex. */
mario@68: /****************************************************/
mario@15: static int goto_body_state__ = 0;
mario@15:
mario@15: void cmd_goto_body_state(void) {goto_body_state__ = 1;}
mario@15: int get_goto_body_state(void) {return goto_body_state__;}
mario@15: void rst_goto_body_state(void) {goto_body_state__ = 0;}
mario@15:
mario@68: /*************************************************************/
mario@68: /* Controlling the entry to the sfc_qualifier_state in flex. */
mario@68: /*************************************************************/
mario@68: static int goto_sfc_qualifier_state__ = 0;
mario@68:
mario@68: void cmd_goto_sfc_qualifier_state(void) {goto_sfc_qualifier_state__ = 1;}
mario@68: int get_goto_sfc_qualifier_state(void) {return goto_sfc_qualifier_state__;}
mario@68: void rst_goto_sfc_qualifier_state(void) {goto_sfc_qualifier_state__ = 0;}
mario@68:
mario@74: /*************************************************************/
mario@86: /* Controlling the entry to the sfc_priority_state in flex. */
mario@86: /*************************************************************/
mario@86: static int goto_sfc_priority_state__ = 0;
mario@86:
mario@86: void cmd_goto_sfc_priority_state(void) {goto_sfc_priority_state__ = 1;}
mario@86: int get_goto_sfc_priority_state(void) {return goto_sfc_priority_state__;}
mario@86: void rst_goto_sfc_priority_state(void) {goto_sfc_priority_state__ = 0;}
mario@86:
mario@86: /*************************************************************/
mario@74: /* Controlling the entry to the sfc_qualifier_state in flex. */
mario@74: /*************************************************************/
mario@74: static int goto_task_init_state__ = 0;
mario@74:
mario@74: void cmd_goto_task_init_state(void) {goto_task_init_state__ = 1;}
mario@74: int get_goto_task_init_state(void) {return goto_task_init_state__;}
mario@74: void rst_goto_task_init_state(void) {goto_task_init_state__ = 0;}
mario@68:
mario@68: /****************************************************************/
mario@68: /* Returning to state in flex previously pushed onto the stack. */
mario@68: /****************************************************************/
mario@68: static int pop_state__ = 0;
mario@68:
mario@68: void cmd_pop_state(void) {pop_state__ = 1;}
mario@68: int get_pop_state(void) {return pop_state__;}
mario@68: void rst_pop_state(void) {pop_state__ = 0;}
mario@15:
mario@15:
mario@15: /*********************************/
mario@15: /* The global symbol tables... */
mario@15: /*********************************/
mario@15: /* NOTE: only accessed indirectly by the lexical parser (flex)
mario@15: * through the function get_identifier_token()
mario@15: */
msousa@354: /* NOTE: BOGUS_TOKEN_ID is defined in the bison generated file iec_bison.h.
mario@15: * We need this constant defined before we can declare the symbol tables.
msousa@354: * However, we cannot #include "iec_bison.h" in this file (stage1_2_priv.hh) directly
mario@15: * because of the way bison ver. 3.2 is copying all declarations in the prologue
msousa@354: * of iec.y to the iec_bison.h file (including an #include stage1_2_priv.hh).
msousa@354: * So, if we were to include "iec_bison.h" here, we would get a circular include.
mario@15: * All this means that whoever includes this file (stage1_2_priv.hh) will need
msousa@354: * to take care to first inlcude iec_bison.h !!
mario@15: */
mario@15: /* A symbol table to store all the library elements */
mario@15: /* e.g.:
mario@15: *
mario@15: *
mario@15: *
mario@15: *
mario@15: */
mario@15: /* static */ symtable_c library_element_symtable;
mario@15:
mario@15: /* A symbol table to store the declared variables of
mario@15: * the function currently being parsed...
mario@15: */
mario@15: /* static */ symtable_c variable_name_symtable;
mario@15:
lbessard@175: /* A symbol table to store the declared direct variables of
lbessard@175: * the function currently being parsed...
lbessard@175: */
lbessard@175: /* static */ symtable_c direct_variable_symtable;
mario@15:
mario@15: /* Function only called from within flex!
mario@15: *
mario@15: * search for a symbol in either of the two symbol tables
mario@15: * declared above, and return the token id of the first
mario@15: * symbol found.
mario@15: * Searches first in the variables, and only if not found
mario@15: * does it continue searching in the library elements
mario@15: */
mario@15: int get_identifier_token(const char *identifier_str) {
mario@15: // std::cout << "get_identifier_token(" << identifier_str << "): \n";
mario@15: int token_id;
mario@15:
mario@15: if ((token_id = variable_name_symtable.find_value(identifier_str)) == variable_name_symtable.end_value())
mario@15: if ((token_id = library_element_symtable.find_value(identifier_str)) == library_element_symtable.end_value())
mario@15: return identifier_token;
mario@15: return token_id;
mario@15: }
mario@15:
lbessard@175: /* Function only called from within flex!
lbessard@175: *
lbessard@175: * search for a symbol in direct variables symbol table
lbessard@175: * declared above, and return the token id of the first
lbessard@175: * symbol found.
lbessard@175: */
lbessard@175: int get_direct_variable_token(const char *direct_variable_str) {
lbessard@175: int token_id;
lbessard@175:
lbessard@175: if ((token_id = direct_variable_symtable.find_value(direct_variable_str)) == direct_variable_symtable.end_value())
lbessard@175: return direct_variable_token;
lbessard@175: return token_id;
lbessard@175: }
mario@15:
mario@15: /************************/
mario@15: /* Utility Functions... */
mario@15: /************************/
mario@15:
mario@15: /*
mario@15: * Join two strings together. Allocate space with malloc(3).
mario@15: */
mario@15: char *strdup2(const char *a, const char *b) {
mario@15: char *res = (char *)malloc(strlen(a) + strlen(b) + 1);
mario@15:
mario@15: if (!res)
mario@15: return NULL;
mario@15: return strcat(strcpy(res, a), b); /* safe, actually */
mario@15: }
mario@15:
mario@15: /*
mario@15: * Join three strings together. Allocate space with malloc(3).
mario@15: */
mario@15: char *strdup3(const char *a, const char *b, const char *c) {
mario@15: char *res = (char *)malloc(strlen(a) + strlen(b) + strlen(c) + 1);
mario@15:
mario@15: if (!res)
mario@15: return NULL;
mario@15: return strcat(strcat(strcpy(res, a), b), c); /* safe, actually */
mario@15: }
mario@15:
lbessard@136: /*************************/
lbessard@136: /* Tracking Functions... */
lbessard@136: /*************************/
lbessard@136:
lbessard@136: extern tracking_t* current_tracking;
lbessard@136:
lbessard@136: /*--------------------------------------------------------------------
lbessard@136: * GetNextChar
lbessard@136: *
lbessard@136: * reads a character from input for flex
lbessard@136: *------------------------------------------------------------------*/
lbessard@136: int GetNextChar(char *b, int maxBuffer) {
lbessard@136: char *p;
lbessard@136:
lbessard@136: if ( current_tracking->eof )
lbessard@136: return 0;
lbessard@136:
lbessard@136: while ( current_tracking->currentChar >= current_tracking->lineLength ) {
lbessard@136: current_tracking->currentChar = 0;
lbessard@136: current_tracking->currentTokenStart = 1;
lbessard@136: current_tracking->eof = false;
lbessard@136:
lbessard@136: p = fgets(current_tracking->buffer, MAX_BUFFER_LENGTH, current_tracking->in_file);
lbessard@136: if ( p == NULL ) {
lbessard@136: if ( ferror(current_tracking->in_file) )
lbessard@136: return 0;
lbessard@136: current_tracking->eof = true;
lbessard@136: return 0;
lbessard@136: }
lbessard@136:
lbessard@136: current_tracking->lineNumber++;
lbessard@136: current_tracking->lineLength = strlen(current_tracking->buffer);
lbessard@136: }
lbessard@136:
lbessard@136: b[0] = current_tracking->buffer[current_tracking->currentChar];
lbessard@136: if (b[0] == ' ' || b[0] == '\t')
lbessard@136: current_tracking->currentTokenStart++;
lbessard@136: current_tracking->currentChar++;
lbessard@136:
lbessard@136: return b[0]==0?0:1;
lbessard@136: }
lbessard@136:
lbessard@136: tracking_t* GetNewTracking(FILE* in_file) {
lbessard@136: tracking_t* new_env = new tracking_t;
lbessard@136: new_env->eof = 0;
lbessard@136: new_env->lineNumber = 0;
lbessard@136: new_env->currentChar = 0;
lbessard@136: new_env->lineLength = 0;
lbessard@136: new_env->currentTokenStart = 0;
lbessard@136: new_env->buffer = (char*)malloc(MAX_BUFFER_LENGTH);
lbessard@136: new_env->in_file = in_file;
lbessard@136: return new_env;
lbessard@136: }
lbessard@136:
lbessard@136: /***********************************************************************/
lbessard@136: /***********************************************************************/
lbessard@136: /***********************************************************************/
lbessard@136: /***********************************************************************/
lbessard@136: /***********************************************************************/
lbessard@136: /***********************************************************************/
lbessard@136: /***********************************************************************/
lbessard@136: /***********************************************************************/
lbessard@136: /***********************************************************************/
lbessard@136: /***********************************************************************/
lbessard@136:
lbessard@136:
mario@177: int stage2__(const char *filename,
mario@177: const char *includedir, /* Include directory, where included files will be searched for... */
mario@177: symbol_c **tree_root_ref,
mario@177: bool full_token_loc /* error messages specify full token location */
mario@177: );
mario@177:
mario@177:
mario@177: int stage1_2(const char *filename, symbol_c **tree_root_ref, stage1_2_options_t options) {
mario@177: /* NOTE: we only call stage2 (bison - syntax analysis) directly, as stage 2 will itself call stage1 (flex - lexical analysis)
mario@177: * automatically as needed
mario@177: */
mario@177:
mario@177: /* NOTE: Since we do not call stage1__ (flex) directly, we cannot directly pass any parameters to that function either.
mario@177: * In this case, we use callback functions, i.e. stage1__ (i.e. flex) will call functions defined in this file
mario@177: * whenever it needs info/parameters coming from stage1_2().
mario@177: * These callback functions will get their data from local (to this file) global variables...
mario@177: * We now set those variables...
mario@177: */
mario@177: safe_extensions_ = options.safe_extensions;
mario@177:
mario@177: return stage2__(filename, options.includedir, tree_root_ref, options.full_token_loc);
mario@177: }
mario@177: