Context-sensitive Analysis Part IV Ad-hoc syntax-directed translation, Symbol Tables, andtypes

Similar documents
Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Bottom-up Parser. Jungsik Choi

Computing Inside The Parser Syntax-Directed Translation. Comp 412

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Introduction to Parsing. Comp 412

Context-sensitive Analysis. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

Building a Parser Part III

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

CMSC 430 Introduction to Compilers. Fall Lexing and Parsing

CSCI312 Principles of Programming Languages

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

Syntax Analysis, VI Examples from LR Parsing. Comp 412

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

CS 406/534 Compiler Construction LR(1) Parsing and CSA

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

Lecture 8: Deterministic Bottom-Up Parsing

Lecture 7: Deterministic Bottom-Up Parsing

CS 4120 Introduction to Compilers

Syntactic Analysis. Top-Down Parsing

Bottom-Up Parsing. Lecture 11-12

Context-free grammars

Compiler Construction: Parsing

CSE302: Compiler Design

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

Project Compiler. CS031 TA Help Session November 28, 2011

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

March 13/2003 Jayakanth Srinivasan,

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

Alternatives for semantic processing

Compiler construction in4303 lecture 3

COMP 181. Prelude. Prelude. Summary of parsing. A Hierarchy of Grammar Classes. More power? Syntax-directed translation. Analysis

Parsing III. (Top-down parsing: recursive descent & LL(1) )

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

Compiler construction lecture 3

Yacc: A Syntactic Analysers Generator

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

Parser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23

Bottom-Up Parsing. Lecture 11-12

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

Chapter 4. Lexical and Syntax Analysis

Syntactic Directed Translation

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Configuration Sets for CSX- Lite. Parser Action Table

How do LL(1) Parsers Build Syntax Trees?

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

Syntax-Directed Translation. Lecture 14

Compilers. Bottom-up Parsing. (original slides by Sam

Intro to Bottom-up Parsing. Lecture 9

LL(1) predictive parsing

Introduction to Bottom-Up Parsing

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

Syntax-Directed Translation Part II

CS 406: Syntax Directed Translation

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

CS415 Compilers. LR Parsing & Error Recovery

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

LL(1) predictive parsing

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Outline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles

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


MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Compilation 2013 Parser Generators, Conflict Management, and ML-Yacc

CSX-lite Example. LL(1) Parse Tables. LL(1) Parser Driver. Example of LL(1) Parsing. An LL(1) parse table, T, is a twodimensional

LR Parsing LALR Parser Generators

CA Compiler Construction

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

CS 406/534 Compiler Construction Parsing Part I

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

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

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 INSTRUCTIONS

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

Top down vs. bottom up parsing

Lecture Bottom-Up Parsing

S Y N T A X A N A L Y S I S LR

Conflicts in LR Parsing and More LR Parsing Types

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

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

4. Lexical and Syntax Analysis

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

LR Parsing LALR Parser Generators

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).

4. Lexical and Syntax Analysis

CS 11 Ocaml track: lecture 6

Error Recovery. Computer Science 320 Prof. David Walker - 1 -

Context-sensitive Analysis

Syntax Analysis, III Comp 412

Syntax Directed Translation

LR Parsing - The Items

LR Parsing - Conflicts

More Bottom-Up Parsing

Transcription:

Context-sensitive Analysis Part IV Ad-hoc syntax-directed translation, Symbol Tables, andtypes

Quiz Name two differences between attribute grammars and ad-hoc syntax directed translation techniques?

Example: Processing C Declarations

Example: Processing C Declarations

Example: Processing C Declarations

Example: Processing C Declarations

Example: Processing C Declarations

Example: Processing C Declarations

Example: Processing C Declarations

Example: Processing C Declarations

Example: Processing C Declarations

Example: Processing C Declarations

Example: Processing C Declarations

Building a Symbol Table Enter declaration information as processed At end of declaration syntax, do some post processing Use table to check errors as parsing progresses assumes table is global

Symbol Table: Typical Uses Define before use lookup on reference Dimension, type,... check as encountered Type checking of expression bottom-up walk

Symbol Table: Typical Uses Procedure interfaces are harder Build a representation for parameter list & types Create list of sites to check

