1 Terminology l Statement ( 敘述 ) »declaration, assignment containing expression ( 運算式 ) l Grammar ( 文法 ) »a set of rules specify the form of legal statements.

Slides:



Advertisements
Similar presentations
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Advertisements

Compilers and Language Translation
Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
Mooly Sagiv and Roman Manevich School of Computer Science
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
CPSC Compiler Tutorial 9 Review of Compiler.
Introduction The compilation approach uses a program called a compiler, which translates programs written in a high-level programming language into machine.
1 Chapter 5 Compilers Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj.
Chapter 4 Lexical and Syntax Analysis Sections 1-4.
ISBN Chapter 4 Lexical and Syntax Analysis.
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.
Chapter 2 A Simple Compiler
Chapter 2 A Simple Compiler
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Compilers -- Basic functions
Invitation to Computer Science 5th Edition
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
Syntax and Semantics Structure of programming languages.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
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.
PART I SISTEM UTILITIES Lecture 6 Compilers Ştefan Stăncescu 1.
Programming Languages Third Edition Chapter 6 Syntax.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
Bernd Fischer RW713: Compiler and Software Language Engineering.
CPS 506 Comparative Programming Languages Syntax Specification.
Syntactic Analysis Operator-Precedence Parsing Recursive-Descent Parsing.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Introduction to Compiling
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 4.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
ISBN Chapter 4 Lexical and Syntax Analysis.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.3-1 Language Specification and Translation Lecture 8.
Compiler Syntactic Analysis r Two general classes of parsing techniques m Bottom-up (Operator-Precedence parsing) Begin with the terminal nodes.
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.
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:
Compiler Summary Lexical analysis—scanner – String of characters  token – Finite automata. Syntactical analysis – parser – Sequence of tokens –> language.
CS 3304 Comparative Languages
System Software Theory (5KS03).
Chapter 3 – Describing Syntax
Programming Languages Translator
Parsing and Parser Parsing methods: top-down & bottom-up
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
PROGRAMMING LANGUAGES
CS416 Compiler Design lec00-outline September 19, 2018
Introduction CI612 Compiler Design CI612 Compiler Design.
CPSC 388 – Compiler Design and Construction
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
R.Rajkumar Asst.Professor CSE
CS 3304 Comparative Languages
CS 3304 Comparative Languages
CS416 Compiler Design lec00-outline February 23, 2019
High-Level Programming Language
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Presentation transcript:

1 Terminology l Statement ( 敘述 ) »declaration, assignment containing expression ( 運算式 ) l Grammar ( 文法 ) »a set of rules specify the form of legal statements l Syntax ( 語法 ) vs. Semantics ( 語意 ) »Example: assuming J,K:integer and X,Y:float »I:=J+K vs I:=X+Y l Compilation: 編譯 »matching statements written by the programmer to structures defined by the grammar and generating the appropriate object code

2 System Software l Assembler l Loader and Linker l Macro Processor l Compiler l Operating System l Other System Software »RDBS »Text Editors »Interactive Debugging System

3 Basic Compiler l Lexical analysis -- scanner »scanning the source statement, recognizing and classifying the various tokens l Syntactic analysis -- parser »recognizing the statement as some language construct l Code generation --

4 Scanner PROGRAM STATS VAR SUM, SUMSQ, I SUM := 0 ; SUMSQ := READ ( VALUE ) ;

5 Parser l Grammar: a set of rules »Backus-Naur Form (BNF) »Ex: Figure 5.2 l Terminology »Define symbol ::= »Nonterminal symbols <> »Alternative symbols | »Terminal symbols

6 Simplified Pascal Grammar

7 Parser l ::= READ ( ) l ::= id |,id l ::= id := l ::= | + | - l ::= | * | DIV l ::= id | int | l READ(VALUE) l SUM := 0 l SUM := SUM + VALUE l MEAN := SUM DIV 100

8 Syntax Tree

9 Syntax Tree for Program 5.1

10 Lexical Analysis l Function »scanning the program to be compiled and recognizing the tokens that make up the source statements l Tokens »Tokens can be keywords, operators, identifiers, integers, floating-point numbers, character strings, etc. »Each token is usually represented by some fixed-length code, such as an integer, rather than as a variable-length character string (see Figure 5.5) »Token type, Token specifier (value) (see Figure 5.6)

11 Scanner Output l Token specifier »identifier name, integer value l Token coding scheme »Figure 5.5

12 Example - Figure 5.6

13 Token Recognizer l By grammar » ::= | | » ::= A | B | C | D | … | Z » ::= 0 | 1 | 2 | 3 | … | 9 l By scanner - modeling as finite automata »Figure 5.8(a)

14 Recognizing Identifier l Identifiers allowing underscore (_) »Figure 5.8(b)

15 Recognizing Integer l Allowing leading zeroes »Figure 5.8(c) l Disallowing leading zeroes »Figure 5.8(d)

16 Scanner -- Implementation l Figure 5.10 (a) »Algorithmic code for identifer recognition l Tabular representation of finite automaton for Figure 5.9

17 Syntactic Analysis l Recognize source statements as language constructs or build the parse tree for the statements »bottom-up: operator-precedence parsing »top-down:: recursive-descent parsing

18 Operator-Precedence Parsing l Operator »any terminal symbol (or any token) l Precedence »* » + »+ « * l Operator-precedence »precedence relations between operators

Precedence Matrix for the Fig. 5.2

20 Operator-Precedence Parse Example BEGIN READ ( VALUE ) ;

(i) … id1 := id2 DIV     (ii) … id1 := DIV int -      (iii) … id1 := DIV -     (iv) … id1 := - id3 *      (v) … id1 := - * id4 ;     

(vi) … id1 := - * ;      (vi) … id1 := - ;     (vii) … id1 := ;   

23 Operator-Precedence Parsing l Bottom-up parsing l Generating precedence matrix »Aho et al. (1988)

24 Shift-reduce Parsing with Stack l Figure 5.14

25 Recursive-Descent Parsing l Each nonterminal symbol in the grammar is associated with a procedure l ::= READ ( ) l ::= | | | l Left recursion » ::= | ; l Modification » ::= {; }

26

27 Recursive-Descent Parse of READ

28 Simplified Pascal Grammar for Recursive- Descent Parser

29

30

31

32

33

34

35

36

37

38

39