Syntax-Directed Translation Part II

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
1 Compiler Construction Intermediate Code Generation.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
Lecture # 17 Syntax Directed Definition. 2 Translation Schemes A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”)
Syntax Trees MathWorks Compiler Course – Day 5. Syntax Trees MathWorks Compiler Course – Day 5 Parser lexemes shift/reduce seq. Cfg tables Tree Symbols.
Intermediate Code Generation Professor Yihjia Tsai Tamkang University.
Syntax Directed Translation
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Syntax-Directed Translation
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Syntax Directed Definitions Synthesized Attributes
Semantic Analysis (Generating An AST) CS 471 September 26, 2007.
Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Topic: Syntax Directed Translations
COP4020 Programming Languages Semantics Prof. Xin Yuan.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Chapter 8: Intermediate Code Generation
1 Semantic Analysis. 2 Semantic Analyzer Attribute Grammars Syntax Tree Construction Top-Down Translators Bottom-Up Translators Recursive Evaluators Type.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 Syntax-Directed Translation We associate information with the programming language constructs by attaching attributes to grammar symbols. 2.Values.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
1 Syntax-Directed Translation Part II Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Semantics Analysis.
Syntax-Directed Translation
A Simple Syntax-Directed Translator
Constructing Precedence Table
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Chapter 3 Context-Free Grammar and Parsing
Compiler Construction
Syntax Analysis Part I Chapter 4
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Compiler design Bottom-up parsing: Canonical LR and LALR
Syntax-Directed Translation Part I
4 (c) parsing.
Syntax-Directed Translation Part II
Intermediate Code Generation Part I
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax-Directed Definition
פרק 5 תרגום מונחה תחביר תורת הקומפילציה איתן אביאור.
Intermediate Code Generation Part I
Syntax-Directed Translation Part II
Syntax-Directed Translation Part II
Intermediate Code Generation Part I
Syntax Analysis - Parsing
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Directed Acyclic Graphs (DAG)
Intermediate Code Generation Part I
Syntax-Directed Translation Part I
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Syntax-Directed Translation Part II Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2013

Translation Schemes using Marker Nonterminals Need a stack to keep track of gotos to backpatch! (to handle nested if-then) S  if E {push(pc); emit(ifeq, 0) } then S { backpatch(top(), pc-top()); pop() } Synthesized attribute (automatically stacked in shift-reduce parser!) Insert marker nonterminal S  if E M then S { backpatch(M.loc, pc-M.loc) } M   { M.loc := pc; emit(ifeq, 0) }

Translation Schemes using Marker Nonterminals in Yacc S : IF E M THEN S { backpatch($3, pc-$3); } ; M : /* empty */ { $$ = pc; emit3(ifeq, 0); } ; …

Replacing Inherited Attributes with Synthesized Lists D  T L { for all id  L.list : addtype(id.entry, T.type) } T  int { T.type := ‘integer’ } T  real { T.type := ‘real’ } L  L1 , id { L.list := L1.list + [id] } L  id { L.list := [id] } D T.type = ‘real’ L.list = [id1,id2,id3] real L.list = [id1,id2] , id3.entry L.list = [id1] , id2.entry id1.entry

