%prefixes% terminal = lex_; %maps% /* ENTRY POINT The main entry point for the grammar is given by unit. */ unit -> read_lex; /* TYPE MAPPINGS These mappings give the correspondences between syntax types and C types. */ BOOL -> int; CHARACTERS -> SID_CHARS; IDENTIFIER -> SID_STRING; STRING -> SID_STRING; %header% @{ /* * Copyright (c) 2007 The Akuma Project * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. * * $Id: syntax.act 130 2007-05-29 12:54:04Z asmodai $ */ /* Crown Copyright (c) 1997 This TenDRA(r) Computer Program is subject to Copyright owned by the United Kingdom Secretary of State for Defence acting through the Defence Evaluation and Research Agency (DERA). It is made available to Recipients with a royalty-free licence for its use, reproduction, transfer to other parties and amendment for any purpose not excluding product development provided that any such use et cetera shall be deemed to be acceptance of the following conditions:- (1) Its Recipients shall ensure that this Notice is reproduced upon any copies or amended versions of it; (2) Any amended version of it shall be clearly marked to show both the nature of and the organisation responsible for the relevant amendment or amendments; (3) Its onward transfer from a recipient to another party shall be deemed to be that party's acceptance of these conditions; (4) DERA gives no warranty or assurance as to its quality or suitability for any purpose and DERA accepts no liability whatsoever in relation to any use to which it may be put. */ #include #include #include "char.h" #include "error.h" #include "lex.h" #include "syntax.h" #include "xalloc.h" /* PARSER TYPES These types give the implementation of the types used in the syntax. */ typedef letter *SID_CHARS; typedef char *SID_STRING; /* SID IDENTIFIER PREFIX This string is added to the start of each sid identifier. */ char *sid_prefix = "lex_"; /* CURRENT CONDITIONAL This variable is used to record the current conditional. */ static char *crt_cond = NULL; @}, @{ /* Crown Copyright (c) 1997 This TenDRA(r) Computer Program is subject to Copyright owned by the United Kingdom Secretary of State for Defence acting through the Defence Evaluation and Research Agency (DERA). It is made available to Recipients with a royalty-free licence for its use, reproduction, transfer to other parties and amendment for any purpose not excluding product development provided that any such use et cetera shall be deemed to be acceptance of the following conditions:- (1) Its Recipients shall ensure that this Notice is reproduced upon any copies or amended versions of it; (2) Any amended version of it shall be clearly marked to show both the nature of and the organisation responsible for the relevant amendment or amendments; (3) Its onward transfer from a recipient to another party shall be deemed to be that party's acceptance of these conditions; (4) DERA gives no warranty or assurance as to its quality or suitability for any purpose and DERA accepts no liability whatsoever in relation to any use to which it may be put. */ #ifndef SYNTAX_INCLUDED #define SYNTAX_INCLUDED extern char *sid_prefix; @}; %terminals% /* IDENTIFIER TERMINAL This action gives the terminal for identifiers. The identifier text is built up in token_buff by the lexical routines. */ identifier : () -> (i : IDENTIFIER) = @{ @i = xstrcpy(token_buff); @}; /* SID IDENTIFIER TERMINAL This action gives the terminal for sid-style identifiers. The identifier text is built up in token_buff by the lexical routines. */ sid-identifier : () -> (i : IDENTIFIER) = @{ int n; char *s; char buff[1000]; strcpy(buff, sid_prefix); n = (int) strlen(buff); for (s = token_buff; *s; s++) { if (*s == '-') { buff[n++] = '_'; buff[n++] = 'H'; } else if (*s == '_') { buff[n++] = '_'; buff[n++] = '_'; } else { buff[n++] = *s; } if (n >= 900) { error(ERROR_SERIOUS, "Identifier too long"); break; } } buff[n] = 0; @i = xstrcpy(buff); @}; /* STRING TERMINAL This action gives the terminal for strings. The string text is built up in token_buff by the lexical routines. */ string : () -> (s : STRING) = @{ @s = xstrcpy(token_buff); @}; /* SPECIAL STRING TERMINALS These actions give a number of special strings. */ upper : () -> (s : STRING) = @{ @s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; @}; lower : () -> (s : STRING) = @{ @s = "abcdefghijklmnopqrstuvwxyz"; @}; digit : () -> (s : STRING) = @{ @s = "0123456789"; @}; %actions% /* CONCATENATE TWO STRINGS This action concatenates two strings. */ : (a : STRING, b : STRING) -> (s : STRING) = @{ @s = xstrcat (@a, @b); @}; /* CREATE A CHARACTER STRING This action maps an input string into its internal representation as an array of character codes. */ : (s : STRING) -> (c : CHARACTERS) = @{ @c = make_string(@s); @}; /* SET WHITE SPACE VARIABLE This action sets the white space variable to the given array of characters. There is a check to make sure that it has not already been set. */ : (s : CHARACTERS) -> () = @{ if (white_space) { error(ERROR_SERIOUS, "White space group already defined"); } white_space = @s; @}; /* CREATE A CHARACTER GROUP This action defines the character group i to be s. */ : (i : IDENTIFIER, s : CHARACTERS) -> () = @{ make_group(@i, @s); @}; /* CREATE A PRE-PASS MAPPING This action creates a pre-pass mapping from s to t. */ : (s : CHARACTERS, t : STRING) -> () = @{ char *data[3]; data[0] = @t; data[1] = NULL; data[2] = crt_cond; add_char(pre_pass, @s, data); @}; /* CREATE A MAIN-PASS MAPPING This action creates a main-pass mapping from s to the lexical token i. */ : (s : CHARACTERS, i : IDENTIFIER, b : BOOL) -> () = @{ char *data[3]; data[0] = @i; data[1] = (@b ? "()" : NULL); data[2] = crt_cond; add_char(main_pass, @s, data); @}; /* CREATE A KEYWORD This action creates a keyword called s. */ : (s : STRING, i : IDENTIFIER, b : BOOL) -> () = @{ char *data[3]; data[0] = @i; data[1] = (@b ? "()" : NULL); data[2] = crt_cond; add_keyword(@s, data); @}; /* ADD A CONDITION This action adds i to the current condition. */ : (i : IDENTIFIER) -> () = @{ if (crt_cond) { crt_cond = xstrcat(crt_cond, xstrcat(" && ", @i)); } else { crt_cond = @i; } @}; /* COMPLEMENT CONDITION This action complements the current condition. */ : () -> () = @{ if (crt_cond) { if (strchr(crt_cond, '&')) { crt_cond = xstrcat(xstrcat("!(", crt_cond), ")"); } else { crt_cond = xstrcat("!", crt_cond); } } @}; /* GET CONDITION This action gets the current condition. */ : () -> (i : IDENTIFIER) = @{ @i = crt_cond; @}; /* SET CONDITION This action sets the current condition. */ : (i : IDENTIFIER) -> () = @{ crt_cond = @i; @}; /* BOOLEANS These actions give the booleans true and false. */ : () -> (b : BOOL) = @{ @b = 1; @}; : () -> (b : BOOL) = @{ @b = 0; @}; /* SYNTAX ERROR This action reports a syntax error. */ : () -> () = @{ error (ERROR_SERIOUS, "Syntax error"); @}; %trailer% @{ @}, @{ #endif @};