1 February 23, 2016 1 February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.

Slides:



Advertisements
Similar presentations
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
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.
Context-Free Grammars Lecture 7
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.1 Spring 2010 Marco Valtorta
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
COP4020 Programming Languages
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
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.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
1 October 1, October 1, 2015October 1, 2015October 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 October 2, October 2, 2015October 2, 2015October 2, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
1 October 16, October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Context-Free Grammars
Grammars CPSC 5135.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
May 31, May 31, 2016May 31, 2016May 31, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
1 November 1, November 1, 2015November 1, 2015November 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
11 Chapter 4 Grammars and Parsing Grammar Grammars, or more precisely, context-free grammars, are the formalism for describing the structure of.
Syntax Context-Free Grammars, Syntax Trees. CFG for Arithmetic Expressions E::= E op E| (E) | num op ::= + | * num ::= 0 | 1 | 2 | … Backus-Naur Form.
Bernd Fischer RW713: Compiler and Software Language Engineering.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CPS 506 Comparative Programming Languages Syntax Specification.
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
LESSON 04.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
1 January 18, January 18, 2016January 18, 2016January 18, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 CS308 Compiler Theory. 2 Course Information Instructor : –Prof. Minyi Guo –Yao Shen Course.
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 and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
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.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
COMP 3438 – Part II - Lecture 4 Syntax Analysis I Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
Chapter 3 – Describing Syntax
A Simple Syntax-Directed Translator
Constructing Precedence Table
Chapter 3 Context-Free Grammar and Parsing
Chapter 3 – Describing Syntax
Syntax (1).
Context-Free Grammars
CSE 3302 Programming Languages
Compiler Design 4. Language Grammars
Context-Free Grammars
CPSC 388 – Compiler Design and Construction
Context-Free Grammars
Chapter 2: A Simple One Pass Compiler
CSC 4181Compiler Construction Context-Free Grammars
CSC 4181 Compiler Construction Context-Free Grammars
Context-Free Grammars
Context-Free Grammars
Programming Languages 2nd edition Tucker and Noonan
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
Faculty of Computer Science and Information System
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

2 A Simple Syntax-Directed Translator A Simple Syntax-Directed Translator -- One-Pass Compiler to Generate Bytecode for the JVM Chapter 2 February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

3 This chapter contains introductory material to Chapters 3 to 8 of the Dragon book  Text: Compilers -- Principles, Techniques, & Tools (2nd Ed) by Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman Combined with material on the JVM to prepare for the laboratory assignments  FYI: “The Java TM Virtual Machine Specification”, 2nd edition and class handouts February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Overview

4 Building a compiler involves: –Defining the syntax of a programming language –Develop a source code parser: for our compiler we will use predictive parsing –Implementing syntax directed translation to generate intermediate code: our target is the JVM abstract stack machine –Generating Java bytecode for the JVM –Optimize the Java bytecode (optional) February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Building a Simple Compiler

5 Lexical analyzer Syntax-directed translator Character stream Token stream Java bytecode Syntax definition (BNF grammar) Develop parser and code generator for translator JVM specification February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction The Structure of the Compiler

6 February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Keep in mind following questions Syntax definition –What is it? –What is ambiguity? –How to remove ambiguity? Why we need syntax –How to derive from syntax? –What inspires you? What is your reflection –Like it, why? –Hate it, why?

7 Context-free grammar is a 4-tuple with –A set of tokens (terminal symbols) –A set of nonterminals –A set of productions –A designated start symbol February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Syntax Definition

8 list  list + digit list  list - digit list  digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 G = with productions P = Context-free grammar for simple expressions: February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Example Grammars list  [list +|-] digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 list  [list + | -] (0 | 1 | … | 9)

9 Given a CF grammar we can determine the set of all strings (sequences of tokens) generated by the grammar using derivation –We begin with the start symbol –In each step, we replace one nonterminal in the current sentential form with one of the right- hand sides of a production for that nonterminal February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Derivation

10 list  list + digit  list - digit + digit  digit - digit + digit  9 - digit + digit  digit  This is an example leftmost derivation, because we replaced the leftmost nonterminal (underlined) in each step. Likewise, a rightmost derivation replaces the rightmost nonterminal in each step February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Derivation for the Example Grammar

11 The root of the tree is labeled by the start symbol Each leaf of the tree is labeled by a terminal (=token) or  Each interior node is labeled by a nonterminal If A  X 1 X 2 … X n is a production, then node A has immediate children X 1, X 2, …, X n where X i is a (non)terminal or  (  denotes the empty string) February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Parse Trees

12 Parse tree of the string using grammar G list digit list digit The sequence of leafs is called the yield of the parse tree February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Parse Tree for the Example Grammar

13 string  string + string | string - string | 0 | 1 | … | 9 G = with production P = Consider the following context-free grammar: This grammar is ambiguous, because more than one parse tree represents the string February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Ambiguity

14 string February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Ambiguity

15 right  term = right | term left  left + term | term Left-associative operators have left-recursive productions Right-associative operators have right-recursive productions String a=b=c has the same meaning as a=(b=c) String a+b+c has the same meaning as (a+b)+c February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Associativity of Operators

16 expr  expr + term | term term  term * factor | factor factor  number | ( expr ) Operators with higher precedence “bind more tightly” String 2+3*5 has the same meaning as 2+(3*5) expr term factor +23*5 term factor term factor number February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Precedence of Operators

17 stmt  id := expr | if expr then stmt | if expr then stmt else stmt | while expr do stmt | begin opt_stmts end opt_stmts  stmt ; opt_stmts |  February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Syntax of Statements Assignment Selective Iterative Block

18 February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Got it with following questions Syntax definition –W–What is it? –W–What is ambiguity? –H–How to remove ambiguity? Why we need syntax –H–How to derive from syntax? –W–What inspires you? What is your reflection –L–Like it, why? –H–Hate it, why?

19 Thank you very much! Questions? February 23, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Syntax Definition