Skip to content

Commit cf84019

Browse files
author
KEYU PU
committed
class : merge the part of attribute of the class
2 parents f2bcfc3 + 5a82b80 commit cf84019

File tree

5 files changed

+189
-136
lines changed

5 files changed

+189
-136
lines changed

Evaluator/Class.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class point{
2+
i;
3+
}

Main/Compile.ml

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
open LexicalAnalyser
2-
open SyntaxAnalyser
3-
open Expression
4-
5-
let print_expressions_or_classes exp =
6-
(* Print the AST *)
7-
print_string (string_of_expr exp)
8-
9-
(* verbose is a boolean that you can use to switch to a verbose output (for example, to dump all the ast) *)
10-
let execute lexbuf verbose =
11-
12-
try
13-
(* to enable LexicalAnalyser, plase un comment this block *)
14-
(* print_endline ("Doing LexicalAnalyser....");
15-
LexicalAnalyser.examine_all lexbuf;
16-
print_string "\n"; *)
17-
print_endline ("Doing SyntaxAnalyser....");
18-
let exp_list = SyntaxAnalyser.expression nexttoken lexbuf in
19-
print_endline("Start printing AST...");
20-
List.iter print_expressions_or_classes exp_list;
21-
print_newline();
22-
with
23-
| LexicalAnalyser.Error (kind, s, e) ->
24-
print_string("LexicalAnalyser error: ");
25-
report_error kind;
26-
let pos = Location.curr lexbuf in
27-
Location.print pos;
28-
print_newline();
29-
| Error ->
30-
print_string("SyntaxAnalyser error: ");
31-
let pos = Location.curr lexbuf in
32-
Location.print pos;
33-
| _ -> print_string("Unknown error")
1+
open LexicalAnalyser
2+
open SyntaxAnalyser
3+
open Expression
4+
5+
let print_expressions_or_classes exp =
6+
(* Print the AST *)
7+
print_string (string_of_class_or_expr exp)
8+
9+
(* verbose is a boolean that you can use to switch to a verbose output (for example, to dump all the ast) *)
10+
let execute lexbuf verbose =
11+
12+
try
13+
(* to enable LexicalAnalyser, plase un comment this block *)
14+
(* print_endline ("Doing LexicalAnalyser....");
15+
LexicalAnalyser.examine_all lexbuf;
16+
print_string "\n"; *)
17+
print_endline ("Doing SyntaxAnalyser....");
18+
let exp_list = SyntaxAnalyser.content nexttoken lexbuf in
19+
print_endline("Start printing AST...");
20+
List.iter print_expressions_or_classes exp_list;
21+
print_newline();
22+
with
23+
| LexicalAnalyser.Error (kind, s, e) ->
24+
print_string("LexicalAnalyser error: ");
25+
report_error kind;
26+
let pos = Location.curr lexbuf in
27+
Location.print pos;
28+
print_newline();
29+
| Error ->
30+
print_string("SyntaxAnalyser error: ");
31+
let pos = Location.curr lexbuf in
32+
Location.print pos;
33+
| _ -> print_string("Unknown error")

Parsing/Expression.ml

