Writing a compiler - Compilers
This is a discussion on Writing a compiler - Compilers ; Hello, I am designing a programming language that will run natively.
That is to say, it will run like C, without a need of an interpreter
nor a standard library (well, there will be, but I want to be able ...
-
-
Re: Writing a compiler
andresj schrieb:
> * I need help in finding an appropiate way of structuring my XML. Any
> ideas/links?
Use the semantic structure: subroutines, statements, expressions, terms.
> The next part is where I need more help in: how to convert this tree
> into assembly language.
Depends on the target machine architecture and instruction set. The
hardware also dictates the interrupt table layout, memory management
and other privileged tasks and instructions. You may be better off
with giving such code immediately in assembly language, or in a
runtime library written in assembly language.
> * Is there any documentation on this part of writing a compiler?
"Register allocation" comes into mind...
> This compiler will not need to be fast (might be written in Python or
> Ruby or Java---although I don't like Java that much---, instead of C
> or C++, which is counterintuitive for me); I will write a new compiler
> in the language I am creating (kind of what was done for C.)
When you want to write operating systems, you need an linker which can
produce something loadable by the bootstrap loader (BIOS). The compiler
is less important, you can use almost any C compiler for your first
experiments.
DoDi
-
Re: Writing a compiler
On Tue, Oct 21, 2008 at 6:20 AM, andresj <andresjriofrio@gmail.com> wrote:
> * Any other articles/ideas on writing compilers or designing
> programming languages are welcome. 
>
For introduction to compilers read this book:
Keith Cooper & Linda Torczon
Engineering a Compiler
If you need more then read the Dragon Book:
Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman
Compilers: Principles, Techniques, and Tools (2nd Edition)
Regards,
Aleksey
-
Re: Writing a compiler
andresj <andresjriofrio@gmail.com> writes:
> The next part is where I need more help in: how to convert this tree
> into assembly language.
>
> * Is there any documentation on this part of writing a compiler?
You can download my book "Basics of Compiler Design" for free from
http://www.diku.dk/~torbenm/~Basics . It has chapters on type checking
and code generation, which should help you to get all the way from
syntax tree to symbolic aaembly language. I suggest you use existing
assemblers and linkers for generating binaries.
Mind you, the book describes principles and contains no code that you
can copy-paste into your own compiler. So, "some assembly required".
Torben
-
Re: Writing a compiler
andresj wrote:
> Hello, I am designing a programming language that will run natively.
> That is to say, it will run like C, without a need of an interpreter
> nor a standard library (well, there will be, but I want to be able to
> write an operating system, too.)
>
> The language parsing (converting a=b into <assignment><variable
> name="a"/><variable name="b"/></assignment>---which is not necessarily
> correct, anyways) I will skip for now; I will write directly in XML
> and use a XML parser to get a tree of my program.
This sounds strange. You say you are designing a language, but you
will skip syntax definitiona and language parsing for the moment ?
> * I need help in finding an appropiate way of structuring my XML. Any
> ideas/links?
There have been several attempts to use XML data as an intermediate
representation of source code. Use Google with "xml intermediate
language parser" and you will find sources like these:
http://www.idealliance.org/papers/xm.../03-05-04.html
http://portal.acm.org/citation.cfm?doid=1026487.1008001
http://www.sable.mcgill.ca/jil/docs/jil10.pdf
> The next part is where I need more help in: how to convert this tree
> into assembly language.
>
> * Is there any documentation on this part of writing a compiler?
My impression is that all attempts at using XML data as intermediate
representation were mostly academic. If you find anything useful on
the Internet that is more than a toy, please post it here. Writing a
code generator for such an intermediate representation should be an
interesting lesson in XML processing.
> * Any other articles/ideas on writing compilers or designing
> programming languages are welcome. 
Compiler writing and language design are well established disciplines
for some decades now.
http://en.wikipedia.org/wiki/Compiler#Compiler_design
http://en.wikipedia.org/wiki/Program...nguage#History
There are many excellent text books at Amazon.com.
-
Re: Writing a compiler
andresj escribis:
> Hello, I am designing a programming language that will run natively.
> That is to say, it will run like C, without a need of an interpreter
> nor a standard library (well, there will be, but I want to be able to
> write an operating system, too.)
>
> The language parsing (converting a=b into <assignment><variable
> name="a"/><variable name="b"/></assignment>---which is not necessarily
> correct, anyways) I will skip for now; I will write directly in XML
> and use a XML parser to get a tree of my program.
>
> * I need help in finding an appropiate way of structuring my XML. Any
> ideas/links?
There are several projects/tools that use XML to represent the AST.
Please look at:
http://lml.ls.fi.upm.es/~mcollado/em...ode-other.html
>
> The next part is where I need more help in: how to convert this tree
> into assembly language.
Just use XSLT to generate the assembler code.
>
> * Is there any documentation on this part of writing a compiler?
Look at the code generation chapters in any good compiler textbook.
--
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado
-
Re: Writing a compiler
> Just use XSLT to generate the assembler code.
If I can make a suggestion, use C or C++ as target language. Here
you don't have to reinvent subroutine calling and the like, and you
maintain compatibility with other things on the OS. Not to mention
ease of moving around different OSes. And troubleshooting. Much
easier.
[Quite a reasonable idea unless your plan was to learn about code
generation. -John]
-
Re: Writing a compiler
On Tue, Oct 21, 2008 at 7:20 AM, andresj <andresjriofrio@gmail.com> wrote:
> Hello, I am designing a programming language that will run natively.
> That is to say, it will run like C, without a need of an interpreter
> nor a standard library (well, there will be, but I want to be able to
> write an operating system, too.)
>
> The language parsing (converting a=b into <assignment><variable
> name="a"/><variable name="b"/></assignment>---which is not necessarily
> correct, anyways) I will skip for now; I will write directly in XML
> and use a XML parser to get a tree of my program.
>
> * I need help in finding an appropiate way of structuring my XML. Any
> ideas/links?
I think you are saying something like http://www.gccxml.org/ ??
> The next part is where I need more help in: how to convert this tree
> into assembly language.
>
> * Is there any documentation on this part of writing a compiler?
>
> This compiler will not need to be fast (might be written in Python or
> Ruby or Java---although I don't like Java that much---, instead of C
> or C++, which is counterintuitive for me); I will write a new compiler
> in the language I am creating (kind of what was done for C.)
>
> Thanks for your help. 
>
> * Any other articles/ideas on writing compilers or designing
> programming languages are welcome. 
-
Re: Writing a compiler
On Oct 26, 4:57 am, Nick <Ibeam2...@gmail.com> wrote:
> If I can make a suggestion, use C or C++ as target language. Here
> you don't have to reinvent subroutine calling and the like, and you
> maintain compatibility with other things on the OS. Not to mention
> ease of moving around different OSes. And troubleshooting. Much
> easier.
> [Quite a reasonable idea unless your plan was to learn about code
> generation. -John]
That is a reasonable idea, thanks. :-) The only problem is that there
would be a dependency on a complicated C or C++ compiler, which might
make the language more difficult to manage.
Andres
[If you're careful, you can generate extremely portable C code. Not so sure
about C++, though. -John]
-
Re: Writing a compiler
On Oct 21, 4:20 am, andresj <andresjriof...@gmail.com> wrote:
> Hello, I am designing a programming language that will run natively.
> That is to say, it will run like C, without a need of an interpreter
you must mean you want to write a bootstrap compiler for a particular
chip set.
> nor a standard library (well, there will be, but I want to be able to
> write an operating system, too.)
>
hmm. microsoft has something in the works called singularity (OS +
compiler + loader all combined into one).
> The language parsing (converting a=b into <assignment><variable
> name="a"/><variable name="b"/></assignment>---which is not necessarily
> correct, anyways) I will skip for now; I will write directly in XML
> and use a XML parser to get a tree of my program.
>
> * I need help in finding an appropiate way of structuring my XML. Any
> ideas/links?
>
> The next part is where I need more help in: how to convert this tree
> into assembly language.
There are some intermediate formats out in the public domain. So, you
need to write the front-end to generate 3-address code which is
compliant with what the IM accepts.
> * Is there any documentation on this part of writing a compiler?
you just need to bridge/handshake the front-end with the IM
> This compiler will not need to be fast (might be written in Python or
> Ruby or Java---although I don't like Java that much---, instead of C
> or C++, which is counterintuitive for me); I will write a new compiler
That is just your constraint/preference. From the compiler writers
standpoint, you need a method to get tokens for terminals, and a
parser generator that generates a parser to do SDT (syntax directed
translation). I am not aware of whether there are parser generators to
generate python code (but there are for java).
> in the language I am creating (kind of what was done for C.)
>
> Thanks for your help. 
>
> * Any other articles/ideas on writing compilers or designing
> programming languages are welcome. :-)
the design of a programming language depends on what it is going to be
used for. So, you need to look at who the end user is, what the
purpose of using that language is etc.. and then provide a grammar
that makes their job easier.
regards
-kamal