Commit 9e304cc5 authored by Nicaudie Charlotte's avatar Nicaudie Charlotte

Worked on lexer/parser/ast

parent ee1677a2
(*
Float range for XY coordinates
X between float1 and float2
Y between float3 and float4
*)
type init = float * float * float * float
type stmt =
| Translation of float * float
| Rotation of float * float * float
| StmtList of stmt list
| Or of stmt * stmt * float (* Float is the left-member's random weightening *)
| Iter of stmt * int * int (* Min and max number of iteration *)
type program = init * stmt
......@@ -14,7 +14,7 @@
"translation", TRANSLATION;
"rotation", ROTATION;
"or", OR;
"if", IF;
(* "if", IF;
"else", ELSE;
"def", DEF;
"repeat", REPEAT;
......@@ -29,7 +29,7 @@
"red", RED;
"green", GREEN;
"blue", BLUE;
]
*) ]
let id_or_kwd =
let h = Hashtbl.create 17 in
......@@ -56,10 +56,10 @@ rule get_token = parse
| '\n' { newline lexbuf; token lexbuf }
| space+ { token lexbuf }
| ident as id { id_or_kwd id }
| '+' { PLUS }
(* | '+' { PLUS }
| '-' { MINUS }
| '*' { TIMES }
| '/' { DIV }
| '/' { DIV }*)
| '(' { LPAREN }
| ')' { RPAREN }
| '{' { BEGIN }
......@@ -68,7 +68,7 @@ rule get_token = parse
| ';' { SEMICOLON }
| "(*" { comment lexbuf }
| integer as s { INT (int_of_string s) }
| float as f { FLOAT (float_of_string s) }
| float as f { FLOAT (float_of_string f) }
| "#" [^ '\n']* eof
| eof { EOF }
| _ as c { raise (Lexing_error ("illegal character: " ^ String.make 1 c)) }
......
......@@ -7,33 +7,58 @@
%token INIT ITER
%token TRANSLATION ROTATION
%token COMMA SEMICOLON
%token BEGIN END DEF
%token BEGIN END
%token OR
%token IF ELSE
%token LPAREN RPAREN
%token EOF
(*
%token DEF IF ELSE
%token PLUS MINUS TIMES DIV
%token REPEAT PENUP PENDOWN FORWARD TURNLEFT TURNRIGHT
%token COLOR BLACK WHITE RED GREEN BLUE
%token EOF
*)
/*
Tokens priorities and associativities
*/
(*
%left MINUS PLUS
%left TIMES DIV
%nonassoc uminus // used to negate a number
%nonassoc IF
%nonassoc ELSE
*)
/*
Grammar entry point
*/
%start prog
%start program
/*
Value types returned by the parser
*/
%type <Ast.program> prog
%type <Ast.program> program
%%
program:
| INIT LPAREN xmin = FLOAT COMMA xmax = FLOAT COMMA ymin = FLOAT COMMA ymax = FLOAT RPAREN s = stmt EOF
{
((xmin, xmax, ymin, ymax), s)
}
stmt:
| TRANSLATION LPAREN x = FLOAT COMMA y = FLOAT RPAREN SEMICOLON
{ Translation (x, y) }
| ROTATION LPAREN x = FLOAT COMMA y = FLOAT COMMA theta = FLOAT RPAREN SEMICOLON
{ Rotation (x, y, theta) }
| s1 = stmt OR s2 = stmt weight = FLOAT
{ Or (s1, s2, weight) }
| ITER LPAREN it1 = INT COMMA it2 = INT RPAREN s = stmt
{ Iter (s, it1, it2) }
| BEGIN stmtList = stmt* END
{ StmtList stmtList }
;
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment