etisserant@0: /* etisserant@0: * (c) 2003 Mario de Sousa etisserant@0: * etisserant@0: * Offered to the public under the terms of the GNU General Public License etisserant@0: * as published by the Free Software Foundation; either version 2 of the etisserant@0: * License, or (at your option) any later version. etisserant@0: * etisserant@0: * This program is distributed in the hope that it will be useful, but etisserant@0: * WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General etisserant@0: * Public License for more details. etisserant@0: * etisserant@0: * This code is made available on the understanding that it will not be etisserant@0: * used in safety-critical situations without a full and competent review. etisserant@0: */ etisserant@0: etisserant@0: /* etisserant@0: * An IEC 61131-3 IL and ST compiler. etisserant@0: * etisserant@0: * Based on the etisserant@0: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) etisserant@0: * etisserant@0: */ etisserant@0: etisserant@0: /* etisserant@0: * Stage 1 etisserant@0: * ======= etisserant@0: * etisserant@0: * This file contains the lexical tokens definitions, from which etisserant@0: * the flex utility will generate a lexical parser function. etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: /*****************************/ etisserant@0: /* Lexical Parser Options... */ etisserant@0: /*****************************/ etisserant@0: etisserant@0: /* The lexical analyser will never work in interactive mode, etisserant@0: * i.e., it will only process programs saved to files, and never etisserant@0: * programs being written inter-actively by the user. etisserant@0: * This option saves the resulting parser from calling the etisserant@0: * isatty() function, that seems to be generating some compile etisserant@0: * errors under some (older?) versions of flex. etisserant@0: */ etisserant@0: %option never-interactive etisserant@0: etisserant@0: /* Have the lexical analyser use a 'char *yytext' instead of an etisserant@0: * array of char 'char yytext[??]' to store the lexical token. etisserant@0: */ etisserant@0: %pointer etisserant@0: etisserant@0: etisserant@0: /* Have the lexical analyser ignore the case of letters. etisserant@0: * This will occur for all the tokens and keywords, but etisserant@0: * the resulting text handed up to the syntax parser etisserant@0: * will not be changed, and keep the original case etisserant@0: * of the letters in the input file. etisserant@0: */ etisserant@0: %option case-insensitive etisserant@0: etisserant@0: /* Have the generated lexical analyser keep track of the etisserant@0: * line number it is currently analysing. etisserant@0: * This is used to pass up to the syntax parser etisserant@0: * the number of the line on which the current etisserant@0: * token was found. It will enable the syntax parser etisserant@0: * to generate more informatve error messages... etisserant@0: */ etisserant@0: %option yylineno etisserant@0: etisserant@0: /* required for the use of the yy_pop_state() and etisserant@0: * yy_push_state() functions etisserant@0: */ etisserant@0: %option stack etisserant@0: etisserant@0: /* The '%option stack' also requests the inclusion of etisserant@0: * the yy_top_state(), however this function is not etisserant@0: * currently being used. This means that the compiler etisserant@0: * is complaining about the existance of this function. etisserant@0: * The following option removes the yy_top_state() etisserant@0: * function from the resulting c code, so the compiler etisserant@0: * no longer complains. etisserant@0: */ etisserant@0: %option noyy_top_state etisserant@0: etisserant@0: /**************************************************/ etisserant@0: /* External Variable and Function declarations... */ etisserant@0: /**************************************************/ etisserant@0: etisserant@0: etisserant@0: %{ etisserant@0: /* Define TEST_MAIN to include a main() function. etisserant@0: * Useful for testing the parser generated by flex. etisserant@0: */ etisserant@0: /* etisserant@0: #define TEST_MAIN etisserant@0: */ etisserant@0: /* If lexical parser is compiled by itself, we need to define the following etisserant@0: * constant to some string. Under normal circumstances LIBDIRECTORY is set etisserant@0: * in the syntax parser header file... etisserant@0: */ etisserant@0: #ifdef TEST_MAIN etisserant@40: #define DEFAULT_LIBDIR "just_testing" etisserant@0: #endif etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* Required for strdup() */ etisserant@0: #include etisserant@0: etisserant@0: /* Required only for the declaration of abstract syntax classes etisserant@0: * (class symbol_c; class token_c; class list_c;) etisserant@0: * These will not be used in flex, but the token type union defined etisserant@0: * in iec.hh contains pointers to these classes, so we must include etisserant@0: * it here. etisserant@0: */ etisserant@0: #include "../absyntax/absyntax.hh" etisserant@0: mario@15: etisserant@0: /* generated by bison. etisserant@0: * Contains the definition of the token constants, and the etisserant@0: * token value type YYSTYPE (in our case, a 'const char *') etisserant@0: */ etisserant@0: #include "iec.y.hh" mario@15: #include "stage1_2_priv.hh" mario@15: etisserant@0: etisserant@0: /* Variable defined by the bison parser, etisserant@0: * where the value of the tokens will be stored etisserant@0: */ etisserant@0: extern YYSTYPE yylval; etisserant@0: etisserant@0: /* The name of the file currently being parsed... etisserant@0: * This variable is declared and read from the code generated by bison! etisserant@0: * Note that flex accesses and updates this global variable etisserant@0: * apropriately whenever it comes across an (*#include *) etisserant@0: * directive... etisserant@0: */ mario@15: /* mario@15: NOTE: already defined in iec.y.hh etisserant@0: extern const char *current_filename; mario@15: */ mario@15: etisserant@0: etisserant@0: /* We will not be using unput() in our flex code... */ etisserant@0: #define YY_NO_UNPUT etisserant@0: etisserant@0: /* Variable defined by the bison parser. etisserant@0: * It must be initialised with the location etisserant@0: * of the token being parsed. etisserant@0: * This is only needed if we want to keep etisserant@0: * track of the locations, in order to give etisserant@0: * more meaningful error messages! etisserant@0: */ etisserant@0: extern YYLTYPE yylloc; etisserant@0: etisserant@0: /* Macro that is executed for every action. etisserant@0: * We use it to pass the location of the token etisserant@0: * back to the bison parser... etisserant@0: */ etisserant@0: #define YY_USER_ACTION { \ etisserant@0: yylloc.first_line = yylloc.last_line = yylineno; \ etisserant@0: yylloc.first_column = yylloc.last_column = 0; \ etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /* Since this lexical parser we defined only works in ASCII based etisserant@0: * systems, we might as well make sure it is being compiled on etisserant@0: * one... etisserant@0: * Lets check a few random characters... etisserant@0: */ etisserant@0: #if (('a' != 0x61) || ('A' != 0x41) || ('z' != 0x7A) || ('Z' != 0x5A) || \ etisserant@0: ('0' != 0x30) || ('9' != 0x39) || ('(' != 0x28) || ('[' != 0x5B)) etisserant@0: #error This lexical analyser is not portable to a non ASCII based system. etisserant@0: #endif etisserant@0: etisserant@0: etisserant@0: /* Function only called from within flex, but defined etisserant@0: * in iec.y! lbessard@3: * We declare it here... etisserant@0: * etisserant@0: * Search for a symbol in either of the two symbol tables etisserant@0: * and return the token id of the first symbol found. etisserant@0: * Searches first in the variables, and only if not found etisserant@0: * does it continue searching in the library elements etisserant@0: */ etisserant@0: //token_id_t get_identifier_token(const char *identifier_str); etisserant@0: int get_identifier_token(const char *identifier_str); etisserant@0: %} etisserant@0: etisserant@0: etisserant@0: /***************************************************/ etisserant@0: /* Forward Declaration of functions defined later. */ etisserant@0: /***************************************************/ etisserant@0: etisserant@0: %{ etisserant@0: /* return all the text in the current token back to the input stream. */ etisserant@0: void unput_text(unsigned int n); etisserant@0: %} etisserant@0: etisserant@0: etisserant@0: etisserant@0: /****************************/ etisserant@0: /* Lexical Parser States... */ etisserant@0: /****************************/ etisserant@0: etisserant@0: /* NOTE: Our psrser can parse st or il code, intermixed etisserant@0: * within the same file. etisserant@0: * With IL we come across the issue of the EOL (end of line) token. etisserant@0: * ST, and the declaration parts of IL do not use this token! etisserant@0: * If the lexical analyser were to issue this token during ST etisserant@0: * language parsing, or during the declaration of data types, etisserant@0: * function headers, etc. in IL, the syntax parser would crash. etisserant@0: * etisserant@0: * We can solve this issue using one of three methods: etisserant@0: * (1) Augment all the syntax that does not accept the EOL etisserant@0: * token to simply ignore it. This makes the syntax etisserant@0: * definition (in iec.y) very cluttered! etisserant@0: * (2) Let the lexical parser figure out which language etisserant@0: * it is parsing, and decide whether or not to issue etisserant@0: * the EOL token. This requires the lexical parser etisserant@0: * to have knowledge of the syntax!, making for a poor etisserant@0: * overall organisation of the code. It would also make it etisserant@0: * very difficult to understand the lexical parser as it etisserant@0: * would use several states, and a state machine to transition etisserant@0: * between the states. The state transitions would be etisserant@0: * intermingled with the lexical parser defintion! etisserant@0: * (3) Use a mixture of (1) and (2). The lexical analyser etisserant@0: * merely distinguishes between function headers and function etisserant@0: * bodies, but no longer makes a distinction between il and etisserant@0: * st language bodies. When parsing a body, it will return etisserant@0: * the EOL token. In other states '\n' will be ignored as etisserant@0: * whitespace. etisserant@0: * The ST language syntax has been augmented in the syntax etisserant@0: * parser configuration to ignore any EOL tokens that it may etisserant@0: * come across! etisserant@0: * This option has both drawbacks of option (1) and (2), but etisserant@0: * much less intensely. etisserant@0: * The syntax that gets cluttered is limited to the ST statements etisserant@0: * (which is rather limited, compared to the function headers and etisserant@0: * data type declarations, etc...), while the state machine in etisserant@0: * the lexical parser becomes very simple. All state transitions etisserant@0: * can be handled within the lexical parser by itself, and can be etisserant@0: * easily identified. Thus knowledge of the syntax required by etisserant@0: * the lexical parser is very limited! etisserant@0: * etisserant@0: * Amazingly enough, I (Mario) got to implement option (3) etisserant@0: * at first, requiring two basic states, decl and body. etisserant@0: * The lexical parser will enter the body state when etisserant@0: * it is parsing the body of a function/program/function block. The etisserant@0: * state transition is done when we find a VAR_END that is not followed etisserant@0: * by a VAR! This is the syntax knowledge that gets included in the etisserant@0: * lexical analyser with this option! etisserant@0: * Unfortunately, getting the st syntax parser to ignore EOL anywhere etisserant@0: * where they might appear leads to conflicts. This is due to the fact etisserant@0: * that the syntax parser uses the single look-ahead token to remove etisserant@0: * possible conflicts. When we insert a possible EOL, the single etisserant@0: * look ahead token becomes the EOL, which means the potential conflicts etisserant@0: * could no longer be resolved. etisserant@0: * Removing these conflicts would make the st syntax parser very convoluted, etisserant@0: * and adding the extraneous EOL would make it very cluttered. etisserant@0: * This option was therefore dropped in favour of another! etisserant@0: * etisserant@0: * I ended up implementing (2). Unfortunately the lexical analyser can etisserant@0: * not easily distinguish between il and st code, since function etisserant@0: * calls in il are very similar to function block calls in st. etisserant@0: * We therefore use an extra 'body' state. When the lexical parser etisserant@0: * finds that last END_VAR, it enters the body state. This state etisserant@0: * must figure out what language is being parsed from the first few etisserant@0: * tokens, and switch to the correct state (st or il) according to the etisserant@0: * language. This means that we insert quite a bit of knowledge of the etisserant@0: * syntax of the languages into the lexical parser. This is ugly, but it etisserant@0: * works, and at least it is possible to keep all the state changes together etisserant@0: * to make it easier to remove them later on if need be. etisserant@0: * The body state returns any matched text back to the buffer with unput(), etisserant@0: * to be later matched correctly by the apropriate language parser (st or il). etisserant@0: * The state machine has 6 possible states (INITIAL, config, decl, body, st, il) etisserant@0: * Possible state changes are: lbessard@3: * INITIAL -> decl_state (when a FUNCTION, FUNCTION_BLOCK, or PROGRAM is found, etisserant@0: * and followed by a VAR declaration) lbessard@3: * INITIAL -> il_st_state (when a FUNCTION, FUNCTION_BLOCK, or PROGRAM is found, etisserant@0: * and _not_ followed by a VAR declaration) lbessard@3: * INITIAL -> config_state (when a CONFIGURATION is found) lbessard@3: * decl_state -> il_st_state (when the last END_VAR is found, i.e. the function body starts) lbessard@3: * il_st_state -> sfc_state (when it figures out it is parsing sfc language) lbessard@3: * il_st_state -> st_state (when it figures out it is parsing st language) lbessard@3: * il_st_state -> il_state (when it figures out it is parsing il language) lbessard@3: * decl_state -> INITIAL (when a END_FUNCTION, END_FUNCTION_BLOCK, or END_PROGRAM is found) lbessard@3: * st_state -> INITIAL (when a END_FUNCTION, END_FUNCTION_BLOCK, or END_PROGRAM is found) lbessard@3: * sfc_state -> INITIAL (when a END_FUNCTION, END_FUNCTION_BLOCK, or END_PROGRAM is found) lbessard@3: * il_state -> INITIAL (when a END_FUNCTION, END_FUNCTION_BLOCK, or END_PROGRAM is found) lbessard@3: * config_state -> INITIAL (when a END_CONFIGURATION is found) etisserant@0: */ etisserant@0: /* we are parsing a configuration. */ lbessard@3: %s config_state etisserant@0: etisserant@0: /* we are parsing a function, program or function block declaration */ lbessard@3: %s decl_state etisserant@0: etisserant@0: /* we will be parsing a function body. Whether il/st is remains unknown */ lbessard@3: %x il_st_state etisserant@0: etisserant@0: /* we are parsing il code -> flex must return the EOL tokens! */ lbessard@3: %s il_state etisserant@0: etisserant@0: /* we are parsing st code -> flex must not return the EOL tokens! */ lbessard@3: %s st_state etisserant@0: etisserant@1: /* we are parsing sfc code -> flex must not return the EOL tokens! */ lbessard@3: %s sfc_state etisserant@0: etisserant@0: etisserant@0: etisserant@0: /*******************/ etisserant@0: /* File #include's */ etisserant@0: /*******************/ etisserant@0: etisserant@0: /* We extend the IEC 61131-3 standard syntax to allow inclusion etisserant@0: * of other files, using the IEC 61131-3 pragma directive... etisserant@0: * The accepted syntax is: etisserant@0: * {#include ""} etisserant@0: */ etisserant@0: etisserant@0: /* the "include" states are used for picking up the name of an include file */ etisserant@0: %x include_beg etisserant@0: %x include_filename etisserant@0: %x include_end etisserant@0: etisserant@0: etisserant@0: file_include_pragma_filename [^\"]* etisserant@0: file_include_pragma_beg "{#include"{st_whitespace_only}\" etisserant@0: file_include_pragma_end \"{st_whitespace_only}"}" etisserant@0: file_include_pragma {file_include_pragma_beg}{file_include_pragma_filename}{file_include_pragma_end} etisserant@0: etisserant@0: etisserant@0: %{ etisserant@0: #define MAX_INCLUDE_DEPTH 16 etisserant@0: etisserant@0: typedef struct { etisserant@0: YY_BUFFER_STATE buffer_state; etisserant@0: int lineno; etisserant@0: const char *filename; etisserant@0: } include_stack_t; etisserant@0: etisserant@0: include_stack_t include_stack[MAX_INCLUDE_DEPTH]; etisserant@0: int include_stack_ptr = 0; etisserant@0: etisserant@0: const char *INCLUDE_DIRECTORIES[] = { etisserant@40: DEFAULT_LIBDIR, etisserant@40: ".", etisserant@40: "/lib", etisserant@40: "/usr/lib", etisserant@40: "/usr/lib/iec", etisserant@0: NULL /* must end with NULL!! */ etisserant@0: }; etisserant@0: etisserant@0: %} etisserant@0: etisserant@0: etisserant@0: etisserant@0: /*****************************/ etisserant@0: /* Prelimenary constructs... */ etisserant@0: /*****************************/ etisserant@0: etisserant@0: etisserant@0: /* A pragma... */ etisserant@0: etisserant@0: pragma "{"[^}]*"}" etisserant@0: etisserant@0: /* NOTE: this seemingly unnecessary complex definition is required etisserant@0: * to be able to eat up comments such as: etisserant@0: * '(* Testing... ! ***** ******)' etisserant@0: * without using the trailing context command in flex (/{context}) etisserant@0: * since {comment} itself will later be used with etisserant@0: * trailing context ({comment}/{context}) etisserant@0: */ etisserant@0: not_asterisk [^*] etisserant@0: not_close_parenthesis_nor_asterisk [^*)] etisserant@0: asterisk "*" etisserant@0: comment_text {not_asterisk}|(({asterisk}+){not_close_parenthesis_nor_asterisk}) etisserant@0: etisserant@0: comment "(*"({comment_text}*)({asterisk}+)")" etisserant@0: etisserant@0: etisserant@0: /* etisserant@0: 3.1 Whitespace etisserant@0: (NOTE: Whitespace IS clearly defined, to include newline!!! See section 2.1.4!!!) etisserant@0: No definition of whitespace is given, in other words, the characters that may be used to seperate language tokens are not pecisely defined. One may nevertheless make an inteligent guess of using the space (' '), and other characters also commonly considered whitespace in other programming languages (horizontal tab, vertical tab, form feed, etc.). etisserant@0: The main question is whether the newline character should be considered whitespace. IL language statements use an EOL token (End Of Line) to distinguish between some language constructs. The EOL token itself is openly defined as "normally consist[ing] of the 'paragraph separator' ", leaving the final choice open to each implemention. If we choose the newline character to represent the EOL token, it may then not be considered whitespace. etisserant@0: On the other hand, some examples that come in a non-normative annex of the specification allow function declarations to span multiple3.1 Whitespace etisserant@0: (NOTE: Whitespace IS clearly defined, to include newline!!! See section 2.1.4!!!) etisserant@0: No definition of whitespace is given, in other words, the characters that may be used to seperate language tokens are not pecisely defined. One may nevertheless make an inteligent guess of using the space (' '), and other characters also commonly considered whitespace in other programming languages (horizontal tab, vertical tab, form feed, etc.). etisserant@0: The main question is whether the newline character should be considered whitespace. IL language statements use an EOL token (End Of Line) to distinguish between some language constructs. The EOL token itself is openly defined as "normally consist[ing] of the 'paragraph separator' ", leaving the final choice open to each implemention. If we choose the newline character to represent the EOL token, it may then not be considered whitespace. etisserant@0: On the other hand, some examples that come in a non-normative annex of the specification allow function declarations to span multiple lines, which means that the newline character is being considered as whitespace. etisserant@0: Our implementation works around this issue by including the new line character in the whitespace while parsing function declarations and the ST language, and parsing it as the EOL token only while parsing IL language statements. This requires the use of a state machine in the lexical parser that needs at least some knowledge of the syntax itself. etisserant@0: */ etisserant@0: /* NOTE: Our definition of whitespace will only work in ASCII! etisserant@0: * etisserant@0: * Since the IL language needs to know the location of newline etisserant@0: * (token EOL -> '\n' ), we need one definition of whitespace etisserant@0: * for each language... etisserant@0: */ etisserant@0: /* etisserant@0: * NOTE: we cannot use etisserant@0: * st_whitespace [:space:]* etisserant@0: * since we use {st_whitespace} as trailing context. In our case etisserant@0: * this would not constitute "dangerous trailing context", but the etisserant@0: * lexical generator (i.e. flex) does not know this (since it does etisserant@0: * not know which characters belong to the set [:space:]), and will etisserant@0: * generate a "dangerous trailing context" warning! etisserant@0: * We use this alternative just to stop the flex utility from etisserant@0: * generating the invalid (in this case) warning... etisserant@0: */ etisserant@0: etisserant@0: st_whitespace_only [ \f\n\r\t\v]* etisserant@0: il_whitespace_only [ \f\r\t\v]* etisserant@0: etisserant@0: st_whitespace_text {st_whitespace_only}|{comment}|{pragma} etisserant@0: il_whitespace_text {il_whitespace_only}|{comment}|{pragma} etisserant@0: etisserant@0: st_whitespace {st_whitespace_text}* etisserant@0: il_whitespace {il_whitespace_text}* etisserant@0: etisserant@0: st_whitespace_text_no_pragma {st_whitespace_only}|{comment} etisserant@0: il_whitespace_text_no_pragma {il_whitespace_only}|{comment} etisserant@0: etisserant@0: st_whitespace_no_pragma {st_whitespace_text_no_pragma}* etisserant@0: il_whitespace_no_pragma {il_whitespace_text_no_pragma}* etisserant@0: etisserant@0: qualified_identifier {identifier}(\.{identifier})? etisserant@0: etisserant@0: etisserant@0: etisserant@0: /*****************************************/ etisserant@0: /* B.1.1 Letters, digits and identifiers */ etisserant@0: /*****************************************/ etisserant@0: /* NOTE: The following definitions only work if the host computer etisserant@0: * is using the ASCII maping. For e.g., with EBCDIC [A-Z] etisserant@0: * contains non-alphabetic characters! etisserant@0: * The correct way of doing it would be to use etisserant@0: * the [:upper:] etc... definitions. etisserant@0: * etisserant@0: * Unfortunately, further on we need all printable etisserant@0: * characters (i.e. [:print:]), but excluding '$'. etisserant@0: * Flex does not allow sets to be composed by excluding etisserant@0: * elements. Sets may only be constructed by adding new etisserant@0: * elements, which means that we have to revert to etisserant@0: * [\x20\x21\x23\x25\x26\x28-x7E] for the definition etisserant@0: * of the printable characters with the required exceptions. etisserant@0: * The above also implies the use of ASCII, but now we have etisserant@0: * no way to work around it| etisserant@0: * etisserant@0: * The conclusion is that our parser is limited to ASCII etisserant@0: * based host computers!! etisserant@0: */ etisserant@0: letter [A-Za-z] etisserant@0: digit [0-9] etisserant@0: octal_digit [0-7] etisserant@0: hex_digit {digit}|[A-F] etisserant@0: identifier ({letter}|(_({letter}|{digit})))((_?({letter}|{digit}))*) etisserant@0: etisserant@0: etisserant@0: /*******************/ etisserant@0: /* B.1.2 Constants */ etisserant@0: /*******************/ etisserant@0: etisserant@0: /******************************/ etisserant@0: /* B.1.2.1 Numeric literals */ etisserant@0: /******************************/ etisserant@0: integer {digit}((_?{digit})*) etisserant@0: binary_integer 2#{bit}((_?{bit})*) etisserant@0: bit [0-1] etisserant@0: octal_integer 8#{octal_digit}((_?{octal_digit})*) etisserant@0: hex_integer 16#{hex_digit}((_?{hex_digit})*) etisserant@0: exponent [Ee]([+-]?){integer} etisserant@0: /* The correct definition for real would be: etisserant@0: * real {integer}\.{integer}({exponent}?) etisserant@0: * etisserant@0: * Unfortunately, the spec also defines fixed_point (B 1.2.3.1) as: etisserant@0: * fixed_point {integer}\.{integer} etisserant@0: * etisserant@0: * This means that {integer}\.{integer} could be interpreted etisserant@0: * as either a fixed_point or a real. etisserant@0: * I have opted to interpret {integer}\.{integer} as a fixed_point. etisserant@0: * In order to do this, the definition of real has been changed to: etisserant@0: * real {integer}\.{integer}{exponent} etisserant@0: * etisserant@0: * This means that the syntax parser now needs to define a real to be etisserant@0: * either a real_token or a fixed_point_token! etisserant@0: */ etisserant@0: real {integer}\.{integer}{exponent} etisserant@0: etisserant@0: etisserant@0: /*******************************/ etisserant@0: /* B.1.2.2 Character Strings */ etisserant@0: /*******************************/ etisserant@0: /* etisserant@0: common_character_representation := etisserant@0: etisserant@0: |'$$' etisserant@0: |'$L'|'$N'|'$P'|'$R'|'$T' etisserant@0: |'$l'|'$n'|'$p'|'$r'|'$t' etisserant@0: etisserant@0: NOTE: $ = 0x24 etisserant@0: " = 0x22 etisserant@0: ' = 0x27 etisserant@0: etisserant@0: printable chars in ASCII: 0x20-0x7E etisserant@0: */ etisserant@0: etisserant@0: esc_char_u $L|$N|$P|$R|$T etisserant@0: esc_char_l $l|$n|$p|$r|$t etisserant@0: esc_char $$|{esc_char_u}|{esc_char_l} etisserant@0: double_byte_char (${hex_digit}{hex_digit}{hex_digit}{hex_digit}) etisserant@0: single_byte_char (${hex_digit}{hex_digit}) etisserant@0: etisserant@0: /* WARNING: etisserant@0: * This definition is only valid in ASCII... etisserant@0: * etisserant@0: * Flex includes the function print_char() that defines etisserant@0: * all printable characters portably (i.e. whatever character etisserant@0: * encoding is currently being used , ASCII, EBCDIC, etc...) etisserant@0: * Unfortunately, we cannot generate the definition of etisserant@0: * common_character_representation portably, since flex etisserant@0: * does not allow definition of sets by subtracting etisserant@0: * elements in one set from another set. etisserant@0: * This means we must build up the defintion of etisserant@0: * common_character_representation using only set addition, etisserant@0: * which leaves us with the only choice of defining the etisserant@0: * characters non-portably... etisserant@0: */ etisserant@0: common_character_representation [\x20\x21\x23\x25\x26\x28-\x7E]|{esc_char} etisserant@0: double_byte_character_representation $\"|'|{double_byte_char}|{common_character_representation} etisserant@0: single_byte_character_representation $'|\"|{single_byte_char}|{common_character_representation} etisserant@0: etisserant@0: etisserant@0: double_byte_character_string \"({double_byte_character_representation}*)\" etisserant@0: single_byte_character_string '({single_byte_character_representation}*)' etisserant@0: etisserant@0: etisserant@0: /************************/ etisserant@0: /* B 1.2.3.1 - Duration */ etisserant@0: /************************/ etisserant@0: fixed_point {integer}\.{integer} etisserant@0: etisserant@0: fixed_point_d {fixed_point}d etisserant@0: integer_d {integer}d etisserant@0: etisserant@0: fixed_point_h {fixed_point}h etisserant@0: integer_h {integer}h etisserant@0: etisserant@0: fixed_point_m {fixed_point}m etisserant@0: integer_m {integer}m etisserant@0: etisserant@0: fixed_point_s {fixed_point}s etisserant@0: integer_s {integer}s etisserant@0: etisserant@0: fixed_point_ms {fixed_point}ms etisserant@0: integer_ms {integer}ms etisserant@0: etisserant@0: etisserant@0: /********************************************/ etisserant@0: /* B.1.4.1 Directly Represented Variables */ etisserant@0: /********************************************/ etisserant@0: /* The correct definition, if the standard were to be followed... */ mario@11: mario@11: location_prefix [IQM] mario@11: size_prefix [XBWDL] mario@11: direct_variable_standard %{location_prefix}({size_prefix}?){integer}((.{integer})*) mario@11: etisserant@0: etisserant@0: /* For the MatPLC, we will accept % etisserant@0: * as a direct variable, this being mapped onto the MatPLC point etisserant@0: * named etisserant@0: */ etisserant@0: /* TODO: we should not restrict it to only the accepted syntax etisserant@0: * of as specified by the standard. MatPLC point names etisserant@0: * have a more permissive syntax. etisserant@0: * etisserant@0: * e.g. "P__234" etisserant@0: * Is a valid MatPLC point name, but not a valid !! etisserant@0: * The same happens with names such as "333", "349+23", etc... etisserant@0: * How can we handle these more expressive names in our case? etisserant@0: * Remember that some direct variable may remain anonymous, with etisserant@0: * declarations such as: etisserant@0: * VAR etisserant@0: * AT %I3 : BYTE := 255; etisserant@0: * END_VAR mario@11: * in which case we are currently using "%I3" as the variable mario@11: * name. mario@11: */ mario@11: direct_variable_matplc %{identifier} mario@11: mario@11: direct_variable {direct_variable_standard}|{direct_variable_matplc} etisserant@0: etisserant@0: /******************************************/ etisserant@0: /* B 1.4.3 - Declaration & Initialisation */ etisserant@0: /******************************************/ etisserant@0: incompl_location %[IQM]\* etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: %% etisserant@0: /* fprintf(stderr, "flex: state %d\n", YY_START); */ etisserant@0: etisserant@0: /*****************************************************/ etisserant@0: /*****************************************************/ etisserant@0: /*****************************************************/ etisserant@0: /***** *****/ etisserant@0: /***** *****/ etisserant@0: /***** F I R S T T H I N G S F I R S T *****/ etisserant@0: /***** *****/ etisserant@0: /***** *****/ etisserant@0: /*****************************************************/ etisserant@0: /*****************************************************/ etisserant@0: /*****************************************************/ etisserant@0: mario@13: if (get_goto_body_state()) { mario@6: yy_push_state(il_st_state); mario@13: rst_goto_body_state(); mario@6: } lbessard@3: etisserant@0: /*********************************/ etisserant@0: /* Handle the pragmas! */ etisserant@0: /*********************************/ etisserant@0: etisserant@0: /* We start off by searching for the pragmas we handle in the lexical parser. */ etisserant@0: {file_include_pragma} unput_text(0); yy_push_state(include_beg); etisserant@0: etisserant@0: /* Any other pragma we find, we just pass it up to the syntax parser... */ mario@13: /* Note that the state is exclusive, so we have to include it here too. */ etisserant@0: {pragma} {/* return the pragmma without the enclosing '{' and '}' */ mario@13: yytext[strlen(yytext)-1] = '\0'; etisserant@0: yylval.ID=strdup(yytext+1); etisserant@0: return pragma_token; etisserant@0: } lbessard@3: {pragma} {/* return the pragmma without the enclosing '{' and '}' */ mario@13: yytext[strlen(yytext)-1] = '\0'; etisserant@0: yylval.ID=strdup(yytext+1); etisserant@0: return pragma_token; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /*********************************/ etisserant@0: /* Handle the file includes! */ etisserant@0: /*********************************/ etisserant@0: {file_include_pragma_beg} BEGIN(include_filename); etisserant@0: etisserant@0: {file_include_pragma_filename} { etisserant@0: /* got the include file name */ etisserant@0: int i; etisserant@0: etisserant@0: if (include_stack_ptr >= MAX_INCLUDE_DEPTH) { etisserant@0: fprintf(stderr, "Includes nested too deeply\n"); etisserant@0: exit( 1 ); etisserant@0: } etisserant@0: etisserant@0: (include_stack[include_stack_ptr]).buffer_state = YY_CURRENT_BUFFER; etisserant@0: (include_stack[include_stack_ptr]).lineno = yylineno; etisserant@0: (include_stack[include_stack_ptr]).filename = current_filename; etisserant@0: include_stack_ptr++; etisserant@0: yylineno = 1; etisserant@0: current_filename = strdup(yytext); etisserant@0: etisserant@0: for (i = 0, yyin = NULL; (INCLUDE_DIRECTORIES[i] != NULL) && (yyin == NULL); i++) { etisserant@40: char *full_name = strdup3(INCLUDE_DIRECTORIES[i], "/", yytext); etisserant@0: if (full_name == NULL) { etisserant@0: fprintf(stderr, "Out of memory!\n"); etisserant@0: exit( 1 ); etisserant@0: } etisserant@0: yyin = fopen(full_name, "r"); etisserant@0: free(full_name); etisserant@0: } etisserant@0: etisserant@0: if (!yyin) { etisserant@0: fprintf(stderr, "Error opening included file %s\n", yytext); etisserant@0: exit( 1 ); etisserant@0: } etisserant@0: etisserant@0: /* switch input buffer to new file... */ etisserant@0: yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE)); etisserant@0: /* switch to whatever state was active before the include file */ etisserant@0: yy_pop_state(); etisserant@0: /* now process the new file... */ etisserant@0: } etisserant@0: etisserant@0: etisserant@0: <> { etisserant@0: if (--include_stack_ptr < 0) { etisserant@0: yyterminate(); etisserant@0: } else { etisserant@0: yy_delete_buffer(YY_CURRENT_BUFFER); etisserant@0: yy_switch_to_buffer((include_stack[include_stack_ptr]).buffer_state); etisserant@0: yylineno = include_stack[include_stack_ptr].lineno; etisserant@0: /* removing constness of char *. This is safe actually, etisserant@0: * since the only real const char * that is stored on the stack is etisserant@1: * the first one (i.e. the one that gets stored in include_stack[0], etisserant@0: * which is never free'd! etisserant@0: */ etisserant@0: free((char *)current_filename); etisserant@0: current_filename = include_stack[include_stack_ptr].filename; etisserant@0: yy_push_state(include_end); etisserant@0: } etisserant@0: } etisserant@0: etisserant@0: {file_include_pragma_end} yy_pop_state(); etisserant@0: etisserant@0: etisserant@0: /*********************************/ etisserant@0: /* Handle all the state changes! */ etisserant@0: /*********************************/ etisserant@0: lbessard@3: /* INITIAL -> decl_state */ etisserant@0: { lbessard@3: /* NOTE: how about functions that do not declare variables, and go directly to the il_st_state??? etisserant@0: * - According to Section 2.5.1.3 (Function Declaration), item 2 in the list, a FUNCTION etisserant@0: * must have at least one input argument, so a correct declaration will have at least etisserant@0: * one VAR_INPUT ... VAR_END construct! etisserant@0: * - According to Section 2.5.2.2 (Function Block Declaration), a FUNCTION_BLOCK etisserant@0: * must have at least one input argument, so a correct declaration will have at least etisserant@0: * one VAR_INPUT ... VAR_END construct! etisserant@0: * - According to Section 2.5.3 (Programs), a PROGRAM must have at least one input etisserant@0: * argument, so a correct declaration will have at least one VAR_INPUT ... VAR_END etisserant@0: * construct! etisserant@0: * etisserant@0: * All the above means that we needn't worry about PROGRAMs, FUNCTIONs or lbessard@3: * FUNCTION_BLOCKs that do not have at least one VAR_END before the il_st_state. etisserant@0: * If the code has an error, and no VAR_END before the body, we will simply lbessard@3: * continue in the state, untill the end of the FUNCTION, FUNCTION_BLOCK etisserant@0: * or PROGAM. etisserant@0: */ lbessard@3: FUNCTION BEGIN(decl_state); return FUNCTION; lbessard@3: FUNCTION_BLOCK BEGIN(decl_state); return FUNCTION_BLOCK; lbessard@3: PROGRAM BEGIN(decl_state); return PROGRAM; lbessard@3: CONFIGURATION BEGIN(config_state); return CONFIGURATION; etisserant@0: } etisserant@0: lbessard@3: /* INITIAL -> il_st_state */ etisserant@0: /* required if the function, program, etc.. has no VAR block! */ mario@6: /* We comment it out since the standard does not allow this. */ mario@6: /* NOTE: Even if we were to include the following code, it */ mario@6: /* would have no effect whatsoever since the above */ mario@6: /* rules will take precendence! */ mario@6: /* etisserant@0: { lbessard@3: FUNCTION BEGIN(il_st_state); return FUNCTION; lbessard@3: FUNCTION_BLOCK BEGIN(il_st_state); return FUNCTION_BLOCK; lbessard@3: PROGRAM BEGIN(il_st_state); return PROGRAM; etisserant@0: } mario@6: */ mario@6: mario@6: /* decl_state -> (il_st_state | sfc_state) */ lbessard@3: { mario@6: END_VAR{st_whitespace}VAR {unput_text(strlen("END_VAR")); mario@6: return END_VAR; mario@6: } mario@6: END_VAR{st_whitespace}INITIAL_STEP {unput_text(strlen("END_VAR")); mario@9: yy_push_state(sfc_state); mario@6: return END_VAR; mario@6: } mario@6: END_VAR{st_whitespace} {unput_text(strlen("END_VAR")); mario@6: cmd_goto_body_state(); mario@6: return END_VAR; mario@6: } etisserant@0: } etisserant@0: mario@6: /* il_st_state -> (il_state | st_state) */ lbessard@3: { mario@13: {st_whitespace_no_pragma} /* Eat any whitespace */ lbessard@3: {qualified_identifier}{st_whitespace}":=" unput_text(0); BEGIN(st_state); lbessard@58: {direct_variable}{st_whitespace}":=" unput_text(0); BEGIN(st_state); lbessard@3: {qualified_identifier}"[" unput_text(0); BEGIN(st_state); lbessard@3: lbessard@3: RETURN unput_text(0); BEGIN(st_state); lbessard@3: IF unput_text(0); BEGIN(st_state); lbessard@3: CASE unput_text(0); BEGIN(st_state); lbessard@3: FOR unput_text(0); BEGIN(st_state); lbessard@3: WHILE unput_text(0); BEGIN(st_state); lbessard@3: REPEAT unput_text(0); BEGIN(st_state); lbessard@3: EXIT unput_text(0); BEGIN(st_state); mario@6: /* ':=' occurs only in transitions, and not Function or FB bodies! */ mario@6: := unput_text(0); BEGIN(st_state); etisserant@0: etisserant@0: etisserant@0: {identifier} {int token = get_identifier_token(yytext); etisserant@0: if (token == prev_declared_fb_name_token) { etisserant@0: /* the code has a call to a function block */ lbessard@3: BEGIN(st_state); etisserant@0: } else { lbessard@3: BEGIN(il_state); etisserant@0: } etisserant@0: unput_text(0); etisserant@0: } lbessard@3: . unput_text(0); BEGIN(il_state); lbessard@3: } /* end of il_st_state lexical parser */ lbessard@3: lbessard@3: /* (il_state | st_state) -> $previous_state (decl_state or sfc_state) */ lbessard@3: { lbessard@3: END_FUNCTION yy_pop_state(); unput_text(0); lbessard@3: END_FUNCTION_BLOCK yy_pop_state(); unput_text(0); lbessard@3: END_PROGRAM yy_pop_state(); unput_text(0); lbessard@3: END_TRANSITION yy_pop_state(); unput_text(0); mario@6: END_ACTION yy_pop_state(); unput_text(0); lbessard@3: } lbessard@3: lbessard@4: /* sfc_state -> INITIAL */ lbessard@4: { lbessard@4: END_FUNCTION yy_pop_state(); unput_text(0); lbessard@4: END_FUNCTION_BLOCK yy_pop_state(); unput_text(0); lbessard@4: END_PROGRAM yy_pop_state(); unput_text(0); lbessard@4: } lbessard@4: lbessard@4: /* decl_state -> INITIAL */ lbessard@4: { etisserant@0: END_FUNCTION BEGIN(INITIAL); return END_FUNCTION; etisserant@0: END_FUNCTION_BLOCK BEGIN(INITIAL); return END_FUNCTION_BLOCK; etisserant@0: END_PROGRAM BEGIN(INITIAL); return END_PROGRAM; lbessard@3: } etisserant@0: /* config -> INITIAL */ etisserant@0: END_CONFIGURATION BEGIN(INITIAL); return END_CONFIGURATION; etisserant@0: etisserant@0: etisserant@0: etisserant@0: /***************************************/ etisserant@0: /* Next is to to remove all whitespace */ etisserant@0: /***************************************/ etisserant@0: /* NOTE: pragmas are handled right at the beginning... */ etisserant@0: mario@13: {st_whitespace_no_pragma} /* Eat any whitespace */ mario@13: {il_whitespace_no_pragma} /* Eat any whitespace */ etisserant@0: etisserant@0: etisserant@0: /*****************************************/ etisserant@0: /* B.1.1 Letters, digits and identifiers */ etisserant@0: /*****************************************/ etisserant@0: /* NOTE: 'R1', 'IN', etc... are IL operators, and therefore tokens etisserant@0: * On the other hand, the spec does not define them as keywords, etisserant@0: * which means they may be re-used for variable names, etc...! etisserant@0: * The syntax parser already caters for the possibility of these etisserant@0: * tokens being used for variable names in their declarations. etisserant@0: * When they are declared, they will be added to the variable symbol table! etisserant@0: * Further appearances of these tokens must no longer be parsed etisserant@0: * as R1_tokens etc..., but rather as variable_name_tokens! etisserant@0: * etisserant@0: * That is why the first thing we do with identifiers, even before etisserant@0: * checking whether they may be a 'keyword', is to check whether etisserant@0: * they have been previously declared as a variable name, etisserant@0: * mario@13: * However, we have a dilema! Should we here also check for mario@13: * prev_declared_derived_function_name_token? mario@13: * If we do, then the 'MOD' default library function (defined in mario@13: * the standard) will always be returned as a function name, and mario@13: * it will therefore not be possible to use it as an operator as mario@13: * in the following ST expression 'X := Y MOD Z;' ! mario@13: * If we don't, then even it will not be possible to use 'MOD' mario@13: * as a funtion as in 'X := MOD(Y, Z);' mario@13: * We solve this by NOT testing for function names here, and mario@13: * handling this function and keyword clash in bison! etisserant@0: */ etisserant@0: {identifier} {int token = get_identifier_token(yytext); etisserant@0: if ((token == prev_declared_variable_name_token) || mario@13: // (token == prev_declared_derived_function_name_token) || // DO NOT add this condition! etisserant@0: (token == prev_declared_fb_name_token)) { etisserant@0: /* etisserant@0: if (token != identifier_token) etisserant@0: */ mario@13: /* NOTE: if we replace the above uncommented conditions with mario@13: * the simple test of (token != identifier_token), then mario@13: * 'MOD' et al must be removed from the mario@13: * library_symbol_table as a default function name! etisserant@0: */ etisserant@0: yylval.ID=strdup(yytext); etisserant@0: return token; etisserant@0: } etisserant@0: /* otherwise, leave it for the other lexical parser rules... */ etisserant@0: REJECT; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /******************************************************/ etisserant@0: /******************************************************/ etisserant@0: /******************************************************/ etisserant@0: /***** *****/ etisserant@0: /***** *****/ etisserant@0: /***** N O W D O T H E K E Y W O R D S *****/ etisserant@0: /***** *****/ etisserant@0: /***** *****/ etisserant@0: /******************************************************/ etisserant@0: /******************************************************/ etisserant@0: /******************************************************/ etisserant@0: etisserant@0: etisserant@0: EN return EN; etisserant@0: ENO return ENO; etisserant@0: etisserant@0: etisserant@0: /******************************/ etisserant@0: /* B 1.2.1 - Numeric Literals */ etisserant@0: /******************************/ etisserant@0: TRUE return TRUE; lbessard@58: BOOL#1 return TRUE; lbessard@58: BOOL#TRUE return TRUE; etisserant@0: FALSE return FALSE; lbessard@58: BOOL#0 return FALSE; lbessard@58: BOOL#FALSE return FALSE; etisserant@0: etisserant@0: etisserant@0: /************************/ etisserant@0: /* B 1.2.3.1 - Duration */ etisserant@0: /************************/ etisserant@0: t# return T_SHARP; etisserant@0: T# return T_SHARP; etisserant@0: TIME return TIME; etisserant@0: etisserant@0: etisserant@0: /************************************/ etisserant@0: /* B 1.2.3.2 - Time of day and Date */ etisserant@0: /************************************/ etisserant@0: TIME_OF_DAY return TIME_OF_DAY; etisserant@0: TOD return TIME_OF_DAY; etisserant@0: DATE return DATE; etisserant@0: d# return D_SHARP; etisserant@0: D# return D_SHARP; etisserant@0: DATE_AND_TIME return DATE_AND_TIME; etisserant@0: DT return DATE_AND_TIME; etisserant@0: etisserant@0: etisserant@0: /***********************************/ etisserant@0: /* B 1.3.1 - Elementary Data Types */ etisserant@0: /***********************************/ etisserant@0: BYTE return BYTE; etisserant@0: WORD return WORD; etisserant@0: DWORD return DWORD; etisserant@0: LWORD return LWORD; etisserant@0: etisserant@0: etisserant@0: /********************************/ etisserant@0: /* B 1.3.2 - Generic data types */ etisserant@0: /********************************/ etisserant@0: /* Strangely, the following symbols do not seem to be required! */ etisserant@0: /* But we include them so they become reserved words, and do not etisserant@0: * get passed up to bison as an identifier... etisserant@0: */ etisserant@0: ANY return ANY; etisserant@0: ANY_DERIVED return ANY_DERIVED; etisserant@0: ANY_ELEMENTARY return ANY_ELEMENTARY; etisserant@0: ANY_MAGNITUDE return ANY_MAGNITUDE; etisserant@0: ANY_NUM return ANY_NUM; etisserant@0: ANY_REAL return ANY_REAL; etisserant@0: ANY_INT return ANY_INT; etisserant@0: ANY_BIT return ANY_BIT; etisserant@0: ANY_STRING return ANY_STRING; etisserant@0: ANY_DATE return ANY_DATE; etisserant@0: etisserant@0: etisserant@0: /********************************/ etisserant@0: /* B 1.3.3 - Derived data types */ etisserant@0: /********************************/ etisserant@0: ":=" return ASSIGN; etisserant@0: ".." return DOTDOT; etisserant@0: TYPE return TYPE; etisserant@0: END_TYPE return END_TYPE; etisserant@0: ARRAY return ARRAY; etisserant@0: OF return OF; etisserant@0: STRUCT return STRUCT; etisserant@0: END_STRUCT return END_STRUCT; etisserant@0: etisserant@0: etisserant@0: /*********************/ etisserant@0: /* B 1.4 - Variables */ etisserant@0: /*********************/ etisserant@0: REAL return REAL; etisserant@0: LREAL return LREAL; etisserant@0: etisserant@0: SINT return SINT; etisserant@0: INT return INT; etisserant@0: DINT return DINT; etisserant@0: LINT return LINT; etisserant@0: etisserant@0: USINT return USINT; etisserant@0: UINT return UINT; etisserant@0: UDINT return UDINT; etisserant@0: ULINT return ULINT; etisserant@0: etisserant@0: etisserant@0: WSTRING return WSTRING; etisserant@0: STRING return STRING; etisserant@0: BOOL return BOOL; etisserant@0: etisserant@0: TIME return TIME; etisserant@0: DATE return DATE; etisserant@0: DT return DT; etisserant@0: TOD return TOD; etisserant@0: DATE_AND_TIME return DATE_AND_TIME; etisserant@0: TIME_OF_DAY return TIME_OF_DAY; etisserant@0: etisserant@0: etisserant@0: /******************************************/ etisserant@0: /* B 1.4.3 - Declaration & Initialisation */ etisserant@0: /******************************************/ etisserant@0: VAR_INPUT return VAR_INPUT; etisserant@0: VAR_OUTPUT return VAR_OUTPUT; etisserant@0: VAR_IN_OUT return VAR_IN_OUT; etisserant@0: VAR_EXTERNAL return VAR_EXTERNAL; etisserant@0: VAR_GLOBAL return VAR_GLOBAL; etisserant@0: END_VAR return END_VAR; etisserant@0: RETAIN return RETAIN; etisserant@0: NON_RETAIN return NON_RETAIN; etisserant@0: R_EDGE return R_EDGE; etisserant@0: F_EDGE return F_EDGE; etisserant@0: AT return AT; etisserant@0: etisserant@0: etisserant@0: /***********************/ etisserant@0: /* B 1.5.1 - Functions */ etisserant@0: /***********************/ etisserant@0: FUNCTION return FUNCTION; etisserant@0: END_FUNCTION return END_FUNCTION; etisserant@0: VAR return VAR; etisserant@0: CONSTANT return CONSTANT; etisserant@0: etisserant@0: etisserant@0: /*****************************/ etisserant@0: /* B 1.5.2 - Function Blocks */ etisserant@0: /*****************************/ etisserant@0: FUNCTION_BLOCK return FUNCTION_BLOCK; etisserant@0: END_FUNCTION_BLOCK return END_FUNCTION_BLOCK; etisserant@0: VAR_TEMP return VAR_TEMP; etisserant@0: VAR return VAR; etisserant@0: NON_RETAIN return NON_RETAIN; etisserant@0: END_VAR return END_VAR; etisserant@0: etisserant@0: etisserant@0: /**********************/ etisserant@0: /* B 1.5.3 - Programs */ etisserant@0: /**********************/ etisserant@0: PROGRAM return PROGRAM; etisserant@0: END_PROGRAM return END_PROGRAM; etisserant@0: etisserant@0: etisserant@0: /********************************************/ etisserant@0: /* B 1.6 Sequential Function Chart elements */ etisserant@0: /********************************************/ etisserant@0: /* NOTE: the following identifiers/tokens clash with the R and S IL operators, as well etisserant@0: .* as other identifiers that may be used as variable names inside IL and ST programs. etisserant@0: * They will have to be handled when we include parsing of SFC... For now, simply etisserant@0: * ignore them! etisserant@0: */ etisserant@1: lbessard@3: { etisserant@0: ACTION return ACTION; etisserant@0: END_ACTION return END_ACTION; etisserant@0: etisserant@0: TRANSITION return TRANSITION; etisserant@0: END_TRANSITION return END_TRANSITION; etisserant@0: FROM return FROM; etisserant@0: TO return TO; etisserant@0: PRIORITY return PRIORITY; etisserant@0: etisserant@0: INITIAL_STEP return INITIAL_STEP; etisserant@0: STEP return STEP; etisserant@0: END_STEP return END_STEP; etisserant@0: etisserant@0: L return L; etisserant@0: D return D; etisserant@0: SD return SD; etisserant@0: DS return DS; etisserant@0: SL return SL; etisserant@0: etisserant@0: N return N; etisserant@0: P return P; etisserant@0: etisserant@0: R return R; etisserant@0: S return S; etisserant@1: } etisserant@0: etisserant@0: etisserant@0: /********************************/ etisserant@0: /* B 1.7 Configuration elements */ etisserant@0: /********************************/ etisserant@0: CONFIGURATION return CONFIGURATION; etisserant@0: END_CONFIGURATION return END_CONFIGURATION; etisserant@0: TASK return TASK; etisserant@0: RESOURCE return RESOURCE; etisserant@0: ON return ON; etisserant@0: END_RESOURCE return END_RESOURCE; etisserant@0: VAR_CONFIG return VAR_CONFIG; etisserant@0: VAR_ACCESS return VAR_ACCESS; etisserant@0: END_VAR return END_VAR; etisserant@0: WITH return WITH; etisserant@0: PROGRAM return PROGRAM; etisserant@0: RETAIN return RETAIN; etisserant@0: NON_RETAIN return NON_RETAIN; etisserant@0: PRIORITY return PRIORITY; etisserant@0: SINGLE return SINGLE; etisserant@0: INTERVAL return INTERVAL; etisserant@0: READ_WRITE return READ_WRITE; etisserant@0: READ_ONLY return READ_ONLY; etisserant@0: etisserant@0: etisserant@0: /***********************************/ etisserant@0: /* B 2.1 Instructions and Operands */ etisserant@0: /***********************************/ lbessard@3: \n return EOL; etisserant@0: etisserant@0: etisserant@0: /*******************/ etisserant@0: /* B 2.2 Operators */ etisserant@0: /*******************/ etisserant@0: /* NOTE: we can't have flex return the same token for etisserant@0: * ANDN and &N, neither for AND and &, since etisserant@0: * AND and ANDN are considered valid variable etisserant@0: * function or functionblock type names! etisserant@0: * This means that the parser may decide that the etisserant@0: * AND or ANDN strings found in the source code etisserant@0: * are being used as variable names etisserant@0: * and not as operators, and will therefore transform etisserant@0: * these tokens into indentifier tokens! etisserant@0: * We can't have the parser thinking that the source etisserant@0: * code contained the string AND (which may be interpreted etisserant@0: * as a vairable name) when in reality the source code etisserant@0: * merely contained the character &, so we use two etisserant@0: * different tokens for & and AND (and similarly etisserant@0: * ANDN and &N)! etisserant@0: */ etisserant@0: LD return LD; etisserant@0: LDN return LDN; etisserant@0: ST return ST; etisserant@0: STN return STN; etisserant@0: NOT return NOT; etisserant@0: S return S; etisserant@0: R return R; etisserant@0: S1 return S1; etisserant@0: R1 return R1; etisserant@0: CLK return CLK; etisserant@0: CU return CU; etisserant@0: CD return CD; etisserant@0: PV return PV; etisserant@0: IN return IN; etisserant@0: PT return PT; etisserant@0: AND return AND; etisserant@0: & return AND2; etisserant@0: OR return OR; etisserant@0: XOR return XOR; etisserant@0: ANDN return ANDN; etisserant@0: &N return ANDN2; etisserant@0: ORN return ORN; etisserant@0: XORN return XORN; etisserant@0: ADD return ADD; etisserant@0: SUB return SUB; etisserant@0: MUL return MUL; etisserant@0: DIV return DIV; etisserant@0: MOD return MOD; etisserant@0: GT return GT; etisserant@0: GE return GE; etisserant@0: EQ return EQ; etisserant@0: LT return LT; etisserant@0: LE return LE; etisserant@0: NE return NE; etisserant@0: CAL return CAL; etisserant@0: CALC return CALC; etisserant@0: CALCN return CALCN; etisserant@0: RET return RET; etisserant@0: RETC return RETC; etisserant@0: RETCN return RETCN; etisserant@0: JMP return JMP; etisserant@0: JMPC return JMPC; etisserant@0: JMPCN return JMPCN; etisserant@0: etisserant@0: etisserant@0: /***********************/ etisserant@0: /* B 3.1 - Expressions */ etisserant@0: /***********************/ etisserant@0: "**" return OPER_EXP; etisserant@0: "<>" return OPER_NE; etisserant@0: ">=" return OPER_GE; etisserant@0: "<=" return OPER_LE; etisserant@0: AND return AND; etisserant@0: XOR return XOR; etisserant@0: OR return OR; etisserant@0: NOT return NOT; etisserant@0: MOD return MOD; etisserant@0: etisserant@0: etisserant@0: /*****************************************/ etisserant@0: /* B 3.2.2 Subprogram Control Statements */ etisserant@0: /*****************************************/ etisserant@0: := return ASSIGN; etisserant@0: => return SENDTO; etisserant@0: RETURN return RETURN; etisserant@0: etisserant@0: etisserant@0: /********************************/ etisserant@0: /* B 3.2.3 Selection Statements */ etisserant@0: /********************************/ etisserant@0: IF return IF; etisserant@0: THEN return THEN; etisserant@0: ELSIF return ELSIF; etisserant@0: ELSE return ELSE; etisserant@0: END_IF return END_IF; etisserant@0: etisserant@0: CASE return CASE; etisserant@0: OF return OF; etisserant@0: ELSE return ELSE; etisserant@0: END_CASE return END_CASE; etisserant@0: etisserant@0: etisserant@0: /********************************/ etisserant@0: /* B 3.2.4 Iteration Statements */ etisserant@0: /********************************/ etisserant@0: FOR return FOR; etisserant@0: TO return TO; etisserant@0: BY return BY; etisserant@0: DO return DO; etisserant@0: END_FOR return END_FOR; etisserant@0: etisserant@0: WHILE return WHILE; etisserant@0: DO return DO; etisserant@0: END_WHILE return END_WHILE; etisserant@0: etisserant@0: REPEAT return REPEAT; etisserant@0: UNTIL return UNTIL; etisserant@0: END_REPEAT return END_REPEAT; etisserant@0: etisserant@0: EXIT return EXIT; etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: /********************************************************/ etisserant@0: /********************************************************/ etisserant@0: /********************************************************/ etisserant@0: /***** *****/ etisserant@0: /***** *****/ etisserant@0: /***** N O W W O R K W I T H V A L U E S *****/ etisserant@0: /***** *****/ etisserant@0: /***** *****/ etisserant@0: /********************************************************/ etisserant@0: /********************************************************/ etisserant@0: /********************************************************/ etisserant@0: etisserant@0: etisserant@0: /********************************************/ etisserant@0: /* B.1.4.1 Directly Represented Variables */ etisserant@0: /********************************************/ etisserant@0: {direct_variable} {yylval.ID=strdup(yytext); return direct_variable_token;} etisserant@0: etisserant@0: etisserant@0: /******************************************/ etisserant@0: /* B 1.4.3 - Declaration & Initialisation */ etisserant@0: /******************************************/ etisserant@0: {incompl_location} {yylval.ID=strdup(yytext); return incompl_location_token;} etisserant@0: etisserant@0: etisserant@0: /************************/ etisserant@0: /* B 1.2.3.1 - Duration */ etisserant@0: /************************/ etisserant@0: {fixed_point} {yylval.ID=strdup(yytext); return fixed_point_token;} etisserant@0: etisserant@0: {fixed_point_d} {yylval.ID=strdup(yytext); yylval.ID[yyleng-1] = '\0'; return fixed_point_d_token;} etisserant@0: {integer_d} {yylval.ID=strdup(yytext); yylval.ID[yyleng-1] = '\0'; return integer_d_token;} etisserant@0: etisserant@0: {fixed_point_h} {yylval.ID=strdup(yytext); yylval.ID[yyleng-1] = '\0'; return fixed_point_h_token;} etisserant@0: {integer_h} {yylval.ID=strdup(yytext); yylval.ID[yyleng-1] = '\0'; return integer_h_token;} etisserant@0: etisserant@0: {fixed_point_m} {yylval.ID=strdup(yytext); yylval.ID[yyleng-1] = '\0'; return fixed_point_m_token;} etisserant@0: {integer_m} {yylval.ID=strdup(yytext); yylval.ID[yyleng-1] = '\0'; return integer_m_token;} etisserant@0: etisserant@0: {fixed_point_s} {yylval.ID=strdup(yytext); yylval.ID[yyleng-1] = '\0'; return fixed_point_s_token;} etisserant@0: {integer_s} {yylval.ID=strdup(yytext); yylval.ID[yyleng-1] = '\0'; return integer_s_token;} etisserant@0: etisserant@0: {fixed_point_ms} {yylval.ID=strdup(yytext); yylval.ID[yyleng-2] = '\0'; return fixed_point_ms_token;} etisserant@0: {integer_ms} {yylval.ID=strdup(yytext); yylval.ID[yyleng-2] = '\0'; return integer_ms_token;} etisserant@0: etisserant@0: etisserant@0: /*******************************/ etisserant@0: /* B.1.2.2 Character Strings */ etisserant@0: /*******************************/ etisserant@0: {double_byte_character_string} {yylval.ID=strdup(yytext); return double_byte_character_string_token;} etisserant@0: {single_byte_character_string} {yylval.ID=strdup(yytext); return single_byte_character_string_token;} etisserant@0: etisserant@0: etisserant@0: /******************************/ etisserant@0: /* B.1.2.1 Numeric literals */ etisserant@0: /******************************/ etisserant@0: {integer} {yylval.ID=strdup(yytext); return integer_token;} etisserant@0: {real} {yylval.ID=strdup(yytext); return real_token;} etisserant@0: {binary_integer} {yylval.ID=strdup(yytext); return binary_integer_token;} etisserant@0: {octal_integer} {yylval.ID=strdup(yytext); return octal_integer_token;} etisserant@0: {hex_integer} {yylval.ID=strdup(yytext); return hex_integer_token;} etisserant@0: etisserant@0: etisserant@0: /*****************************************/ etisserant@0: /* B.1.1 Letters, digits and identifiers */ etisserant@0: /*****************************************/ lbessard@3: {identifier}/({st_whitespace})"=>" {yylval.ID=strdup(yytext); return sendto_identifier_token;} lbessard@3: {identifier}/({il_whitespace})"=>" {yylval.ID=strdup(yytext); return sendto_identifier_token;} etisserant@0: {identifier} {yylval.ID=strdup(yytext); etisserant@0: /*printf("returning identifier...: %s, %d\n", yytext, get_identifier_token(yytext));*/ etisserant@0: return get_identifier_token(yytext);} etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: /************************************************/ etisserant@0: /************************************************/ etisserant@0: /************************************************/ etisserant@0: /***** *****/ etisserant@0: /***** *****/ etisserant@0: /***** T H E L E F T O V E R S . . . *****/ etisserant@0: /***** *****/ etisserant@0: /***** *****/ etisserant@0: /************************************************/ etisserant@0: /************************************************/ etisserant@0: /************************************************/ etisserant@0: etisserant@0: /* do the single character tokens... etisserant@0: * etisserant@0: * e.g.: ':' '(' ')' '+' '*' ... etisserant@0: */ etisserant@0: . {return yytext[0];} etisserant@0: etisserant@0: etisserant@0: %% etisserant@0: etisserant@0: etisserant@0: /***********************************/ etisserant@0: /* Utility function definitions... */ etisserant@0: /***********************************/ etisserant@0: etisserant@0: /* print the include file stack to stderr... */ etisserant@0: void print_include_stack(void) { etisserant@0: int i; etisserant@0: etisserant@0: if ((include_stack_ptr - 1) >= 0) etisserant@0: fprintf (stderr, "in file "); etisserant@0: for (i = include_stack_ptr - 1; i >= 0; i--) etisserant@0: fprintf (stderr, "included from file %s:%d\n", include_stack[i].filename, include_stack[i].lineno); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /* return all the text in the current token back to the input stream, except the first n chars. */ etisserant@0: void unput_text(unsigned int n) { etisserant@0: /* it seems that flex has a bug in that it will not correctly count the line numbers etisserant@0: * if we return newlines back to the input stream. These newlines will be re-counted etisserant@0: * a second time when they are processed again by flex. etisserant@0: * We therefore determine how many newlines are in the text we are returning, etisserant@0: * and decrement the line counter acordingly... etisserant@0: */ etisserant@0: unsigned int i; etisserant@0: etisserant@0: for (i = n; i < strlen(yytext); i++) etisserant@0: if (yytext[i] == '\n') etisserant@0: yylineno--; etisserant@0: etisserant@0: /* now return all the text back to the input stream... */ etisserant@0: yyless(n); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /* Called by flex when it reaches the end-of-file */ etisserant@0: int yywrap(void) etisserant@0: { etisserant@0: /* We reached the end of the input file... */ etisserant@0: etisserant@0: /* Should we continue with another file? */ etisserant@0: /* If so: etisserant@0: * open the new file... etisserant@0: * return 0; etisserant@0: */ etisserant@0: etisserant@0: /* to we stop processing... etisserant@0: * etisserant@0: * return 1; etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: return 1; /* Stop scanning at end of input file. */ etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: /*************************************/ etisserant@0: /* Include a main() function to test */ etisserant@0: /* the token parsing by flex.... */ etisserant@0: /*************************************/ etisserant@0: #ifdef TEST_MAIN etisserant@0: etisserant@0: #include "../util/symtable.hh" etisserant@0: etisserant@0: yystype yylval; etisserant@0: YYLTYPE yylloc; etisserant@0: etisserant@0: const char *current_filename; etisserant@0: mario@15: mario@15: etisserant@0: int get_identifier_token(const char *identifier_str) {return 0;} etisserant@0: etisserant@0: etisserant@0: etisserant@0: int main(int argc, char **argv) { etisserant@0: etisserant@0: FILE *in_file; etisserant@0: int res; etisserant@0: etisserant@0: if (argc == 1) { etisserant@0: /* Work as an interactive (command line) parser... */ etisserant@0: while((res=yylex())) etisserant@0: fprintf(stderr, "(line %d)token: %d\n", yylineno, res); etisserant@0: } else { etisserant@0: /* Work as non-interactive (file) parser... */ etisserant@0: if((in_file = fopen(argv[1], "r")) == NULL) { etisserant@0: char *errmsg = strdup2("Error opening main file ", argv[1]); etisserant@0: perror(errmsg); etisserant@0: free(errmsg); etisserant@0: return -1; etisserant@0: } etisserant@0: etisserant@0: /* parse the file... */ etisserant@0: yyin = in_file; etisserant@0: current_filename = argv[1]; etisserant@0: while(1) { etisserant@0: res=yylex(); etisserant@0: fprintf(stderr, "(line %d)token: %d (%s)\n", yylineno, res, yylval.ID); etisserant@0: } etisserant@0: } etisserant@0: etisserant@0: return 0; etisserant@0: etisserant@0: } etisserant@0: #endif