Basic Program Analysis: AST

Slides:



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

Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CPSC 388 – Compiler Design and Construction
Control Flow Analysis (Chapter 7) Mooly Sagiv (with Contributions by Hanne Riis Nielson)
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.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Compiler Summary Mooly Sagiv html://
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
Chapter 2 A Simple Compiler
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
1 Lecture 10 Syntax-Directed Translation grammar disambiguation, Earley parser, syntax-directed translation Ras Bodik Shaon Barman Thibaud Hottelier Hack.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Prof. Bodik CS 164 Lecture 51 Building a Parser I CS164 3:30-5:00 TT 10 Evans.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CSC 4181 Compiler Construction
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
Lecture 9 Symbol Table and Attributed Grammars
Announcements/Reading
Chapter 3 – Describing Syntax
Compiler Design (40-414) Main Text Book:
Basic Program Analysis
CS 3304 Comparative Languages
A Simple Syntax-Directed Translator
Constructing Precedence Table
Introduction to Parsing
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Structure of programming languages
Textbook:Modern Compiler Design
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
PROGRAMMING LANGUAGES
Compiler Lecture 1 CS510.
CS 536 / Fall 2017 Introduction to programming languages and compilers
Syntax-Directed Translation
Syntax Errors; Static Semantics
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
Compilers B V Sai Aravind (11CS10008).
COP4020 Programming Languages
R.Rajkumar Asst.Professor CSE
CS 3304 Comparative Languages
Syntax-Directed Translation
Designing a Predictive Parser
CS 3304 Comparative Languages
Data Flow Analysis Compiler Design
Topic 2: Compiler Front-End
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Faculty of Computer Science and Information System
Presentation transcript:

Basic Program Analysis: AST CS 4501 Baishakhi Ray

Compiler Overview Abstract Syntax Tree : Source code parsed to produce AST Control Flow Graph: AST is transformed to CFG Data Flow Analysis: operates on CFG

The Structure of a Compiler Source code (stream of characters) scanner stream of tokens parser Abstract Syntax Tree (AST) checker AST with annotations (types, declarations) code gen Machine/byte code Adopted From UC Berkeley: Prof. Bodik CS 164 Lecture 5

Adopted From UC Berkeley: Prof. Bodik CS 164 Lecture 5 Syntactic Analysis Input: sequence of tokens from scanner Output: abstract syntax tree Actually, parser first builds a parse tree AST is then built by translating the parse tree parse tree rarely built explicitly; only determined by, say, how parser pushes stuff to stack Adopted From UC Berkeley: Prof. Bodik CS 164 Lecture 5

Example Source Code 4*(2+3) Parser input Parser output (AST): * NUM(4) NUM(4) TIMES LPAR NUM(2) PLUS NUM(3) RPAR Parser output (AST): * NUM(4) + NUM(2) NUM(3) Adopted From UC Berkeley: Prof. Bodik CS 164 Lecture 5

Parse tree for the example: 4*(2+3) EXPR EXPR EXPR NUM(4) TIMES LPAR NUM(2) PLUS NUM(3) RPAR leaves are tokens Adopted From UC Berkeley: Prof. Bodik CS 164 Lecture 5

IF LPAR ID EQ ID RPAR LBR ID AS INT SEMI RBR Another example Source Code if (x == y) { a=1; } Parser input IF LPAR ID EQ ID RPAR LBR ID AS INT SEMI RBR Parser output (AST): IF-THEN == ID = INT Adopted From UC Berkeley: Prof. Bodik CS 164 Lecture 5

Parse tree for example: if (x==y) {a=1;} STMT BLOCK STMT EXPR EXPR IF LPAR ID == ID RPAR LBR ID = INT SEMI RBR leaves are tokens Adopted From UC Berkeley: Prof. Bodik CS 164 Lecture 5

Parse Tree Representation of grammars in a tree-like form. Is a one-to-one mapping from the grammar to a tree-form. A parse tree pictorially shows how the start symbol of a grammar derives a string in the language. … Dragon Book

C Statement: return a + 2 a very formal representation that strictly shows how the parser understands the statement return a + 2;

Abstract Syntax Tree (AST) Simplified syntactic representations of the source code, and they're most often expressed by the data structures of the language used for implementation Without showing the whole syntactic clutter, represents the parsed string in a structured way, discarding all information that may be important for parsing the string, but isn't needed for analyzing it. ASTs differ from parse trees because superficial distinctions of form, unimportant for translation, do not appear in syntax trees.. … Dragon Book

AST C Statement: return a + 2

The Structure of a Compiler Source code (stream of characters) scanner stream of tokens parser Abstract Syntax Tree (AST) checker AST with annotations (types, declarations) code gen Machine/byte code Adopted From UC Berkeley: Prof. Bodik CS 164 Lecture 5

Checker (Semantic Analysis) Determine whether source is meaningful Check for semantic errors Check for type errors Gather type information for subsequent stages – Relate variable uses to their declarations Some semantic analysis takes place during parsing Example errors (from C) function1 = 3.14159; x = 570 + “hello, world!” scalar[i];

Syntactic & Semantic Analysis Symbol Tables Compile-time data structures Hold names, type information, and scope information for variables Scopes A name space e.g., In C, each set of curly braces defines a new scope – Can create a separate symbol table for each scope

Syntactic & Semantic Analysis Using Symbol Tables For each variable declaration: Check for symbol table entry Add new entry (parsing); add type info (semantic analysis) For each variable use: Check symbol table entry (semantic analysis)

Disadvantages of ASTs AST has many similar forms E.g., for, while, repeat...until E.g., if, ?:, switch Expressions in AST may be complex, nested (x * y) + (z > 5 ? 12 * z : z + 20) Want simpler representation for analysis ...at least, for dataflow analysis int x = 1 // what’s the value of x ? // AST traversal can give the answer, right? What about int x; x = 1; or int x= 0; x += 1; ?