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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (download) (annotate)
Thu Feb 11 01:04:20 2010 UTC (9 years, 5 months ago) by jhr
File size: 6057 byte(s)
  Fixing parser/scanner bugs
(* 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_die		("die")
  | KW_else		("else")
  | KW_field		("field")
  | KW_if		("if")
  | KW_image		("image")
  | KW_input		("input")
  | KW_int		("int")
  | KW_kernel		("kernel")
  | KW_new		("new")
  | KW_real		("real")
  | KW_stabilize	("stabilize")
  | KW_string		("string")
  | KW_tensor		("tensor")
  | KW_vec		("vec")
  | 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_slash		("/")
  | OP_at		("@")
  | LP			("(")
  | RP			(")")
  | LB			("[")
  | RB			("]")
  | LCB			("{")
  | RCB			("}")
  | COMMA		(",")
  | SEMI		(";")
  | HASH		("#")
  | BANG		("!")
  | POSINT of IntInf.int
  | NEGINT of IntInf.int
  | FLOAT of FloatLit.float
  | STRING of string
  | ID of Atom.atom
  ;

%defs (
  structure PT = ParseTree
  structure L = Literal
  structure Op = Operators

(* 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)

(* 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 flatten NONE = []
    | flatten (SOME(x, xs)) = x::xs

);

File
	: Decl+		=> (PT.Program Decl)
	;

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


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

InputDecl
	: "input" Type ID ( "=" Expr )? ";"
		=> (markDecl(FULL_SPAN, PT.D_Input(Type, 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 ")" "{" VarDecl* ActorMethod+ "}"
		=> (markDecl(FULL_SPAN, PT.D_Actor{
		      name = ID, params = Params, state = VarDecl,
		      methods = ActorMethod
		    }))
	;

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

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

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

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)
	;


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

Type	: "image" "<" Dimensions ">"
		=> (markTy(FULL_SPAN, PT.T_Image Dimensions))
	| "tensor" "<" Dimensions ">"
		=> (markTy(FULL_SPAN, PT.T_Tensor Dimensions))
	| "field" "#" Dimension "<" Dimensions ">"
		=> (markTy(FULL_SPAN, PT.T_Field(Dimension, Dimensions)))
	| "kernel" "#" Dimension
		=> (markTy(FULL_SPAN, PT.T_Kernel Dimension))
	| "vec" "<" Dimension ">"
		=> (markTy(FULL_SPAN, PT.T_Vec Dimension))
	| "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
	: POSINT
		=> (POSINT)
	;

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

Expr
	: AndExpr ( "||" AndExpr => (Op.orElse, AndExpr) )*
		=> (mkLBinExp (AndExpr, SR))
	;

AndExpr
	: CmpExpr ( "&&" CmpExpr => (Op.andAlso, CmpExpr) )*
		=> (mkLBinExp (CmpExpr, SR))
	;

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

CmpOp
	: "<" => (Op.lt) | "<=" => (Op.lte)
	| "==" => (Op.equ) | "!=" => (Op.neq)
	| ">=" => (Op.gte) | ">" => (Op.gt)
	;

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

AddOp
	: "+" => (Op.plus) | "-" => (Op.minus)
	;

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

MulOp
	: "*" => (Op.times) | "/" => (Op.divide)
	;

SelExpr
	: PrefixExpr ( "@" PrefixExpr => (Op.at, PrefixExpr) | "[" Expr "]" => (Op.sub, Expr) )*
		=> (mkRBinExp (PrefixExpr, SR))
	;

PrefixExpr
	: AtomExp
		=> (AtomExp)
	| "-" PrefixExpr
		=> (markExpr(FULL_SPAN, PT.E_UnaryOp(Op.uMinus, PrefixExpr)))
	| "!" PrefixExpr
		=> (markExpr(FULL_SPAN, PT.E_UnaryOp(Op.bNot, PrefixExpr)))
	;

AtomExp
	: 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 *))
	| POSINT
		=> (markExpr(FULL_SPAN, PT.E_Lit(L.Int POSINT)))
	| NEGINT
		=> (markExpr(FULL_SPAN, PT.E_Lit(L.Int NEGINT)))
	| 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 *))
	;

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