Programming Language Syntax 5

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

lec02-parserCFG March 27, 2017 Syntax Analyzer
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Top-Down Parsing.
Parsing III (Eliminating left recursion, recursive descent parsing)
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
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.
Comp 311 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright August 28, 2009.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
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.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Introduction to Parsing
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Comp 411 Principles of Programming Languages Lecture 3 Parsing
Parsing #1 Leonidas Fegaras.
lec02-parserCFG May 8, 2018 Syntax Analyzer
Parsing — Part II (Top-down parsing, left-recursion removal)
CS 326 Programming Languages, Concepts and Implementation
Programming Languages Translator
CS510 Compiler Lecture 4.
Unit-3 Bottom-Up-Parsing.
CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Textbook:Modern Compiler Design
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
CS 404 Introduction to Compiler Design
4 (c) parsing.
Syntax Analysis Sections :.
Parsing Techniques.
CS 3304 Comparative Languages
Syntax Analysis Sections :.
Programming Language Syntax 6
Programming Language Syntax 7
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Syntax-Directed Translation
Top-Down Parsing CS 671 January 29, 2008.
Programming Language Syntax 2
COP4020 Programming Languages
Compiler Design 7. Top-Down Table-Driven Parsing
Chapter 2: A Simple One Pass Compiler
Lecture 7: Introduction to Parsing (Syntax Analysis)
Lecture 8: Top-Down Parsing
R.Rajkumar Asst.Professor CSE
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Parsing IV Bottom-up Parsing
Parsing — Part II (Top-down parsing, left-recursion removal)
LL and Recursive-Descent Parsing
4d Bottom Up Parsing.
Kanat Bolazar February 16, 2010
BNF 9-Apr-19.
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Recursive descent parsing
Nonrecursive Predictive Parsing
4d Bottom Up Parsing.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
lec02-parserCFG May 27, 2019 Syntax Analyzer
Recursive descent parsing
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Programming Language Syntax 5 http://flic.kr/p/zCyMp

What’s a context-free grammar (CFG)? What distinguishes it from other grammars? What is a context-free language? What is a parser?

What’s a context-free grammar (CFG)? Rules for how to generate sentences in some lang. What distinguishes it from other grammars? The types of rules that can be expressed What is a context-free language? A lang. that can be generated with a CFG What is a parser? A program that recognizes sentences in a lang.

What’s does it mean to derive a string from a CFG? What is a parse tree? What is a left-most derivation? What is a right-most derivation?

CFG: String to derive: Derivation:

Parse Tree: Graphical Representation of Derivation

Right-most versus Left-most derivation

What’s does it mean to derive a string from a CFG? Iteratively expand CFG rules to yield string What is a parse tree? Graphical representation of a derivation Also, an output of a parser What is a left-most derivation? Expand left-most non-terminal first What is a right-most derivation? Expand right-most non-terminal first

What is an LL parser? What is an LR parser? What is an LL grammar? LR grammar? Why do we care about LL/LR parsers?

What is an LL parser? Parser that reads input left-to-right and produces left-most derivation What is an LR parser? Same as LL parser, except produces right-most derivation What is an LL grammar? LR grammar? Grammar that can be parsed by an LL/LR parser Why do we care about LL/LR parsers? More efficient implementations – O(n) vs O(n3)

Let’s delve deeper into LL parsers (ANTLR is one) Recall this example…

Start with the start rule (id_list) id(A) , id(B) , id(C) ; Start with the start rule (id_list)

Predict replacement for id_list id(A) , id(B) , id(C) ; Predict replacement for id_list

id(A) , id(B) , id(C) ; ? Only one choice

Read token and match terminal What if terminal doesn’t match? id(A) , id(B) , id(C) ; Read token and match terminal What if terminal doesn’t match?

Predict replacement for id_list_tail id(A) , id(B) , id(C) ; Predict replacement for id_list_tail

id(A) , id(B) , id(C) ; Can’t decide? Peek!

id(A) , id(B) , id(C) ; ?

Read token, match terminal id(A) , id(B) , id(C) ; ? Read token, match terminal

Read token, match terminal id(A) , id(B) , id(C) ; Read token, match terminal

Predict replacement for id_list_tail id(A) , id(B) , id(C) ; Predict replacement for id_list_tail

id(A) , id(B) , id(C) ; Can’t decide? Peek!

id(A) , id(B) , id(C) ; ?

Read token, match terminal id(A) , id(B) , id(C) ; ? Read token, match terminal

Read token, match terminal id(A) , id(B) , id(C) ; Read token, match terminal

Predict replacement for id_list_tail id(A) , id(B) , id(C) ; Predict replacement for id_list_tail

id(A) , id(B) , id(C) ; Can’t decide? Peek!

id(A) , id(B) , id(C) ;

Read token, match terminal id(A) , id(B) , id(C) ; Read token, match terminal

Done! No more non-terminals. No more input. id(A) , id(B) , id(C) ; Done! No more non-terminals. No more input.

Recall that peeking may be restricted 0 tokens of look-ahead (predict without peeking) LL(1) Up to 1 token of look-ahead (like the last example) LL(2) Up to 2 tokens of look-ahead LL(*) Unlimited tokens of look-ahead (ANTLR 3)

If you’re writing a grammar from which to generate an LL(1) parser, you must have an LL(1) grammar Otherwise, you get conflict errors…

Consider what happens if an LL(1) parser tries to parse this non-LL(1) grammar

Can we predict the next replacement? Yes! id_list_prefix ; id(A) , id(B) , id(C) ; id_list Can we predict the next replacement?

Can we predict the next replacement? id(A) , id(B) , id(C) ; id_list id_list_prefix ; Can we predict the next replacement? Nope. Let’s peek.

Can we predict the next replacement? id(A) , id(B) , id(C) ; id_list id_list_prefix ; Can we predict the next replacement? Hm. Still can’t. The problem is the left recursion in id_list_prefix—both its alternatives start with id!

But how do we know if our grammar is an LL(1) grammar But how do we know if our grammar is an LL(1) grammar? To find out, let’s look in more detail at how to implement a parser

Consider this LL(1) grammar

Here is a recursive descent parser that recognizes the grammar *See handout

Observe the pattern… One procedure for each production … …

Each procedure has a case stmt that considers the next input token Observe the pattern… Each procedure has a case stmt that considers the next input token … …

Observe the pattern… One case per right-hand side (+error) … …

Each case tries to predict the right-hand side to expand Observe the pattern… Each case tries to predict the right-hand side to expand … …

Observe the pattern… … … Based on prediction: Call appropriate procedure for non-terminals Call match() for terminals … …

Activity: Execute recursive-descent parser on each of these inputs Create a parse tree as you go Each time a procedure is called, hang a node off the calling procedure Be sure to keep track of the call stack

Solution

Next time: How to predict … …