1 COMP313A Programming Languages Syntax Analysis (2)

Slides:



Advertisements
Similar presentations
lec02-parserCFG March 27, 2017 Syntax Analyzer
Advertisements

Lecture # 11 Grammar Problems.
Session 14 (DM62) / 15 (DM63) Recursive Descendent Parsing.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Chapter 4 Lexical Analysis.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
1 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
Chapter 3: Formal Translation Models
1 Chapter 3 Context-Free Grammars and Parsing. 2 Parsing: Syntax Analysis decides which part of the incoming token stream should be grouped together.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Compiler Principle and Technology Prof. Dongming LU Mar. 7th, 2014.
Syntax and Semantics Structure of programming languages.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
1 Week 3 Questions / Concerns What’s due: Lab1b due Friday at midnight Lab1b check-off next week (schedule will be announced on Monday) Homework #2 due.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
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.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Context-Free Grammars
Context-Free Grammars and Parsing
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 4. Syntax Analysis (1). 2 Application of a production  A  in a derivation step  i   i+1.
Left Recursion Lecture 7 Fri, Feb 4, 2005.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
COMP 3438 – Part II - Lecture 4 Syntax Analysis I Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Syntax Analysis By Noor Dhia Left Recursion: Example1: S → S0s1s | 01 The grammar after eliminate left recursion is: S → 01 S’ S' → 0s1sS’
Last Chapter Review Source code characters combination lexemes tokens pattern Non-Formalization Description Formalization Description Regular Expression.
Introduction to Parsing
CSE 3302 Programming Languages
lec02-parserCFG May 8, 2018 Syntax Analyzer
Programming Languages Translator
Chapter 3 Context-Free Grammar and Parsing
Chapter 4 Top-Down Parsing Part-1 September 8, 2018
Syntax versus Semantics
Compiler Construction (CS-636)
Syntax Analysis Sections :.
CSE 3302 Programming Languages
Syntax Analysis Sections :.
Lecture 3: Introduction to Syntax (Cont’)
Parsing & Context-Free Grammars Hal Perkins Autumn 2011
Compiler Design 4. Language Grammars
Top-Down Parsing CS 671 January 29, 2008.
Some CFL Practicalities
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Chapter 2: A Simple One Pass Compiler
Lecture 7: Introduction to Parsing (Syntax Analysis)
Lecture 8: Top-Down Parsing
R.Rajkumar Asst.Professor CSE
Introduction to Parsing
Introduction to Parsing
LL and Recursive-Descent Parsing
BNF 9-Apr-19.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
lec02-parserCFG May 27, 2019 Syntax Analyzer
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

1 COMP313A Programming Languages Syntax Analysis (2)

2 More on ambiguous grammars Predictive parsing Nonrecursive Predictive Parsing

3 Parsing token sequence: id + id * id E  E + E | E * E | ( E ) | - E | id

4 Ambiguous Grammars A grammar that generates a string with 2 distinct parse trees is called an ambiguous grammar –2+3*4 = 2 + (3*4) = 14 –2+3*4 = (2+3) * 4 = 20 Our experience of maths says interpretation 1 is correct but the grammar does not express this: –E  E + E | E * E | ( E ) | - E | id

5 Removing Ambiguity Two methods 1. Disambiguating Rules –+ve leaves grammar unchanged –-ve grammar is not sole source of syntactic knowledge 2. Rewrite the Grammar Using knowledge of the meaning that we want to use later in the translation into object code to guide grammar alteration

6 Precedence E  E addop E | Term Addop  + | - Term  Term * Term | Factor Factor  ( exp ) | number | id Operators of equal precedence are grouped together at the same ‘level’ of the grammar  ’precedence cascade’

7 Associativity ? 30 or 40 Subtraction is left associative, left to right (=30) E  E addop E | Term Does not tell us how to split up E  E addop Term | Term Forces left associativity via left recursion Precedence & associativity remove ambiguity of arithmetic expressions –Which is what our maths teachers took years telling us!

8 Ambiguous grammars Statement -> If-statement | other If-statement -> if (Exp) Statement | if (Exp) Statement else Statement Exp -> 0 | 1 Parse if (0) if (1) other else other

9 Removing ambiguity Statement -> Matched-stmt | Unmatched-stmt Matched-stmt -> if (Exp) Matched-stmt else Matched-stmt | other Unmatched-stmt ->if (Exp) Statement | if (Exp) Matched-stmt else Unmatched-stmt

10 Predictive Parsing Top down parsing LL(1) parsing Table driven predictive parsing versus recursive descent parsing No backtracking E -> E + T | T T -> T * F | F F -> (E) | id

11 Two grammar problems Eliminating left recursion A -> A  |  A ->  A’ A’ ->  A’ |  Example E -> E + T | T T -> T * F | F F -> (E) | id The general case A -> A  1 | A  2 | …| A  m |  1 |  2 | …|  n

12 Two grammar problems Eliminating left recursion involving derivations of two or more steps S -> Aa | b A -> Ac | Sd |  A -> Ac | Aad | bd | 

13 Two grammar problems… Left factoring Stmt -> if Exp then Stmt else Stmt | if Expr then Stmt A ->  1 |  2 A ->  A’ A’ ->  1 |  2

14 exercises Eliminate left recursion from the following grammars. a)S->(L) | a L->L,S | S b)Bexpr ->Bexpr or Bterm | Bterm Bterm -> Bterm and Bfactor | Bfactor Bfactor -> not Bfactor | (Bexpr) | true | false