home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / deansi < prev    next >
Text File  |  1989-03-20  |  6KB  |  253 lines

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