More On Syntax Directed Translation

Size: px
Start display at page:

Download "More On Syntax Directed Translation"

Transcription

1 More On Syntax Directed Translation 1

2 Types of Attributes We have productions of the form: A X 1 X 2 X 3... X n with semantic rules of the form: b:= f(c 1, c 2, c 3,..., c n ) where b and the c s are attributes of the grammar symbols b is called a synthesized attribute if: b is an attribute of A (i.e. the LHS), and the c s are all attributes of the X s (symbols on the RHS) 35

3 Synthesized Attributes b:= f(c 1, c 2, c 3 ) A b X 1 X 2 X 3 c 1 c 2 c 3 Information to compute b is passed up the parse tree 36

4 Inherited Attributes A X 1 X 2 X 3... X n b:= f(c 1, c 2, c 3,..., c n ) b is an inherited attribute if b is an attribute of one of the X s (i.e. RHS) and the c s are attributes of A and/or one or more of the other X s which means they are beside or above where b is needed 37

5 Inherited Attributes b:= f(c 1, c 2, c 3 ) A c 2 X 1 X 2 X 3 b c 1 c 3 Information to compute b must be passed down the parse tree Note that X 2 is only associated with X 1 and X 3 via their appearance on the RHS of a production for A -so information must flow through A in the tree 38

6 S-Attributed Definitions A syntax directed definition is S-attributed if it uses only synthesized attributes Which implies that the definition can be annotated by evaluating semantic rules for nodes bottom-up which fits naturally with bottom-up parsers and can also be evaluated with top down parsers since both types perform depth-first traversals But the presence of inherited attributes poses a problem. 39

7 Inherited Attributes Example A definition for C-style declarations: Production Semantic Rules D T L L.type := T.type (inherited attribute passed down to L) T int T.type := INTEGER T float T.type := FLOAT L L 1, id L 1.type := L.type (inherited from LHS) settype(id.entry, L.type) L id settype(id.entry, L.type) 40

8 Inherited Attributes in C-Declarations D T T.type = INT L L.type = INT settype(y.entry,int) int L L.type = INT settype(x.entry,int), id (y) id (x) 41

9 Computing Synthesized and Inherited Attributes Synthesized Attributes Natural fit for bottom up parsers Yacc: $$ = $1 + $3; etc. Can use parsing function return value in recursive descent Inherited Attributes Natural for top-down parsers Recursive descent: parameters in parsing function call Quite troublesome for bottom-up parsers Especially getting at the attribute of the left-hand-side Tricks such as reaching under the stack Sometimes can be done with embedded actions But usually dealt with later during traversal of the AST 42

10 L-attributed Definitions a syntax directed definition is L-attributed if every inherited attribute of some symbol X i on the RHS of some production A X 1..X i-1 X i.. X n depends only on the attributes of A (the LHS), and on the attributes of the symbols X 1..X i-1 to the left of symbol X i in the production This implies that the definition can be annotated by evaluating semantic rules for nodes in a depth first, left-to-right traversal of the tree 43

11 Evaluation of L-Attributed Definitions An L-attributed definition can be evaluated in a depth-first tree traversal as follows: procedure dfvisit(n:node); begin for each child m of n, from left to right do begin evaluate inherited attributes of m; dfvisit(m) end; evaluate synthesized attributes of n end which means it can be evaluated on the fly driven by a parser Note that every S-attributed definition is also L- attributed 44

12 Translation Schemes A CFG with attributes associated with grammar symbols and semantic actions enclosed in braces { } embedded within the RHS s to indicate the time during the processing when the action should be executed to evaluate its attribute 45

13 Translation Schemes An action can only be executed when all the attributes it refers to have already been evaluated For a synthesized attribute: the action can simply be placed at the end of the production For an inherited attribute: an inherited attribute for a symbol on the RHS must be evaluated in an action before that symbol (and then passed down), and an action cannot refer to a synthesized attribute to the right of the action 46

