ParsingParsing. 2 Front-End: Parser  Checks the stream of words and their parts of speech for grammatical correctness scannerparser source code tokens.

Slides:



Advertisements
Similar presentations
Programming Languages Third Edition Chapter 6 Syntax.
Advertisements

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
ISBN Chapter 3 Describing Syntax and Semantics.
Compiler Construction Dr. Naveed Ejaz Lecture 2. 2 Two-pass Compiler Front End Back End source code IR machine code errors.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
PZ02A - Language translation
Context-Free Grammars Lecture 7
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #5 Introduction.
COP4020 Programming Languages
Compiler Construction CS 606 Sohail Aslam Lecture 2.
1 Chapter 3 Context-Free Grammars and Parsing. 2 Parsing: Syntax Analysis decides which part of the incoming token stream should be grouped together.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
EECS 6083 Intro to Parsing Context Free Grammars
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Introduction to Parsing Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Syntax and Backus Naur Form
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
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
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
PART I: overview material
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
作者 : 陳鍾誠 單位 : 金門技術學院資管系 URL : 日期 : 2016/6/4 程式語言的語法 Grammar.
Bernd Fischer RW713: Compiler and Software Language Engineering.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
Introduction to Parsing
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
ISBN Chapter 3 Describing Syntax and Semantics.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Syntax Analysis – Part I EECS 483 – Lecture 4 University of Michigan Monday, September 17, 2006.
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.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Module 2 Compiler and their Working Software Construction Lecture 10,11 and 12.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
C H A P T E R T W O Syntax and Semantic. 2 Introduction Who must use language definitions? Other language designers Implementors Programmers (the users.
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 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #6 Parsing.
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
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Introduction to Parsing
CS510 Compiler Lecture 4.
Chapter 3 – Describing Syntax
What does it mean? Notes from Robert Sebesta Programming Languages
Compiler Design 4. Language Grammars
Lecture 7: Introduction to Parsing (Syntax Analysis)
CSC 4181Compiler Construction Context-Free Grammars
Introduction to Parsing
Introduction to Parsing
CSC 4181 Compiler Construction Context-Free Grammars
High-Level Programming Language
Presentation transcript:

ParsingParsing

2 Front-End: Parser  Checks the stream of words and their parts of speech for grammatical correctness scannerparser source code tokens IR errors

3 Front-End: Parser  Determines if the input is syntactically well formed scannerparser source code tokens IR errors

4 Front-End: Parser  Guides context-sensitive (“semantic”) analysis (type checking) scannerparser source code tokens IR errors

5 Front-End: Parser  Builds IR for source program scannerparser source code tokens IR errors

6 Syntactic Analysis  Natural language analogy: consider the sentence Hewrotetheprogram

7 Syntactic Analysis Hewrotetheprogram nounverbarticlenoun

8 Syntactic Analysis Hewrotetheprogram nounverbarticlenoun subjectpredicateobject

9 Syntactic Analysis  Natural language analogy Hewrotetheprogram nounverbarticlenoun subjectpredicateobject sentence

10 Syntactic Analysis  Programming language if( b <= 0 )a = b bool expr assignment if-statement

11 Syntactic Analysis syntax errors int* foo(int i, int j)) { for(k=0; i j; ) fi( i > j ) return j; }

Compiler Construction Sohail Aslam Lecture 11

13 Syntactic Analysis int* foo(int i, int j)) { for(k=0; i j; ) fi( i > j ) return j; } extra parenthesis Missing expression not a keyword

14 Semantic Analysis  Grammatically correct Hewrotethecomputer nounverbarticlenoun subjectpredicateobject sentence

15 Semantic Analysis  semantically (meaning) wrong! Hewrotethe computer nounverbarticlenoun subjectpredicateobject sentence

16 Semantic Analysis int* foo(int i, int j) { for(k=0; i < j; j++ ) if( i < j-2 ) sum = sum+i return sum; } undeclared var return type mismatch

17 Role of the Parser  Not all sequences of tokens are program.  Parser must distinguish between valid and invalid sequences of tokens.

18 Role of the Parser  Not all sequences of tokens are program.  Parser must distinguish between valid and invalid sequences of tokens.

19 Role of the Parser What we need  An expressive way to describe the syntax  An acceptor mechanism that determines if input token stream satisfies the syntax

