Making a compiler.

This is a discussion on Making a compiler. within the C++ Programming forums, part of the General Programming Boards category; I am thinking of making my own programming language, How would i make a compiler in C or C++?? know ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    Making a compiler.

    I am thinking of making my own programming language,

    How would i make a compiler in C or C++??

    know where i can get a good tutorial?

    Please help...

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    >> know where i can get a good tutorial?

    I doubt that it could be all summed up in a single tutorial!

    I'm not completely sure, but I think you'd probably have to use inline asm in your C/C++ app to directly access the hardware. Dont know how you would create the actual .exe assuming you are talking about Windows, though...sorry, I dont know much on this topic.

    After taking in all considerations and observing your posts, I think that its fair to say that you are not likely to be ready for this type of task.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You could invent your own language, and when compiling it you transform it into C/C++ code, ready to be compiled with a C-compiler.

    Other than that, I guess you can create EXE files using binary write functions, though it will be very hard .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    you can create a script that would be interpreted by a program you write. thats what i did for my bot

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    I know, i am not ready to make a compiler, i just want to get some info, and a tutorial on the basics, can i just see some example code, on how to read in text from a text file, then output it into another text file, but the code has to be interpreted, into C++ or any other language, i just want to see an example of how this is done

    I know i am not ready to build a compiler (since i'm stating off in C++) but i am going to make my own programming language, i just want to know the basics.

    So please, tell me if you know where i can get some good tutorials, Please help...

    Ciao

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Just make a calculator that lets you write something like
    this
    x = 5;
    y = 3;
    x + y * 5 + (3 + x) ;

    It would then printout
    x = 5
    y = 3
    28
    You can do this by writing a grammer for the language.
    A good example is http://java.sun.com/docs/books/jls/s.../jTOC.doc.html
    Then you have to figure out a way to parse the data.
    The easiest way doing it yourself is recursive decent.
    If you use c io the functions you would use are
    fgetc, ungetc, scanf

  7. #7
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    http://compilers.iecc.com/crenshaw/

    "are you talking about making a compiler or making a high-level language?
    Source code in a high-level language is converted to assembly, which then is converted to binary. Assembly is kind of easy, but you know how it is...for one line of a high-level language, you have *at least* 3 lines of assembly code...so that'll get really long.

    Once you have the assembly code, you can then use an assembly compiler.

    Thus if you wanted your own language, you'll need to determine the rules, tokens, etc...and then develop the compiler that compiles it into intermediate code (ie: assembly).

    Now, if you wanted to convert assembly to binary...it shouldn't be too difficult. In my electrical engineering class, we had to learn how to translate assembly code into binary (there's the op code, source register, destination register, and the data). It's dependent on the machine that it runs on though.

    Doesn't sound too hard, but it does sound very, very long. By myself, it would probably take a year or so :P"

    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    For now this is all i want to know, if i typed in the word:

    MainFunction

    how would i convert it to to the word:

    Main

    this is the sort of thing i want to do (this example converts a number, i want to know how to convert words):

    #include <iostream.h>

    int main(int arg, char* args[])
    {
    int number = 1;

    if(number == 1)
    {
    cout << "2" << endl;
    }

    return 0;
    }

    This converts a number. What i want to do is something like this:
    (This is incorrect, i know i can't do this, but i am using it as an example!) So please somebody tell me how i can do something liker this:

    #include <iostream.h>

    int main(int arg, char* args)
    {
    char string = "MainFunction";

    if(string == "MainFunction")
    {
    cout << "Main" << endl;
    }

    return 0;
    }

    Do you understand what i am getting at? so how could i do something like that?

  9. #9
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    int main(int arg, char* args)
    {
    char string = "MainFunction";

    if(string == "MainFunction")
    {
    cout << "Main" << endl;
    }

    return 0;
    }

    You should never name anything with string since that's
    a c++ class.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(int arg, char* args)
    {
          char s= "MainFunction";
    
          if(!strcmp(s, "MainFunction"))
          {
               cout << "Main" << endl;
          }
          return 0;
    }

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    char* String1 = "Hello world!";
    char* String2 = "Hello world!";
    
    if(strcmp(String1, String2) == 0)
    {
       cout << "Both strings are equal!";
    }
    else
    {
       cout << "The strings are not equal!";
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    Originally posted by Perica
    Do you understand what i am getting at? so how could i do something like that?
    That's not a compiler, that's even not a parser!

    if(!strcmp(str,"MainFunction")) strcpy(str,"main");

    A compiler or interpreter is much more complex!

    Ich think you should read some tutorials covering the basic of C++
    Hope you don't mind my bad english, I'm Austrian!

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    Hey hey hey,

    wait a minute...i asked them to give me this code and they did. Thank you.

    And you mean i can actuall diclare strings like that:

    char str = "string";

    cool, i never knew

    Thnaks, but, whats the deal with this " char* str;" whats the pointer for, if somebody could please explain.

    And also what is a Parser. And when you said that a High-level Language compiles into assembler, do you mean nasm assembler or what??

    Cya.

  13. #13
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    char Str = "Hello!";

    This is invalid. You can't store a string (x bytes) or the address to a string (4 bytes) in a single char (1 byte).

    What you could do is declare a pointer and make it point to that string:

    char* Str = "Hello again!";

    Or if you want a modifyable string:

    char Str[5] = "Bye!";
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    What do you mean modifyable, can't i modify this:

    char* str = "str";

    ?

    This looks more like an array:

    char* str[5] = "str";

    ciao

  15. #15
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    Hey,

    the only one that worked is the one that Magos wrote, i use MS VC++ 6 by the way.

    But i got a question, how come when you do strcmp, do you check if the strings are equall to 0??

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev C++ Compiler, Indentation?
    By Zeusbwr in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:13 AM
  2. lcc win32 compiler download problems
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-01-2004, 07:39 PM
  3. OpenScript2.0 Compiler
    By jverkoey in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 12:52 PM
  4. Compiler Design... Anyone That Can Help!
    By ComputerNerd888 in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2003, 09:48 AM
  5. help with making Borland C++ compiler work
    By Masada in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2003, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21