Skip to content

Supported subset of Python syntax

Jean Yang edited this page Feb 26, 2014 · 3 revisions

This is the subset of the Python abstract syntax that we support. For reference, here is the full Python abstract syntax.


module Python
{
	mod = Module(stmt* body)

	stmt = FunctionDef(identifier name, arguments args, 
                            stmt* body, expr* decorator_list)
	      | ClassDef(identifier name, expr* bases, stmt* body, expr* decorator_list)
	      | Return(expr? value)

	      | Assign(expr* targets, expr value)
	      | AugAssign(expr target, operator op, expr value)

 	      | Print(expr? dest, expr* values, bool nl)

	      | For(expr target, expr iter, stmt* body, stmt* orelse)
	      | If(expr test, stmt* body, stmt* orelse)

	      | Import(alias* names)
	      | ImportFrom(identifier? module, alias* names, int? level)

	      | Expr(expr value)
	      | Pass

	      attributes (int lineno, int col_offset)

	expr = BoolOp(boolop op, expr* values)
	     | BinOp(expr left, operator op, expr right)
	     | UnaryOp(unaryop op, expr operand)
	     | Lambda(arguments args, expr body)
	     | IfExp(expr test, expr body, expr orelse)
	     | ListComp(expr elt, comprehension* generators)
	     | Compare(expr left, cmpop* ops, expr* comparators)
	     | Call(expr func, expr* args, keyword* keywords,
			 expr? starargs, expr? kwargs)
	     | Repr(expr value)
	     | Num(object n)
	     | Str(string s)

	     | Attribute(expr value, identifier attr, expr_context ctx)
	     | Subscript(expr value, slice slice, expr_context ctx)
	     | Name(identifier id, expr_context ctx)
	     | List(expr* elts, expr_context ctx) 

	      attributes (int lineno, int col_offset)

	expr_context = Load | Store | Del | AugLoad | AugStore | Param

	slice = Index(expr value) 

	boolop = And | Or 

	operator = Add | Sub | Mult | Div | Mod | Pow | LShift 
                 | RShift | BitOr | BitXor | BitAnd | FloorDiv

	unaryop = Invert | Not | UAdd | USub

	cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | In | NotIn

	comprehension = (expr target, expr iter, expr* ifs)

	arguments = (expr* args, identifier? vararg, 
		     identifier? kwarg, expr* defaults)

        alias = (identifier name, identifier? asname)
}
Clone this wiki locally