c05 - CS421 COMPILERS AND INTERPRETERS CS421 Compiler...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS421 COMPILERS AND INTERPRETERS CS421 Compiler Front-End COMPILERS AND INTERPRETERS “Concrete” vs. “Abstract” Syntax • Almost all compilers and interpreters contain the same front-end --- it • The grammar specified in “tiger.grm” (for Yacc) is mainly used for consists of three components: 1. Lexical Analysis --- report lexical errors, output a list of tokens 2. Syntax Analysis --- report syntactic errors, output a parse tree 3. Semantic Analysis --- report semantic errors (e.g., type-errors, undefined identifiers, ...) --- generate a clean and error-free “abstract syntax tree” parsing only ---------- the key is to resolve all ambiguities. This grammar is called Concrete Syntax. • Abstract Syntax (Absyn) is used to characterize the essential structure of the program ----------- the key is to be as simple as possible; Absyn may contain ambiguities. • The grammar for Abstract Syntax is defined using ML datatypes. parse tree token source program lexical analyzer parser • Traditional Compilers: do semantic analysis on Concrete Syntax --error-free abstract syntax semantic analysis get next token • Modern Compilers: “tiger.grm” constructs the Abstract Syntax tree; the semantic analysis is performed on the Absyn later after parsing! Copyright 1994 - 2010 Zhong Shao, Yale University Abstract Syntax : Page 1 of 10 CS421 implemented as “actions” in Section 3 of “tiger.grm” file (for Yacc) COMPILERS AND Copyright 1994 - 2010 Zhong Shao, Yale University INTERPRETERS Abstract Syntax : Page 2 of 10 CS421 Tiger Compiler Front End COMPILERS AND INTERPRETERS Tiger Program and Expression • A Tiger program prog is just an expression exp report all lexical errors tiger source program report all syntactic errors absy token lexical analyzer report all semantic errors parser semantic analysis correct absyn get next token ML-Lex tiger.lex Copyright 1994 - 2010 Zhong Shao, Yale University ML-Yacc tiger.grm The Compiler Frontend generates an “abstract syntax” tree which does not contain any lexical, syntactic, or semantic errors ! Abstract Syntax : Page 3 of 10 • An expression can be any of the following: l-value Nil Integer literal String literal Sequencing Function call Arithmetic expression Comparison expression Boolean operators Record creation Array creation Assignment Copyright 1994 - 2010 Zhong Shao, Yale University foo, foo.bar, foo[1] nil 34 “Hello, World\n” (exp; exp; ...; exp) id(), id(exp{,exp}) exp arith-op exp exp comp-op exp exp & exp, exp | exp ty-id {id = exp, ...}, {} ty-id [exp1] of exp2 lvalue := exp Abstract Syntax : Page 4 of 10 CS421 COMPILERS AND INTERPRETERS CS421 Tiger Expression and Declaration • More Tiger expressions: If-then-else If-then While-expression For-expression Break-expression Let-expression COMPILERS AND INTERPRETERS Tiger Type Declaration • Tiger Type declarations: if exp1 then exp2 else exp3 if exp1 then exp2 while exp1 do exp2 for id:=exp1 to exp2 do exp3 break let decsq in {exp} end • A Tiger declaration sequence is a sequence of type, variable, and tydec -> type id = ty ty -> id | { tyfields } | array of id tyfields -> | id : type-id {,id: type-id} • You can define mutually-recursive types using a consecutive sequence of type declarations type tree = {key : int, children : treelist} type treelist = {hd : tree, tl : treelist} function declarations: dec -> tydec | vardec | fundec decsq -> decsq dec | recursion cycle must pass through a record or array type ! Copyright 1994 - 2010 Zhong Shao, Yale University Abstract Syntax : Page 5 of 10 CS421 COMPILERS AND Copyright 1994 - 2010 Zhong Shao, Yale University Abstract Syntax : Page 6 of 10 INTERPRETERS CS421 Variable and Function Declaration COMPILERS AND INTERPRETERS Tiger Absyn “Hack” • Tiger Variable declarations: • When translating from Concrete Syntax to Abstract Syntax, we can do short-form: long-form: vardec -> var id := exp vardec -> var id : type-id := exp “var x := 3” in Tiger is equivalent to “val x = ref 3” in ML certain syntactic transformations MINUS exp ===> 0 MINUS exp exp1 & exp2 procedure: fundec -> function id (tyfields) := exp function: fundec -> function id (tyfields):type-id := exp • Function declarations may be mutually recursive --- must be declared in ===> if exp1 then exp2 else 0 exp1 | exp2 • Tiger Function declarations: ===> if exp1 then 1 else exp2 This can make Abstract Syntax even simpler. Toy does not support Macros. If the source language supports macros, they can be processed here. a sequence of consecutive function declarations! Variable declarations cannot be mutually recursive ! Copyright 1994 - 2010 Zhong Shao, Yale University Abstract Syntax : Page 7 of 10 Copyright 1994 - 2010 Zhong Shao, Yale University Abstract Syntax : Page 8 of 10 CS421 COMPILERS AND INTERPRETERS CS421 Tiger Semantics • nil AND INTERPRETERS An Example (* A program to solve the 8-queens problem, see Appel’s book *) --- a value belong to every record type. let • Scope rule --- similar to PASCAL, Algol ---- support nested scope for types, variables, and functions; redeclaration will hide the same name. function f(v : int) = let var v := 6 in print(v); let var v := 7 in print(v) end; print(v); let var v := 8 in print(v) end; print(v) end var N := 8 type intArray = array var row := intArray [ var col := intArray [ var diag1 := intArray var diag2 := intArray of int N ] of 0 N ] of 0 [N+N-1] of 0 [N+N-1] of 0 function printboard() = (for i := 0 to N-1 do (for j := 0 to N-1 do print(if col[i]=j then “ O” else “ .”); print(“\n”)); print(“\n”)) • Support two different name space: one for types, and one for functions and variables. You can have a type called foo and a variable foo in scope at same time. Copyright 1994 - 2010 Zhong Shao, Yale University COMPILERS function try(c:int) = (* for i:= 0 to c do print(“.”); print(“\n”); flush(); *) if c=N then printboard() else for r := 0 to N-1 do if row[r]=0 & diag1[r+c]=0 & diag2[r+7-c]=0 then (row[r]:=1; diag1[r+c]:=1; diag2[r+7-c]:=1; col[c]:=r; try(c+1); row[r]:=0; diag1[r+c]:=0; diag2[r+7-c]:=0) in try(0) end Abstract Syntax : Page 9 of 10 Copyright 1994 - 2010 Zhong Shao, Yale University Abstract Syntax : Page 10 of 10 ...
View Full Document

This document was uploaded on 02/20/2012.

Ask a homework question - tutors are online