Lines changed: 116 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,116 @@
1-
type binop =
2-
| Badd | Bsub | Bmul | Bdiv | Bmod
3-
| Bgt | Blt | Bge | Ble | Bequal | Bnotequal
4-
| Band | Bor
5-
6-
type unop =
7-
| Uplus | Uminus | Unot | Uincre | Udecre
8-
9-
type postfix =
10-
| Pincre | Pdecre
11-
12-
type expression =
13-
| Int of int
14-
| Var of string
15-
| Bool of bool
16-
| Null
17-
| String of string
18-
| Assign of string * expression
19-
| Binop of binop * expression * expression
20-
| Unop of unop * expression
21-
| Postop of expression * postfix
22-
| Semi of expression
23-
| IfThen of expression * expression
24-
| IfThenElse of expression * expression * expression
25-
26-
27-
exception Unbound_variable of string
28-
29-
let get_op_u = function
30-
| Uplus -> fun x -> x
31-
| Uminus -> fun x -> -x
32-
(* | Unot -> fun x -> not x *)
33-
| Uincre -> fun x -> x + 1
34-
| Udecre -> fun x -> x - 1
35-
36-
let get_op_b op x y =
37-
match op with
38-
| Badd -> x + y
39-
| Bsub -> x - y
40-
| Bmul -> x * y
41-
| Bdiv -> x / y
42-
| Bmod -> x mod y
43-
44-
let string_of_op_u = function
45-
| Uplus -> "+"
46-
| Uminus -> "-"
47-
| Unot -> "not"
48-
| Uincre -> "++"
49-
| Udecre -> "--"
50-
51-
let string_of_op_p = function
52-
| Pincre -> "++"
53-
| Pdecre -> "--"
54-
55-
let string_of_op_b = function
56-
| Badd -> "+"
57-
| Bsub -> "-"
58-
| Bmul -> "*"
59-
| Bdiv -> "/"
60-
| Bmod -> "%"
61-
| Bgt -> " > "
62-
| Blt -> " < "
63-
| Bge -> " >= "
64-
| Ble -> " <= "
65-
| Bequal -> " = "
66-
| Bnotequal -> " != "
67-
| Band -> " && "
68-
| Bor -> " || "
69-
70-
let rec string_of_expr exp =
71-
match exp with
72-
| Int c -> "Int("^string_of_int c^")"
73-
| Var v -> "Var("^v^")"
74-
| Bool b -> string_of_bool b
75-
| Null -> "null"
76-
| String s -> "String("^s^")"
77-
| Binop(op, e1, e2) -> "(" ^(string_of_expr e1)^ (string_of_op_b op) ^(string_of_expr e2)^ ")"
78-
| Unop(op, e) -> "(" ^ (string_of_op_u op) ^(string_of_expr e)^ ")"
79-
| Postop(e, op) -> "(" ^ (string_of_expr e) ^(string_of_op_p op)^ ")"
80-
| Assign(s,e) -> "Assign(" ^s^ "=" ^(string_of_expr e)^ ")"
81-
| Semi(e1) -> (string_of_expr e1)^ ";\n"
82-
| IfThen(e1,e2) -> "If("^(string_of_expr e1)^") {\n"^(string_of_expr e2 )^" })\n"
83-
| IfThenElse(e1,e2,e3) -> "If("^(string_of_expr e1)^") {\n"^(string_of_expr e2 )^" } Else {\n"^(string_of_expr e3)^" })\n"
84-
1+
type binop =
2+
| Badd | Bsub | Bmul | Bdiv | Bmod
3+
| Bgt | Blt | Bge | Ble | Bequal | Bnotequal
4+
| Band | Bor
5+
6+
type unop =
7+
| Uplus | Uminus | Unot | Uincre | Udecre
8+
9+
type postfix =
10+
| Pincre | Pdecre
11+
12+
type expression =
13+
| Int of int
14+
| Var of string
15+
| Bool of bool
16+
| Null
17+
| String of string
18+
| Assign of string * expression
19+
| Binop of binop * expression * expression
20+
| Unop of unop * expression
21+
| Postop of expression * postfix
22+
| Semi of expression
23+
| IfThen of expression * expression
24+
| IfThenElse of expression * expression * expression
25+
26+
type attr =
27+
| Attr of string
28+
| AttrWithAssign of string
29+
30+
type attr_or_method =
31+
| Attribute of attr
32+
33+
type class_ =
34+
| Class_ of string* attr_or_method list
35+
36+
type class_or_expr =
37+
| Class of class_
38+
| Expr of expression
39+
40+
exception Unbound_variable of string
41+
42+
let get_op_u = function
43+
| Uplus -> fun x -> x
44+
| Uminus -> fun x -> -x
45+
(* | Unot -> fun x -> not x *)
46+
| Uincre -> fun x -> x + 1
47+
| Udecre -> fun x -> x - 1
48+
49+
let get_op_b op x y =
50+
match op with
51+
| Badd -> x + y
52+
| Bsub -> x - y
53+
| Bmul -> x * y
54+
| Bdiv -> x / y
55+
| Bmod -> x mod y
56+
57+
let string_of_op_u = function
58+
| Uplus -> "+"
59+
| Uminus -> "-"
60+
| Unot -> "not"
61+
| Uincre -> "++"
62+
| Udecre -> "--"
63+
64+
let string_of_op_p = function
65+
| Pincre -> "++"
66+
| Pdecre -> "--"
67+
68+
let string_of_op_b = function
69+
| Badd -> "+"
70+
| Bsub -> "-"
71+
| Bmul -> "*"
72+
| Bdiv -> "/"
73+
| Bmod -> "%"
74+
| Bgt -> " > "
75+
| Blt -> " < "
76+
| Bge -> " >= "
77+
| Ble -> " <= "
78+
| Bequal -> " = "
79+
| Bnotequal -> " != "
80+
| Band -> " && "
81+
| Bor -> " || "
82+
83+
let rec string_of_expr exp =
84+
match exp with
85+
| Int c -> "Int("^string_of_int c^")"
86+
| Var v -> "Var("^v^")"
87+
| Bool b -> string_of_bool b
88+
| Null -> "null"
89+
| String s -> "String("^s^")"
90+
| Binop(op, e1, e2) -> "(" ^(string_of_expr e1)^ (string_of_op_b op) ^(string_of_expr e2)^ ")"
91+
| Unop(op, e) -> "(" ^ (string_of_op_u op) ^(string_of_expr e)^ ")"
92+
| Postop(e, op) -> "(" ^ (string_of_expr e) ^(string_of_op_p op)^ ")"
93+
| Assign(s,e) -> "Assign(" ^s^ "=" ^(string_of_expr e)^ ")"
94+
| Semi(e1) -> (string_of_expr e1)^ ";\n"
95+
| IfThen(e1,e2) -> "If("^(string_of_expr e1)^") {\n"^(string_of_expr e2 )^" })\n"
96+
| IfThenElse(e1,e2,e3) -> "If("^(string_of_expr e1)^") {\n"^(string_of_expr e2 )^" } Else {\n"^(string_of_expr e3)^" })\n"
97+
98+
99+
100+
let string_of_attr = function
101+
| Attr a -> "Attr" ^a
102+
| AttrWithAssign a -> "AttrWithAssign" ^a
103+
104+
let string_of_attr_or_method = function
105+
| Attribute a -> string_of_attr a
106+
107+
let rec string_of_attrs_or_methods = function
108+
| [] -> ""
109+
| a::l -> (string_of_attr_or_method a) ^ " " ^ (string_of_attrs_or_methods l)
110+
111+
let string_of_class = function
112+
| Class_(id,am) -> "Class(" ^id^ "{" ^ (string_of_attrs_or_methods am) ^ " })"
113+
114+
let string_of_class_or_expr = function
115+
| Class c -> string_of_class c
116+
| Expr e -> string_of_expr e

