Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /trunk/src/compiler/parser/diderot.grm
ViewVC logotype

View of /trunk/src/compiler/parser/diderot.grm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 382 - (download) (annotate)
Tue Oct 5 21:46:21 2010 UTC (9 years ago) by jhr
File size: 8546 byte(s)
  Fixed bug in grammar
(* diderot.grm
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
 * All rights reserved.
 *)

%name Diderot;

%tokens
  : KW_actor		("actor")
  | KW_bool		("bool")
  | KW_D		("D")
  | KW_die		("die")
  | KW_else		("else")
  | KW_field		("field")
  | KW_if		("if")
  | KW_image		("image")
  | KW_input		("input")
  | KW_in		("in")
  | KW_initially	("initially")
  | KW_int		("int")
  | KW_kernel		("kernel")
  | KW_new		("new")
  | KW_output		("output")
  | KW_real		("real")
  | KW_stabilize	("stabilize")
  | KW_string		("string")
  | KW_tensor		("tensor")
  | KW_update		("update")
  | KW_vec2		("vec2")
  | KW_vec3		("vec3")
  | KW_vec4		("vec4")
  | OP_eq		("=")
  | OP_orelse		("||")
  | OP_andalso		("&&")
  | OP_lt		("<")
  | OP_lte		("<=")
  | OP_eqeq		("==")
  | OP_neq		("!=")
  | OP_gte		(">=")
  | OP_gt		(">")
  | OP_plus		("+")
  | OP_minus		("-")
  | OP_star		("*")
  | OP_convolve		("(*)")
  | OP_slash		("/")
  | OP_at		("@")
  | LP			("(")
  | RP			(")")
  | LB			("[")
  | RB			("]")
  | LCB			("{")
  | RCB			("}")
  | COMMA		(",")
  | SEMI		(";")
  | COLON		(":")
  | HASH		("#")
  | BANG		("!")
  | BAR			("|")
  | DOTDOT		("..")
  | INT of IntInf.int
  | FLOAT of FloatLit.float
  | STRING of string
  | ID of Atom.atom
  ;