14 Pascal-Style Declarations The C declaration example was L-Attributed..but the following obvious grammar is not: D var id IDLIST : T IDLIST, id IDLIST ε T integer real (Symbol T is to the right of the IDLIST in the first production, so the type cannot be passed down the tree during a left-to-right traversal) 47

15 Rewriting Grammars to Facilitate Translation The Pascal declaration grammar can be re-written to permit the use of only synthesized attributes: D var id LIS { settype(id.entry, LIS.type) } LIS, id LIS 1 { settype(id.entry, LIS 1.type) LIS.type := LIS 1.type } : T { LIS.type := T.type } T integer { T.type := INTEGER } real { T.type := FLOAT } 48

16 Attribute Evaluation with Revised Grammar D settype(a.entry,integer) var ID LIS settype(b.entry,integer) LIS.type := INTEGER a, ID LIS settype(c.entry,integer) LIS.type := INTEGER b, ID LIS LIS.type := INTEGER c : T T.type := INTEGER integer 49

17 Symbol Tables & Abstract Syntax Trees 50

18 Symbol Tables Symbol tables can take many forms We have seen the simple linked list form as used in the type checking example For a language like Java there are typically several tables such as: A linked list of CLASS descriptors For each CLASS, a linked list of METHOD and FIELD descriptors For each method, a list of formal parameters, local variables and a pointer to an AST structure for the actual code 51

19 Tree Structures as Intermediate Code Representation Production compilers usually build some form of intermediate code during parsing, postponing target code generation until later after optimizations can be performed The intermediate language is generally quite independent from the nitty-gritty details of any particular ISA of real machines A common form of intermediate code is a tree structure This cleanly separates the front end (source language analysis) from the back end (code generation for a specific target machine) 52

20 Parse Trees and Abstract Syntax Trees Parse trees could be an intermediate form, but are cumbersome E E + E E E op E E * E c ( E ) - E ID a ( E ) String: a * ( -b ) + c - E Only 3 operations, But 7 interior nodes! b 53

21 Abstract Syntax Trees Abstract Syntax Trees eliminate the clutter and capture meaning in a minimal form E E op E String: ( E ) - E ID a * ( -b ) + c a + * c - b 3 interior nodes 54

22 Translation Scheme for Abstract Syntax Trees NODE *MakeNODE(op, left, right) NODE *MakeUNARY(op, arg) NODE *MakeLEAF(id) E E 1 op E 2 { E.n=MakeNODE(op, E 1.n, E 2.n) } ( E 1 ) { E.n = E 1.n } - E 1 { E.n = MakeUNARY( -, E 1.n) } ID { E.n = MakeLEAF(id) } AST is a very convenient representation for machineindependent optimizations Generate target code later via post-order tree traversal a a * ( -b ) + c + * c - b 55

23 Abstract Syntax Trees The AST is simply a tree structure that is a simplification of a parse tree that contains only the significant information without the syntactic sugar. The symbol table is actually just another form of AST, which captures the relevant information about classes, attributes, and methods, while ignoring the syntactic details of how these are declared. It usually also contains other information fields not necessarily filled in at parse time for use in semantic analysis or code generation AST s are actually very straightforward to construct in a parser. 56

24 Expression Tree (AST) (Examples are from an AST-based type checker) Binary operator case: struct AST {int opr; struct AST *left; struct AST *right; }; An operator, and pointers to AST nodes for the left and right operands. 57

25 Creating an AST node in a parser exp : exp '+' exp { $$=make_binary ('+', $1, $3); } with similar semantic actions creating variants of the AST node for different syntactic structures We pass the pointer up the tree via $$, so when the final reduction to the start symbol occurs, it gets a pointer to the root of the whole tree 58

26 AST Node types There are different operators (binary, unary ) and different language structures (if, while ), so we actually need many different kinds of nodes. We can create a unique data structure for each node type This would lead to a large number of unique data structures to keep track of So we use a more general purpose structure with variants If coding in an object-oriented language, we can use subclasses of a generic node class In C, we have to use a "general purpose" structure, perhaps using a C union to deal with special cases 59

