Download Language development case study (Pdf, 133 pages)

Transcript
94
MINITREE
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
{ A MULTIPLE PASS COMPILER
e0 ::= e1^^ [ '>'^^^ e1
'<'^^^ e1
'>='^^^ e1
'<='^^^ e1
'=='^^^ e1
'!='^^^ e1
] .
|
|
|
|
|
(*
(*
(*
(*
(*
(*
e1 ::= e2^^ { '+'^^^ e2
'-'^^^ e2
} .
|
(* Add *)
(* Subtract *)
e2 ::= e3^^ { '*'^^^ e3
'/'^^^ e3
} .
|
(* Multiply *)
(* Divide *)
e3 ::= e4^^ |
'+'^ e3 |
'-'^^ e3 .
Greater than *)
Less than *)
Greater than or equal *)
Less than or equal *)
Equal *)
Not equal *)
(* Posite: note suppression from intermediate form! *)
(* Negate *)
e4 ::= e5 [ '**'^^ e4 ]:^^.
e5 ::= ID:name^^ check_declared | (* Variable access *)
INTEGER^^ |
(* Numeric literal *)
'('^ e1^^ ')'^.
(* Parenthesised expression *)
comment ::= COMMENT_NEST('(*' '*)').
String^ ::= STRING_ESC('"' '\\').
(* Comments: stripped by lexer *)
(* Strings for print *)
(* End of minitree.bnf *)
Figure 8.8 An rdp BNF specication for minitree part 2: expressions
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
/*******************************************************************************
*
* RDP release 1.50 by Adrian Johnstone ([email protected]) 20 December 1997
*
* mt_aux.c - Minitree multiple pass compiler semantic routines
*
* This file may be freely distributed. Please mail improvements to the author.
*
*******************************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "graph.h"
#include "memalloc.h"
#include "textio.h"
#include "minitree.h"
#include "ml_aux.h"
#include "mt_aux.h"
Figure 8.9 minitree auxiliary functions part 1: declarations