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.

Slides:



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

Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Mooly Sagiv and Roman Manevich School of Computer Science
Lexical and Syntactic Analysis Here, we look at two of the tasks involved in the compilation process –Given source code, we need to first break it into.
Bottom-Up Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter (modified)
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.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 4: Syntax Specification COMP 144 Programming Language Concepts Spring 2002 Felix.
Chapter 2 A Simple Compiler
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
1 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice Grammars and Parsing.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
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.
1 Lecture 11: Semantic Analysis (Section ) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Syntax and Semantics Structure of programming languages.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
Copyright © 2009 Elsevier Chapter 2 :: Programming Language Syntax Programming Language Pragmatics Michael L. Scott.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
1 October 2, October 2, 2015October 2, 2015October 2, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Parsing G Programming Languages May 24, 2012 New York University Chanseok Oh
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Syntax Specification (Sections ) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos at UNC.
Syntax and Semantics Structure of programming languages.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
COP4020 Programming Languages Syntax Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
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.
1 Parsers and Grammar. 2 Categories of Grammar Rules  Declarations or definitions. AttributeDeclaration ::= [ final ] [ static ] [ access ] datatype.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Introduction to Compiling
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 4.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
Syntax Analyzer (Parser)
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 due on Monday February 8 th Name and date your submission Submit electronically in Homework Server.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
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:
2016/7/9Page 1 Lecture 11: Semester Review COMP3100 Dept. Computer Science and Technology United International College.
Syntax and Semantics Structure of programming languages.
Chapter 3 – Describing Syntax
CS 326 Programming Languages, Concepts and Implementation
Programming Languages Translator
CS510 Compiler Lecture 4.
50/50 rule You need to get 50% from tests, AND
Lexical and Syntax Analysis
Chapter 3 – Describing Syntax
Context-free Languages
CS 3304 Comparative Languages
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
CPSC 388 – Compiler Design and Construction
Lecture 7: Introduction to Parsing (Syntax Analysis)
Programming Language Syntax 5
Presentation transcript:

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 Chapel Hill

2 Review: Compilation/Interpretation Compiler or Interpreter Translation Execution Source Code Target Code Interpre-tation

3 Review: Syntax Analysis Compiler or Interpreter Translation Execution Source Code Specifying the formSpecifying the form of a programming language –Tokens »Regular Expressions (also F.A.s & Reg. Grammars) –Syntax »Context-Free Grammars (also P.D.A.s) Target Code

4 Phases of Compilation

5 Syntax Analysis Syntax:Syntax: –Webster’s definition: 1 a : the way in which linguistic elements (as words) are put together to form constituents (as phrases or clauses) The syntax of a programming languageThe syntax of a programming language –Describes its form »Organization of tokens »Context Free Grammars (CFGs) –Must be recognizable by compilers and interpreters »Parsing »LL and LR parsers

6 Context Free Grammars CFGsCFGs –Add recursion to regular expressions »Nested constructions –Notation expression  identifier | number | - expression | ( expression ) | ( expression ) | expression operator expression | expression operator expression operator  + | - | * | / »Terminal symbols »Non-terminal symbols »Production rule (i.e. substitution rule) terminal symbol  terminal and non-terminal symbols

7 Parsing Parsing an arbitrary Context Free GrammarParsing an arbitrary Context Free Grammar –O(n 3 ) –Too slow for large programs Linear-time parsingLinear-time parsing –LL parsers (a ‘Left-to-right, Left-most’ derivation) »Recognize LL grammar »Use a top-down strategy –LR parsers (a ‘Left-to-right, Right-most’ derivation) »Recognize LR grammar »Use a bottom-up strategy

8 Parsing example Example: comma-separated list of identifierExample: comma-separated list of identifier –CFG id_list  id id_list_tail id_list_tail , id_list_tail id_list_tail  ; –Parsing A, B, C;

9 Top-down derivation of A, B, C; CFG Left-to-right, Left-most derivation LL(1) parsing

10 Top-down derivation of A, B, C; CFG

11 Bottom-up parsing of A, B, C; CFG Left-to-right, Right-most derivation LR parsing (a shift-reduce parser)

12 Bottom-up parsing of A, B, C; CFG

13 CFG

14 LR Parsing vs. LL Parsing LLLL –A ‘top-down’ or ‘predictive’ parser –Predict needed productions based on the current left-most non-terminal in the tree and the current input token –The top-of-stack contains the left-most non-terminal –The stack contains a record of what the parser expects to see LRLR –A ‘bottom-up’ or shift-reduce parser –Shifts tokens onto the stack until it recognizes a right-hand side then reduces those tokens to their left-hand side –The stack contains a record of what the parser has already seen

15 An appropriate LR Grammar id_list  id_list_prefix ; id_list_prefix  id_list_prefix, id  id  id This grammar can’t be parsed top-down! Problems for LL grammars: - left recursion, example above - common prefixes, example: stmt  id := expr | id (arg_list)

16 LL(1) Grammar for the Calculator Language

17 LR(1) Grammar for the Calculator Language

18 Hierarchy of Linear Parsers Basic containment relationshipBasic containment relationship –All CFGs can be recognized by LR parser –Only a subset of all the CFGs can be recognized by LL parsers LL parsing CFGs LR parsing

19 Bigger Picture Chomsky Hierarchy of GrammarsChomsky Hierarchy of Grammars RegularGrammar Context Free Grammar Context Sensitive Grammar Unrestricted Grammar

20 Implementation of an LL Parser Two options:Two options: –A recursive descent parser (section 2.2.3) »For LL grammars only –Parse table and a driver (section 2.2.5) »LR parsers covered in section 2.2.6

21 Recursive Descent Parser Example LL(1) grammarLL(1) grammar

22 Recursive Descent Parser Example Outline ofOutline of recursive parser –This parser only verifies syntax –match is the scanner

23 Recursive Descent Parser Example

24

25 A program that develops recursive decent parsers: JavaCC JavaCC

26 Semantic Analysis Compiler or Interpreter Translation Execution Source Code Specifying the meaningSpecifying the meaning of a programming language –Attribute Grammars Target Code