how to make....

OinkBoink

Senior member
Nov 25, 2003
700
0
71
how do u "make" programs like c++ etc???

are they just direct links to the hardware???

 

JustAnAverageGuy

Diamond Member
Aug 1, 2003
9,057
0
76
Are you asking "how do you create a programming language" or "how do programming languages communicate with the hardware"?
 

thermalpaste

Senior member
Oct 6, 2004
445
0
0
To build a compiler I guess you have to build it from ground up. So you can use assemblers or machine language in order to build a compiler.
 

borealiss

Senior member
Jun 23, 2000
913
0
0
Building a programming language depends on what you're going to do with it. If you're going to be doing a lot of parsing, something like an intrepreted language like Perl might be fine. If you're going to be writing an application, java will probably be good because of it's fast development times and its "compile once run everwhere." If you're going to be building an OS, you need something that will be powerful yet can have access to low level hardware, like C/C++.

After you decide what type of applications you're going to be building with your programming language, you need to specify a syntax and grammar. Again, our target application base is going to determine a lot in our syntax/grammar. Dealing with lots of strings means we're going to have lots of operators that are string friendly. Usually you specify a syntax in the form of a BNF. It stands for Backus Naur Form and is a way of specifying what tokens are permissable in certain sequences. You next need to specify a grammar, which will tell you what context certain tokens can be used in. For example, you can't use a strcmp() function on an integer, fp operation on a string, etc... While you're hammering this out, you need to make the determination somewhere about whether your programming language will be interpreted, compiled to an intermediate language, or compiled directly to machine code. Your target application is going to determine a lot in this area. Let's say you're going to target an operating system. After you've specified your grammar and syntax, it's time to add code to your compiler to construct a parse tree. A parse tree is basically a structure that stores all the information about the program that was just scanned by our lexer and syntactical analyzer and put into a structured form that we can make sense of. It's almost like a heap, but the depth in your tree will depend on the scope of your variables/functions/etc... in your program. This way, when you traverse the tree using depth-first search, variables that have the smallest scope do not get mixed up with variables that have greater scope because a DFS is going to generate code for the highest nested loops first. After you've written a tree traversal function, you next need to tie this into code generation. This is basically taking certain structures and spitting out equivalent assembly for it. So if you are in a forloop node, you'll probably spit out a "JLE" somewhere in your code generator and do "CMP"'s to do your check. It's basically going to involve translating by hand the code you've interpreted in your parse tree. Some compilers are really smart and will actually run more than 2 scans of the program being compiled and rearrange some things to make it run faster. Provided you get this far, you've got yourself a programming language and a compiler ready to go. Hammer out the holes in your language specification and you're all set.
 

mcade

Junior Member
Oct 30, 2004
5
0
0
You asked how to make a programming language, so I will try to answer that question. Though, I would like to say that the process for creating a real, workable language like C/C++ or Java, or even Python is a tremendous task and not one for mere mortals to undertake.

In theory, it's pretty simple. You write a compiler for this theoretical language, then write a program for it and compile it. You have invented a language. That's it really.


Assuming you don't know what a compiler is:

A compiler is, in a nutshell, a program that reads instructions from a file and translates those instructions into the most basic, low level instructions for the processor. So when you write something like, "print 'This is a new programming language' and compile it, the compiler breaks it down into machine language that the processor can understand. Things like "Shift this bit left", "Add this bit to that bit", or "Halt all functions". You would have to have a different compiler for each and every kind of machine you have. Processors based on the x86 (386, 486, pentium, etc) instruction set can run programs compiled for any of those machines becuase the basic instructions for operating are the same. A program compiled for MacOSX won't run on windows because the calls to the processor are different and an Intel machine wouldn't understand it.

So, you write a compiler, you can use C/C++ to do this, it doesn't have to be done in Assembly. You write it so that it knows that when you give it a particular instruction, it turns that instruction into a particular machine code instruction. Do that, and you have made a language.


I am stopping right here because this went on for longer than I meant it to. Oh, and it isn't all strictly true by the way, but it is for our purposes close enough.
 

borealiss

Senior member
Jun 23, 2000
913
0
0
and how do u make compilers?
construct BNF
code lexer (lex)
code syntactical analyzer (yacc)
decide whether compile/interpreted/etc...
code parse tree constructor
code code generator (parse tree traverser)
insert code-generation code into traversal function
 

Gioron

Member
Jul 22, 2004
73
0
0
In a smaller nutshell:
You have to start with a language already written, or else start from assembly or machine code. Its basicly a long series of bootstrapping where simpler compilers are used to make more complex compilers, which are used in turn to make more complex compilers. I think at this point the code for most C compilers is actually written in C, but they had to start out by writing them in annother language that was already working and then port it over once they had a working C compiler.

Yes, I know that what I said isn't technically correct, but its close enough for people who won't be coding a compiler in the next couple years.
 

mcade

