[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [GNU-COBOL] Beginings



Laura Tweedy wrote:

> The latest stuff I've been writing is on
> http://www.gnu.org/software/cobol/tmp as I said before.  I'm just
> overwriting the old tarballs each time.
> 
..sorry I forgot I will have another look

> Tim, I sorry I don't have my email archive handy.  Did you say you had
> written a preprocessor for cobol at one time?  Do you still have that
> code?
I did write a C preprocessor once (superceded by GCC of course),
and I am writing a COBOL "preprocessor" now. 
The idea is that it will handle
- comment-entries
- continuations
- COPY
- REPLACE
and output a file that has none of these horrors but includes
info on the line numbers for feeding to the debugger/listing.
Then the main compiler has a pretty simple task for lexical
analysis.

It is all written bu not all tested (I strayed from the extreme
path for a while - never again). I will post it as soon as it is
tested. I think the lexical parts can be reused (cut paste and
edit) for the main compiler. Have a look at it when I post it -
now promises but I hope it will be ready soon.

By the way, I have attached the hack to handle reference
modifications in the presprocessor. The reason for needing it is
that the replacing can have the 'from' or 'to' as an identifier.
For example


A of B of C (1 2 3 4 5 6 7) (function factorial(3): function
floor(3.2))

The line above is an identifier. So is this:

A (1:2)

You need to scan ahead to tell whether it is an array reference
or a reference modification (maybe this is debatable).

We don't want too much of that sort of code if there is an
alternative.

Tim Josling
 
/*************************************************************
 * hack:
 *
 * having found an identifier (TOK_NUMBERORNAMEORPIC)
 * work out if it has a reference modification right away
 * or one after an array reference
 * or a ref mod after a function call
 *
 * A (EXPRESSION:EXPRESSION) -> TOK_NUMBERORNAMEORPIC_REF_MOD_ONLY
 * A (EXPRESSION ...) ([EXPRESSION]:[EXPRESSION]) 
 * -> TOK_NUMBERORNAMEORPIC_ARRAY_REF_MOD
 * A  -> TOK_NUMBERORNAMEORPIC
 * A (EXPRESSION ...)  -> TOK_NUMBERORNAMEORPIC
 * NUMBER -> TOK_NUMBER
 * 
 * doesn't update *current_token_ptr due to nested identifiers.
 *************************************************************/
 
void 
grok_identifier_type(struct token_struct** current_token_ptr) {

   struct token_struct * work_token;
   struct token_struct * tk1;
   int paren_level=0;
   int got_colon=0;
   int got_array=0;
   int state=0; /* 1=after of/in */
   unsigned char * tkchars;
   int charno;

   /* check if it is a number */
   tk1=*current_token_ptr;
   tkchars=tk1->token_chars;

   tk1->token_type=TOK_NUMBER;
   for (charno=0; charno<tk1->token_length; charno++) {
     unsigned char ch;
     ch=tkchars[charno];
     if (ch=='.') {
       continue;
     }
     if (isdigit(ch)) {
       continue;
     }
     if (ch=='-') {
       if (charno==0) continue;
     }
     if (ch=='+') {
       continue;
     }
     tk1->token_type=TOK_NUMBERORNAMEORPIC_IDENTIFIER_BASE;
     break;
   }

   if (tk1->token_type==TOK_NUMBER) {
     return;
   }

   /* kludgy scan follows */

   work_token=(*current_token_ptr)->token_next;
   
   while (work_token) {
     switch (work_token->token_type) {
     case TOK_LPAREN: 
       if (state!=0) goto done;
       paren_level++;
       work_token=work_token->token_next;
       break;
     case TOK_RPAREN: 
       if (state!=0) goto done;
       paren_level--;
       if (!paren_level) {
         if (got_array) goto done; 
         got_array++;
       }
       work_token=work_token->token_next;
       break;
     case TOK_COLON: 
       if (state!=0) goto done;
       if (paren_level==1) {
         got_colon=1;
         goto done;
       }
       if (paren_level<1) goto done;
       work_token=work_token->token_next;
       break;
     case TOK_PERIOD_SPACE:
     case TOK_PERIOD_NOSPACE:
       goto done;
     case TOK_OF:
     case TOK_IN:
       if ((state)&&(!paren_level)) goto done;
       if (!state&&!paren_level) {
         state=1;
       }
       work_token=work_token->token_next;
       break;
     case TOK_NUMBERORNAMEORPIC:
       if (state==2) {
         state=0;
         work_token=work_token->token_next;
         break;
       }
       if ((state!=1)&&(!paren_level)) goto done;
       state=0;
       work_token=work_token->token_next;
       break;
     default: 
       if (!paren_level) goto done;
       break;
     }
   }
   done:
     if (got_colon) {
       if (got_array) {
         (**current_token_ptr).token_type=TOK_NUMBERORNAMEORPIC_ARRAY_REF_MOD;
       } else {
         (**current_token_ptr).token_type=TOK_NUMBERORNAMEORPIC_REF_MOD_ONLY;
       }
     }
 }