# Outline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013

Size: px
Start display at page:

Transcription

1 Outline Top Down Parsing Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form Recursive-descent parsing 1 CS 412/413 Spring 2008 Introduction to Compilers 2 Where We Are SLL(1) Parsing Source code (character stream) Token stream Abstract Syntax Tree (AST) if (b == 0) a = b; if ( b == 0 ) a = b ; if == b 0 = a b Lexical Analysis Syntax Analysis (Parsing) Semantic Analysis SLL(1) parsing goal Determine a Leftmost derivation of the input while reading the input from Left to right while looking ahead at most 1 input token Beginning with the start symbol, grow a parse tree topdown in left-to-right preorder while looking ahead at most 1 input token beyond the input prefix matched by the parse tree derived so far CS 412/413 Spring 2008 Introduction to Compilers 3 CS 412/413 Spring 2008 Introduction to Compilers 4 1

2 Expression Grammar Consider the grammar E (E + E) int and the string (2+3) E (E + E) (2 + E) (2 + 3) How could we decide between E int E (E+E) in the first derivation step? Examine the next unread token in the input If it is an integer, use the production E int If it is (, use the production E (E+E) Otherwise, parsing error. This rule works for all derivation steps, not just the first. Next unread token in input is called look-ahead For this grammar, a look-ahead of one token is enough to let us parse strings in the language CS 412/413 Spring 2008 Introduction to Compilers 5 Non-SLL(1) Grammar Consider the grammar S E + S E E num (S) and the two derivations S E (S) (E) (3) S E+S (S)+S (E)+E (3)+E (3)+4 How could we decide between S E S E+S as the first derivation step based on finite number of lookahead symbols? We can t! The sample grammar is not SLL(1) The sample grammar is not SLL(k) for any k. CS 412/413 Spring 2008 Introduction to Compilers 6 Making a grammar SLL(1) S E+S S E E num E (S) S ES S S +S E num E (S) Problem: can t decide which S production to apply until we see symbol after first expression Left-factoring: Factor common S prefix E, add new non-terminal S for what follows that prefix Also: convert left-recursion to right-recursion Predictive Parsing SLL(1) grammar G = V,,S, For a given nonterminal, the look-ahead symbol uniquely determines the production to apply Top-down parsing a.k.a. predictive parsing Driven by predictive parsing table that maps V ( { ) to the production to apply (or error) CS 412/413 Spring 2008 Introduction to Compilers 7 CS 412/413 Spring 2008 Introduction to Compilers 8 2

3 Using Table E int (E+E) Using Table S ES S +S E num (S) E ( (1+2) (E+E) ( (1+2) (E+E) 1 (1+2) (int+e) 1 (1+2) (int+e) + (1+2) (int+e) 2 (1+2) (int+int) 2 (1+2) (int+int) ) (1+2) (int+int) EOF (1+2) E int + ( ) EOF int (E+E) S ( (1+2+(3+4))+5 ES ( (1+2+(3+4))+5 (S)S 1 (1+2+(3+4))+5 (ES )S 1 (1+2+(3+4))+5 (1S )S + (1+2+(3+4))+5 (1+S)S 2 (1+2+(3+4))+5 (1+ES )S 2 (1+2+(3+4))+5 (1+2S )S + (1+2+(3+4))+5 num + ( ) EOF S ES ES S +S E num (S) CS 412/413 Spring 2008 Introduction to Compilers 9 CS 412/413 Spring 2008 Introduction to Compilers 10 How to Implement, Version 1 A table-driven parser void parse(nonterminal A) { int i; let A X 0 X 1 X n = TABLE[A,token] for (i=0; i<=n; i++) { if (X i in ) if (token == X i ) token = input.read(); else throw new ParseError(); else parse(x i ); return; CS 412/413 Spring 2008 Introduction to Compilers 11 How to Implement, Version 2 Convert table into a recursive-descent parser num + ( ) S ES ES S +S E num (S) Three procedures: parse_s, parse_s, parse_e CS 412/413 Spring 2008 Introduction to Compilers 12 3

4 Recursive-Descent Parser void parse_s () { lookahead token switch (token) { case num: parse_e(); parse_s (); return; case ( : parse_e(); parse_s (); return; default: throw new ParseError(); num + ( ) S ES ES S +S E num (S) Recursive-Descent Parser void parse_s () { switch (token) { case + : token = input.read(); parse_s(); return; case ) : return; case EOF: return; default: throw new ParseError(); num + ( ) S ES ES S +S E num (S) CS 412/413 Spring 2008 Introduction to Compilers 13 CS 412/413 Spring 2008 Introduction to Compilers 14 Recursive-Descent Parser void parse_e() { switch (token) { case number: token = input.read(); return; case ( : token = input.read(); parse_s(); if (token!= ) ) throw new ParseError(); token = input.read(); return; default: throw new ParseError(); num + ( ) S ES ES S +S E num (S) CS 412/413 Spring 2008 Introduction to Compilers 15 Call Tree = Parse Tree parse_s parse_e parse_s parse_s parse_s parse_e parse_s parse_s parse_e parse_s parse_s parse_e parse_s parse_s (1+2+(3+4))+5 S E S ( S ) + S E S S E S 2 + S E S ( S ) E S 3 + S E 4 CS 412/413 Spring 2008 Introduction to Compilers 16 4

5 How to Construct Parsing Tables Needed: algorithm for automatically generating a predictive parsing table from a grammar Easy for Bali S ES S +S E num (S)? N + ( ) S ES ES S +S E N (S) CS 412/413 Spring 2008 Introduction to Compilers 17 Filling in parsing table First, consider grammars that do not have - productions. Suppose we have a non-terminal E for which all productions begin with a terminal symbol (eg) E axy byy stx Easy to fill in parsing table in Table[E,a] put rule E axy etc. What if we have a non-terminal in which some rules begin with a non-terminal? (eg) E axy YZ For what look-ahead symbols should we use the rule E YZ? Obvious answer: for any terminal symbol that begins a string that can be derived from Y 18 Formalization Formalize intuition using FIRST relation FIRST (E) (recall: is the set of terminals) terminal symbol t is in FIRST(E) iff there is a string starting with t that can be derived from E useful to extend FIRST to terminal symbols by definition FIRST(t) = {t in general, FIRST( ) for string contains terminal t if there is a string starting with t that can be derived from Example S AB BA A tb a B xa FIRST(A) = {t,a FIRST(B) = {x FIRST(S) = {t,a,x General picture Need to compute three relations called NULLABLE, FIRST and FOLLOW NULLABLE: set of non-terminals that can be rewritten to the empty string FIRST( ) T U { if can be rewritten to a string starting with terminal t, then t is in FIRST( ) if can be rewritten to, then is in FIRST( ) FOLLOW(A) T set of terminals that can follow A in a sentential form

6 Constructing parsing table Enter production A Y 1 Y n into Table[A,t] for all t such that t NULLABLE and FIRST G = <N,T,P,S> (N: set of non-terminals, T: set of terminals, P: set of productions, S:start symbol) Notation: t will stand for an element of T, will denote a string of terminals and non-terminals (possibly empty) NULLABLE N 1.. 1,.. FIRST(A) T U { 1.. 1,, 1 Extend FIRST to strings FOLLOW Helper function: + 1 binary operation on subsets of T U {e concatenate and truncate to 1 symbol {,a,b + 1 {,a = {,a,b FIRST( ) = { FIRST(t) = {t FIRST(Y 1 Y n ) = FIRST(Y 1 ) + 1 FIRST(Y 2 ) + 1 FIRST(Y n ) FOLLOW(A) T U {\$ \$

7 Non-trivial example Grammar for arithmetic expressions S E \$ E E+T T T T*F F F (E) int + * ( ) int \$ Grammar is not LL(1) S S E \$ S E \$ Massaged grammar S E \$ E T E E E T E E T E E + T E T F T E + T E E E T * F T F (E) int T T F T T F T Nullable = {E,T FIRST T T T *FT T T E= {(,int FOLLOW E ={\$,) F F (E) F int T = {\$,) Non-SLL(1) grammars Construction of predictive parse table for grammar results in conflicts S S+S S*S num FIRST(S+S) = FIRST(S*S) = FIRST(num) = { num S num + * num, S+S, S*S CS 412/413 Spring 2008 Introduction to Compilers 26 Summary SLL(k) grammars left-to-right scanning leftmost derivation can determine what production to apply from the next k symbols Can automatically build predictive parsing tables Predictive parsers Can be easily built for SLL(1) grammars from the parsing tables Also called recursive-descent, or top-down parsers Assignment For Bali grammar, we can write simple recursivedescent parser that consists of a set of mutually recursive procedures one procedure for each non-terminal in the grammar responsible for reading in a substring and parsing it as that non-terminal body of procedure is a switch/case statement that looks at the next unread terminal symbol and decides which production to use for that non-terminal Hand-crafted recursive-descent parsers can handle some non-sll(1) grammars using ad hoc techniques more difficult to do in table-driven approach CS 412/413 Spring 2008 Introduction to Compilers 27 7

8 Helper class: SamTokenizer Read the on-line code for Tokenizer: interface SamTokenizer: code Code lets you open file for input: SamTokenizer f = new SamTokenizer(String-for-file-name) examine what the next thing in file is: f.peekatkind:() TokenType TokenType: enum {INTEGER, FLOAT, WORD, OPERATOR, INTEGER: such as 3, -34, 46 WORD: such as x, r45, y78z (variable name in Java) OPERATOR: such as +, -, *, (, ), etc.. read next thing from file (or throw TokenizerException): f.getint/peekint () int f.getword/peekword:() String f.getop/peekop:() char get eats up token from file, while peek does not advance the pointer into the file Useful methods in SamTokenizer class: f.check(char c): char boolean Example: f.check( * ); //true if next thing in input is * Check if next thing in input is c if so, eat it up and return true otherwise, return false f.check(string s): String boolean Example of its use: f.check( if ); Check if next word in input matches s if so, eat it up and return true otherwise, return false f.checkint(): () boolean check if next token is an integer and if so, eat it up and return true otherwise, return false f.match(char c): char void like f.check but throws TokenizerException if next token in input is not c f.match(string s): string void (eg) f.match( if ) Parser for simple expressions Expression integer Expression ( Expression + Expression ) Input: file Output: true if a file contains a single expression as defined by this grammar, false otherwise Note: file must contain exactly one expression File: (2+3) (3+4) will return false Parser for expression language static boolean expparser(string filename) {//parser for expression in file try { SamTokenizer f = new SamTokenizer (filename); return getexp(f) && (f.peekatkind() == Tokenizer.TokenType.EOF) ;//must be at EOF catch (Exception e) { System.out.println("Aaargh"); return false; static boolean getexp(samtokenizer f) { switch (f.peekatkind()) { case INTEGER: //E -> integer {f.checkint(); return true; case OPERATOR: //E ->(E+E) return f.check('(') && getexp(f) && f.check('+') && getexp(f) && f.check(')'); default: return false; 8

9 Note on boolean operators Java supports two kinds of boolean operators: E1 & E2: Evaluate both E1 and E2 and compute their conjunction (i.e., and ) E1 && E2: Evaluate E1. If E1 is false, E2 is not evaluated, and value of expression is false. If E1 is true, E2 is evaluated, and value of expression is the conjunction of the values of E1 and E2. In our parser code, we use && if f.check( ( ) returns false, we simply return false without trying to read anything more from input file. This gives a graceful way to handling errors. Tracing recursive calls to getexp Modifying parser to do SaM code generation Let us modify the parser so that it generates SaM code to evaluate arithmetic expressions: (eg) 2 : PUSHIMM 2 STOP (2 + 3) : PUSHIMM 2 PUSHIMM 3 ADD STOP Idea Recursive method getexp should return a string containing SaM code for expression it has parsed. Top-level method expparser should tack on a STOP command after code it receives from getexp. Method getexp generates code in a recursive way: For integer i, it returns string PUSHIMM + i + \n For (E1 + E2), recursive calls return code for E1 and E2 say these are strings S1 and S2 method returns S1 + S2 + ADD\n 9

10 CodeGen for expression language static String expcodegen(string filename) {//returns SaM code for expression in file try { SamTokenizer f = new SamTokenizer (filename); String pgm = getexp(f); return pgm + "STOP\n"; catch (Exception e) { System.out.println("Aaargh"); return "STOP\n"; static String getexp(samtokenizer f) { switch (f.peekatkind()) { case INTEGER: //E -> integer return "PUSHIMM " + f.getint() + "\n"; case OPERATOR: //E ->(E+E) { f.match('('); // must be ( String s1 = getexp(f); f.match('+'); //must be + String s2 = getexp(f); f.match(')'); //must be ) return s1 + s2 + "ADD\n"; default: return "ERROR\n"; Tracing recursive calls to getexp PUSHIMM 34 PUSHIMM 3 PUSHIMM 23 ADD PUSHIMM 23 PUSHIMM 34 PUSHIMM 3 PUSHIMM 34 PUSHIMM 23 ADD ADD Top-Down Parsing We can use recursive descent to build an abstract syntax tree too Creating the AST abstract class Expr { class Add extends Expr { Expr left, right; Add(Expr L, Expr R) { left = L; right = R; class Num extends Expr { int value; Num (int v) { value = v; Class Hierarchy Num Expr Add CS 412/413 Spring 2008 Introduction to Compilers 39 CS 412/413 Spring 2008 Introduction to Compilers 40 10

11 AST Representation Creating the AST ( (3 + 4)) + 5 Add Add Num (5) Num(1) Add Num(2) Add Just add code to each parsing routine to create the appropriate nodes! Works because parse tree and call tree have same shape parse_s, parse_s, parse_e all return an Expr: Num(3) Num(4) How can we generate this structure during recursive-descent parsing? void parse_e() void parse_s() void parse_s () Expr parse_e() Expr parse_s() Expr parse_s () CS 412/413 Spring 2008 Introduction to Compilers 41 CS 412/413 Spring 2008 Introduction to Compilers 42 AST Creation: parse_e Conclusion Expr parse_e() { switch(token) { case num: // E num Expr result = Num (token.value); token = input.read(); return result; case ( : // E ( S ) token = input.read(); Expr result = parse_s(); if (token!= ) ) throw new ParseError(); token = input.read(); return result; default: throw new ParseError(); There is a systematic way of generating parsing tables for recursivedescent parsers Recursive descent parsers were among the first parsers invented by compiler writers Ideally, we would like to be able generate parsers directly from the grammar software maintenance would be much easier maintain the parser-generator for everyone maintain specification of your grammar Today we have lots of tools that can generate parsers automatically from many grammars Javacc, ANTLR: produce recursive descent parsers from suitable grammars CS 412/413 Spring 2008 Introduction to Compilers 43 11

### Application of recursion. Grammars and Parsing. Recursive grammar. Grammars

Application of recursion Grammars and Parsing So far, we have written recursive programs on integers. Let us now consider a new application, grammars and parsing, that shows off the full power of recursion.

### CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

CS1622 Lecture 9 Parsing (4) CS 1622 Lecture 9 1 Today Example of a recursive descent parser Predictive & LL(1) parsers Building parse tables CS 1622 Lecture 9 2 A Recursive Descent Parser. Preliminaries

### 8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

8 Parsing Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces strings A parser constructs a parse tree for a string

### Top down vs. bottom up parsing

Parsing A grammar describes the strings that are syntactically legal A recogniser simply accepts or rejects strings A generator produces sentences in the language described by the grammar A parser constructs

### Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

Building a Parser III CS164 3:30-5:00 TT 10 Evans 1 Overview Finish recursive descent parser when it breaks down and how to fix it eliminating left recursion reordering productions Predictive parsers (aka

### LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Predictive Parsers LL(k) Parsing Can we avoid backtracking? es, if for a given input symbol and given nonterminal, we can choose the alternative appropriately. his is possible if the first terminal of

### Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

LL(k) Grammars We need a bunch of terminology. For any terminal string a we write First k (a) is the prefix of a of length k (or all of a if its length is less than k) For any string g of terminal and

### Compilers. Predictive Parsing. Alex Aiken

Compilers Like recursive-descent but parser can predict which production to use By looking at the next fewtokens No backtracking Predictive parsers accept LL(k) grammars L means left-to-right scan of input

### MIT Top-Down Parsing. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

MIT 6.035 Top-Down Parsing Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Orientation Language specification Lexical structure regular expressions Syntactic structure

### Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Top-Down Parsing and Intro to Bottom-Up Parsing Lecture 7 1 Predictive Parsers Like recursive-descent but parser can predict which production to use Predictive parsers are never wrong Always able to guess

### Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Top-Down Parsing and Intro to Bottom-Up Parsing Lecture 7 1 Predictive Parsers Like recursive-descent but parser can predict which production to use Predictive parsers are never wrong Always able to guess

### CSCI312 Principles of Programming Languages

Copyright 2006 The McGraw-Hill Companies, Inc. CSCI312 Principles of Programming Languages! LL Parsing!! Xu Liu Derived from Keith Cooper s COMP 412 at Rice University Recap Copyright 2006 The McGraw-Hill

### Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

Syntax Analysis Martin Sulzmann Martin Sulzmann Syntax Analysis 1 / 38 Syntax Analysis Objective Recognize individual tokens as sentences of a language (beyond regular languages). Example 1 (OK) Program

### Abstract Syntax Trees & Top-Down Parsing

Review of Parsing Abstract Syntax Trees & Top-Down Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree

### Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing Review of Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree

### Abstract Syntax Trees & Top-Down Parsing

Review of Parsing Abstract Syntax Trees & Top-Down Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree

### Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

C12/1 Introduction to Compilers and Translators pring 00 Outline More tips forll1) grammars Bottom-up parsing LR0) parser construction Lecture 5: Bottom-up parsing Lecture 5 C 12/1 pring '00 Andrew Myers

### Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Parsing Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students

### Question Points Score

CS 453 Introduction to Compilers Midterm Examination Spring 2009 March 12, 2009 75 minutes (maximum) Closed Book You may use one side of one sheet (8.5x11) of paper with any notes you like. This exam has

### 1 Introduction. 2 Recursive descent parsing. Predicative parsing. Computer Language Implementation Lecture Note 3 February 4, 2004

CMSC 51086 Winter 2004 Computer Language Implementation Lecture Note 3 February 4, 2004 Predicative parsing 1 Introduction This note continues the discussion of parsing based on context free languages.

### CSE 401 Midterm Exam Sample Solution 2/11/15

Question 1. (10 points) Regular expression warmup. For regular expression questions, you must restrict yourself to the basic regular expression operations covered in class and on homework assignments:

### LL parsing Nullable, FIRST, and FOLLOW

EDAN65: Compilers LL parsing Nullable, FIRST, and FOLLOW Görel Hedin Revised: 2014-09- 22 Regular expressions Context- free grammar ATribute grammar Lexical analyzer (scanner) SyntacKc analyzer (parser)

### The procedure attempts to "match" the right hand side of some production for a nonterminal.

Parsing A parser is an algorithm that determines whether a given input string is in a language and, as a side-effect, usually produces a parse tree for the input. There is a procedure for generating a

### Syntax Analysis. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Syntax Analysis Björn B. Brandenburg The University of North Carolina at Chapel Hill Based on slides and notes by S. Olivier, A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts. The Big Picture Character

### JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

JavaCC Parser The Compilation Task Input character stream Lexer stream Parser Abstract Syntax Tree Analyser Annotated AST Code Generator Code CC&P 2003 1 CC&P 2003 2 Automated? JavaCC Parser The initial

### Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Parsing III (Top-down parsing: recursive descent & LL(1) ) (Bottom-up parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,

### CA Compiler Construction

CA4003 - Compiler Construction David Sinclair A top-down parser starts with the root of the parse tree, labelled with the goal symbol of the grammar, and repeats the following steps until the fringe of

### Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Roadmap > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing The role of the parser > performs context-free syntax analysis > guides

### Syntax Analysis, III Comp 412

COMP 412 FALL 2017 Syntax Analysis, III Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp

### COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]

### Syntax Analysis. The Big Picture. The Big Picture. COMP 524: Programming Languages Srinivas Krishnan January 25, 2011

Syntax Analysis COMP 524: Programming Languages Srinivas Krishnan January 25, 2011 Based in part on slides and notes by Bjoern Brandenburg, S. Olivier and A. Block. 1 The Big Picture Character Stream Token

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Parsing CMSC 330 - Spring 2017 1 Recall: Front End Scanner and Parser Front End Token Source Scanner Parser Stream AST Scanner / lexer / tokenizer converts

### CS 132 Compiler Construction, Fall 2012 Instructor: Jens Palsberg Multiple Choice Exam, Nov 6, 2012

CS 132 Compiler Construction, Fall 2012 Instructor: Jens Palsberg Multiple Choice Exam, Nov 6, 2012 ID Name This exam consists of 22 questions. Each question has four options, exactly one of which is correct,

### Announcements. Application of Recursion. Motivation. A Grammar. A Recursive Grammar. Grammars and Parsing

Grammars and Lecture 5 CS211 Fall 2005 CMS is being cleaned If you did not turn in A1 and haven t already contacted David Schwartz, you need to contact him now Java Reminder Any significant class should

### PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering

TEST 1 Date : 24 02 2015 Marks : 50 Subject & Code : Compiler Design ( 10CS63) Class : VI CSE A & B Name of faculty : Mrs. Shanthala P.T/ Mrs. Swati Gambhire Time : 8:30 10:00 AM SOLUTION MANUAL 1. a.

### Concepts of Programming Languages Recitation 1: Predictive Parsing

Concepts of Programming Languages Recitation 1: Predictive Parsing Oded Padon odedp@mail.tau.ac.il Reference: Modern Compiler Implementation in Java by Andrew W. Appel Ch. 3 Administrative Course website

### Syntax Analysis, III Comp 412

Updated algorithm for removal of indirect left recursion to match EaC3e (3/2018) COMP 412 FALL 2018 Midterm Exam: Thursday October 18, 7PM Herzstein Amphitheater Syntax Analysis, III Comp 412 source code

### Chapter 3. Parsing #1

Chapter 3 Parsing #1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and generates Abstract Syntax Trees (ASTs)

### 3. Parsing. Oscar Nierstrasz

3. Parsing Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/ http://www.cs.purdue.edu/homes/hosking/

### Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Susan Eggers 1 CSE 401 Syntax Analysis/Parsing Context-free grammars (CFG s) Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax

### CS502: Compilers & Programming Systems

CS502: Compilers & Programming Systems Top-down Parsing Zhiyuan Li Department of Computer Science Purdue University, USA There exist two well-known schemes to construct deterministic top-down parsers:

### Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Type Inference Systems CS412/CS413 Introduction to Compilers Tim Teitelbaum Type inference systems define types for all legal programs in a language Type inference systems are to type-checking: As regular

### Lexical and Syntax Analysis. Top-Down Parsing

Lexical and Syntax Analysis Top-Down Parsing Easy for humans to write and understand String of characters Lexemes identified String of tokens Easy for programs to transform Data structure Syntax A syntax

### Syntactic Analysis. Top-Down Parsing

Syntactic Analysis Top-Down Parsing Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in Compilers class at University of Southern California (USC) have explicit permission to make

### CS 230 Programming Languages

CS 230 Programming Languages 10 / 16 / 2013 Instructor: Michael Eckmann Today s Topics Questions/comments? Top Down / Recursive Descent Parsers Top Down Parsers We have a left sentential form xa Expand

### Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

Comp 411 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright January 11, 2019 Top Down Parsing What is a context-free grammar (CFG)? A recursive definition of a set of strings; it is

### LECTURE 7. Lex and Intro to Parsing

LECTURE 7 Lex and Intro to Parsing LEX Last lecture, we learned a little bit about how we can take our regular expressions (which specify our valid tokens) and create real programs that can recognize them.

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Context Free Grammars and Parsing 1 Recall: Architecture of Compilers, Interpreters Source Parser Static Analyzer Intermediate Representation Front End Back

### Compiler construction in4303 lecture 3

Compiler construction in4303 lecture 3 Top-down parsing Chapter 2.2-2.2.4 Overview syntax analysis: tokens AST program text lexical analysis language grammar parser generator tokens syntax analysis AST

### EDA180: Compiler Construc6on. Top- down parsing. Görel Hedin Revised: a

EDA180: Compiler Construc6on Top- down parsing Görel Hedin Revised: 2013-01- 30a Compiler phases and program representa6ons source code Lexical analysis (scanning) Intermediate code genera6on tokens intermediate

### Compilers. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers Parsing Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Next step text chars Lexical analyzer tokens Parser IR Errors Parsing: Organize tokens into sentences Do tokens conform

### Course Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend:

Course Overview Introduction (Chapter 1) Compiler Frontend: Today Lexical Analysis & Parsing (Chapter 2,3,4) Semantic Analysis (Chapter 5) Activation Records (Chapter 6) Translation to Intermediate Code

### Peace cannot be kept by force; it can only be achieved by understanding. Albert Einstein

Semantics COMP360 Peace cannot be kept by force; it can only be achieved by understanding. Albert Einstein Snowflake Parser A recursive descent parser for the Snowflake language is due by noon on Friday,

### Extra Credit Question

Top-Down Parsing #1 Extra Credit Question Given this grammar G: E E+T E T T T * int T int T (E) Is the string int * (int + int) in L(G)? Give a derivation or prove that it is not. #2 Revenge of Theory

### Parsing Part II (Top-down parsing, left-recursion removal)

Parsing Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

### Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

Syntax Syntax Syntax defines what is grammatically valid in a programming language Set of grammatical rules E.g. in English, a sentence cannot begin with a period Must be formal and exact or there will

### Administrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.

Administrativia Building a Parser III CS164 3:30-5:00 10 vans WA1 due on hu PA2 in a week Slides on the web site I do my best to have slides ready and posted by the end of the preceding logical day yesterday,

### CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

CSE P 501 Compilers LR Parsing Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 D-1 Agenda LR Parsing Table-driven Parsers Parser States Shift-Reduce and Reduce-Reduce conflicts UW CSE P 501 Spring 2018

### Context-free grammars (CFG s)

Syntax Analysis/Parsing Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax tree (AST) AST: captures hierarchical structure of

### CS Parsing 1

CS414-20034-03 Parsing 1 03-0: Parsing Once we have broken an input file into a sequence of tokens, the next step is to determine if that sequence of tokens forms a syntactically correct program parsing

### CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 2: Lexical Analysis 23 Jan 08

CS412/413 Introduction to Compilers Tim Teitelbaum Lecture 2: Lexical Analysis 23 Jan 08 Outline Review compiler structure What is lexical analysis? Writing a lexer Specifying tokens: regular expressions

### Types of parsing. CMSC 430 Lecture 4, Page 1

Types of parsing Top-down parsers start at the root of derivation tree and fill in picks a production and tries to match the input may require backtracking some grammars are backtrack-free (predictive)

### Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

Eliminating Ambiguity Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity. Example: consider the following grammar stat if expr then stat if expr then stat else stat other One can

### Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

Parsing #1 Leonidas Fegaras CSE 5317/4305 L3: Parsing #1 1 Parser source file get next character scanner get token parser AST token A parser recognizes sequences of tokens according to some grammar and

### CSE 3302 Programming Languages Lecture 2: Syntax

CSE 3302 Programming Languages Lecture 2: Syntax (based on slides by Chengkai Li) Leonidas Fegaras University of Texas at Arlington CSE 3302 L2 Spring 2011 1 How do we define a PL? Specifying a PL: Syntax:

SYNTAX ANALYSIS 1. Define parser. Hierarchical analysis is one in which the tokens are grouped hierarchically into nested collections with collective meaning. Also termed as Parsing. 2. Mention the basic

### COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview Tokens and regular expressions Syntax and context-free grammars Grammar derivations More about parse trees Top-down and bottom-up

### Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

Grammars & Parsing Lecture 12 CS 2112 Fall 2018 Motivation The cat ate the rat. The cat ate the rat slowly. The small cat ate the big rat slowly. The small cat ate the big rat on the mat slowly. The small

### Introduction to Parsing. Lecture 8

Introduction to Parsing Lecture 8 Adapted from slides by G. Necula Outline Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Languages and Automata Formal languages

### Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008. Outline. Top-down versus Bottom-up Parsing. Recursive Descent Parsing. Left Recursion Removal. Left Factoring. Predictive Parsing. Introduction.

### Lexical and Syntax Analysis

Lexical and Syntax Analysis In Text: Chapter 4 N. Meng, F. Poursardar Lexical and Syntactic Analysis Two steps to discover the syntactic structure of a program Lexical analysis (Scanner): to read the input

### CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

Chapter 4 Lexical and Syntax Analysis Introduction - Language implementation systems must analyze source code, regardless of the specific implementation approach - Nearly all syntax analysis is based on

### BSCS Fall Mid Term Examination December 2012

PUNJAB UNIVERSITY COLLEGE OF INFORMATION TECHNOLOGY University of the Punjab Sheet No.: Invigilator Sign: BSCS Fall 2009 Date: 14-12-2012 Mid Term Examination December 2012 Student ID: Section: Morning

### Lexical and Syntax Analysis

Lexical and Syntax Analysis (of Programming Languages) Top-Down Parsing Lexical and Syntax Analysis (of Programming Languages) Top-Down Parsing Easy for humans to write and understand String of characters

### Compiler construction lecture 3

Compiler construction in4303 lecture 3 Top-down parsing Chapter 2.2-2.2.4 Overview syntax analysis: tokens AST language grammar parser generator program text lexical analysis tokens syntax analysis AST

### Syntax. In Text: Chapter 3

Syntax In Text: Chapter 3 1 Outline Syntax: Recognizer vs. generator BNF EBNF Chapter 3: Syntax and Semantics 2 Basic Definitions Syntax the form or structure of the expressions, statements, and program

### Part 3. Syntax analysis. Syntax analysis 96

Part 3 Syntax analysis Syntax analysis 96 Outline 1. Introduction 2. Context-free grammar 3. Top-down parsing 4. Bottom-up parsing 5. Conclusion and some practical considerations Syntax analysis 97 Structure

### Compiler Design 1. Top-Down Parsing. Goutam Biswas. Lect 5

Compiler Design 1 Top-Down Parsing Compiler Design 2 Non-terminal as a Function In a top-down parser a non-terminal may be viewed as a generator of a substring of the input. We may view a non-terminal

### The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

ICOM 4036 Programming Languages Lexical and Syntax Analysis Lexical Analysis The Parsing Problem Recursive-Descent Parsing Bottom-Up Parsing This lecture covers review questions 14-27 This lecture covers

### Left to right design 1

Left to right design 1 Left to right design The left to right design method suggests that the structure of the program should closely follow the structure of the input. The method is effective when the

### Topdown parsing with backtracking

Top down parsing Types of parsers: Top down: repeatedly rewrite the start symbol; find a left-most derivation of the input string; easy to implement; not all context-free grammars are suitable. Bottom

### Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Section A 1. What do you meant by parser and its types? A parser for grammar G is a program that takes as input a string w and produces as output either a parse tree for w, if w is a sentence of G, or

### A simple syntax-directed

Syntax-directed is a grammaroriented compiling technique Programming languages: Syntax: what its programs look like? Semantic: what its programs mean? 1 A simple syntax-directed Lexical Syntax Character

### Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Parsing: continued David Notkin Autumn 2008 Parsing Algorithms Earley s algorithm (1970) works for all CFGs O(N 3 ) worst case performance O(N 2 ) for unambiguous grammars Based on dynamic programming,

### Fall Compiler Principles Lecture 2: LL parsing. Roman Manevich Ben-Gurion University of the Negev

Fall 2017-2018 Compiler Principles Lecture 2: LL parsing Roman Manevich Ben-Gurion University of the Negev 1 Books Compilers Principles, Techniques, and Tools Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman

### COP4020 Programming Assignment 2 Spring 2011

COP4020 Programming Assignment 2 Spring 2011 Consider our familiar augmented LL(1) grammar for an expression language (see Syntax lecture notes on the LL(1) expression grammar): ->

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

### Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 1. Top-Down Parsing. Lect 5. Goutam Biswas

Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 1 Top-Down Parsing Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 2 Non-terminal as a Function In a top-down parser a non-terminal may be viewed

### CS2210: Compiler Construction Syntax Analysis Syntax Analysis

Comparison with Lexical Analysis The second phase of compilation Phase Input Output Lexer string of characters string of tokens Parser string of tokens Parse tree/ast What Parse Tree? CS2210: Compiler

### Syntax-Directed Translation. Lecture 14

Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik) 9/27/2006 Prof. Hilfinger, Lecture 14 1 Motivation: parser as a translator syntax-directed translation stream of tokens parser ASTs,

### CS 4120 Introduction to Compilers

CS 4120 Introduction to Compilers Andrew Myers Cornell University Lecture 6: Bottom-Up Parsing 9/9/09 Bottom-up parsing A more powerful parsing technology LR grammars -- more expressive than LL can handle

### Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3 Describing Syntax and Semantics ISBN 0-321-49362-1 Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Copyright 2009 Addison-Wesley. All

### QUESTIONS RELATED TO UNIT I, II And III

QUESTIONS RELATED TO UNIT I, II And III UNIT I 1. Define the role of input buffer in lexical analysis 2. Write regular expression to generate identifiers give examples. 3. Define the elements of production.

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 5: Syntax Analysis (Parsing) Zheng (Eddy) Zhang Rutgers University January 31, 2018 Class Information Homework 1 is being graded now. The sample solution

### Programming Language Syntax and Analysis

Programming Language Syntax and Analysis 2017 Kwangman Ko (http://compiler.sangji.ac.kr, kkman@sangji.ac.kr) Dept. of Computer Engineering, Sangji University Introduction Syntax the form or structure of

### CPS 506 Comparative Programming Languages. Syntax Specification

CPS 506 Comparative Programming Languages Syntax Specification Compiling Process Steps Program Lexical Analysis Convert characters into a stream of tokens Lexical Analysis Syntactic Analysis Send tokens

### 4. Lexical and Syntax Analysis

4. Lexical and Syntax Analysis 4.1 Introduction Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler