Re: Absolute beginner - Need some pointers

"Bartc" <bc@freeuk.com>
Fri, 29 Feb 2008 19:00:07 GMT

          From comp.compilers

Related articles
Absolute beginner - Need some pointers nacarlson@gmail.com (NickCarlson) (2008-02-27)
Re: Absolute beginner - Need some pointers DrDiettrich1@aol.com (Hans-Peter Diettrich) (2008-02-28)
Re: Absolute beginner - Need some pointers bc@freeuk.com (Bartc) (2008-02-29)
Re: Absolute beginner - Need some pointers anton@mips.complang.tuwien.ac.at (2008-03-02)
Re: Absolute beginner - Need some pointers anton@mips.complang.tuwien.ac.at (2008-03-03)
Re: Absolute beginner - Need some pointers anton@mips.complang.tuwien.ac.at (2008-03-04)
Re: Absolute beginner - Need some pointers gah@ugcs.caltech.edu (glen herrmannsfeldt) (2008-03-05)
Re: Absolute beginner - Need some pointers sandmann@daimi.au.dk (Soeren Sandmann) (2008-03-07)
| List of all articles for this month |

From: "Bartc" <bc@freeuk.com>
Newsgroups: comp.compilers
Date: Fri, 29 Feb 2008 19:00:07 GMT
Organization: Compilers Central
References: 08-02-091
Keywords: practice
Posted-Date: 01 Mar 2008 12:14:00 EST

"NickCarlson" <nacarlson@gmail.com> wrote in message


> (we'll call my new language Omega for now)
> i. Write a lexical analyzer to convert to Omega code into a tree
> structure that the parser can parse.
> 2. Write a parser to parse the tree structure into bytecode.
> C. Write a virtual machine that can execute the bytecode.


The parser retrieves tokens from the lexer, and analyses the symbols to
build the syntax tree.


The code generator turns the tree into possible bytecode.


In-between you may need to do further analysis (datatyping and so on).


You can actually execute this tree directly as has been mentioned (you will
of course need to set up the execution environment: variables, registers,
probably a stack, but you know all this).


But it is a little more satisfying to write the bytecode to a file. Then you
can distribute this without the compiler or source code.


Take this code snippet:


fred:
if x then
  a:=b+c
  goto fred
end


When written to bytecode you might end up with something like the following
(this uses a stack):


00019 %1: ;fred
00021 Pushm x
00023 Jumpf %2
00025 Pushm b
00027 Pushm c
00029 Add
00030 Popm a
00032 Jump %1
00034 %2:


In other words, a linear sequence of opcodes reassuringly similar to
assembly language. The numbers indicate byte-offsets, and you can manipulate
your program counter very easily. But there are many ways of handling
bytecode.


--
Bart


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.