Parsing/LexicalAnalyser.mll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ let not_star_not_slash = [^ '*' '/']
8585
let traditional_comment = "/*" not_star* "*"+ (not_star_not_slash not_star* "*"+)* "/"
8686

8787
rule nexttoken = parse
88+
(*Class*)
89+
| "class" { CLASS }
90+
| "new" { NEW }
91+
| "this" { THIS }
92+
8893
| newline { Location.incr_line lexbuf; nexttoken lexbuf }
8994
| space+ { nexttoken lexbuf }
9095
| endofline_comment as c { ENDOFLINECOMMENT c}
@@ -122,10 +127,7 @@ rule nexttoken = parse
122127
| upper_id as str { UPPERIDENT str }
123128
| _ as c { raise_error (Illegal_character(c)) lexbuf }
124129

125-
(*Class*)
126-
| "class" { CLASS }
127-
| "new" { NEW }
128-
| "this" { THIS }
130+
129131

130132
{
131133
let rec examine_all lexbuf =

Parsing/SyntaxAnalyser.mly

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@
3333
/* Keywords */
3434
%token IF ELSE
3535

36+
<<<<<<< HEAD
3637
/* Declarations of variables */
3738
%token <string> TYPE
3839

3940
/* Start symbols and types */
4041
%start expression
4142
%type < Expression.expression list> expression
43+
=======
44+
>>>>>>> 5a82b8075c979133d6f3b2baf678e96925c2cdd7
4245

4346
/* Priority and associativity */
4447
%right SEMICOLON
@@ -53,39 +56,46 @@
5356
%left PINCRE PDECRE
5457

5558
/* End of Declarations */
59+
60+
61+
/* Start symbols and types */
62+
%start content
63+
%type < Expression.class_or_expr list> content
64+
5665
%%
5766

5867
/* Start of Rules */
59-
expression:
60-
68+
content:
6169
| comment* EOF {[]}
62-
| e=comment_or_expression r=expression EOF
63-
{ e::r }
64-
| c=comment_or_class r=expression EOF
70+
| c=class_or_expression r=content EOF
6571
{ c::r }
6672

73+
class_or_expression:
74+
| e=comment_or_expression {e}
75+
| c=comment_or_class {c}
76+
6777
comment:
6878
| ENDOFLINECOMMENT {}
6979
| TRADITIONALCOMMENT {}
7080

7181
comment_or_expression:
72-
| comment* e=expr { e }
82+
| comment* e=expr { Expr(e) }
7383

7484
comment_or_class:
75-
| comment* c=class { c }
85+
| comment* c=class_ { Class(c) }
7686

77-
class:
78-
| CLASS LBRACE a=attributes_or_methods RBRACE
87+
class_:
88+
| CLASS id=LOWERIDENT LBRACE a=attributes_or_methods RBRACE
89+
{ Class_(id,a) }
7990

80-
attributes_or_methods:
81-
| comment* EOF {[]}
82-
| a=attribute_or_method r=attributes_or_methods
83-
{a::r}
91+
attributes_or_methods:
92+
| comment* { [] }
93+
| a=attribute_or_method r=attributes_or_methods { a::r }
8494

8595
attribute_or_method:
86-
| comment* a=attribute SEMICOLON
87-
| comment* m=method
96+
| comment* a=attribute SEMICOLON { Attribute(a) }
8897

98+
<<<<<<< HEAD
8999
method:
90100
| STATIC t=TYPE id=LOWERIDENT LPAR p=params RPAR LBRACE e=expr RBRACE
91101
{ Method(true, t, id, p, e) }
@@ -107,6 +117,12 @@ params:
107117
| t=TYPE id=LOWERIDENT COMMA r=param+
108118
{ Param(t,id) :: r}
109119

120+
=======
121+
attribute:
122+
| id=LOWERIDENT {Attr(id)}
123+
| id=LOWERIDENT ASSIGN expr {AttrWithAssign(id)}
124+
125+
>>>>>>> 5a82b8075c979133d6f3b2baf678e96925c2cdd7
110126
expr:
111127
| e1=expr SEMICOLON
112128
{ Semi(e1)}

0 commit comments

Comments
 (0)