27 General Purpose AST Node #define MAXCHILDREN 2 typedef enum { binary_exp, int_const, bool_const, var_exp, assign_ast, prog_ast} NodeKind; typedef struct AST { struct AST * child[maxchildren]; struct AST * next; int lineno; NodeKind nodekind; union { int op; int val; char * name; struct VAR *vartable; } attr; Type type; /* for type checking of exps */ } AST; Example: If we have an AST pointer a and know that the "nodekind" is binary_exp, we can refer to the leftoperand of the operator as: a->child[0] the right-operand as a->child[1] and the operator will be a->attr.op etc. 60

28 Node Creation #define NEW(type) (type *) calloc(1,sizeof(type)) AST *make_binary(int opt, AST *left, AST *right) { AST * e = NEW(AST); e->nodekind = binary_exp; e->attr.op = opt; e->child[0] = left; e->child[1] = right; e->lineno = lineno; /* from global variable in scanner */ return e; }...and one of these for each AST subtype 61

29 Type Checking by AST Traversal Assume a two-pass type checker: 1. First it parses the input and builds an AST and a symbol table as it goes 2. Then we traverse the AST recursively computing the types of expressions and checking all semantic rules 62

30 Expression Productions e : e '=' e { $$ = make_binary ('=', $1, $3); } e '+' e { $$ = make_binary ('+', $1, $3); } e tor e { $$ = make_binary (tor, $1, $3);} '(' e ')' { $$ = $2; } NUM { $$ = make_int($1); } ttrue { $$ = make_bool(1); } tfalse { $$ = make_bool(0); } ID { $$ = make_var($1); } ; 64

31 Tree Traversal to Decorate Tree and Check Type Rules Type type_check(ast *a) { Type t1,t2; int op; switch (a->nodekind) { case binary_exp: t1 = type_check(a->child[0]); t2 = type_check(a->child[1]); op = a->attr.op; if (op=='+') { /* arithmetic */ ASSERT(t1==typeINT,"Left operand not Int",a->lineno); ASSERT(t2==typeINT,"Right operand not Int",a->lineno); a->type = typeint; } else if (op== xxx) ( for each of the other operators)... break; case... (for all other AST variants) } /* end of switch */ return a->type; } 65

32 AST Typecheck Demo 1: program test; 2: var a: integer; 3: b: integer; 4: c: boolean; 5: begin 6: a := c; 7: c := a=b; 8: a := true; 9: x := y+3; 10: b := a or c; 11: end. % typecheck < test.p line 6: Assignment type mismatch line 8: Assignment type mismatch line 9: Undeclared identifier line 9: LHS of assignment not declared line 9: Undeclared identifier line 9: Left operand not Int line 9: Assignment type mismatch line 10: Left operand not boolean line 10: Assignment type mismatch % 67

Syntactic Directed Translation

Syntactic Directed Translation Syntactic Directed Translation Translation Schemes Copyright 2016, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer. The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees

More information

Summary: Semantic Analysis

Summary: Semantic Analysis Summary: Semantic Analysis 1 Basic Concepts When SA is performed: Semantic Analysis may be performed: In a two-pass compiler: after syntactic analysis is finished, the semantic analyser if called with

More information

COP5621 Exam 3 - Spring 2005