Is This Really Ad-hoc? Relationship between practice and attribute grammars Similarities Both rules & actions associated with productions Application order determined by tools, not author

Is This Really Ad-hoc? Relationship between practice and attribute grammars Differences Actions applied as a unit; not true for AG rules Anything goes in ad-hoc actions; AG rules are functional

Making Ad-hoc SDT Work How do we fit this into an LR(1) parser? stack.push(invalid); stack.push(s 0 ); // initial state token = scanner.next_token(); loop forever { s = stack.top(); if ( ACTION[s,token] == reduce A β ) then { stack.popnum(2* β ); // pop 2* β symbols s = stack.top(); stack.push(a); // push A stack.push(goto[s,a]); // push next state } else if ( ACTION[s,token] == shift s i ) then { stack.push(token); stack.push(s i ); token scanner.next_token(); } else if ( ACTION[s,token] == accept & token == EOF ) then break; else throw a syntax error; } report success; From an earlier lecture

stack.push(invalid); stack.push(null); stack.push(s 0 ); // initial state token = scanner.next_token(); loop forever { s = stack.top(); if ( ACTION[s,token] == reduce A β ) then { /* insert case statement here */ stack.popnum(3* β ); // pop 3* β symbols s = stack.top(); stack.push($$); // push result stack.push(a); // push A stack.push(goto[s,a]); // push next state } else if ( ACTION[s,token] == shift s i ) then { stack.push(attr); stack.push(token); stack.push(s i ); token scanner.next_token(); } else if ( ACTION[s,token] == accept & token == EOF ) then break; else throw a syntax error; } report success; To add yacc-like actions Stack has 3 items per symbol rather than 2 (3 rd is $$)

stack.push(invalid); stack.push(null); stack.push(s 0 ); // initial state token = scanner.next_token(); loop forever { s = stack.top(); if ( ACTION[s,token] == reduce A β ) then { /* insert case statement here */ stack.popnum(3* β ); // pop 3* β symbols s = stack.top(); stack.push($$); // push result stack.push(a); // push A stack.push(goto[s,a]); // push next state } else if ( ACTION[s,token] == shift s i ) then { stack.push(attr); stack.push(token); stack.push(s i ); token scanner.next_token(); } else if ( ACTION[s,token] == accept & token == EOF ) then break; else throw a syntax error; } report success; Add case statement to the reduction processing section Case switches on production number

stack.push(invalid); stack.push(null); stack.push(s 0 ); // initial state token = scanner.next_token(); loop forever { s = stack.top(); if ( ACTION[s,token] == reduce A β ) then { /* insert case statement here */ stack.popnum(3* β ); // pop 3* β symbols s = stack.top(); stack.push($$); // push result stack.push(a); // push A stack.push(goto[s,a]); // push next state } else if ( ACTION[s,token] == shift s i ) then { stack.push(attr); stack.push(token); stack.push(s i ); token scanner.next_token(); } else if ( ACTION[s,token] == accept & token == EOF ) then break; else throw a syntax error; } report success; Each case clause holds the code snippet for that production Substitute appropriate names for $$, $1, $2,

Making Ad-hoc SDT Work How do we fit this into an LR(1) parser? Need a naming scheme to access them $n translates into stack location (top - 3n) Need to sequence rule applications On every reduce action, perform the action rule Add a giant case statement to the parser top 5 $1 = 3 6 4 (top 3(1)) 3 2 1 0 $2 = 0 (top 3(2))

Making Ad-hoc SDT Work What about a rule that must work in mid-production? Can transform the grammar Split it into two parts at the point where rule must go Apply the rule on reduction to the appropriate part Can also handle reductions on shift actions Add a production to create a reduction Was: fee fum Make it: fee fie fum and tie the action to this new reduction Together, these let us apply rule at any point in the parse

Alternative Strategy What if you need to perform actions that do not fit well into the Ad-hoc Syntax-Directed Translation framework? Build the abstract syntax tree using SDT Perform the actions during one or more treewalks In an OOL, think of this problem as a classic application of the visitor pattern Perform arbitrary computation in treewalk order Make multiple passes if necessary Again, a competent junior or senior CS major would derive this solution after a couple of minutes of thought.