home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume06 / deansi < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  6.3 KB

  1. From decwrl!purdue!mailrus!tut.cis.ohio-state.edu!ucbvax!agate!helios.ee.lbl.gov!ncis.llnl.gov!lll-winken!uunet!allbery Fri Mar 24 22:26:05 PST 1989
  2. Article 835 of comp.sources.misc:
  3. Path: decwrl!purdue!mailrus!tut.cis.ohio-state.edu!ucbvax!agate!helios.ee.lbl.gov!ncis.llnl.gov!lll-winken!uunet!allbery
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Newsgroups: comp.sources.misc
  6. Subject: v06i068: Lex programme to convert pANS to Classic C
  7. Message-ID: <51319@uunet.UU.NET>
  8. Date: 21 Mar 89 01:47:57 GMT
  9. Sender: allbery@uunet.UU.NET
  10. Reply-To: lupton@uhccux.uhcc.Hawaii.Edu (Robert Lupton )
  11. Lines: 246
  12. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  13.  
  14. Posting-number: Volume 6, Issue 68
  15. Submitted-by: lupton@uhccux.uhcc.Hawaii.Edu (Robert Lupton )
  16. Archive-name: deansi
  17.  
  18. [Also see "agcpp", as posted to comp.os.minix.  ++bsa]
  19.  
  20. My postnews spat this out (`error 11' -- ?), so here it is by mail.
  21. [Ummm, sounds like a segmentation violation.  Contact your local guru.  ++bsa]
  22.  
  23. Here is a simple-minded Lex programme to convert ANSI to Classic C,
  24. or at least to try. It will convert prototypes external to functions
  25. to extern declarations, convert function declarations from the form
  26. "foo(int a,char *b)" to "foo(a,b) int a; char *b;" and remove a couple
  27. of keywords (volatile, const). No attempt is made to deal with
  28. pre-processor incompatibilities.
  29.  
  30. You are welcome to copy, modify, or (even |-) improve the code,
  31. providing that my name remains on it, you don't make money, and these
  32. conditions remain on the fruits of your labours. Also, please post
  33. any significant improvements.
  34.  
  35.             Robert Lupton
  36.             
  37. : =-=-=-=-=-=-=-=-=-=-= Cut Here =-=-=-=-=-=-=-=-=-=-=
  38. PATH=/bin:/usr/bin:/usr/ucb:/etc:$PATH
  39. export PATH
  40. echo Extracting Makefile
  41. if [ -w Makefile ]; then
  42.     echo File already exists - saving as Makefile.old
  43.     mv Makefile Makefile.old
  44.     chmod 444 Makefile.old
  45. fi
  46. sed 's/^X//' <<'//go.sysin dd *' >Makefile
  47. #
  48. # Take an ansi programme, and prepare it for a K&R1 compiler.
  49. # No attempt is made to deal with preprocessor differences,
  50. # but some keywords are deleted (volatile, const), and declarations
  51. # are modified to be acceptable
  52. #
  53. CFLAGS = -g
  54. #
  55. deansi : deansi.o
  56.     cc -o deansi deansi.o -ll
  57.     @- rm -f deansi.o
  58. #
  59. tidy :
  60.     rm -f *~ deansi.c *.o core a.out
  61. empty : tidy
  62.     rm -f deansi
  63. //go.sysin dd *
  64. if [ `wc -c < Makefile` != 374 ]; then
  65. made=FALSE
  66. echo error transmitting Makefile --
  67. echo length should be 374, not `wc -c < Makefile`
  68. else
  69.     made=TRUE
  70. fi
  71. if [ $made = TRUE ]; then
  72.     chmod 644 Makefile
  73.     echo -n  ; ls -ld Makefile
  74. fi
  75. echo Extracting deansi.l
  76. if [ -w deansi.l ]; then
  77.     echo File already exists - saving as deansi.l.old
  78.     mv deansi.l deansi.l.old
  79.     chmod 444 deansi.l.old
  80. fi
  81. sed 's/^X//' <<'//go.sysin dd *' >deansi.l
  82. %%
  83. %{
  84. X/*
  85.  * Syntax: deansi [ file ]
  86.  *
  87.  * This lex programme takes an ANSI style (prototyped) function declaration,
  88.  * and makes it acceptable to a K&R-1 style (`Classic') compiler. It assumes
  89.  * that the last valid name in a declaration is the name of the variable.
  90.  *
  91.  * Varargs functions (with ...) are not understood.
  92.  *
  93.  * Prototypes ending in a ; are converted to old-style declarations
  94.  *
  95.  * A couple of ANSI keywords are deleted (const, volatile)
  96.  *
  97.  * No attempt is made to deal with preprocessor directives
  98.  *
  99.  * Anyone is permitted to copy, modify, or improve this programme,
  100.  * providing that this notice appears on the final result, and that
  101.  * they don't make any money out of it. 
  102.  * 
  103.  *            Robert Lupton
  104.  */
  105. #include <ctype.h>
  106. #define isok(C) (isalnum(C) || (C) == '_')
  107.  
  108. int brace = 0,                /* level of {} grouping */
  109.     in_comment = 0;            /* am I in a comment? */
  110.  
  111. %}
  112.  
  113. \([ \t]*void[ \t]*\)    {        /* functions declared (void) */
  114.            if(brace == 0) {
  115.               printf("()");
  116.            } else {
  117.               REJECT;
  118.            }
  119.         }
  120.  
  121. \([^{};]*\)[ \t]*;    {        /* prototypes */
  122.            int i,
  123.                paren = 0;    /* level of parens */
  124.  
  125.            if(brace == 0 && !in_comment) {
  126.               for(i = 0;i < yyleng;i++) {
  127.              if(paren == 0) putchar(yytext[i]);
  128.              if(yytext[i] == '(') paren++;
  129.              if(yytext[i] == ')') {
  130.                 paren--;
  131.                 if(paren == 0) putchar(')');
  132.              }
  133.               }
  134.            } else {
  135.               REJECT;
  136.            }
  137.         }
  138.  
  139. \([^{};]*\)    {            /* declarations */
  140.            char *decl;
  141.            int i,j,k;
  142.  
  143.            if(brace == 0 && !in_comment) {
  144.               /* printf("<%s>",yytext); */
  145.               yytext[--yyleng] = '\0'; /* strip closing ')' */
  146.  
  147.               putchar('(');
  148.               for(j = 1;j < yyleng;) {
  149.                        for(i = 0,decl = &yytext[j];
  150.                      decl[i] != '\0' && decl[i] != ',';i++) ;
  151.              j += i + 1;
  152.                        for(;!isok(decl[i]);i--) ;
  153.                        for(k = 0;isok(decl[i - k]);k++) ;
  154.              printf("%.*s",k,&decl[i - k + 1]);
  155.              if(j < yyleng - 1) putchar(',');
  156.               }
  157.               printf(")\n");
  158.  
  159.               for(j = 1;j < yyleng;) {
  160.                        for(i = 0,decl = &yytext[j];
  161.                      decl[i] != ',' && decl[i] != '\0';i++) ;
  162.              
  163.              printf("%.*s;",i,decl);
  164.              j += i + 1;
  165.              if(j < yyleng - 1) putchar('\n');
  166.               }
  167.            } else {
  168.               REJECT;
  169.            }
  170.         }
  171.  
  172. "/*"        { in_comment = 1; ECHO; }
  173.  
  174. "*/"        { in_comment = 0; ECHO; }
  175.  
  176. \"[^"]*\"    { ECHO; }
  177.  
  178. "{"        { if(!in_comment) brace++; ECHO; }
  179.  
  180. "}"        { if(!in_comment) brace--; ECHO; }
  181.  
  182. const[ \t]*    |
  183. volatile[ \t]*    ;
  184.  
  185. [a-z]*        |
  186. .|\n        ECHO;
  187. %%
  188. #include <stdio.h>
  189.  
  190. main(ac,av)
  191. int ac;
  192. char **av;
  193. {
  194.    if(ac > 1) {
  195.       if(freopen(av[1],"r",stdin) == NULL) {
  196.      fprintf(stderr,"Can't open %s\n",av[1]);
  197.      exit(-1);
  198.       }
  199.    }
  200.    yylex();
  201. }
  202. //go.sysin dd *
  203. if [ `wc -c < deansi.l` != 2666 ]; then
  204. made=FALSE
  205. echo error transmitting deansi.l --
  206. echo length should be 2666, not `wc -c < deansi.l`
  207. else
  208.     made=TRUE
  209. fi
  210. if [ $made = TRUE ]; then
  211.     chmod 644 deansi.l
  212.     echo -n  ; ls -ld deansi.l
  213. fi
  214. echo Extracting tst.c
  215. if [ -w tst.c ]; then
  216.     echo File already exists - saving as tst.c.old
  217.     mv tst.c tst.c.old
  218.     chmod 444 tst.c.old
  219. fi
  220. sed 's/^X//' <<'//go.sysin dd *' >tst.c
  221. X/*
  222.  * A test (with parentheses)
  223.  */
  224. extern int main(register int,char **),
  225.        func(void);
  226. static int func2(void (*my_func)(),const char *);
  227.  
  228. main(register int ac,char **av)
  229. {
  230.    volatile int interrupt;
  231.    const char *ptr = "World\n";
  232.  
  233.    (void)func2(func,ptr);
  234. }
  235.  
  236. int
  237. func(void)
  238. {
  239.    printf("Hello ");
  240. }
  241.  
  242. static int
  243. func2(void (*my_func)(),const char *s)
  244. {
  245.    (*my_func)();
  246.    return(printf("%s",s));
  247. }
  248. //go.sysin dd *
  249. if [ `wc -c < tst.c` != 404 ]; then
  250. made=FALSE
  251. echo error transmitting tst.c --
  252. echo length should be 404, not `wc -c < tst.c`
  253. else
  254.     made=TRUE
  255. fi
  256. if [ $made = TRUE ]; then
  257.     chmod 644 tst.c
  258.     echo -n  ; ls -ld tst.c
  259. fi
  260.  
  261.  
  262.