generate abstract machine code from simple programming instructions

241 views Asked by At

My question is what I will need learn to complete that task ...the task is to create automatic code generator from simple programming instructions like

(x:=5-z; while x<z do (x:=x*2; z:=z+x) ) 

and translate it to ABSTRACT MACHINE INSTUCTIONS ..

PUSH-5:FETCH-z:SUB:STORE-x: LOOP ...etc....

so my question is...Where to start?? , I will need some parser and the parser have to work with some XML ..but I really donĀ“t know how to define XML ..

please help where to start ..thanks ..

I will code it in JAVA swing UI

2

There are 2 answers

3
Christoph Bimminger On BEST ANSWER

it's not so hard to implement your own parser if you know the syntax you support. Parse the string char by char, detect operators, literal terminations or variable name endings by next operator or blank, and get the previously stored characters as token. Identify the kind of token, and build your object oriented model in Java. As soon as you have an object oriented model of your expression, you can use e.g. the Visitor pattern to PRETTY-PRINT, EVALUATE or even TRANSLATE TO MACHINE CODE, depending on your visitor implementations.

(Out of perspective: from your question, it's not clear how this parser has to deal with XML...)

Years ago I implemented such a parser that evaluates such expressions (but not yet converts to machine code). My old project could give you an idea how I solved this task.

1
Michael Kay On

You need to read a couple of books on compiler writing. This is much too big a question for StackOverflow - for a start, it's impossible to answer without knowing how much computer science knowledge you have (for example, do you understand stacks and finite state automata?)

The classic book in this field is Aho and Ullmann, which is now ancient but to be honest the basic ideas haven't changed all that much, and I still use it.

The basic steps are:

  1. Specify the syntax and semantics of the programming language you want to implement. (Some of the subsequent detail will depend on what kind of language it is, e.g. whether it is procedural or declarative, and what kind of type system it has; but the overall architecture of the compiler will still be the same).

  2. Implement a parser (and tokenizer) for this language which constructs a representation of the source code as an expression tree.

  3. Write lots of test cases. (Some would say you should do this earlier, but my own preference is to write one test case, then write enough code to pass that test, then write lots more tests.)

  4. Implement the semantic phase which is responsible for tasks such as binding variable and function names and type checking.

  5. At this stage I would suggest writing an interpreter for the language, since this is rather easier than writing a code generator and it will help you to validate that the previous phases are working properly.

  6. Before writing a code generator you need to (a) have a specification for the (Virtual) machine code that you are targetting, and (b) have a design for the run-time memory management: how are you going to allocate stack space and heap space, and do garbage collection?

  7. Write your code generator. Keep it as simple as possible initially, get it working for all your test cases, then think about optimizations.