Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.

Slides:



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

Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
SYNTAX DIRECTED TRANSLATION 11CS Types of Attributes There are two types of attributes for non- terminals :- Synthesized Attributes : For a non-terminal.
Semantics Static semantics Dynamic semantics attribute grammars
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Context-Free Grammars Lecture 7
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.
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
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
LANGUAGE TRANSLATORS: WEEK 17 scom.hud.ac.uk/scomtlm/cis2380/ See Appel’s book chapter 3 for support reading Last Week: Top-down, Table driven parsers.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Overview of Previous Lesson(s) Over View  An ambiguous grammar which fails to be LR and thus is not in any of the classes of grammars i.e SLR, LALR.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
# 1 CMPS 450 Syntax-Directed Translations CMPS 450 J. Moloney.
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
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.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
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.
Semantic analysis Jakub Yaghob
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Parsing and Parser Parsing methods: top-down & bottom-up
Unit-3 Bottom-Up-Parsing.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Compiler Construction
Chapter 5 Syntax Directed Translation
Syntax-Directed Translation Part I
4 (c) parsing.
Presentation by Julie Betlach 7/02/2009
CS 3304 Comparative Languages
Syntax-Directed Translation
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax-Directed Definition
Chapter 2: A Simple One Pass Compiler
Subject Name:Sysytem Software Subject Code: 10SCS52
Lecture 7: Introduction to Parsing (Syntax Analysis)
COMPILER DESIGN 11CS30013 & 11CS30014 Group October 2013
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax Analysis - 3 Chapter 4.
Compiler Lecture Note, Miscellaneous
Syntax-Directed Translation Part I
Chapter 5 Syntax Directed Translation
Presentation transcript:

Syntax Directed Translation

Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We can also think of it as compilation Each node in a parse tree produces a value – That value depends on the type of the node – And on the values produced by its children The value is usually produced by a rule associated with the node Just the rules in Yacc, e.g.: { $$ = $1 + $3; }

Why is a 200 after this? pcalc> calc 331 Calculator (type ? for help and. to exit) >> = a 1 1 >> if 1 (= a 100) (= a 200) 100 >> a 200 >>

Example grammar + input string => parse tree e e + + e e e e a a * * e: e '+' e {$$ = $1+$3;} |e '*' e {$$ = $1*$3;} | NAME {$$ = $1->value;} | NUMBER {$$ = $1;} e: e '+' e {$$ = $1+$3;} |e '*' e {$$ = $1*$3;} | NAME {$$ = $1->value;} | NUMBER {$$ = $1;} 1 * 2 + a

Example grammar + input string => parse tree + annotations e e + + e e e e a a * * e: e '+' e {$$ = $1+$3;} |e '*' e {$$ = $1*$3;} | NAME {$$ = $1->value;} | NUMBER {$$ = $1;} e: e '+' e {$$ = $1+$3;} |e '*' e {$$ = $1*$3;} | NAME {$$ = $1->value;} | NUMBER {$$ = $1;} 1 * 2 + a $$ = $1+$3 $$ = $1*$3 $$ = $1->value a’s symbol table entry

Example Do a post order traversal of the annotated parse tree to determine the execution order of the nodes. e e + + e e e e a a * * e: e '+' e {$$ = $1+$3;} |e '*' e {$$ = $1*$3;} | NAME {$$ = $1->value;} | NUMBER {$$ = $1;} e: e '+' e {$$ = $1+$3;} |e '*' e {$$ = $1*$3;} | NAME {$$ = $1->value;} | NUMBER {$$ = $1;} 1 * 2 + a $$ = $1+$3 $$ = $1*$3 $$ = $1->value a’s symbol table entry

Conditionals If evaluates all three args and selects the value to return Evaluation is bottom up, left to right Watch out for side effects! e e e e = = a a 1 1 e e = = a a if if ($2) $$=$3; else $$=$4; $1->value = $3; $$=$3;