Thread: Language extension

  1. #1
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897

    Language extension

    The task is to extend the C or C++ language by writing a preprocessor. The preprocessor need only be a program that reads the extended source file, makes textual changes, and saves the resulting C or C++ source file.

    Beginners
    ---------

    Extend either C or C++ to use a Pascal style for loop. The grammer is:
    Code:
    for identifier := i (to|downto) n
      statement
    For example, a loop to count from 1 to 10 would be written in the extended language as:
    Code:
    for i := 1 to 10 {
      printf("%d\n", i);
    }
    And to count down from 10 to 1:
    Code:
    for i := 10 downto 1 {
      printf("%d\n", i);
    }
    Intermediate
    ------------

    Extend either C or C++ to perform run-time boundary checking on arrays. If a subscript would cause the array access to go out of bounds, the program should terminate with an appropriate error message. The check must be performed before the array is accessed, otherwise undefined behavior would make this extension useless.

    Advanced
    ---------

    This task is for C only. Write an extension that gives C a built in string type. The following operators are required for the C string type:

    ==, !=, <, >, <=, >=

    C strings must be able to grow dynamically. The null character has no special meaning in a C string. Strings may use the subscript operator to obtain and modify a specific character. If the subscript is out of range then the string will grow to meet the demand. Negative subscripts will walk backward from the end of the string.

    Entries will be judged on how complete and robust the extension is. Sign-up for the contest will last one month. When all entries that have signed-up are complete, the entries will be judged. Entries that have not signed-up will be accepted provided they are submitted before all signed-up entries.
    My best code is written with the delete key.

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    So essentially, you would send a file through the preprocessor and it would do this?
    Another great contest .
    Could we do more than one part? IE Beginners, Intermediate, and Advanced?
    Do not make direct eye contact with me.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So essentially, you would send a file through the preprocessor and it would do this?
    Yes.

    >Could we do more than one part? IE Beginners, Intermediate, and Advanced?
    Way ahead of you. The beginner extension only requires simple textual replacement. The intermediate extension pretty much requires a symbol table for arrays with size information. And the advanced extension is just plain tedious.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    58
    dont u think even the one u classified as a beginner category is way ahead for a beginner. i am not sure though. can u further suggest what to do here. how to connect to the prepocessor. i mean how to make a check. like for example in case of tc we hit alt-f9 ctrl-f9 right. so when will the preprocessor take over and how to make it take over.
    2. same to be told how to do in case of gcc.
    clarify that pls.thanx.
    even a fish would not have been in danger had it kept its mouth shut.

  5. #5
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Prelude means a seperate program. For example, you would create extcpp.exe, and run it. It would take in the name of the file and run through it, changing the extended code to C++ and leaving everything else alone. The program would then end. You could then send the pure C++ file through a true compiler.
    Do not make direct eye contact with me.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >dont u think even the one u classified as a beginner category is way ahead for a beginner.
    It's just simple textual substitution. I think anyone who can read from a file and parse a simple string should have little trouble with it.

    >how to connect to the prepocessor.
    The preprocessor is just a separate program that you run on the source code before really compiling it. For example, you have a .ec file that uses extended C:
    Code:
    #include <stdio.h>
    
    int
    main(void)
    {
      printf("My extended C program\n");
      for i := 1 to 10 {
        printf("%d\n", i);
      }
      printf("Bye bye!\n");
    
      return 0;
    }
    Then you would run the preprocessor program independently of the compiler to get a .c file (assuming you call your preprocessor program ppec and it takes two arguments, the first being the extended C and the second being the resulting C source file):
    Code:
    $ ppec prog.ec prog.c
    $ cat prog.c
    #include <stdio.h>
    
    int
    main(void)
    {
      printf("My extended C program\n");
      {
        int i;
        for (i = 1; i <= 10; i++) {
          printf("%d\n", i);
        }
      }
      printf("Bye bye!\n");
    
      return 0;
    }
    Then you run the compiler on that C source file:
    Code:
    $ gcc prog.c
    $ ./a.out
    My extended C program
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Bye bye!
    My best code is written with the delete key.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I am signing up for the first one, I want to sign up for the second one but I am not sure what you mean. I understand run-time checking of an array for out of bounds limits, but what do you mean by subscript

  8. #8
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    The subscript operator is [].
    Basically, if the number within said operator is too high / low, then an error should occur.
    Do not make direct eye contact with me.

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    here is what I get from it
    Code:
    int main(void){
         int array[10];
         int i;
         for(i=1;i<=10;i++)
              array[i]=i;
        return 0;
    }
    this or is there more to it. This is a bound overrun. But it is precompiled are you talking about a user inputting something. Then see if it is too big in the code itself?

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    One small correction. On the pascal for loop the actual syntax is:
    Code:
    for identifier := i (to|downto) n do
      statement
    On the C string do we have to provide normal 0 base counting for the accessing of a character? Does it have to support any length or is their a limit?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >On the pascal for loop the actual syntax is:
    I know, but I was going for a decent merge rather than an exact duplicate of the Pascal syntax. A duplicate would be more difficult.

    >On the C string do we have to provide normal 0 base counting for the accessing of a character?
    You aren't required to, but it would certainly make sense for most C users.

    >Does it have to support any length or is their a limit?
    The limit is when you run out of memory.

    linuxdude: What I mean is this, based on the code you posted. The loop execution will go as follows:
    Code:
    array[1] = 1;
    array[2] = 2;
    array[3] = 3;
    array[4] = 4;
    array[5] = 5;
    array[6] = 6;
    array[7] = 7;
    array[8] = 8;
    array[9] = 9;
    Error.
    Because 10 is out of bounds for the array, a run-time error is given and the program terminates without accessing the out of bounds index. The equivalent explicit check would be:
    Code:
    int main(void){
         int array[10];
         int i;
         for(i=1;i<=10;i++) {
            assert(i >= 0 && i < 10);
            array[i]=i;
        }
        return 0;
    }
    My best code is written with the delete key.

  12. #12
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    what about different formatting. I am checking to see if it is a normal for loop, but someone might do for\t( or for\n( or for ( or for( do you know what I mean. Do I have to keep looking until i find a ( or a variable to see what type it is?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Do I have to keep looking until i find a ( or a variable to see what type it is?
    Pretty much, yes.
    My best code is written with the delete key.

  14. #14
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I guess I will go for the advanced, but when is it due?
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  15. #15
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Quote Originally Posted by Prelude
    Sign-up for the contest will last one month. When all entries that have signed-up are complete, the entries will be judged. Entries that have not signed-up will be accepted provided they are submitted before all signed-up entries.
    Looks like about a month.
    Do not make direct eye contact with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What language did they make Java in?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-03-2005, 04:18 PM
  2. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 02:40 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM
  5. Languages dying
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 07-29-2003, 10:08 AM