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: * The private interface to stage1_2.cc mario@15: */ mario@15: mario@15: mario@15: mario@20: /* !!! WARNING !!! mario@20: * mario@20: * Whoever includes this file (stage1_2_priv.hh) will need Edouard@822: * to first inlcude iec_bison.hh !! mario@20: * mario@20: * Read other comments further down to understand why we don't Edouard@822: * include iec_bison.hh in this file. mario@20: */ mario@20: mario@20: mario@20: mario@15: /* file with the declarations of symbol tables... */ mario@15: #include "../util/symtable.hh" mario@177: #include "stage1_2.hh" mario@15: mario@15: mario@15: /* mario@15: * This file includes the interface through which the lexical parser (stage 1 - flex) mario@15: * and the syntax analyser (stage 2 - bison) interact between themselves. mario@15: * mario@15: * This is mostly through direct access to shared global variables, however some mario@15: * of the global variables will only be accessed through some accessor functions. mario@15: * mario@177: * This file also includes the interface between the main stage1_2() functions and mario@177: * the flex lexical parser. mario@177: * mario@15: * This file also includes some utility functions (strdupX() ) that are both used mario@15: * in the lexical and syntax analysers. mario@15: */ mario@15: mario@15: mario@15: mario@15: mario@177: /*************************************************************/ mario@177: /*************************************************************/ mario@177: /**** *****/ mario@177: /**** I n t e r f a c e B e t w e e n *****/ mario@177: /**** F l e x a n d s t a g e 1 _ 2 () *****/ mario@177: /**** *****/ mario@177: /*************************************************************/ mario@177: /*************************************************************/ mario@177: 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 get_opt_safe_extensions(); mario@177: mjsousa@867: /************************************/ mjsousa@867: /* whether to allow nested comments */ mjsousa@867: /************************************/ mjsousa@867: bool get_opt_nested_comments(); mario@177: mjsousa@934: /**************************************************************************/ mjsousa@934: /* whether to allow REF(), DREF(), REF_TO, NULL and ^ operators/keywords */ mjsousa@934: /**************************************************************************/ mjsousa@934: bool get_opt_ref_standard_extensions(); mjsousa@873: mjsousa@873: mario@177: mario@177: /*************************************************************/ mario@177: /*************************************************************/ mario@177: /**** *****/ mario@177: /**** I n t e r f a c e B e t w e e n *****/ mario@177: /**** F l e x a n d B i s o n *****/ mario@177: /**** *****/ mario@177: /*************************************************************/ mario@177: /*************************************************************/ mario@15: mario@15: /*********************************************/ mario@15: /* print the include file stack to stderr... */ mario@15: /*********************************************/ mario@15: /* This is a service that flex provides to bison... */ mario@15: void print_include_stack(void); mario@15: msousa@756: /*****************************************************/ msousa@756: /* Ask flex to include the source code in the string */ msousa@756: /*****************************************************/ msousa@756: /* This is a service that flex provides to bison... */ msousa@756: /* The string should contain valid IEC 61131-3 source code. Bison will ask flex to insert source msousa@756: * code into the input stream of IEC code being parsed. The code to be inserted is typically msousa@756: * generated automatically. msousa@756: * Currently this is used to insert conversion functions ***_TO_*** (as defined by the standard) msousa@756: * between user defined (i.e. derived) enumerated datatypes, and some basic datatypes msousa@756: * (e.g. INT, STRING, etc...) msousa@756: */ msousa@756: void include_string(const char *source_code); mario@15: msousa@757: msousa@757: /**********************************/ msousa@757: /* Tell flex which file to parse. */ msousa@757: /**********************************/ msousa@757: /* This is a service that flex provides to bison... */ msousa@757: /* Tell flex which file to parse. This function will not imediately start parsing the file. msousa@757: * To parse the file, you then need to call yyparse() msousa@757: * mjsousa@761: * Returns NULL on error opening the file (and a valid errno), or 0 on success. mjsousa@761: * Caller must close the file! mjsousa@761: */ mjsousa@761: FILE *parse_file(const char *filename); msousa@757: msousa@757: mario@15: mario@68: /****************************************************/ mario@68: /* Controlling the entry to the body_state in flex. */ mario@68: /****************************************************/ mario@15: void cmd_goto_body_state(void); mario@15: int get_goto_body_state(void); mario@15: void rst_goto_body_state(void); mario@15: mario@15: mario@68: /*************************************************************/ mario@68: /* Controlling the entry to the sfc_qualifier_state in flex. */ mario@68: /*************************************************************/ mario@68: void cmd_goto_sfc_qualifier_state(void); mario@68: int get_goto_sfc_qualifier_state(void); mario@68: void rst_goto_sfc_qualifier_state(void); mario@68: mario@68: mario@86: /*************************************************************/ mario@86: /* Controlling the entry to the sfc_priority_state in flex. */ mario@86: /*************************************************************/ mario@86: void cmd_goto_sfc_priority_state(void); mario@86: int get_goto_sfc_priority_state(void); mario@86: void rst_goto_sfc_priority_state(void); mario@86: mario@86: mario@74: /*********************************************************/ mario@74: /* Controlling the entry to the task_init_state in flex. */ mario@74: /*********************************************************/ mario@74: void cmd_goto_task_init_state(void); mario@74: int get_goto_task_init_state(void); mario@74: void rst_goto_task_init_state(void); mario@74: mario@74: mario@68: /****************************************************************/ mario@68: /* Returning to state in flex previously pushed onto the stack. */ mario@68: /****************************************************************/ mario@68: void cmd_pop_state(void); mario@68: int get_pop_state(void); mario@68: void rst_pop_state(void); mario@68: mario@68: mario@68: 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@177: * mario@177: * Bison accesses these data structures directly. mario@177: * mario@177: * In essence, they are a data passing mechanism between Bison and Flex. mario@15: */ Edouard@822: /* NOTE: BOGUS_TOKEN_ID is defined in the bison generated file iec_bison.hh. mario@15: * We need this constant defined before we can declare the symbol tables. Edouard@822: * However, we cannot #include "iec_bison.hh" in this file (stage1_2_priv.hh) directly mario@20: * because of the way bison ver. 2.3 is copying all declarations in the prologue Edouard@822: * of iec.y to the iec_bison.hh file (including an #include stage1_2_priv.hh). Edouard@822: * So, if we were to include "iec_bison.hh" here, we would get a circular include. mario@15: * All this means that whoever includes this file (stage1_2_priv.hh) will need Edouard@822: * to take care to first inlcude iec_bison.hh !! mario@15: */ mario@15: /* A symbol table to store all the library elements */ mario@15: /* e.g.: mario@15: * mario@15: * mario@15: * mario@15: * mario@15: */ mario@15: extern 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: extern 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: extern symtable_c direct_variable_symtable; lbessard@175: 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: 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); mario@15: mario@15: mario@177: /*************************************************************/ mario@177: /*************************************************************/ mario@177: /**** *****/ mario@177: /**** U t i l i t y F u n c t i o n s ... *****/ mario@177: /**** *****/ mario@177: /**** *****/ mario@177: /*************************************************************/ mario@177: /*************************************************************/ mario@15: mario@15: /* Join two strings together. Allocate space with malloc(3). */ mario@15: char *strdup2(const char *a, const char *b); mario@15: mario@15: /* Join three strings together. Allocate space with malloc(3). */ mario@15: char *strdup3(const char *a, const char *b, const char *c); mario@15: mario@15: mario@15: