| | |
own programming language?
Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
![]() |
You mean, like how do you write a compiler?
Well, there are many good books and such on the topic; Google can be very helpful there. But generally the hardest part is to define the language in a rigourous way.
If you can write a BNF for your language, you can then translate the BNF into code and *presto* you have a start on a compiler.
Look up 'BNF' in Google and you are on your way!
Well, there are many good books and such on the topic; Google can be very helpful there. But generally the hardest part is to define the language in a rigourous way.
If you can write a BNF for your language, you can then translate the BNF into code and *presto* you have a start on a compiler.
Look up 'BNF' in Google and you are on your way!
So you searched and found Backus Naur Form. And maybe you saw some examples like (this is from Oberon):
number = integer | real.
integer = digit {digit} | digit {hexDigit} "H" .
real = digit {digit} "." {digit} [ScaleFactor].
ScaleFactor = ("E" | "D") ["+" | "-"] digit {digit}.
hexDigit = digit | "A" | "B" | "C" | "D" | "E" | "F".
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".
So now you can define your language!
Then you can parse it following these rules.
number = integer | real.
integer = digit {digit} | digit {hexDigit} "H" .
real = digit {digit} "." {digit} [ScaleFactor].
ScaleFactor = ("E" | "D") ["+" | "-"] digit {digit}.
hexDigit = digit | "A" | "B" | "C" | "D" | "E" | "F".
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".
So now you can define your language!
Then you can parse it following these rules.
•
•
Join Date: Nov 2004
Posts: 36
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Chainsaw
So you searched and found Backus Naur Form. And maybe you saw some examples like (this is from Oberon):
number = integer | real.
integer = digit {digit} | digit {hexDigit} "H" .
real = digit {digit} "." {digit} [ScaleFactor].
ScaleFactor = ("E" | "D") ["+" | "-"] digit {digit}.
hexDigit = digit | "A" | "B" | "C" | "D" | "E" | "F".
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".
So now you can define your language!
Then you can parse it following these rules.
Care to explain what all that was about ?
Frankly, I am interested to write my own C like compiler too.
Example, in C, you use PRINTF to spit an input on the screen but my language's command would be simply PRINT.
So, how can I teach this to the C compiler so if anyone now types in a source code PRINT it actually means PRINTF ?
I mean, underneath since C is programmed to understand the PRINTF only then I must teach it to understand the PRINT command too.
SO, how do I do it ?
What's the first thing I do ?
I will not only change 1 (PRINTF to PRINT) only but I will change others too.
For example PRINT in my language is CHAP so if anyone writes in the source-code CHAP then my compiler must do the same function as PRINTF.
Understand ?
Frankly, I really do not want to write a whole compiler but teach the C compiler to understand other words (PRINT, CHAP) too.
Get it ?
This way, people of my country who do not know english can learn the C in our own language.
So, to do all this, which part of the C compiler's source codes should I change/update ?
Or alternatively, shall I give you my function words (PRINT, CHAP, etc.) and you can change/add them on the C compiler ?
Afterwards, I can check the source code to see which parts you have updated. This way, I can learn which parts need updating should I wish to create another version of C in another language this time such as Arabic and then Hindi and then Urdu etc.
Cheers.
I would like to come-up with my own "Compression Algorithm" and teach that to the browsers so you can now show streaming videos and lengthy animations from your website without losing an arm and a leg on your band-width.
There are various tools that can turn a BNF description of your language into state tables or code; you might check out YACC, but there are others (see Google). Another approach is to build your own parser; that generally gives you more control over error messages and the like. Plus, its a bunch of fun to see your parser come to life!
"Recursive Descent" parsers are easy to build and match the BNF pretty well. Here's a funky and simplified example, based on the first line of that bnf (number = integer | real):
So that gives you something to start with. Each BNF line roughly cooresponds to a parser method, and if you can, phrase the parser method in the same manner as the BNF so it is easy to read and verify that you are doing the right thing.
Then you can see that you need some helper routines to manage the source text, especially tracking what row and column you are on for error reporting and debugging.
Once you have a parse tree, you know that your code is syntactically correct. Then you need to resolve named references (variables), maybe do optimizations, and generate code or pseudocode or just exececute (depending on if this is a compiler, interpreter, or something in-between).
"Recursive Descent" parsers are easy to build and match the BNF pretty well. Here's a funky and simplified example, based on the first line of that bnf (number = integer | real):
// Lets say we have a class called 'AParser' that contains all the methods to parse
// your BNF. The end result of parsing is one or more errors, OR a 'parse tree', which
// is the internal representation of the language in tree form. So, the parser's
// job is to build the tree and detect errors.
// Each production from the BNF can be represented as a single method.
// The result of that method is an error or a node filled in for the caller to
// do whatever it needs to (generally adding it to HER node and passing that
// up the call chain too).
bool AParser::Number( AParseTreeNode& node )
{
// The result of this routine will be to fill in 'node' or
// give an error. For working, we need to pass another
// node to routines we call....
AParseTreeNode childNode;
// Set up the passed-in node with info about who I am for later reference
// and/or error messages:
node.SetType( kNumber );
// And you may want to know later what source line and col this node came
// from (for debugging output, say, or setting breakpoints, or whatnot)
SetLineNumberInfoIntoNode( node );
// Now, from the BNF, we know that a number is either an int or a real:
if (Integer( childNode ) || Real( childNode ))
{
// Here we glue the child node onto our node, or maybe sometimes you might
// morph the child node INTO our node, or whatever, depending on your needs.
node.AddChild( childNode ); // makes a copy of childNode
}
else
{
// This might display the source line and column, too
Error("Expected a NUMBER here.");
return false;
}
return true;
}So that gives you something to start with. Each BNF line roughly cooresponds to a parser method, and if you can, phrase the parser method in the same manner as the BNF so it is easy to read and verify that you are doing the right thing.
Then you can see that you need some helper routines to manage the source text, especially tracking what row and column you are on for error reporting and debugging.
Once you have a parse tree, you know that your code is syntactically correct. Then you need to resolve named references (variables), maybe do optimizations, and generate code or pseudocode or just exececute (depending on if this is a compiler, interpreter, or something in-between).
For Onauc:
There are a few different ways to simply change the language of the keywords. One method is to take a running C compiler and tinker with the parser; where the bnf says 'WHILE' you could change that to something else.
Things like 'printf' are library routines, so you can add your own library routines instead of the standard ones, without changing the C compiler at all. In fact, if you just want to RENAME the standard routines to, say, German, you could do something like this:
#define DerPrinten printf
(I don't know German, of course, but 'DerPrinten' sounds fun)
Then in your source code you could include this definition from a file you've written and wherever someone coded DerPrinten the compiler would change that to 'printf'.
Another, totally different approach, would be to write a PRE-compiler that just looked for things like 'DerPrinten' and changed them to printf, and then the output of this pre-compiler would be fed to a standard C compiler.
Just some ideas!
There are a few different ways to simply change the language of the keywords. One method is to take a running C compiler and tinker with the parser; where the bnf says 'WHILE' you could change that to something else.
Things like 'printf' are library routines, so you can add your own library routines instead of the standard ones, without changing the C compiler at all. In fact, if you just want to RENAME the standard routines to, say, German, you could do something like this:
#define DerPrinten printf
(I don't know German, of course, but 'DerPrinten' sounds fun)
Then in your source code you could include this definition from a file you've written and wherever someone coded DerPrinten the compiler would change that to 'printf'.
Another, totally different approach, would be to write a PRE-compiler that just looked for things like 'DerPrinten' and changed them to printf, and then the output of this pre-compiler would be fed to a standard C compiler.
Just some ideas!
•
•
Join Date: Nov 2004
Posts: 36
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Chainsaw
For Onauc:
There are a few different ways to simply change the language of the keywords. One method is to take a running C compiler and tinker with the parser; where the bnf says 'WHILE' you could change that to something else.
Things like 'printf' are library routines, so you can add your own library routines instead of the standard ones, without changing the C compiler at all. In fact, if you just want to RENAME the standard routines to, say, German, you could do something like this:
#define DerPrinten printf
(I don't know German, of course, but 'DerPrinten' sounds fun)
Then in your source code you could include this definition from a file you've written and wherever someone coded DerPrinten the compiler would change that to 'printf'.
Another, totally different approach, would be to write a PRE-compiler that just looked for things like 'DerPrinten' and changed them to printf, and then the output of this pre-compiler would be fed to a standard C compiler.
Just some ideas!
I was aware of the pre-compiler technique but the pre-compiler technique does not sound too professional, tasty or satisfying so it is better to change the english C functions to another language and create a vew version of C.
I would like to come-up with my own "Compression Algorithm" and teach that to the browsers so you can now show streaming videos and lengthy animations from your website without losing an arm and a leg on your band-width.
![]() |
Similar Threads
- Making Your Own Programming Language (Java)
- how would you make your own programming language (Computer Science)
- programming language recommendation (Computer Science)
- java programming language (Java)
- Programming Language required (Computer Science)
- Suggestions on what type of programming language would be best for the situation (Computer Science)
Other Threads in the Computer Science Forum
- Previous Thread: plz explain as soon as possible thanks
- Next Thread: list operation in DrScheme
| Thread Tools | Search this Thread |
ai algorithm algorithms amazon assignment assignmenthelp assignments automata battery bigbrother binary bittorrent bletchleypark blogging bomb business cern codebreaker compiler computer computers computerscience connect conversion csc dataintepretation development dfa dissertation dissertations dissertationthesis dissertationtopic ebook employment energy floatingpoint foreclosure foreclosuresoftware fuel gadgets geeks givemetehcodez government graphics hardware history homeowners homeworkassignment homeworkhelp humor ibm idea ideas internet iphone ipod itcontracts jobs kindle laser laws linkbait lsmeans mainframes marketing mobileapplication msaccess nano netbeans networking news os p2p piracy piratebay principles programming rasterizer research sam-being-cute sas science security sex simulation software spying stephenfry study supercomputer supercomputing sweden technology textfield turing turingtest two'scompliment uk virus ww2






)