COP5621 Exam 3 - Spring 2005 COP5621 Exam 3 - Spring 2005 Name: (Please print) Put the answers on these sheets. Use additional sheets when necessary. Show how you derived your answer when applicable (this is required for full cred

More information

Semantic Analysis. Role of Semantic Analysis

Semantic Analysis. Role of Semantic Analysis Semantic Analysis Chapter 4 Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are semantic analysis (intermediate) code generation The principal job of the semantic

More information

CPS 506 Comparative Programming Languages. Syntax Specification

CPS 506 Comparative Programming Languages. Syntax Specification CPS 506 Comparative Programming Languages Syntax Specification Compiling Process Steps Program Lexical Analysis Convert characters into a stream of tokens Lexical Analysis Syntactic Analysis Send tokens

More information

LECTURE 3. Compiler Phases

LECTURE 3. Compiler Phases LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent

More information

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres dgriol@inf.uc3m.es Introduction He am a driver might be syntactically correct but semantically wrong. Semantic

More information

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

Syntax-Directed Translation

Syntax-Directed Translation Syntax-Directed Translation 1 Syntax-Directed Translation 1. We associate information with the programming language constructs by attaching attributes to grammar symbols. 2. Values of these attributes

More information

Syntax-Directed Translation. Introduction

Syntax-Directed Translation. Introduction Syntax-Directed Translation Introduction Translation of languages guided by context-free grammars Attach attributes to the grammar symbols Values of the attributes are computed by semantic rules associated

More information

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

SEMANTIC ANALYSIS TYPES AND DECLARATIONS SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether

More information

A Simple Syntax-Directed Translator

A Simple Syntax-Directed Translator Chapter 2 A Simple Syntax-Directed Translator 1-1 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called

More information

Syntax-Directed Translation Part I

Syntax-Directed Translation Part I 1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011 2 The Structure of our Compiler Revisited Character stream

More information

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019 Compiler Principle and Technology Prof. Dongming LU April 15th, 2019 PART TWO 6. Semantic Analysis Contents Part One 6.1 Attributes and Attribute Grammars Part Two 6.2 Algorithms for Attribute Computation

More information

[Syntax Directed Translation] Bikash Balami

[Syntax Directed Translation] Bikash Balami 1 [Syntax Directed Translation] Compiler Design and Construction (CSc 352) Compiled By Central Department of Computer Science and Information Technology (CDCSIT) Tribhuvan University, Kirtipur Kathmandu,

More information

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1 Compilers 5. Attributed Grammars Laszlo Böszörmenyi Compilers Attributed Grammars - 1 Adding Attributes We connect the grammar rules with attributes E.g. to implement type-checking or code generation A

More information

Chapter 4 :: Semantic Analysis

Chapter 4 :: Semantic Analysis Chapter 4 :: Semantic Analysis Programming Language Pragmatics, Fourth Edition Michael L. Scott Copyright 2016 Elsevier 1 Chapter04_Semantic_Analysis_4e - Tue November 21, 2017 Role of Semantic Analysis

More information

Semantic Analysis Attribute Grammars

Semantic Analysis Attribute Grammars Semantic Analysis Attribute Grammars Martin Sulzmann Martin Sulzmann Semantic Analysis Attribute Grammars 1 / 18 Syntax versus Semantics Syntax Analysis When is a program syntactically valid? Formalism:

More information

Lecture 14 Sections Mon, Mar 2, 2009

Lecture 14 Sections Mon, Mar 2, 2009 Lecture 14 Sections 5.1-5.4 Hampden-Sydney College Mon, Mar 2, 2009 Outline 1 2 3 4 5 Parse A parse tree shows the grammatical structure of a statement. It includes all of the grammar symbols (terminals

More information

Syntax-Directed Translation

Syntax-Directed Translation Syntax-Directed Translation 1 Syntax-Directed Translation 2 Syntax-Directed Translation 3 Syntax-Directed Translation In a syntax-directed definition, each production A α is associated with a set of semantic

More information

Syntax-Directed Translation

Syntax-Directed Translation Syntax-Directed Translation ALSU Textbook Chapter 5.1 5.4, 4.8, 4.9 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 What is syntax-directed translation? Definition: The compilation

More information

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University Semantic Analysis CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Role of Semantic Analysis Syntax vs. Semantics: syntax concerns the form of a

More information

Time : 1 Hour Max Marks : 30

Time : 1 Hour Max Marks : 30 Total No. of Questions : 6 P4890 B.E/ Insem.- 74 B.E ( Computer Engg) PRINCIPLES OF MODERN COMPILER DESIGN (2012 Pattern) (Semester I) Time : 1 Hour Max Marks : 30 Q.1 a) Explain need of symbol table with

More information

UNIT IV INTERMEDIATE CODE GENERATION

UNIT IV INTERMEDIATE CODE GENERATION UNIT IV INTERMEDIATE CODE GENERATION 2 Marks 1. Draw syntax tree for the expression a=b*-c+b*-c 2. Explain postfix notation. It is the linearized representation of syntax tree.it is a list of nodes of

More information

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

COP4020 Programming Languages. Semantics Prof. Robert van Engelen COP4020 Programming Languages Semantics Prof. Robert van Engelen Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees COP4020 Spring 2011 2 Static Semantics Syntax concerns

More information

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1 CSE 401 Compilers Static Semantics Hal Perkins Winter 2009 2/3/2009 2002-09 Hal Perkins & UW CSE I-1 Agenda Static semantics Types Symbol tables General ideas for now; details later for MiniJava project

More information

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Language Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Semantic Analysis Compiler Architecture Front End Back End Source language Scanner (lexical analysis)

More information

CSCI Compiler Design

CSCI Compiler Design University of Southern California CSCI565 Compiler Design Mterm Exam Solution - Spring 2014 CSCI 565 - Compiler Design Spring 2014 Mterm Exam - Solution Problem 1: Context-Free-Grammars and Parsing Algorithms

More information

Syntax-Directed Translation

Syntax-Directed Translation Syntax-Directed Translation What is syntax-directed translation? The compilation process is driven by the syntax. The semantic routines perform interpretation based on the syntax structure. Attaching attributes

More information

Semantic Analysis. Compiler Architecture

Semantic Analysis. Compiler Architecture Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Source Compiler Architecture Front End Scanner (lexical tokens Parser (syntax Parse tree Semantic Analysis

More information

CS 406: Syntax Directed Translation

CS 406: Syntax Directed Translation CS 406: Syntax Directed Translation Stefan D. Bruda Winter 2015 SYNTAX DIRECTED TRANSLATION Syntax-directed translation the source language translation is completely driven by the parser The parsing process

More information

Compilers. Compiler Construction Tutorial The Front-end

Compilers. Compiler Construction Tutorial The Front-end Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers

More information

Semantic actions for expressions

Semantic actions for expressions Semantic actions for expressions Semantic actions Semantic actions are routines called as productions (or parts of productions) are recognized Actions work together to build up intermediate representations

More information

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher COP4020 Programming Languages Semantics Robert van Engelen & Chris Lacher Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees Static Semantics Syntax concerns the form

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 11! Syntax- Directed Transla>on The Structure of the

More information

Syntax Errors; Static Semantics

Syntax Errors; Static Semantics Dealing with Syntax Errors Syntax Errors; Static Semantics Lecture 14 (from notes by R. Bodik) One purpose of the parser is to filter out errors that show up in parsing Later stages should not have to

More information

CSE 431S Type Checking. Washington University Spring 2013

CSE 431S Type Checking. Washington University Spring 2013 CSE 431S Type Checking Washington University Spring 2013 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks

More information

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1 CIT3136 - Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1 Definition of a Context-free Grammar: An alphabet or set of basic symbols (like regular expressions, only now the symbols are whole tokens,

More information

Context-sensitive Analysis. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

Context-sensitive Analysis. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Context-sensitive Analysis Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int

More information

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1 Static Semantics Lecture 15 (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1 Current Status Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing

More information

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions Concepts Introduced in Chapter 5 Syntax-Directed Definitions Translation Schemes Synthesized Attributes Inherited Attributes Dependency Graphs Syntax-Directed Translation Uses a grammar to direct the translation.

More information

Programming Languages

Programming Languages Programming Languages Tevfik Koşar Lecture - IX February 14 th, 2006 1 Roadmap Semantic Analysis Role of Semantic Analysis Static vs Dynamic Analysis Attribute Grammars Evaluating Attributes Decoration

More information

CSC 467 Lecture 13-14: Semantic Analysis

CSC 467 Lecture 13-14: Semantic Analysis CSC 467 Lecture 13-14: Semantic Analysis Recall Parsing is to translate token stream to parse tree Today How to build trees: syntax direction translation How to add information to trees: semantic analysis

More information

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known. SEMANTIC ANALYSIS: Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known. Parsing only verifies that the program consists of tokens

More information

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Type checking Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Summary of parsing Parsing A solid foundation: context-free grammars A simple parser: LL(1) A more powerful parser:

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

A programming language requires two major definitions A simple one pass compiler

A programming language requires two major definitions A simple one pass compiler A programming language requires two major definitions A simple one pass compiler [Syntax: what the language looks like A context-free grammar written in BNF (Backus-Naur Form) usually suffices. [Semantics:

More information

CSCI Compiler Design

CSCI Compiler Design CSCI 565 - Compiler Design Spring 2015 Midterm Exam March 04, 2015 at 8:00 AM in class (RTH 217) Duration: 2h 30 min. Please label all pages you turn in with your name and student number. Name: Number:

More information

Syntax-Directed Translation. Lecture 14

Syntax-Directed Translation. Lecture 14 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik) 9/27/2006 Prof. Hilfinger, Lecture 14 1 Motivation: parser as a translator syntax-directed translation stream of tokens parser ASTs,

More information

Last Time. What do we want? When do we want it? An AST. Now!

Last Time. What do we want? When do we want it? An AST. Now! Java CUP 1 Last Time What do we want? An AST When do we want it? Now! 2 This Time A little review of ASTs The philosophy and use of a Parser Generator 3 Translating Lists CFG IdList -> id IdList comma

More information

Syntax-Directed Translation Part II

Syntax-Directed Translation Part II Syntax-Directed Translation Part II Chapter 5 Slides adapted from : Robert van Engelen, Florida State University Alessandro Artale, Free University of Bolzano Syntax-Directed Translation Schemes Syntax-directed

More information

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis? Semantic analysis and intermediate representations Which methods / formalisms are used in the various phases during the analysis? The task of this phase is to check the "static semantics" and generate

More information

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Compiling and Interpreting Programming. Overview of Compilers and Interpreters Copyright R.A. van Engelen, FSU Department of Computer Science, 2000 Overview of Compilers and Interpreters Common compiler and interpreter configurations Virtual machines Integrated programming environments

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

Semantic Analysis with Attribute Grammars Part 3

Semantic Analysis with Attribute Grammars Part 3 with Attribute Grammars Part 3 Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design Outline of the Lecture Introduction

More information

As we have seen, token attribute values are supplied via yylval, as in. More on Yacc s value stack

As we have seen, token attribute values are supplied via yylval, as in. More on Yacc s value stack More on Yacc s value stack As we noted last time, Yacc uses a second stack to store the attribute values of the tokens and terminals in the parse stack. For a token, the attributes are computed by the

More information

Ambiguity and Errors Syntax-Directed Translation

Ambiguity and Errors Syntax-Directed Translation Outline Ambiguity (revisited) Ambiguity and rrors Syntax-Directed Translation xtensions of CFG for parsing Precedence declarations rror handling Semantic actions Constructing a parse tree CS780(Prasad)

More information

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler) Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler) Lecturers: Paul Kelly (phjk@doc.ic.ac.uk) Office: room 304, William Penney Building Naranker Dulay (nd@doc.ic.ac.uk)

More information

A simple syntax-directed

A simple syntax-directed Syntax-directed is a grammaroriented compiling technique Programming languages: Syntax: what its programs look like? Semantic: what its programs mean? 1 A simple syntax-directed Lexical Syntax Character

More information

Crafting a Compiler with C (II) Compiler V. S. Interpreter

Crafting a Compiler with C (II) Compiler V. S. Interpreter Crafting a Compiler with C (II) 資科系 林偉川 Compiler V S Interpreter Compilation - Translate high-level program to machine code Lexical Analyzer, Syntax Analyzer, Intermediate code generator(semantics Analyzer),

More information

Compiler Design Concepts. Syntax Analysis

Compiler Design Concepts. Syntax Analysis Compiler Design Concepts Syntax Analysis Introduction First task is to break up the text into meaningful words called tokens. newval=oldval+12 id = id + num Token Stream Lexical Analysis Source Code (High

More information

QUESTIONS RELATED TO UNIT I, II And III

QUESTIONS RELATED TO UNIT I, II And III QUESTIONS RELATED TO UNIT I, II And III UNIT I 1. Define the role of input buffer in lexical analysis 2. Write regular expression to generate identifiers give examples. 3. Define the elements of production.

More information

(Not Quite) Minijava

(Not Quite) Minijava (Not Quite) Minijava CMCS22620, Spring 2004 April 5, 2004 1 Syntax program mainclass classdecl mainclass class identifier { public static void main ( String [] identifier ) block } classdecl class identifier

More information

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs. Compilerconstructie najaar 2012 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs.nl college 5, dinsdag 9 oktober 2012 Static Type Checking 1

More information

SML-SYNTAX-LANGUAGE INTERPRETER IN JAVA. Jiahao Yuan Supervisor: Dr. Vijay Gehlot

SML-SYNTAX-LANGUAGE INTERPRETER IN JAVA. Jiahao Yuan Supervisor: Dr. Vijay Gehlot SML-SYNTAX-LANGUAGE INTERPRETER IN JAVA Jiahao Yuan Supervisor: Dr. Vijay Gehlot Target Design SML-Like-Syntax Build Parser in ANTLR Abstract Syntax Tree Representation ANLTER Integration In Interpreter

More information

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans Syntax-Directed Translation CS3300 - Compiler Design Syntax Directed Translation V. Krishna Nandivada IIT Madras Attach rules or program fragments to productions in a grammar. Syntax directed definition

More information

Lexical and Syntax Analysis. Top-Down Parsing

Lexical and Syntax Analysis. Top-Down Parsing Lexical and Syntax Analysis Top-Down Parsing Easy for humans to write and understand String of characters Lexemes identified String of tokens Easy for programs to transform Data structure Syntax A syntax

More information

Type Checking. Chapter 6, Section 6.3, 6.5

Type Checking. Chapter 6, Section 6.3, 6.5 Type Checking Chapter 6, Section 6.3, 6.5 Inside the Compiler: Front End Lexical analyzer (aka scanner) Converts ASCII or Unicode to a stream of tokens Syntax analyzer (aka parser) Creates a parse tree

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

More information

Question Bank. 10CS63:Compiler Design

Question Bank. 10CS63:Compiler Design Question Bank 10CS63:Compiler Design 1.Determine whether the following regular expressions define the same language? (ab)* and a*b* 2.List the properties of an operator grammar 3. Is macro processing a

More information

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules. Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

COP 3402 Systems Software Top Down Parsing (Recursive Descent) COP 3402 Systems Software Top Down Parsing (Recursive Descent) Top Down Parsing 1 Outline 1. Top down parsing and LL(k) parsing 2. Recursive descent parsing 3. Example of recursive descent parsing of arithmetic

More information

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference Type Checking Outline General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1 CSE P 501 Compilers Static Semantics Hal Perkins Winter 2008 1/22/2008 2002-08 Hal Perkins & UW CSE I-1 Agenda Static semantics Types Attribute grammars Representing types Symbol tables Note: this covers

More information

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask? Syntax-directed translation Context-sensitive analysis The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the

More information

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS Objective PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS Explain what is meant by compiler. Explain how the compiler works. Describe various analysis of the source program. Describe the

More information

COP4020 Spring 2011 Midterm Exam

COP4020 Spring 2011 Midterm Exam COP4020 Spring 2011 Midterm Exam Name: (Please print Put the answers on these sheets. Use additional sheets when necessary or write on the back. Show how you derived your answer (this is required for full

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Program Assignment 2 Due date: 10/20 12:30pm

Program Assignment 2 Due date: 10/20 12:30pm Decoration of parse tree for (1 + 3) * 2 N. Meng, S. Arthur 1 Program Assignment 2 Due date: 10/20 12:30pm Bitwise Manipulation of Hexidecimal Numbers CFG E E A bitwise OR E A A A ^ B bitwise XOR A B B

More information

Static Checking and Type Systems

Static Checking and Type Systems 1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 The Structure of our Compiler Revisited Character stream Lexical

More information

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design i About the Tutorial A compiler translates the codes written in one language to some other language without changing the meaning of the program. It is also expected that a compiler should make the target

More information

Chapter 4 - Semantic Analysis. June 2, 2015

Chapter 4 - Semantic Analysis. June 2, 2015 Chapter 4 - Semantic Analysis June 2, 2015 The role of the semantic analyzer Compilers use semantic analysis to enforce the static semantic rules of a language It is hard to generalize the exact boundaries

More information

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Error Handling Syntax-Directed Translation Recursive Descent Parsing Error Handling Syntax-Directed Translation Recursive Descent Parsing Lecture 6 by Professor Vijay Ganesh) 1 Outline Recursive descent Extensions of CFG for parsing Precedence declarations Error handling

More information

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

More information

Course Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend:

Course Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend: Course Overview Introduction (Chapter 1) Compiler Frontend: Today Lexical Analysis & Parsing (Chapter 2,3,4) Semantic Analysis (Chapter 5) Activation Records (Chapter 6) Translation to Intermediate Code

More information

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler. More detailed overview of compiler front end Structure of a compiler Today we ll take a quick look at typical parts of a compiler. This is to give a feeling for the overall structure. source program lexical

More information

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table Formal Languages and Compilers Lecture IX Semantic Analysis: Type Checking & Symbol Table Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

Compilers Project 3: Semantic Analyzer

Compilers Project 3: Semantic Analyzer Compilers Project 3: Semantic Analyzer CSE 40243 Due April 11, 2006 Updated March 14, 2006 Overview Your compiler is halfway done. It now can both recognize individual elements of the language (scan) and

More information

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford) Programming Languages & Translators PARSING Baishakhi Ray Fall 2018 These slides are motivated from Prof. Alex Aiken: Compilers (Stanford) Languages and Automata Formal languages are very important in

More information

Introduction to Syntax Analysis. Compiler Design Syntax Analysis s.l. dr. ing. Ciprian-Bogdan Chirila

Introduction to Syntax Analysis. Compiler Design Syntax Analysis s.l. dr. ing. Ciprian-Bogdan Chirila Introduction to Syntax Analysis Compiler Design Syntax Analysis s.l. dr. ing. Ciprian-Bogdan Chirila chirila@cs.upt.ro http://www.cs.upt.ro/~chirila Outline Syntax Analysis Syntax Rules The Role of the

More information

CSE 12 Abstract Syntax Trees

CSE 12 Abstract Syntax Trees CSE 12 Abstract Syntax Trees Compilers and Interpreters Parse Trees and Abstract Syntax Trees (AST's) Creating and Evaluating AST's The Table ADT and Symbol Tables 16 Using Algorithms and Data Structures

More information

Compiler Construction I

Compiler Construction I TECHNISCHE UNIVERSITÄT MÜNCHEN FAKULTÄT FÜR INFORMATIK Compiler Construction I Dr. Michael Petter, Dr. Axel Simon SoSe 2013 1 / 59 Organizing Master or Bachelor in the 6th Semester with 5 ECTS Prerequisites

More information

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking CSE450 Translation of Programming Languages Lecture 11: Semantic Analysis: Types & Type Checking Structure Project 1 - of a Project 2 - Compiler Today! Project 3 - Source Language Lexical Analyzer Syntax

More information

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis? Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator

More information

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Lecture 18 1 Announcements

More information

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E if E then S while E do S S ; S Type checking of statements The purpose

More information

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B. CSE 3302 Test 1 1. Preprocessor macros are associated with: A. C B. Java C. JavaScript D. Pascal 2. (define x (lambda (y z) (+ y z))) is an example of: A. Applying an anonymous function B. Defining a function

More information

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing

More information