# 1 CMPS 450 Syntax-Directed Translations CMPS 450 J. Moloney.

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

Compiler Construction
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.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
May 2006CLINT-LN Parsing1 Computational Linguistics Introduction Approaches to Parsing.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Top-Down Parsing.
Context-Free Grammars Lecture 7
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
1 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
Professor Yihjia Tsai Tamkang University
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
Lecture 14 Syntax-Directed Translation Harry Potter has arrived in China, riding the biggest initial print run for a work of fiction here since the Communist.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
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.
LESSON 24.
Syntax and Semantics Structure of programming languages.
Chapter 5 Top-Down Parsing.
Syntax-Directed Translation
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
# 1 CMPS 450 Parsing CMPS 450 J. Moloney. # 2 CMPS 450 Check that input is well-formed Build a parse tree or similar representation of input Recursive.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
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.
Syntax and Semantics Structure of programming languages.
11 Chapter 4 Grammars and Parsing Grammar Grammars, or more precisely, context-free grammars, are the formalism for describing the structure of.
Bernd Fischer RW713: Compiler and Software Language Engineering.
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.
LESSON 04.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
Syntax Directed Definition and Syntax directed Translation
UNIT – 5 SYNTAX-DIRECTED TRANSLATION
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Top-Down Parsing.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
Course Revision.. Contents  Lexical Analysis  Our Compiler Structure  Tokens, Lexemes & Patterns  RE’s & Regular Definitions  Transition Diagrams.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
LECTURE 7 Lex and Intro to Parsing. LEX Last lecture, we learned a little bit about how we can take our regular expressions (which specify our valid tokens)
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
SDTs used to implement SDDs A non-cyclic SDD (having definitions of attributes) can always be implemented by a SDT (having actions that assign values to.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Review 1.Structure of the course Lexical Analysis Syntax Analysis Grammar & Language RG & DFA Top-down LL(1) Parsing Bottom-Up LR Layered Automation Semantic.
Syntax and Semantics Structure of programming languages.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
A Simple Syntax-Directed Translator
Programming Languages Translator
CS510 Compiler Lecture 4.
Chapter 5 Syntax Directed Translation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
SYNTAX DIRECTED DEFINITION
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 5 Syntax Directed Translation
Presentation transcript:

# 1 CMPS 450 Syntax-Directed Translations CMPS 450 J. Moloney

# 2 CMPS 450 Generates code while parsing, following rules associated with each production. 1.Construct a parse tree and perform a SDD 2. Recursive procedures associated with nonterminals (like recursive-descent parser). 3. Recursive procedures that spit out code as a side-effect.. 4. SDT on an LL grammar; top-down parse. 5. SDT on an LR grammar; bottom-up parse (1) is completely general. (2) through (5) require an “L- attributed SDD.” SDT (Syntax-Directed Translations) SDT (Syntax-Directed Definition) Defines translations associated with each node of the parse tree. Translation at root is the output. Five styles of Translation

# 3 CMPS 450 Syntax-Directed Translations The most common form of translator is an SDD, where the productions of a grammar have embedded within them certain actions, which cause side-effects. Example: The following grammar generates expressions over + and *, while the SDT translates them into postfix. E  E + T {print ‘+’ ) E  T { } T  T * F {print ‘*’ ) T  F { } F  (E) { } F  i { print i.name Here, i.name is a translation of identifiers, found by taking the “lexical value” for i, which is a pointer to the appropriate symbol table entry, and obtaining from there the name of the identifier.

# 4 CMPS 450 Order of Actions The interpretation of an SDT is that the action assoicated with a production is executed immediately after the terminals generated from any grammar symbols to its left are recognized. Typical case: The parser is bottom-up, and all actions come at the end of the productions (called a postfix SDT). Then the actions take place when we reduce by their productions. General Rule: The actions are viewed as “dummy” terminals embedded in the parse tree. They are executed in the order of a preorder traversal of the tree. Thus, the result of the SDT is the same whether parsing is top-down, bottom- up, or something else.

# 5 CMPS 450 Example: Here is a parse tree from the previous grammar, with actions embedded as terminals. The input is a + b * c. E E+T print ‘+’ TT*Fprint ‘*’ FF i print ‘c’ i print ‘a’ i print ‘b’ The output from a preorder traversal of this tree is abc * +. That is also the output obtained by executing the actions at the time of reduction during a bottom-up parse. The underlying grammar is not suitable for top-down parsing because of left recursion.

# 6 CMPS 450 Conversion ot top-down Parsable SDT The SDT notion is robust enough ot allow conversion to a top-down parsable grammar, while we carry along the actions as though they were terminals. That is, the SDT performed is not chaged by such a transformation, just as the input language is accepted unchanged. Recall that we can eliminate left-recursion from the productions A  A  |  by replacing them with A   B B   B | 

# 7 CMPS 450 Example: Consider E  E + T { print ‘+’ } E  T Here,  = + T { print ‘+’ } and  = T. The new rules for the SDT are thus: E  T D D  + T { print ‘+’ } D D   The fact that the action is embedded within the second production is not, in principle, a problem. It says that immediately after seeing on the input a string consisting of a +-sign and a string that parses to a term (T), we emit the symbol +, before looking for another expression tail (D).

# 8 CMPS 450 If a we make a similar transformation for nonterminal T, we obtain an LL grammar for expressions, whose parse tree, with embedded actions, for the input a + b * c is: E TD F+ T print ‘+’D i print ‘a’FS  i print ‘b’F print ‘*’S i print ‘c’  Notice that a preorder traversal of this tree also yields output abc * +.

# 9 CMPS 450 Syntax-Directed Definitions When translating according to an SDT, it is normal not to construct the parse tree. Rather we perform actions as we parse, that is, we “go through the motions” of building the parse tress. To perform a translation that cannot be expressed by an SDT directly, it helps to build the parse tree first. For example, while we can translate infix to postfix by an SDT, we cannot translate to prefix. Define one or more attributes of each terminal and nonterminal, and give rules relating the values of these translations at the various nodes of the parse tree. Rules can be associated with productions, because they relate translations at a node to translations at its parent and/or children. The rules for computing these translations together form a syntax-directed definition (SDD). Rules with no side effects (such as printing output) are called an attribute grammar.

# 10 CMPS 450 Example: We can translate expressions to prefix form if we are allowed to associate a string (the prefix expression) with every node labeled E, T, or F. The string for a node corresponding to the left side of a production is formed from the strings corresponding to the nonterminals on the right by concatenation, denoted |. Put another way, strings are computed bottom-up, from children to parents. Thus, an SDD for infix-to-prefix translation is E  E 1 + T { E.pre := ‘+’ | E 1.pre | T.pre } E  T { E.pre := T.pre } T  T 1 * F { T.pre := ‘*’ | T 1.pre | F.pre } T  F { T.pre := F.pre } F  ( E ) { F.pre := E.pre } F  i { F.pre := i.name } Note that E1 and T1 are instances of nonterminals E and T, respectively; the “sub 1” is intended to remind the listener that the occurrence on the right is referred to.