1 October 2, 2015 1 October 2, 2015October 2, 2015October 2, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

Slides:



Advertisements
Similar presentations
Chapter 5: Languages and Grammar 1 Compiler Designs and Constructions ( Page ) Chapter 5: Languages and Grammar Objectives: Definition of Languages.
Advertisements

Lecture # 7 Chapter 4: Syntax Analysis. What is the job of Syntax Analysis? Syntax Analysis is also called Parsing or Hierarchical Analysis. A Parser.
Mooly Sagiv and Roman Manevich School of Computer Science
1 May 22, May 22, 2015May 22, 2015May 22, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
Context-Free Grammars Lecture 7
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Compiler Constreuction 1 Chapter 4 Syntax Analysis Topics to cover: Context-Free Grammars: Concepts and Notation Writing and rewriting a grammar Syntax.
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial)
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
COP4020 Programming Languages
1 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice Grammars and Parsing.
Chapter 4 Chang Chi-Chung
Chapter 3 Chang Chi-Chung Parse tree intermediate representation The Role of the Parser Lexical Analyzer Parser Source Program Token Symbol.
Lexical and syntax analysis
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
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 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
1 October 16, October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
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.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Lesson 5 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 Lecture 5: Syntax Analysis (Section 2.2) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC.
1 November 1, November 1, 2015November 1, 2015November 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
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.
Introduction to Parsing
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Introduction to Compiling
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
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.
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.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
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.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
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
Parsing & Context-Free Grammars
Programming Languages Translator
Lexical and Syntax Analysis
Lecture #12 Parsing Types.
Syntax Analysis Part I Chapter 4
Syntax Analysis Chapter 4.
Syntax Analysis Sections :.
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
CPSC 388 – Compiler Design and Construction
Predictive Parsing Program
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
Presentation transcript:

1 October 2, October 2, 2015October 2, 2015October 2, 2015 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 Syntax Analysis Part I Chapter 4 October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

3 Position of a Parser in the Compiler Model Lexical Analyzer Parser and rest of front-end Source Program Token, tokenval Symbol Table Get next token Lexical errorSyntax error Semantic error Intermediate representation October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

4 October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Keep in mind following questions Parser –T–Twofold: –C–Checking syntax –I–Invoking semantic action Error Handling –T–Two abilities: –I–Identifying errors –L–Locating errors Chomsky Hierarchy –F–From Turing machine –t–to Finite Automaton –F–From non to more restriction

5 The Parser A parser implements a C-F grammar The role of the parser is twofold: 1.To check syntax (= string recognizer) –And to report syntax errors accurately 2.To invoke semantic actions –For static semantics checking, e.g. type checking of expressions, functions, etc. –For syntax-directed translation of the source code to an intermediate representation October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

6 Syntax-Directed Translation One of the major roles of the parser is to produce an intermediate representation (IR) of the source program using syntax-directed translation methods Possible IR output: –Abstract syntax trees (ASTs) –Control-flow graphs (CFGs) with triples, three-address code, or register transfer list notation –WHIRL (SGI Pro64 compiler) has 5 IR levels! October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

7  WHIRL Winning Hierarchical Intermediate Representation 5 Levels: –VH, H, M L, VL –Lowering happens when needed –Each optimization performed at the right level October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

8 Error Handling A good compiler should assist in identifying and locating errors –Lexical errors: important, compiler can easily recover and continue –Syntax errors: most important for compiler, can almost always recover –Static semantic errors: important, can sometimes recover –Dynamic semantic errors: hard or impossible to detect at compile time, runtime checks are required –Logical errors: hard or impossible to detect October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

9 Viable-Prefix Property The viable-prefix property of LL/LR parsers allows early detection of syntax errors –Goal: detection of an error as soon as possible without further consuming unnecessary input –How: detect an error as soon as the prefix of the input does not match a prefix of any string in the language … for (;) … … DO 10 I = 1;0 … Error is detected here Prefix October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

10 Error Recovery Strategies Panic mode –Discard input until a token in a set of designated synchronizing tokens is found Phrase-level recovery –Perform local correction on the input to repair the error Error productions –Augment grammar with productions for erroneous constructs Global correction –Choose a minimal sequence of changes to obtain a global least-cost correction October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

11 Grammars (Recap) Context-free grammar is a 4-tuple G = (N, T, P, S) where –T is a finite set of tokens (terminal symbols) –N is a finite set of nonterminals –P is a finite set of productions of the form    where   (N  T)* N (N  T)* and   (N  T)* –S  N is a designated start symbol October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

12 Notational Conventions Used Terminals a,b,c,…  T specific terminals: 0, 1, id, + Nonterminals A,B,C,…  N specific nonterminals: expr, term, stmt Grammar symbols X,Y,Z  (N  T) Strings of terminals u,v,w,x,y,z  T* Strings of grammar symbols , ,   (N  T)* October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

13 Derivations (Recap) The one-step derivation is defined by  A      where A   is a production in the grammar In addition, we define –  is leftmost  lm if  does not contain a nonterminal –  is rightmost  rm if  does not contain a nonterminal –Transitive closure  * (zero or more steps) –Positive closure  + (one or more steps) The language generated by G is defined by L(G) = {w  T* | S  + w} October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

14 Derivation (Example) Grammar G = ({E}, {+,*,(,),-,id}, P, E) with productions P =E  E + E E  E * E E  ( E ) E  - E E  id E  - E  - id E * EE * E E  + id * id + id E  rm E + E  rm E + id  rm id + id Example derivations: E  * id + id October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

15 Chomsky Hierarchy: Language Classification October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Avram Noam Chomsky is an American linguist, philosopher,, and lecturer. He is a professor emeritus of linguistics at the MIT. Chomsky is well known in the academic and scientific community as the father of modern linguistics. In the 1950s, Chomsky began developing his theory of generative grammar, which has had a profound influence on linguistics. He established the Chomsky hierarchy, a classification of formal languages in terms of their generative power. His naturalistic approach to the study of language has affected the philosophy of language and mind.

16 October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction A grammar G is said to be –Regular if it is right linear where each production is of the form A  w Bor A  w or left linear where each production is of the form A  B wor A  w –Context free if each production is of the form A   where A  N and   (N  T)* –Context sensitive if each production is of the form  A      where A  N, , ,   (N  T)*, |  | > 0 –Unrestricted Chomsky Hierarchy: Language Classification

17 Chomsky Hierarchy L (regular)  L (context free)  L (context sensitive)  L (unrestricted) October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

18 Chomsky Hierarchy L (regular)  L (context free)  L (context sensitive)  L (unrestricted) Where L (T) = { L(G) | G is of type T } That is: the set of all languages generated by grammars G of type T L 1 = { a n b n | n  1 } is context free L 2 = { a n b n c n | n  1 } is context sensitive Every finite language is regular! (construct a FSA for strings in L(G)) Examples: October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction unrestricted context sensitive context free regular

19 Parsing Universal (any C-F grammar) –Cocke-Younger-Kasimi –Earley Top-down (C-F grammar with restrictions) –Recursive descent (predictive parsing) –LL (Left-to-right, Leftmost derivation) methods Bottom-up (C-F grammar with restrictions) –Operator precedence parsing –LR (Left-to-right, Rightmost derivation) methods SLR, canonical LR, LALR October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

20 October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Got it with following questions Parser –T–Twofold: –C–Checking syntax –I–Invoking semantic action Error Handling –T–Two abilities: –I–Identifying errors –L–Locating errors Chomsky Hierarchy –F–From Turing machine –t–to Finite Automaton –F–From non to more restriction

21 Thank you very much! Questions? October 2, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction