Re: lex and a C style preprocessor

plugge@biv7.sr.fh-mannheim.de (Michel)
18 Mar 1997 12:45:42 -0500

          From comp.compilers

Related articles
lex and a C style preprocessor sam.ravnborg@image.dk (1997-03-14)
Re: lex and a C style preprocessor plugge@biv7.sr.fh-mannheim.de (1997-03-18)
| List of all articles for this month |
From: plugge@biv7.sr.fh-mannheim.de (Michel)
Newsgroups: comp.compilers
Date: 18 Mar 1997 12:45:42 -0500
Organization: Fachhochschule Mannheim - Hochschule fuer Technik und Gestaltung, Germany
References: 97-03-064
Keywords: C, tools

sam.ravnborg@image.dk (Sam Ravnborg) writes:
|>Anyone out there who have implemented a lexer (based on (f)lex) that
|>supports preprocessor directives?
|>I need only the basic syntax:
|>#define <identifier>
|>#undef <identifier>
|>#ifdef <identifier>
|>#elif <identifier>
|>#else
|>#endif


Sam,


I wrote a preprocessor for a larger project. It supports some logical
tests (identifier can have the values 1 (false), 2 (true) or 3
("both": "true and false"); test can be done for true, false, both,
only true, only false (-> not for "both"), only "both"; it supports
also file inclusion, multiple threads (see the example),
"abbreviation" identifiers (may be different for different threads),
line concatenation and some more. The syntax is different from the C
preprocessor syntax, because I use it for FLEX programs, and it should
_not_ resolve the C commands (otherwise one could also use the CPP
preprocessor).


If you are interested, let me know.


Best regards


Michel


**********************************************************************************
file lx2l_demo.lx:
**********************************************************************************


      * The first part consists of LX declarations. The value of DEBUG and EMBEDDED_LATEX
      * are read from the file config.h, the definitions for GERMAN and USE_PLAIN are
      * included here.


@@# DEBUG * include FLEX debug code for compilation with DEBUG=1
@@# EMBEDDED_LATEX * include rules and code for embedded LaTeX
@@# GERMAN NO * include special rules for german language (txt2ltx)
@@# USE_PLAIN YES * include rules and code for plain text (txt2ltx only)




                                                * now read the include files with the LX conditionals:
{@@+ <config.h>} * general definitions of LX conditionals
{@@+ <split.h>} * definition of SPLIT_CVT2LTX


{@@c}{@output_name <test_c.l>}
{@@i}{@output_name <test_i.l>}
{@@t}{@output_name <test_t.l>}


@# str_cmt
{@@t}
{@@i}<STRING1,STRING2,COMMENT1>
{@@c}<STRING1,STRING2,COMMENT1,COMMENT2>


@# el_cmt
{@@t}<{@@#EMBEDDED_LATEX+}EL,EL_SPC{@@#USE_PLAIN+},{@@#EMBEDDED_LATEX#}PLAIN{@@##}
      {@@#GERMAN+},PLAIN_G{@@#GERMAN#}{@@#USE_PLAIN-}{@@#EMBEDDED_LATEX-}{@@##}
      DUMMY{@@#USE_PLAIN#}{@@#EMBEDDED_LATEX#}>
{@@i}<{@@#EMBEDDED_LATEX+}EL,EL_SPC,{@@#EMBEDDED_LATEX#}COMMENT1>
{@@c}<{@@#EMBEDDED_LATEX+}EL,EL_SPC,{@@#EMBEDDED_LATEX#}COMMENT1,COMMENT2>


@# i_cmt
{@@t}{CMT}{SPO}
{@@i}{CMT}
{@@c}{SPO}
{@@@}


      * the following line denotes the end of the LX declaration section


{@@-}


      /* The following is part of a (or better 3) LEX file */


%{
#include <ctype.h>
#include <stdlib.h>
#include <string.h>


{@@c}#include "c2ltx.h"
{@@i}#include "idl2ltx.h"
{@@t}#include "txt2ltx.h"
{@@@}#include "cvt2ltx.h"
%}


{@@#DEBUG+}
{@@} /* The following option line is included only, if DEBUG is defined as YES */
{@@@}%option debug
{@@#DEBUG#}


%%


{@@#EMBEDDED_LATEX+}
{@@@}
<READ_CFG>^"#embedded " init_tp(NULL,set_value_embedded);
{@@#EMBEDDED_LATEX#}
<READ_CFG>^"#font_size " init_tp(&font_size,set_value_text);
{@@c}
<READ_CFG>^"#header " init_cp(&header,set_value_char);
{@@ci}
{@#el_cmt}\([cC]\) fprintf(yyout,"{\\copyright}"); {@@##}
                  pos+=yyleng; {@@^t}line_length+=12;{@@@}
{@@t}
{@#str_cmt}[^ \f\\\t\n] substitute_string(yytext); char_pos++;


{@@c}<TEST1>{@#i_cmt}"/*\\"[clsv]?{WS} {
{@@i}<TEST1>{@#i_cmt}"/*\\"[iv]?{WS} {
            A_LINE;
{@@t}<TEST1>{@#i_cmt}"/*\\"[cilsv]?{WS} {
{@@@}
            if(*(yytext+yyleng-1)=='\n')line++;
            BEGIN(TEST_EL_CMT);
#if DEBUG
            if(debug && !quiet){
                  fprintf(stderr,"embedded LaTeX in line %d\n",line);
                  warn(1);
            }
#endif
      }


{@@#NEVER+}


**********************************************************************************
File config.h: standard C Header file.
**********************************************************************************


#define DEBUG YES
#define EMBEDDED_LATEX NO


**********************************************************************************
now show the files generated by lx2l (the comments showing the LXLINE can be suppressed):
**********************************************************************************


**********************************************************************************
File test_c.l
**********************************************************************************


%{
#include <ctype.h>
#include <stdlib.h>
#include <string.h>


#include "c2ltx.h"
#include "cvt2ltx.h"
%}


%option debug


%%
  /* LXLINE 56 */
<READ_CFG>^"#font_size " init_tp(&font_size,set_value_text); /* LXLINE 61 */
<READ_CFG>^"#header " init_cp(&header,set_value_char); /* LXLINE 63 */
<COMMENT1,COMMENT2>\([cC]\) fprintf(yyout,"{\\copyright}"); pos+=yyleng;
line_length+=12; /* LXLINE 66 */
<TEST1>{SPO}"/*\\"[clsv]?{WS} { /* LXLINE 70 */
            if(*(yytext+yyleng-1)=='\n')line++;
            BEGIN(TEST_EL_CMT);
#if DEBUG
            if(debug && !quiet){
                  fprintf(stderr,"embedded LaTeX in line %d\n",line);
                  warn(1);
            }
#endif
      }
  /* LXLINE 84 */


**********************************************************************************
File test_i.l
**********************************************************************************


%{
#include <ctype.h>
#include <stdlib.h>
#include <string.h>


#include "idl2ltx.h"
#include "cvt2ltx.h"
%}


%option debug


%%
  /* LXLINE 56 */
<READ_CFG>^"#font_size " init_tp(&font_size,set_value_text); /* LXLINE 61 */
<COMMENT1>\([cC]\) fprintf(yyout,"{\\copyright}"); pos+=yyleng;
#endif




























#endif


#endif


      }
  /* LXLINE 84 */


**********************************************************************************
File test_t.l
**********************************************************************************


%{
#include <ctype.h>
#include <stdlib.h>
#include <string.h>


#include "txt2ltx.h"
#include "cvt2ltx.h"
%}


%option debug


%%
  /* LXLINE 56 */
<READ_CFG>^"#font_size " init_tp(&font_size,set_value_text); /* LXLINE 61 */
  /* LXLINE 66 */
[^ \f\\\t\n] substitute_string(yytext); char_pos++; /* LXLINE 68 */
  /* LXLINE 69 */
<TEST1>{CMT}{SPO}"/*\\"[cilsv]?{WS} { /* LXLINE 73 */
            if(*(yytext+yyleng-1)=='\n')line++;
            BEGIN(TEST_EL_CMT);
#if DEBUG
            if(debug && !quiet){
                  fprintf(stderr,"embedded LaTeX in line %d\n",line);
                  warn(1);
            }
#endif
      }
  /* LXLINE 84 */


--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.