Junior Member
Oct 30, 2004
5
0
0
How do you make compilers? Well, assuming that you can program in some language. You would make a program that does what compilers do. That is, it reads a file (source code) and takes the instructions in that file and turns them into machine code for the particular platform you are working on.

That is the short answer. Before continuing though, let me say that you aren't going to be able to put together a compiler from anything you learn on these forums if you are starting from scratch experience wise as you seem to be. Writing a compiler is a non-trivial programming task that requires you know a great deal about the language and the machine you are writing it for. What follows is sort of an overview of what a compiler is and does.

Let's make up a mythical language made up of a single command and then discuss what the compiler does breifly.

Let's say that you want your language to have a command that prints some given text to the screen. You want to call this command 'print'. You would make your compiler so that when it reads your file (source code) and sees the command, it generates appropriate machine code for making things appear on the screen. Very basic, mundane things like 'copy this bit from this register to that register', 'look up the letter "B" on the ASCII chart and return the correct code', 'look up that bit in memory and tell me what it is', 'copy this bit to this address in memory', 'check to see if a key has been pressed on the keyboard', etc. There can be tons and tons of steps in adding 1+1 when broken down the the lowest level.

Ok, let's say your compiler is made so that when it sees

print "Hello, World";

It recognizes the 'print' as a keyword and knows that this means it has to take action. It looks at the line and sees that after 'print' there are quotations containing the words 'Hello, World'. You have made this compiler to know that what ever is between the quotes is to be taken and printed out to the screen. The semicolon at the end tells the compiler that this is the end of this particular command and it can be done with it. Once it has read the command, it puts out some code that is the machine language equivalent of 'print out the words "Hello, World" to the screen'.

That would more or less be the logical flow of how you would make your compiler work. It's pretty basic and doesn't contain even a tiny bit of real code. I think it gets the point across though.
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Originally posted by: chuckywang
Then how is the "compiler" for machine language coded?

By definition, 'machine language' is as 'complied' as anything can get -- 'machine language' is code that can be directly understood by the processor without any further manipulation. The instructions are already encoded in the proper form to be loaded into the CPU's registers and executed.

Back in Ye Olden Days, before cross-compilers were developed (and even earlier, when people were building the first computers, and before the days of high-level languages), you would have to write an assembler (presumably a pretty simple one!) for a new architecture in machine code for that architecture. Then you could write a compiler for that architecture in assembler language and use your assembler to build that. Then you can use your compiler to write a better compiler, etc., etc... . Nowadays, most high-level compilers can 'cross-compile' to any architecture they know about -- even ones that don't really exist yet! -- and then you can just load the binaries it produces onto the new machine and run them directly. In fact, some programming languages (like Java) take advantage of this by building to a 'virtual machine language' which is then interpreted in real time to run the program (but that's another issue entirely ).
 

cquark

Golden Member
Apr 4, 2004
1,741
0
0
If you actually want to write a compiler and know C, I highly recommend the Compact Guide to Lex and Yacc. Lex and Yacc are C-based compiler building tools. Yacc stands for Yet Another Compiler Compiler, suggesting the abundance of tools in this area, though lex and yacc (or their GNU counterparts flex and bison) are still the most popular.
 

jhu

Lifer
Oct 10, 1999
11,918
9
81
anyone know how the first compiler was written and in what language?
 

cquark

Golden Member
Apr 4, 2004
1,741
0
0
Originally posted by: jhu
anyone know how the first compiler was written and in what language?

FORTRAN was the first language a compiler was written for and since it was before we had the theory of syntax-directed translation using context-free grammars, it was written by hand without any modern compiler-building tools at IBM by a team lead by John Backus. It was written in assembly language and developed from 1954-1957. It ran on mainframes with around 16KB of RAM. Some of the odd quirks of FORTRAN syntax are a result of this early design.
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
Noone has written a compiler from the ground up in assembly for a LONG time. Compilers are written in other compilers. Often using previous iterations of themselves to compile the next version! For a completely new processor platform, the code emitter on a compiler for another processor is modified to be able to create binaries for the new CPU, and that is used to create a version of the compiler that runs on the new CPU.
 

cquark

Golden Member
Apr 4, 2004
1,741
0
0
Originally posted by: glugglug
Noone has written a compiler from the ground up in assembly for a LONG time. Compilers are written in other compilers. Often using previous iterations of themselves to compile the next version! For a completely new processor platform, the code emitter on a compiler for another processor is modified to be able to create binaries for the new CPU, and that is used to create a version of the compiler that runs on the new CPU.

Yes, this is one of the especially nice features of the GNU Compiler Collection. All front-ends (the languages, like C, C++, FORTRAN, Ada, Java, etc.) all generate the same intermediate code. All of the back-ends generate the code for their particular processor using that intermediate code as input. All you need to do to port every language supported by GCC to your new CPU is write a new back-end. Alternately, all you need for your new language to support dozens of processors is to write a new front end for GCC.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |