Chapter 2: A Simple One Pass Compiler

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

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.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Context-Free Grammars Lecture 7
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.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
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 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
1 COMP313A Programming Languages Syntax Analysis (2)
By Neng-Fa Zhou Programming language syntax 4 Three aspects of languages –Syntax How are sentences formed? –Semantics What does a sentence mean? –Pragmatics.
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.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
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.
Chapter 2: A Simple One Pass Compiler
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Introduction to Parsing
CSE 3302 Programming Languages
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
A Simple Syntax-Directed Translator
Introduction to Parsing
CS 404 Introduction to Compiler Design
CS510 Compiler Lecture 4.
Chapter 3 Context-Free Grammar and Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Textbook:Modern Compiler Design
Chapter 3 – Describing Syntax
What does it mean? Notes from Robert Sebesta Programming Languages
Syntax Analysis Chapter 4.
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Compiler Construction
Syntax versus Semantics
CS 363 Comparative Programming Languages
Syntax Analysis Sections :.
Parsing Techniques.
CSE 3302 Programming Languages
Syntax Analysis Sections :.
Department of Software & Media Technology
Top-Down Parsing CS 671 January 29, 2008.
CPSC 388 – Compiler Design and Construction
Syntax-Directed Definition
Chapter 2: A Simple One Pass Compiler
Programming Language Syntax 2
Thinking about grammars
Lecture 7: Introduction to Parsing (Syntax Analysis)
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
Designing a Predictive Parser
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSC 4181 Compiler Construction Context-Free Grammars
Theory of Computation Lecture #
BNF 9-Apr-19.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Discrete Maths 13. Grammars Objectives
Thinking about grammars
Context Free Grammars-II
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:

Chapter 2: A Simple One Pass Compiler

The Entire Compilation Process Grammars for Syntax Definition Syntax-Directed Translation Parsing - Top Down & Predictive Pulling Together the Pieces The Lexical Analysis Process Symbol Table Considerations A Brief Look at Code Generation Concluding Remarks/Looking Ahead

Grammars for Syntax Definition A Context-free Grammar (CFG) Is Utilized to Describe the Syntactic Structure of a Language A CFG Is Characterized By: 1. A Set of Tokens or Terminal Symbols 2. A Set of Non-terminals 3. A Set of Production Rules Each Rule Has the Form NT  {T, NT}* 4. A Non-terminal Designated As the Start Symbol

Grammars for Syntax Definition Example CFG list  list + digit list  list - digit list  digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 (the “|” means OR) (So we could have written list  list + digit | list - digit | digit )

Grammars are Used to Derive Strings: Using the CFG defined on the previous slide, we can derive the string: 9 - 5 + 2 as follows: list  list + digit  list - digit + digit  digit - digit + digit  9 - digit + digit  9 - 5 + digit  9 - 5 + 2 P1 : list  list + digit P2 : list  list - digit P3 : list  digit P4 : digit  9 P4 : digit  5 P4 : digit  2

Grammars are Used to Derive Strings: This derivation could also be represented via a Parse Tree (parents on left, children on right) list  list + digit  list - digit + digit  digit - digit + digit  9 - digit + digit  9 - 5 + digit  9 - 5 + 2 list digit 9 5 2 - +

A More Complex Grammar What is this grammar for ? block  begin opt_stmts end opt_stmts  stmt_list |  stmt_list  stmt_list ; stmt | stmt What is this grammar for ? What does “” represent ? What kind of production rule is this ?

Defining a Parse Tree More Formally, a Parse Tree for a CFG Has the Following Properties: Root Is Labeled With the Start Symbol Leaf Node Is a Token or  Interior Node (Now Leaf) Is a Non-Terminal If A  x1x2…xn, Then A Is an Interior; x1x2…xn Are Children of A and May Be Non-Terminals or Tokens

Other Important Concepts Ambiguity Two derivations (Parse Trees) for the same token string. string - 9 + 5 2 string + 2 - 5 9 Grammar: string  string + string | string – string | 0 | 1 | …| 9 Why is this a Problem ?

Other Important Concepts Associativity of Operators Left vs. Right right letter c b a = list digit 9 5 2 - + list  list + digit | | list - digit | digit digit  0 | 1 | 2 | …| 9 right  letter = right | letter letter  a | b | c | …| z

Embedding Associativity The language of arithmetic expressions with + - (ambiguous) grammar that does not enforce associativity string  string + string | string – string | 0 | 1 | …| 9 non-ambiguous grammar enforcing left associativity (parse tree will grow to the left) string  string + digit | string - digit | digit digit  0 | 1 | 2 | …| 9 non-ambiguous grammar enforcing right associativity (parse tree will grow to the right) string  digit + string | digit - string | digit

Other Important Concepts Operator Precedence What does 9 + 5 * 2 mean? ( ) * / + - is precedence order Typically This can be incorporated into a grammar via rules: expr  expr + term | expr – term | term term  term * factor | term / factor | factor factor  digit | ( expr ) digit  0 | 1 | 2 | 3 | … | 9 Precedence Achieved by: expr & term for each precedence level Rules for each are left recursive or associate to the left