%defs (
  structure PT = ParseTree
  structure L = Literal
  structure B = BasisNames

(* apply a mark constructor to a span and a tree *)
  fun mark cons (span : AntlrStreamPos.span, tr) = cons{span = span, tree = tr}

(* specialize mark functions for common node types *)
  val markDecl = mark PT.D_Mark
  fun markTy (_, e as PT.T_Mark _) = e
    | markTy (sp, tr) = mark PT.T_Mark (sp, tr)
  fun markStmt (_, e as PT.S_Mark _) = e
    | markStmt (sp, tr) = mark PT.S_Mark (sp, tr)
  fun markExpr (_, e as PT.E_Mark _) = e
    | markExpr (sp, tr) = mark PT.E_Mark (sp, tr)

  fun mkCondExp cons = let
	fun mk (e, []) = e
	  | mk (e, [e']) = cons(e, e')
	  | mk (e, e'::r) = cons(e, mk(e', r))
	in
	  mk
	end

(* build an application for an infix binary operator *)
  fun mkBinApp (e1, rator, e2) = PT.E_BinOp(e1, rator, e2)

(* construct application expressions for left-associative binary operators *)
  fun mkLBinExp (e, []) = e
    | mkLBinExp (e, (id, e')::r) = mkLBinExp (mkBinApp(e, id, e'), r)

(* construct application expressions for right-associative binary operators *)
  fun mkRBinExp (e, []) = e
    | mkRBinExp (e, [(id, e')]) = mkBinApp(e, id, e')
    | mkRBinExp (e, (id, e')::r) = mkBinApp(e, id, mkRBinExp(e', r))

  fun mkOptExp (_, e, NONE) = e
    | mkOptExp (spn, e, SOME mk) = mk(spn, e)

  fun flatten NONE = []
    | flatten (SOME(x, xs)) = x::xs

);

File
	: Decl+		=> (PT.Program{span=FULL_SPAN, tree=Decl})
	;

Decl
	: InputDecl
	| VarDecl	=> (PT.D_Var VarDecl)
	| ActorDecl
	| CoordinationDecl
	;


(***** Inputs *****)

InputDecl
	: "input" ValueType ID ( "=" Expr )? ";"
		=> (markDecl(FULL_SPAN, PT.D_Input(ValueType, ID, SR)))
	;

(***** variable declarations *****)

VarDecl
	: Type ID "=" Expr ";"
		=> (mark PT.VD_Mark (FULL_SPAN, PT.VD_Decl(Type, ID, Expr)))
	;


(***** Actors *****)

ActorDecl
	: "actor" ID "(" Params ")" "{" ActorStateDecl* ActorMethod+ "}"
		=> (markDecl(FULL_SPAN, PT.D_Actor{
		      name = ID, params = Params, state = ActorStateDecl,
		      methods = ActorMethod
		    }))
	;

Params
	: ( Param ( "," Param )* )?
		=> (flatten SR)
	;

Param
	: ValueType ID
		=> (mark PT.P_Mark (FULL_SPAN, PT.P_Param(ValueType, ID)))
	;

ActorStateDecl
	: "output" VarDecl
		=> (true, mark PT.VD_Mark (FULL_SPAN, VarDecl))
	| VarDecl
		=> (false, VarDecl)
	;

ActorMethod
	: MethodId Block
		=> (mark PT.M_Mark (FULL_SPAN, PT.M_Method(MethodId, Block)))
	;

MethodId
	: "update"	=> (Atom.atom "update")
	| "stabilize"	=> (Atom.atom "stabilize")
	| ID		=> (ID)
	;

Block
	: "{" Stmt* "}"
		=> (markStmt(FULL_SPAN, PT.S_Block Stmt))
	;

Stmt
	: AtomicStmt
		=> (AtomicStmt)
	| %try "if" "(" Expr ")" Stmt "else" Stmt
		=> (markStmt(FULL_SPAN, PT.S_IfThenElse(Expr, Stmt1, Stmt2)))
	| %try "if" "(" Expr ")" Stmt
		=> (markStmt(FULL_SPAN, PT.S_IfThen(Expr, Stmt)))
	;

AtomicStmt
	: Block
		=> (Block)
	| VarDecl
		=> (PT.S_Decl VarDecl)
	| "stabilize" ";"
		=> (markStmt(FULL_SPAN, PT.S_Stabilize))
	| "die" ";"
		=> (markStmt(FULL_SPAN, PT.S_Die))
	| "new" ID "(" Arguments ")" ";"
		=> (markStmt(FULL_SPAN, PT.S_New(ID, Arguments)))
	| ID "=" Expr ";"
		=> (markStmt(FULL_SPAN, PT.S_Assign(ID, Expr)))
	;

Arguments
	: ( Expr ("," Expr)* )?
		=> (flatten SR)
	;


(***** Coordination declaration *****)

CoordinationDecl
	: "initially" (Array | Collection) ";"
		=> (markDecl(FULL_SPAN, SR))
	;

Array
	: "[" Create "|" Iterations "]"
		=> (PT.D_InitialArray(Create, Iterations))
	;

Create
	: ID "(" Arguments ")"
		=> (mark PT.C_Mark (FULL_SPAN, PT.C_Create(ID, Arguments)))
	;

Iterations
	: Iteration ( "," Iteration )+
		=> (Iteration :: SR)
	;

Iteration
	: ID "in" Expr ".." Expr
		=> (mark PT.I_Mark (FULL_SPAN, PT.I_Range(ID, Expr1, Expr2)))
	;

Collection
	: "{" Create  "|" Iterations "]"
		=> (PT.D_InitialCollection(Create, Iterations))
	;

(***** Types *****)

Type	: "image" "(" Dimension ")" Dimensions
		=> (markTy(FULL_SPAN, PT.T_Image{
			shape = Dimensions, dim = Dimension
		      }))
	| "field" "#" Dimension "(" Dimension ")" Dimensions
		=> (markTy(FULL_SPAN, PT.T_Field{
			diff = Dimension1,
			shape = Dimensions,
			dim = Dimension2
		      }))
	| "kernel" "#" Dimension
		=> (markTy(FULL_SPAN, PT.T_Kernel Dimension))
	| ValueType ("[" Dimension "]")*
		=> (case SR
		     of [] => ValueType
		      | d => (markTy(FULL_SPAN, PT.T_Array(ValueType, d)))
		    (* end case *))
	;

ValueType
	: "tensor" Dimensions
		=> (markTy(FULL_SPAN, PT.T_Tensor Dimensions))
	| "vec2"
		=> (markTy(FULL_SPAN, PT.T_Vec 2))
	| "vec3"
		=> (markTy(FULL_SPAN, PT.T_Vec 3))
	| "vec4"
		=> (markTy(FULL_SPAN, PT.T_Vec 4))
	| "bool"
		=> (markTy(FULL_SPAN, PT.T_Bool))
	| "int"
		=> (markTy(FULL_SPAN, PT.T_Int))
	| "real"
		=> (markTy(FULL_SPAN, PT.T_Real))
	| "string"
		=> (markTy(FULL_SPAN, PT.T_String))
	;

Dimensions
	: "[" ( Dimension ("," Dimension)* )? "]"
		=> (flatten SR)
	;

Dimension
	: INT
		=> (INT)
	;

(***** Expressions *****)

Expr
	: TestExpr ( "if" Expr "else" Expr => (Expr1, Expr2) )?
		=> (case SR
		     of NONE => TestExpr
		      | SOME(e1, e2) => markExpr(FULL_SPAN, PT.E_Cond(TestExpr, e1, e2))
		    (* end case *))
	;

TestExpr
	: AndExpr ( "||" AndExpr )*
		=> (mkCondExp PT.E_OrElse (AndExpr, SR))
	;

AndExpr
	: CmpExpr ( "&&" CmpExpr )*
		=> (mkCondExp PT.E_AndAlso (CmpExpr, SR))
	;

CmpExpr
	: AddExpr ( CmpOp AddExpr )*
		=> (mkLBinExp (AddExpr, SR))
	;

CmpOp
	: "<" => (B.op_lt) | "<=" => (B.op_lte)
	| "==" => (B.op_equ) | "!=" => (B.op_neq)
	| ">=" => (B.op_gte) | ">" => (B.op_gt)
	;

AddExpr
	: MulExpr ( AddOp MulExpr )*
		=> (mkLBinExp (MulExpr, SR))
	;

AddOp
	: "+" => (B.op_add) | "-" => (B.op_sub)
	;

MulExpr
	: ProbeExpr ( MulOp ProbeExpr )*
		=> (mkLBinExp (ProbeExpr, SR))
	;

MulOp
	: "*" => (B.op_mul) | "/" => (B.op_div) | "(*)" => (B.op_convolve)
	;

ProbeExpr
	: PrefixExpr ( "@" AtomExpr => (AtomExpr) )?
		=> (case SR
		     of NONE => PrefixExpr
		      | SOME e => markExpr(FULL_SPAN, PT.E_BinOp(PrefixExpr, B.op_at, e))
		    (* end case *))
	;

PrefixExpr
	: SliceExpr
		=> (SliceExpr)
	| "-" PrefixExpr
		=> (markExpr(FULL_SPAN, PT.E_UnaryOp(B.op_neg, PrefixExpr)))
	| "!" PrefixExpr
		=> (markExpr(FULL_SPAN, PT.E_UnaryOp(B.op_not, PrefixExpr)))
	| "D" PrefixExpr
		=> (markExpr(FULL_SPAN, PT.E_UnaryOp(B.op_D, PrefixExpr)))
	;

SliceExpr
	: AtomExpr ( "[" Indices "]" )?
		=> (case SR
		     of NONE => AtomExpr
		      | SOME s => markExpr(FULL_SPAN, PT.E_Slice(AtomExpr, s))
		    (* end case *))
	;

Indices
	: Index ( "," Index )*
		=> (Index :: SR)
	;

Index
	: ":"
		=> (NONE)
	| Expr
		=> (SOME Expr)
	;

AtomExpr
	: ID ( "(" Arguments ")" )?
		=> (case SR
		     of NONE => markExpr(FULL_SPAN, PT.E_Var ID)
		      | SOME args => markExpr(FULL_SPAN, PT.E_Apply(ID, args))
		    (* end case *))
	| "real" "(" Expr ")"
		=> (markExpr(FULL_SPAN, PT.E_Real Expr))
	| "[" Expr ("," Expr)* "]"
		=> (markExpr(FULL_SPAN, PT.E_Cons(Expr::SR)))
	| INT
		=> (markExpr(FULL_SPAN, PT.E_Lit(L.Int INT)))
	| FLOAT
		=> (markExpr(FULL_SPAN, PT.E_Lit(L.Float FLOAT)))
	| STRING
		=> (markExpr(FULL_SPAN, PT.E_Lit(L.String STRING)))
	| "(" Expr ("," Expr)* ")"
		=> (case SR
		     of [] => Expr
		      | l => markExpr(FULL_SPAN, PT.E_Tuple(Expr::l))
		    (* end case *))
	| "|" Expr "|"
		=> (markExpr(FULL_SPAN, PT.E_UnaryOp(B.op_norm, Expr)))
	;

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0