LESSON 04.

Slides:



Advertisements
Similar presentations
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
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.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Context-Free Grammars Lecture 7
Chapter 3 Describing Syntax and Semantics Sections 1-3.
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.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
Specifying Languages CS 480/680 – Comparative Languages.
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.
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
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.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Context-Free Grammars
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Grammars CPSC 5135.
PART I: overview material
LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
ISBN Chapter 3 Describing Syntax and Semantics.
11 Chapter 4 Grammars and Parsing Grammar Grammars, or more precisely, context-free grammars, are the formalism for describing the structure of.
# 1 CMPS 450 Syntax-Directed Translations CMPS 450 J. Moloney.
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.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Describing Syntax and Semantics
Syntax and Grammars.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Overview of Previous Lesson(s) Over View  In our compiler model, the parser obtains a string of tokens from the lexical analyzer & verifies that the.
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.
Syntax Analyzer (Parser)
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
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:
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
CS510 Compiler Lecture 4.
Chapter 3 Context-Free Grammar and Parsing
Syntax (1).
Compiler Construction
Chapter 2: A Simple One Pass Compiler
Lecture 7: Introduction to Parsing (Syntax Analysis)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Programming Languages 2nd edition Tucker and Noonan
COMPILER CONSTRUCTION
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
Presentation transcript:

LESSON 04

Overview of Previous Lesson(s)

Over View… Decomposition of a compiler. Symbol Table The symbol table, which stores information about the entire source program, is used by all phases of the compiler.

Over View.. Language can also be classified using generations as well. 1st generation programming language (1GL) Architecture specific binary delivered on Switches, Patch Panels and/or Tape. 2nd generation programming language (2GL) Most commonly use in RISC, CISC and x86 as that is what our embedded systems and desktop computers use.

Over View... 3rd generation programming language (3GL) C, C++, C#, Java, Basic, COBOL, Lisp and ML. 4th generation programming language (4GL) SQL, SAS, R, MATLAB's GUIDE, ColdFusion, CSS.  5th generation programming language (5GL) Prolog, Mercury. 

Over View... Modeling in Compiler Design Compiler design is one of the places where theory has had the most impact on practice. Models that have been found useful include automata, grammars, regular expressions, trees, and many others.

Over View… Optimization is to produce code that is more efficient than the obvious code. Compiler optimizations must meet the following design objectives: The optimization must be correct, that is, preserve the meaning of the compiled program. The optimization must improve the performance of many programs. The compilation time must be kept reasonable.

TODAY’S LESSON

Contents Syntax Director Translator Introduction Syntax Definition Context Free Grammars Derivations Parse Trees Ambiguity Associativity of Operators Operator Precedence

Syntax Directed Translator This section illustrates the compiling techniques by developing a program that translates representative programming language statements into three-address code, an intermediate representation. We will focus on Front end of a compiler Lexical analysis Parsing Intermediate code generation.

Syntax Directed Translator.. Model of a Compiler Front End

Introduction Analysis is organized around the "syntax" of the language to be compiled. The syntax of a programming language describes the proper form of its programs. The semantics of the language defines what its programs mean. For specifying syntax, Context-Free Grammars is used. Also known as BNF (Backus-Naur Form) We start with a syntax-directed translation of an infix expression to postfix form. Infix form: 9 – 5 + 2 to Postfix form: 9 5 – 2 +

Syntax Definition Context Free Grammar is used to specify the syntax of the language. Shortly we can say it “Grammar”. A grammar describes the hierarchical structure of most programming language constructs. Ex. if ( expression ) statement else statement

Syntax Definition.. This rule can be expressed as production by using the variable expr to denote an expression and the variable stmt to denote a statement. stmt -> if ( expr ) stmt else stmt In a production lexical elements like the keyword if, else and the parentheses are called terminals. Variables like expr and stmt represent sequences of terminals and are called nonterminals.

Grammars A context-free grammar has four components A set of tokens (terminal symbols) A set of nonterminals A set of productions A designated start symbol Lets check an example that elaborates these components.

