1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer.

Slides:



Advertisements
Similar presentations
Abstract Syntax Mooly Sagiv html:// 1.
Advertisements

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
May 2006CLINT-LN Parsing1 Computational Linguistics Introduction Approaches to Parsing.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
Chapter 2 A Simple Compiler
COP4020 Programming Languages
Winter 2003/4Pls – syntax – Catriel Beeri1 SYNTAX Syntax: form, structure The syntax of a pl: The set of its well-formed programs The rules that define.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
Parser construction tools: YACC
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
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.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
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.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Semantic Analysis (Generating An AST) CS 471 September 26, 2007.
Grammar Variation in Compiler Design Carl Wu. Three topics Syntax Grammar vs. AST Component(?)-based grammar Aspect-oriented grammar.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Parse & Syntax Trees Syntax & Semantic Errors Mini-Lecture.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Part II Describing Syntax and Semantics.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
LESSON 04.
Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
Bernd Fischer COMP2010: Compiler Engineering Abstract Syntax Trees.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Parsing & Context-Free Grammars
Chapter 3 – Describing Syntax
What does it mean? Notes from Robert Sebesta Programming Languages
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
Compiler Construction
Syntax versus Semantics
Basic Program Analysis: AST
Syntax-Directed Translation
Chapter 2: A Simple One Pass Compiler
Computer Science and Engineering
Binary Trees: Motivation
Computer Science and Engineering
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Discrete Maths 13. Grammars Objectives
Programming Languages 2nd edition Tucker and Noonan
Faculty of Computer Science and Information System
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer

2 Abstract Syntax

3 Parse Trees We have been looking at concrete parse trees, in which inner nodes are nonterminals, leaf nodes are terminals children are labeled with the symbols in the RHS of the production Concrete parse trees are inconvenient to use, since they are cluttered with tokens containing no additional information: punctuation symbols (SEMI etc) needed to specify structure when writing code, but the tree structure already describes the program structure stmt SEMI :: stmt  stmt SEMI stmt stmt  …

4 Parse Tree Example

5 Abstract parse trees (aka abstract syntac tree – AST) like concrete parse trees (e.g. inductive datatype, generated as semantic action by YACC) each syntactic category (expressions, statements,..) is represented as a separate datatype, with one constructor for each formation redundant punctuation symbols are left out

6 Abstract parse trees (aka abstract syntac tree – AST) like concrete parse trees (e.g. inductive datatype, generated as semantic action by YACC) each syntactic category (expressions, statements,..) is represented as a separate datatype, with one constructor for each formation redundant punctuation symbols are left out CompoundStmt AssignStmt “a” NumExpr(3) AssignStmt “b”NumExpr(4) datatype stmt = CompoundStmt of stmt * stmt | AssignStmt of string * expr; datatype expr = NumExpr of int | binopExpr of expr * binop * expr;

7 Abstract parse trees (aka abstract syntac tree – AST) like concrete parse trees (e.g. inductive datatype, generated as semantic action by YACC) each syntactic category (expressions, statements,..) is represented as a separate datatype, with one constructor for each formation redundant punctuation symbols are left out CompoundStmt AssignStmt “a” NumExpr(3) AssignStmt “b”NumExpr(4) First approximation: nonterminal  synt. category; CFG rule  constructor But: AST is internal interface between components of compiler, so AST design is up to compiler writer, not the language designer; may deviate from organization suggested by grammar/syntax datatype stmt = CompoundStmt of stmt * stmt | AssignStmt of string * expr; datatype expr = NumExpr of int | binopExpr of expr * binop * expr;

8 Semantic Analysis: Symbol Tables

9 Symbol Table Example function f (b:int, c:int) = (print_int (b+c); let var j:= b var a := “x” in print (a); print_int (j) end; print_int (a) )

10 Symbol Table Implementation

11 Imperative Symbol Tables

12 Functional Symbol Tables Association list (cf HW 1) not efficient (lookup and delete linear)

13 Functional Symbol Tables

14 Functional Symbol Table using BST: lookup Use the “less than” relation to navigate down the tree c -> real f -> int d -> strings -> string t -> int

15 Functional Symbol Table using BST: insertion c -> real f -> int d -> strings -> string t -> int z -> int Insertion of z-> int:1.create node

16 Functional Symbol Table using BST: insertion c -> real f -> int d -> strings -> string t -> int f -> int t -> int z -> int Insertion of z-> int:1.create node 2.“search” for z in old tree; copy ancestor nodes

17 Functional Symbol Table using BST: insertion c -> real f -> int d -> strings -> string t -> int f -> int t -> int z -> int Insertion of z-> int:1.create node 2.“search” for z in old tree; copy ancestor nodes 3.insert links to siblings in original (share subtree)