20 Role of the Parser What we need  An expressive way to describe the syntax  An acceptor mechanism that determines if input token stream satisfies the syntax

21 Role of the Parser What we need  An expressive way to describe the syntax  An acceptor mechanism that determines if input token stream satisfies the syntax

22 Study of Parsing  Parsing is the process of discovering a derivation for some sentence

23 Study of Parsing  Mathematical model of syntax – a grammar G.  Algortihm for testing membership in L(G).

24 Study of Parsing  Mathematical model of syntax – a grammar G.  Algortihm for testing membership in L(G).

25 Context Free Grammars A CFG is a four tuple G=(S,N,T,P)  S is the start symbol  N is a set of non-terminals  T is a set of terminals  P is a set of productions

26 Why Not Regular Expressions? Reason: regular languages do not have enough power to express syntax of programming languages.

27 Limitations of Regular Languages  Finite automaton can’t remember number of times it has visited a particular state

28 Example of CFG  Context-free syntax is specified with a CFG

29 Example of CFG  Example SheepNoise → SheepNoise baa | baa  This CFG defines the set of noises sheep make

30 Example of CFG  We can use the SheepNoise grammar to create sentences  We use the productions as rewriting rules

31 Example of CFG SheepNoise → SheepNoise baa | baa RuleSentential Form -SheepNoise 2baa

32 Example of CFG SheepNoise → SheepNoise baa | baa RuleSentential Form - SheepNoise 1SheepNoise baa 2baa

33 Example of CFG And so on... RuleSentential Form - SheepNoise 1SheepNoise baa 1SheepNoise baa baa 2baa baa baa

34 Example of CFG  While it is cute, this example quickly runs out intellectual steam  To explore uses of CFGs, we need a more complex grammar

35 Example of CFG  While it is cute, this example quickly runs out intellectual steam  To explore uses of CFGs, we need a more complex grammar

36 More Useful Grammar 1expr → expr op expr 2 | num 3 | id 4op → + 5 | – 6 | * 7 | /

37 Backus-Naur Form (BNF)  Grammar rules in a similar form were first used in the description of the Algol60 Language.

38 Backus-Naur Form (BNF)  The notation was developed by John Backus and adapted by Peter Naur for the Algol60 report.  Thus the term Backus-Naur Form (BNF)

39 Backus-Naur Form (BNF)  The notation was developed by John Backus and adapted by Peter Naur for the Algol60 report.  Thus the term Backus-Naur Form (BNF)

40 Derivation:Derivation:  Let us use the expression grammar to derive the sentence x – 2 * y

41 Derivation: x – 2 * y RuleSentential Form - expr 1expr op expr 2 op expr 5 – expr 1 – expr op expr

42 Derivation: x – 2 * y RuleSentential Form 2 – op expr 6 –  expr 3 – 

43 DerivationDerivation  Such a process of rewrites is called a derivation.  Process or discovering a derivations is called parsing

44 DerivationDerivation  Such a process of rewrites is called a derivation.  Process or discovering a derivations is called parsing

45 DerivationDerivation We denote this derivation as: expr → * id – num * id

46 DerivationsDerivations  At each step, we choose a non-terminal to replace  Different choices can lead to different derivations.

47 DerivationsDerivations  At each step, we choose a non-terminal to replace  Different choices can lead to different derivations.

48 DerivationsDerivations  Two derivations are of interest 1.Leftmost derivation 2.Rightmost derivation

49 DerivationsDerivations  Leftmost derivation: replace leftmost non- terminal (NT) at each step  Rightmost derivation: replace rightmost NT at each step

50 DerivationsDerivations  Leftmost derivation: replace leftmost non- terminal (NT) at each step  Rightmost derivation: replace rightmost NT at each step

51 DerivationsDerivations  The example on the preceding slides was leftmost derivation  There is also a rightmost derivation

52 Rightmost Derivation RuleSentential Form - expr 1expr op expr 3expr op 6 expr  1 expr op expr 

53 Derivation: x – 2 * y RuleSentential Form 2 expr op  5 expr –  3 – 

54 DerivationsDerivations  In both cases we have expr → * id – num  id

55 DerivationsDerivations  The two derivations produce different parse trees.  The parse trees imply different evaluation orders!

56 DerivationsDerivations  The two derivations produce different parse trees.  The parse trees imply different evaluation orders!