Grammars.. Expressions … 9 – 5 + 2 , 5 – 4 , 8 … 9 – 5 + 2 , 5 – 4 , 8 … Since a plus or minus sign must appear between two digits, we refer to such expressions as lists of digits separated by plus or minus signs. The productions are List -> list + digit P-1 List -> list – digit P-2 List -> digit P-3 Digit -> 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 P-4

Grammars.. Terminals 0,1,2,3,4,5,6,7,8,9 Non-Terminals list , digit Designated Start Symbol list

Derivations 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

Derivations.. Derivation for our example expression. list Start Symbol  list + digit P-1  list - digit + digit P-2  digit - digit + digit P-3  9 - digit + digit P-4  9 - 5 + digit P-4  9 - 5 + 2 P-4 This is an example of leftmost derivation, because we replaced the leftmost nonterminal (underlined) in each step.

Parse Trees Parsing is the problem of taking a string of terminals and figuring out how to derive it from the start symbol of the grammar. If it cannot be derived from the start symbol of the grammar, then reporting syntax errors within the string. Given a context-free grammar, a parse tree according to the grammar is a tree with the following properties: The root is labeled by the start symbol. Each leaf is labeled by a terminal or by ɛ. Each interior node is labeled by a nonterminal. If A  X1 X2 … Xn is a production, then node A has immediate children X1, X2, …, Xn where Xi is a (non)terminal or .

The sequence of leafs is called the yield of the parse tree Parse Trees.. Parse tree of the string 9-5+2 using grammar G list list digit list digit digit The sequence of leafs is called the yield of the parse tree 9 - 5 + 2

Tree Terminology A tree consists of one or more nodes. Exactly one is the root. If node N is the parent of node M, then M is a child of N. The children of one node are called siblings. They have an order, from the left. A node with no children is called a leaf. A descendant of a node N is either N itself, a child of N, a child of a child of N, and so on.

Ambiguity A grammar can have more than one parse tree generating a given string of terminals. Such a grammar is said to be ambiguous. To show that a grammar is ambiguous, all we need to do is find a terminal string that is the yield of more than one parse tree.

Ambiguity.. Consider the Grammar G = [ {string}, {+,-,0,1,2,3,4,5,6,7,8,9}, P, string ] Its productions are string  string + string | string - string | 0 | 1 | … | 9 This grammar is ambiguous, because more than one parse tree represents the string 9-5+2

Ambiguity… Two Parse Trees for 9 – 5 + 2 string string string string - 5 + 2 9 - 5 + 2 Two Parse Trees for 9 – 5 + 2

Associativity of Operators Left-associative operators have left-recursive productions For instance list  list – digit | digit String 9-5-2 has the same meaning as (9-5)-2 Right-associative operators have right-recursive productions For Instance see the grammar below right  letter = right | letter String a=b=c has the same meaning as a=(b=c)

Associativity of Operators..

Operator Precedence Consider the expression 9+5*2. There are two possible interpretations of this expression: (9+5 ) *2 or 9+ ( 5*2) The associativity rules for + and * apply to occurrences of the same operator, so they do not resolve this ambiguity. A grammar for arithmetic expressions can be constructed from a table showing the associativity and precedence of operators.

Operator Precedence.. Lets see an example of four common arithmetic operators and a precedence table, showing the operators in order of increasing precedence. left-associative: + - left-associative: * / Now we create two nonterminals expr and term for the two levels of precedence, and an extra nonterminal factor for generating basic units in expressions. The basic units in expressions are presently digits and parenthesized expressions. factor -> digit I ( expr )

Operator Precedence.. Now consider the binary operators, * and /, that have the highest precedence and left associativity. term - > term * factor | term / factor | factor Similarly, expr generates lists of terms separated by the additive operators. expr -> expr + term I expr – term I term Final grammar is factor -> digit I ( expr )

Operator Precedence.. Ex. String 2+3*5 has the same meaning as 2+(3*5) expr expr term term term factor factor factor number number number 2 + 3 * 5

Associativity & Precedence Table

Thank You