Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.

Slides:



Advertisements
Similar presentations
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Advertisements

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Programming Languages 2nd edition Tucker and Noonan
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 2: Syntax Fall 2009 Marco.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 3 Lexical and Syntactic Analysis Syntactic.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Syntactic Analysis Dr. Hyunyoung Lee.
Invitation to Computer Science 5th Edition
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
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.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Syntax (Slides mainly based on Tucker.
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.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Syntax – Intro and Overview CS331. Syntax Syntax defines what is grammatically valid in a programming language –Set of grammatical rules –E.g. in English,
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Dr. Philip Cannata 1 Lexical and Syntactic Analysis Chomsky Grammar Hierarchy Lexical Analysis – Tokenizing Syntactic Analysis – Parsing Hmm Concrete Syntax.
1 Programming Languages Fundamentals Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
CPS 506 Comparative Programming Languages Syntax Specification.
CSCE 330 Programming Language Structures Syntax (Slides mainly based on Tucker and Noonan) Fall 2012 A language that is simple to parse for the compiler.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
. 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.
Chapter 1 Introduction Major Data Structures in Compiler
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
Syntax (2).
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R T W O Syntax.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
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.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
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
Compiler Design (40-414) Main Text Book:
CSCE 330 Programming Language Structures Syntax (Slides mainly based on Tucker and Noonan) Fall 2012 A language that is simple to parse for the compiler.
Programming Languages 2nd edition Tucker and Noonan
Semantic Analysis Chapter 4.
A Simple Syntax-Directed Translator
Parsing and Parser Parsing methods: top-down & bottom-up
PROGRAMMING LANGUAGES
Compiler Lecture 1 CS510.
Programming Languages 2nd edition Tucker and Noonan
Programming Languages 2nd edition Tucker and Noonan
Compilers B V Sai Aravind (11CS10008).
R.Rajkumar Asst.Professor CSE
C H A P T E R T W O Syntax.
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Programming Languages 2nd edition Tucker and Noonan
Chapter 10: Compilers and Language Translation
Programming Languages 2nd edition Tucker and Noonan
Faculty of Computer Science and Information System
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth

Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 2.1 Grammars Backus-Naur Form Derivations Parse Trees Associativity and Precedence Ambiguous Grammars 2.2 Extended BNF 2.3 Syntax of a Small Language: Clite Lexical Syntax Concrete Syntax 2.4 Compilers and Interpreters 2.5 Linking Syntax and Semantics Abstract Syntax Abstract Syntax Trees Abstract Syntax of Clite

Copyright © 2006 The McGraw-Hill Companies, Inc. 2.4 Compilers and Interpreters Lexical Analyzer Syntactic Analyzer Semantic Analyzer Code Optimizer Code Generator Tokens Abstract Syntax Machine Code Intermediate Code (IC) Source Program Intermediate Code (IC)

Copyright © 2006 The McGraw-Hill Companies, Inc. Lexer Input: characters Output: tokens Separate: –Speed: 75% of time for non-optimizing –Simpler design –Character sets –End of line conventions

Copyright © 2006 The McGraw-Hill Companies, Inc. Parser Based on BNF/EBNF grammar Input: tokens Output: abstract syntax tree (parse tree) Abstract syntax: parse tree with punctuation, many nonterminals discarded

Copyright © 2006 The McGraw-Hill Companies, Inc. Semantic Analysis Check that all identifiers are declared Perform type checking Insert implied conversion operators (i.e., make them explicit)

Copyright © 2006 The McGraw-Hill Companies, Inc. Code Optimization Evaluate constant expressions at compile-time Reorder code to improve cache performance Eliminate common subexpressions Eliminate unnecessary code

Copyright © 2006 The McGraw-Hill Companies, Inc. Code Generation Output: machine code Instruction selection Register management Peephole optimization

Copyright © 2006 The McGraw-Hill Companies, Inc. Interpreter Replaces last 2 phases of a compiler Input: –Mixed: intermediate code –Pure: stream of ASCII characters Mixed interpreters –Java, Perl, Python, Haskell, Scheme Pure interpreters: –most Basics, shell commands

Copyright © 2006 The McGraw-Hill Companies, Inc. 2.5 Linking Syntax and Semantics Output: parse tree is inefficient Example: Fig. 2.9

Copyright © 2006 The McGraw-Hill Companies, Inc. Parse Tree for z = x + 2*y; Fig. 2.9

Copyright © 2006 The McGraw-Hill Companies, Inc. Finding a More Efficient Tree The shape of the parse tree reveals the meaning of the program. So we want a tree that removes its inefficiency and keeps its shape. –Remove separator/punctuation terminal symbols –Remove all trivial root nonterminals –Replace remaining nonterminals with leaf terminals Example: Fig. 2.10

Copyright © 2006 The McGraw-Hill Companies, Inc. Abstract Syntax Tree for z = x + 2*y; Fig. 2.10

Copyright © 2006 The McGraw-Hill Companies, Inc. Abstract Syntax Pascal while i < n do begin i := i + 1; end; C/C++ while (i < n) { i = i + 1; } Removes “syntactic sugar” and keeps essential elements of a language. E.g., consider the following two equivalent loops: The only essential information in each of these is 1) that it is a loop, 2) that its terminating condition is i < n, and 3) that its body increments the current value of i.

Copyright © 2006 The McGraw-Hill Companies, Inc. Abstract Syntax of Clite Assignments Assignment = Variable target ; Expression source Expression = VariableRef | Value | Binary | Unary VariableRef = Variable | ArrayRef Variable = String id ArrayRef = String id ; Expression index Value = IntValue | BoolValue | FloatValue | CharValue Binary = Operator op ; Expression term1, term2 Unary = UnaryOp op ; Expression term Operator = ArithmeticOp | RelationalOp | BooleanOp IntValue = Integer intValue …

Copyright © 2006 The McGraw-Hill Companies, Inc. Abstract Syntax as Java Classes abstract class Expression { } abstract class VariableRef extends Expression { } class Variable extends VariableRef { String id; } class Value extends Expression { … } class Binary extends Expression { Operator op; Expression term1, term2; } class Unary extends Expression { UnaryOp op; Expression term; }

Copyright © 2006 The McGraw-Hill Companies, Inc. Example Abstract Syntax Tree op term1 term2 Binary node Abstract Syntax Tree for x+2*y (Fig 2.13) Binary Operator Variable Value + 2 y * x

Copyright © 2006 The McGraw-Hill Companies, Inc. Remaining Abstract Syntax of Clite (Declarations and Statements) Fig 2.14