|
1 /* |
|
2 * (c) 2007 Mario de Sousa |
|
3 * |
|
4 * Offered to the public under the terms of the GNU General Public License |
|
5 * as published by the Free Software Foundation; either version 2 of the |
|
6 * License, or (at your option) any later version. |
|
7 * |
|
8 * This program is distributed in the hope that it will be useful, but |
|
9 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
11 * Public License for more details. |
|
12 * |
|
13 * This code is made available on the understanding that it will not be |
|
14 * used in safety-critical situations without a full and competent review. |
|
15 */ |
|
16 |
|
17 /* |
|
18 * An IEC 61131-3 IL and ST compiler. |
|
19 * |
|
20 * Based on the |
|
21 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
22 * |
|
23 */ |
|
24 |
|
25 |
|
26 /* |
|
27 * This file contains the code that calls the stage 1 (lexical anayser) |
|
28 * and stage 2 (syntax parser) during the first pass. |
|
29 */ |
|
30 |
|
31 |
|
32 |
|
33 // #include "stage1_2.hh" |
|
34 #include "iec.y.hh" |
|
35 #include "stage1_2_priv.hh" |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 /**************************************/ |
|
42 /* The name of the file being parsed. */ |
|
43 /**************************************/ |
|
44 /* The name of the file currently being parsed... |
|
45 * Note that flex accesses and updates this global variable |
|
46 * apropriately whenever it comes across an (*#include <filename> *) |
|
47 * directive... |
|
48 * ... and bison will use it when producing error messages. |
|
49 * Note that bison also sets this variable correctly to the first |
|
50 * file being parsed. |
|
51 */ |
|
52 const char *current_filename = NULL; |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 /*****************************************************/ |
|
58 /* Controlling the entry to the st_il_state in flex. */ |
|
59 /*****************************************************/ |
|
60 |
|
61 static int goto_body_state__ = 0; |
|
62 |
|
63 void cmd_goto_body_state(void) {goto_body_state__ = 1;} |
|
64 int get_goto_body_state(void) {return goto_body_state__;} |
|
65 void rst_goto_body_state(void) {goto_body_state__ = 0;} |
|
66 |
|
67 |
|
68 |
|
69 /*********************************/ |
|
70 /* The global symbol tables... */ |
|
71 /*********************************/ |
|
72 /* NOTE: only accessed indirectly by the lexical parser (flex) |
|
73 * through the function get_identifier_token() |
|
74 */ |
|
75 /* NOTE: BOGUS_TOKEN_ID is defined in the bison generated file iec.y.hh. |
|
76 * We need this constant defined before we can declare the symbol tables. |
|
77 * However, we cannot #include "iec.y.hh" in this file (stage1_2_priv.hh) directly |
|
78 * because of the way bison ver. 3.2 is copying all declarations in the prologue |
|
79 * of iec.y to the iec.y.hh file (including an #include stage1_2_priv.hh). |
|
80 * So, if we were to include "iec.y.hh" here, we would get a circular include. |
|
81 * All this means that whoever includes this file (stage1_2_priv.hh) will need |
|
82 * to take care to first inlcude iec.y.hh !! |
|
83 */ |
|
84 /* A symbol table to store all the library elements */ |
|
85 /* e.g.: <function_name , function_decl> |
|
86 * <fb_name , fb_decl> |
|
87 * <type_name , type_decl> |
|
88 * <program_name , program_decl> |
|
89 * <configuration_name , configuration_decl> |
|
90 */ |
|
91 /* static */ symtable_c<int, BOGUS_TOKEN_ID> library_element_symtable; |
|
92 |
|
93 /* A symbol table to store the declared variables of |
|
94 * the function currently being parsed... |
|
95 */ |
|
96 /* static */ symtable_c<int, BOGUS_TOKEN_ID> variable_name_symtable; |
|
97 |
|
98 |
|
99 /* Function only called from within flex! |
|
100 * |
|
101 * search for a symbol in either of the two symbol tables |
|
102 * declared above, and return the token id of the first |
|
103 * symbol found. |
|
104 * Searches first in the variables, and only if not found |
|
105 * does it continue searching in the library elements |
|
106 */ |
|
107 int get_identifier_token(const char *identifier_str) { |
|
108 // std::cout << "get_identifier_token(" << identifier_str << "): \n"; |
|
109 int token_id; |
|
110 |
|
111 if ((token_id = variable_name_symtable.find_value(identifier_str)) == variable_name_symtable.end_value()) |
|
112 if ((token_id = library_element_symtable.find_value(identifier_str)) == library_element_symtable.end_value()) |
|
113 return identifier_token; |
|
114 return token_id; |
|
115 } |
|
116 |
|
117 |
|
118 /************************/ |
|
119 /* Utility Functions... */ |
|
120 /************************/ |
|
121 |
|
122 /* |
|
123 * Join two strings together. Allocate space with malloc(3). |
|
124 */ |
|
125 char *strdup2(const char *a, const char *b) { |
|
126 char *res = (char *)malloc(strlen(a) + strlen(b) + 1); |
|
127 |
|
128 if (!res) |
|
129 return NULL; |
|
130 return strcat(strcpy(res, a), b); /* safe, actually */ |
|
131 } |
|
132 |
|
133 /* |
|
134 * Join three strings together. Allocate space with malloc(3). |
|
135 */ |
|
136 char *strdup3(const char *a, const char *b, const char *c) { |
|
137 char *res = (char *)malloc(strlen(a) + strlen(b) + strlen(c) + 1); |
|
138 |
|
139 if (!res) |
|
140 return NULL; |
|
141 return strcat(strcat(strcpy(res, a), b), c); /* safe, actually */ |
|
142 } |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 /***********************************************************************/ |
|
150 /***********************************************************************/ |
|
151 /***********************************************************************/ |
|
152 /***********************************************************************/ |
|
153 /***********************************************************************/ |
|
154 /***********************************************************************/ |
|
155 /***********************************************************************/ |
|
156 /***********************************************************************/ |
|
157 /***********************************************************************/ |
|
158 /***********************************************************************/ |
|
159 |
|
160 |
|
161 int stage1_2__(const char *filename, const char *includedir, symbol_c **tree_root_ref); |
|
162 |
|
163 |
|
164 int stage1_2(const char *filename, const char *includedir, symbol_c **tree_root_ref) { |
|
165 return stage1_2__(filename, includedir, tree_root_ref); |
|
166 } |
|
167 |