Compilation 2014
Abstract Syntax
Aslan Askarov aslan@cs.au.dk
Revised from slides by E. Ernst
Abstract Syntax Aslan Askarov aslan@cs.au.dk Revised from slides by - - PowerPoint PPT Presentation
Compilation 2014 Abstract Syntax Aslan Askarov aslan@cs.au.dk Revised from slides by E. Ernst Abstract syntax High-level source Pretty printing code Abstract syntax tree Lexing/Parsing Elaboration Lowering Code generation
Aslan Askarov aslan@cs.au.dk
Revised from slides by E. Ernst
High-level source code Low-level target code Lexing/Parsing Lowering Code generation Elaboration Optimization Abstract syntax tree Pretty printing
… exp : ID ( A.Id (ID) ) | INT ( A.Number (INT) ) | LPAREN exp RPAREN ( exp ) | exp PLUS exp ( A.Op (A.Plus, exp1, exp2 )) | exp MINUS exp ( A.Op (A.Minus, exp1, exp2 )) …
datatype aexp = Id of string | Number of int | Op of binop * aexp * aexp and binop = Plus | Minus | Times | Div
semantic actions in parser .grm file ML-code What else can we put as semantic actions? Is it a good idea?
in the parser semantic actions
declarations
an abstract syntax tree (AST)
associativity, precedence, etc
| INT ( A.Number (INT) ) | LPAREN exp RPAREN ( exp )
| exp MINUS exp ( A.Op (A.Minus, exp1, exp2, exp1left ))
| exp DIV exp ( A.Op (A.Div, exp1, exp2, exp1left ))
| Number of int | Op of binop * aexp * aexp * pos
position information
information to go from abstract syntax to concrete via pretty printing, but no more
block
block block x := a + b block block block block block block
complex
using sublists of declarations
| FieldVar of var * S.symbol * pos | SubscriptVar of var * exp * pos
| NilExp | IntExp of int | StringExp of string * pos | CallExp of calldata ... and decl = FunctionDec of fundecldata list | VarDec of vardecldata | TypeDec of tydecldata list ... and fundecldata = { name: S.symbol , params: fielddata list , result: (S.symbol * pos) option , body: exp , pos: pos} …
an array variable */ let type arrtype = array of int var arr1: arrtype := arrtype [10] of 0 in arr1 end
LetExp TypeDec 'arrtype' ArrayTy 'int' VarDec 'arr1: arrtype' ArrayExp 'arrtype' init 10 size init SeqExp body VarExp SimpleVar 'arr1'
* expression 0 is * int = myint */
type myint = int type arrtype = array of myint var arr1: arrtype := arrtype [10] of 0 in arr1 end
LetExp TypeDec 'myint' NameTy 'int' 'arrtype' ArrayTy 'myint' VarDec 'arr1: arrtype' ArrayExp 'arrtype' init 10 size init SeqExp body VarExp SimpleVar 'arr1'
* functions */ let function do_nothing1 (a: int, b: string): int = (do_nothing2(a+1); 0)
(d: int): string = (do_nothing1(d, "str"); " ") in do_nothing1(0, "str2") end
LetExp Function Dec 'do_nothing1 : int' 'a: int' par 'b: string' par SeqExp body Call 'do_nothing2' + arg VarExp 1 SimpleVar 'a' 'do_nothing2: string' 'd: int' par SeqExp body Call 'do_nothing1' VarExp arg SimpleVar 'd' "str" arg " " SeqExp body Call 'do_nothing1' arg "str2 " arg
let type arrtype1 = array of int type rectype1 = {name: string, address: string, id: int, age: int} type arrtype2 = array of rectype1 type rectype2 = {name: string, dates: arrtype1} type arrtype3 = array of string
var arr2 := arrtype2 [5] of rectype1{name="aname", address="somewhere", id=0, age=0} var arr3: arrtype3 := arrtype3 [100] of ""
var rec2 := rectype2{name="Allos", dates = arrtype1 [3] of 1900} in arr1[0] := 1; arr1[9] := 3; arr2[3].name := "kati"; arr2[1].age := 23; arr3[34] := "sfd";
rec2.dates[0] := 2323; rec2.dates[2] := 2323 end
under xleft, x1left
limited in functionality