Example: Getting started

This is an example of how to use the Depot4 system. For a more detailed description see the user manual.

Step 1: Implement the language

We will implement a tiny language called nanoPascal by translating it into C.
This is the complete description of the translator:

	MODULE nanoPascal2C
	  nanoPascal2C= program -> program_.
	  program = block '.' -> '#include <stdio.h>\n' block_ .
          block = ['VAR' ident {',' ident[i]} ';']
              'BEGIN' statement {/..c/';' statement[i] } 'END'
          ->  ['int ' ident_ {',' ident_[i]} ';\n']
              'main() \n{\n'
              statement_ {/..c/ '\n' statement_[i] }
              '}\n'.
          statement = 'WRITE' expression |
              'READ' ident|
              'BEGIN' statement[1] {';' statement[i+1] } 'END' |
              'IF' expression 'THEN' st:statement ['ELSE' ste:statement] |
              'WHILE' expression 'DO' st:statement |
              ident ':=' expression |
          -> 'printf("%d",' expression_ ');' |
             'scanf("%d", &' ident_ ');' |
             '{'  {/..N+1/ statement_[i] '\n' } '}' |
             'if (' expression_ ') ' st_  ['else ' ste_ ]|
             'while (' expression_ ') ' st_ |
             ident_ '=' expression_ ';'| .
          expression = simpleExpr
             [('='|'<='|'>='|'<>'|'<'|'>') sE: simpleExpr ]
          -> simpleExpr_
             [('=='|'<='|'>='|'!='|'<'|'>') sE_ ].
          simpleExpr =
             VAR op: FLEX OF INT;
             ['+'|'-'] term
             {(/op[i]/'+'|'-'|'OR') term[i] }
          -> ['+'|'-'] term_
             {(/op[i]/'+'|'-'|'||') term_[i] }.
          term =
             VAR op: FLEX OF INT;
             factor {(/op[i]/'*'|'DIV'|'AND') f2:factor[i]}
          -> factor_ {(/op[i]/ '*'|'/'|'&&') f2_[i]}.
          factor = num | '(' expression ')'| 'NOT' factor | ident
          ->  num_ |'(' expression_ ')'|'!' factor_ | ident_ .
	END nanoPascal2C.
  1. Select the Translate card in the IDE and copy the above Ml4 code into its text area.
  2. Open the Translate menu and select All or type Ctrl+T.
    Hint: Be always sure that the focus (indicated by a dark blue border) is on the right text area. This border coloring shows the target of the respective action.

    In the log area (bottom area), you should see something similar to:
    
    There is no configuration file: Depot4.cfg
    Depot4: Ml4/Java - Translator 1.9.4 / (c) jl 04.05.2010
    Module nanoPascal2C
      translating nanoPascal2C
      translating program
      translating block
      translating statement
      translating expression
      translating simpleExpr
      translating term
      translating factor
    javac -d . -classpath .;C:\j2sdk1.4.1_01\jre\lib\...\j2sdk1.4.1_01\lib\tools.jar;C:\work tmp\src\nanoPascal2C.java
    done
    2.422 sec.
    
That's it. Your translator is ready for use now.

Remark: To simplify it, all rules have been packed into one module. In a next step, one can build the translator rule by rule. For this just copy only a single rule into the Translate text area or mark it there selected and then execute the Selected action from the Translate menu.

top


Step 2: Use the language

Switch to the Use card and type nanoPascal2C into the root labeled textbox. You are now ready for using the newly created translator.
Copy the following example code of a nanoPascal program into the left text area and execute the All action of the Translate menu.
Hint: Be sure that the keyboard focus has left the root textbox at least once after setting the root name. Otherwise you may be dazzled with error messages saying that <root> could not be loaded.

	VAR X, Y, H, GGT;
	BEGIN
	  READ X;
	  READ Y;
	  WHILE X<>0 DO
	  BEGIN
	    IF X<Y THEN 
	    BEGIN
	      H:= X; X:= Y; Y:= H
  	    END;
	    X:= X-Y;
	    WHILE X<Y DO
	      X:= X-Y
	  END;
	  GGT:= Y;
	  WRITE GGT
	END.
You should then see some C code in the right text area.

to Ml4 syntax  to IDE Help   top


© J. Lampe 1997-2010                  (05.05.2010)