Replacing Inherited Attributes with Synthesized Lists in Yacc %{ typedef struct List { Symbol *entry; struct List *next; } List; %} %union { int type; List *list; Symbol *sym; } %token <sym> ID %type <list> L %type <type> T %% D : T L { List *p; for (p = $2; p; p = p->next) addtype(p->entry, $1); } ; T : INT { $$ = TYPE_INT; } | REAL { $$ = TYPE_REAL; } L : L ‘,’ ID { $$ = malloc(sizeof(List)); $$->entry = $3; $$->next = $1; | ID { $$ = malloc(sizeof(List)); $$->entry = $1; $$->next = NULL; }

Concrete and Abstract Syntax Trees A parse tree is called a concrete syntax tree An abstract syntax tree (AST) is defined by the compiler writer as a more convenient intermediate representation E + E + T id * T T * id id id id id Concrete syntax tree Abstract syntax tree

Generating Abstract Syntax Trees Synthesize AST E.nptr E.nptr + T.nptr T.nptr T.nptr * id id id + id * id id

S-Attributed Definitions for Generating Abstract Syntax Trees Production E  E1 + T E  E1 - T E  T T  T1 * id T  T1 / id T  id Semantic Rule E.nptr := mknode(‘+’, E1.nptr, T.nptr) E.nptr := mknode(‘-’, E1.nptr, T.nptr) E.nptr := T.nptr T.nptr := mknode(‘*’, T1.nptr, mkleaf(id, id.entry)) T.nptr := mknode(‘/’, T1.nptr, mkleaf(id, id.entry)) T.nptr := mkleaf(id, id.entry)

Generating Abstract Syntax Trees with Yacc %{ typedef struct Node { int op; /* node op */ Symbol *entry; /* leaf */ struct Node *left, *right; } Node; %} %union { Node *node; Symbol *sym; } %token <sym> ID %type <node> E T F %% E : E ‘+’ T { $$ = mknode(‘+’, $1, $3); } | E ‘-’ T { $$ = mknode(‘-’, $1, $3); } | T { $$ = $1; } ; T : T ‘*’ F { $$ = mknode(‘*’, $1, $3); } | T ‘/’ F { $$ = mknode(‘/’, $1, $3); } | F { $$ = $1; } ; F : ‘(’ E ‘)’ { $$ = $2; } | ID { $$ = mkleaf($1); } ; %%

Eliminating Left Recursion from a Translation Scheme A  A1 Y { A.a := g(A1.a, Y.y) } A  X { A.a := f(X.x) } A  X { R.i := f(X.x) } R { A.a := R.s } R  Y { R1.i := g(R.i, Y.y) } R1 { R.s := R1.s } R   { R.s := R.i }

Eliminating Left Recursion from a Translation Scheme (cont’d) A.a = g(g(f(X.x), Y1.y), Y2.y) A.a = g(f(X.x), Y1.y) Y2 A.a = f(X.x) Y1 X

Eliminating Left Recursion from a Translation Scheme (cont’d) X R1.i = f(X.x) Y1 R2.i = g(f(X.x), Y1.y) Y2 R3.i = g(g(f(X.x), Y1.y), Y2.y) 1. Flow of inherited attribute values 

Eliminating Left Recursion from a Translation Scheme (cont’d) A.s = R1.s = g(g(f(X.x), Y1.y), Y2.y) X R1.s = R2.s = g(g(f(X.x), Y1.y), Y2.y) Y1 R2.s = R3.s = g(g(f(X.x), Y1.y), Y2.y) Y2 R3.s = R3.i = g(g(f(X.x), Y1.y), Y2.y) 2. Flow of synthesized attribute values 

Generating Abstract Syntax Trees with Predictive Parsers E  E1 + T { E.nptr := mknode(‘+’, E1.nptr, T.nptr) } E  E1 - T { E.nptr := mknode(‘-’, E1.nptr, T.nptr) } E  T { E.nptr := T.nptr } T  id { T.nptr := mkleaf(id, id.entry) } E  T { R.i := T.nptr } R { E.nptr := R.s } R  + T {R1.i := mknode(‘+’, R.i, T.nptr) } R1 { R.s := R1.s } R  - T {R1.i := mknode(‘-’, R.i, T.nptr) } R1 { R.s := R1.s } R   { R.s := R.i } T  id { T.nptr := mkleaf(id, id.entry) }

Generating Abstract Syntax Trees with Predictive Parsers (cont’d) Node *R(Node *i) { Node *s, *i1; if (lookahead == ‘+’) { match(‘+’); s = T(); i1 = mknode(‘+’, i, s); s = R(i1); } else if (lookahead == ‘-’) { … } else s = i; return s; }