SYNTAX DIRECTED DEFINITION

Slides:



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

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)
Compiler Construction
1 Beyond syntax analysis An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what.
Lecture # 17 Syntax Directed Definition. 2 Translation Schemes A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”)
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
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.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
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
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
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.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
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.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Overview of Previous Lesson(s) Over View  In syntax-directed translation 1 st we construct a parse tree or a syntax tree then compute the values of.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Syntax Directed Definition and Syntax directed Translation
UNIT – 5 SYNTAX-DIRECTED TRANSLATION
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
Lecture 9 Symbol Table and Attributed Grammars
Semantic analysis Jakub Yaghob
Semantics Analysis.
Syntax-Directed Translation
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Constructing Precedence Table
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Chapter 5 Syntax Directed Translation
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Syntax-Directed Translation Part I
CS 3304 Comparative Languages
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax-Directed Definition
SYNTAX DIRECTED TRANSLATION
פרק 5 תרגום מונחה תחביר תורת הקומפילציה איתן אביאור.
Chapter 2: A Simple One Pass Compiler
Chapter 2: A Simple One Pass Compiler
COMPILER DESIGN 11CS30013 & 11CS30014 Group October 2013
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Designing a Predictive Parser
Chapter 4 Action Routines.
Syntax-Directed Translation Part I
Syntax-Directed Translation
Compiler Construction
Directed Acyclic Graphs (DAG)
Syntax-Directed Translation Part I
Lecture 16 Boolean Expressions and Control Flow
Chapter 5 Syntax Directed Translation
Compiler Construction
Presentation transcript:

SYNTAX DIRECTED DEFINITION Syntax directed definition specifies the values of attributes by associating semantic rules with the grammar productions. It is a context free grammar with attributes and rules together which are associated with grammar symbols and productions respectively. The process of syntax directed translation is two-fold: • Construction of syntax tree and • Computing values of attributes at each node by visiting the nodes of syntax tree.

Semantic actions Semantic actions are fragments of code which are embedded within production bodies by syntax directed translation. They are usually enclosed within curly braces ({ }). It can occur anywhere in a production but usually at the end of production. (eg.) E---> E1 + T {print ‘+’}

Types of attributes • Inherited attributes    o It is defined by the semantic rule associated with the production at the parent of node.    o Attributes values are confined to the parent of node, its siblings and by itself.    o The non-terminal concerned must be in the body of the production. • Synthesized attributes    o It is defined by the semantic rule associated with the production at the node.    o Attributes values are confined to the children of node and by itself.    o The non terminal concerned must be in the head of production.    o Terminals have synthesized attributes which are the lexical values (denoted by lexval) generated by the lexical analyzer.               

Types of translation • L-attributed translation o It performs translation during parsing itself. o No need of explicit tree construction. o L represents 'left to right'. • S-attributed translation o It is performed in connection with bottom up parsing. o 'S' represents synthesized.

Syntax directed definition of simple desk calculator

Syntax-directed definition-inherited attributes

• Symbol T is associated with a synthesized attribute type • Symbol T is associated with a synthesized attribute type. • Symbol L is associated with an inherited attribute inh,

Types of Syntax Directed Definitions S-attributed Definitions Syntax directed definition that involves only synthesized attributes is called S-attributed. Attribute values for the non-terminal at the head is computed from the attribute values of the symbols at the body of the production. The attributes of a S-attributed SDD can be evaluated in bottom up order of nodes of the parse tree. i.e., by performing post order traversal of the parse tree and evaluating the attributes at a node when the traversal leaves that node for the last time.

L-attributed Definitions The syntax directed definition in which the edges of dependency graph for the attributes in production body, can go from left to right and not from right to left is called L-attributed definitions. Attributes of L-attributed definitions may either be synthesized or inherited. If the attributes are inherited, it must be computed from: • Inherited attribute associated with the production head. • Either by inherited or synthesized attribute associated with the production located to the left of the attribute which is being computed. • Either by inherited or synthesized attribute associated with the attribute under consideration in such a way that no cycles can be formed by it in dependency graph.

In production 1, the inherited attribute T' is computed from the value of F which is to its left. In production 2, the inherited attributed Tl' is computed from T'. inh associated with its head and the value of F which appears to its left in the production. i.e., for computing inherited attribute it must either use from the above or from the left information of SDD.

Syntax Directed Translation Syntax Directed Translation The conceptual view of syntax-directed translation, Input string → parse tree → dependency graph → evaluation order for semantic rules This is a generalization of context free grammar in which each grammar symbol has an associated set of attributes and rules. Attributes are associated with grammar symbols and rules are associated with productions.

Syntax directed translation schemes (SDT) Example : An annotated parse tree for 2*6+ 4n

Top down

Predictive translation

Specification of a Simple Type Checker A type checker for a simple language checks the type of each identifier. The type checker is a translation scheme that synthesizes the type of each expression from the types of its subexpressions. The type checker can handle arrays, pointers, statements and functions.

A Simple Language Consider the following grammar: P → D ; E D → D ; D | id : T T → char | integer | array [ num ] of T | ↑ T E → literal | num | id | E mod E | E [ E ] | E ↑ Translation scheme: D → D ; D D → id : T { addtype (id.entry , T.type) } T → char { T.type : = char } T → integer { T.type : = integer } T → ↑ T1 { T.type : = pointer(T1.type) } T → array [ num ] of T1 { T.type : = array ( 1… num.val , T1.type) }

Boolean Expressions and Control Flow BOOLEAN EXPRESSIONS are constructed using Boolean operators.

We will consider here the following rules.