Parsing Techniques. AST Review. AST Data Structures. Implicit AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

Similar documents
Parsing Techniques. AST Review. AST Data Structures. LL AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

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

Recursive Descent Parsers

Plan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars

CS 4120 Introduction to Compilers

Syntax-Directed Translation. Lecture 14

LR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(0) Grammar. LR(0) Parsing Table CS412/CS413

Summary: Semantic Analysis

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

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

A programming language requires two major definitions A simple one pass compiler

Parser and syntax analyzer. Context-Free Grammar Definition. Scanning and parsing. How bottom-up parsing works: Shift/Reduce tecnique.

Context-free grammars

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

Introduction to Compiler Design

CS 314 Principles of Programming Languages

Compiler Construction: Parsing

Syntax-Directed Translation. Introduction

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

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

Lecture 14 Sections Mon, Mar 2, 2009

A Simple Syntax-Directed Translator

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

Bottom-Up Parsing. Lecture 11-12

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

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

CPS 506 Comparative Programming Languages. Syntax Specification

COP5621 Exam 3 - Spring 2005

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

Compilers. Compiler Construction Tutorial The Front-end

Parsing II Top-down parsing. Comp 412

Syntax Analysis Part I

Lexical and Syntax Analysis. Top-Down Parsing

How do LL(1) Parsers Build Syntax Trees?

LR Parsing LALR Parser Generators

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

Introduction to Parsing. Lecture 8

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

Bottom-Up Parsing. Lecture 11-12

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

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

Compiler Design Concepts. Syntax Analysis

Syntax-Directed Translation

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

Building Compilers with Phoenix

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

CSCI312 Principles of Programming Languages!

As we have seen, token attribute values are supplied via yylval, as in. More on Yacc s value stack

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

4. Lexical and Syntax Analysis

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

CSE302: Compiler Design

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

Downloaded from Page 1. LR Parsing

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

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

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

LECTURE 7. Lex and Intro to Parsing

4. Lexical and Syntax Analysis

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

Lexical and Syntax Analysis. Bottom-Up Parsing

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

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Programs as data Parsing cont d; first-order functional language, type checking

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

In One Slide. Outline. LR Parsing. Table Construction


3. Parsing. Oscar Nierstrasz

CS 406: Syntax Directed Translation

Conflicts in LR Parsing and More LR Parsing Types

Syntax-Directed Translation Part I

II. Language Processing ystem skeletal source program preprocessor source program compiler target object assembly program assembler relocatable machin

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Chapter 4. Lexical and Syntax Analysis

shift-reduce parsing

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

Syntax Directed Translation

Lexical Analysis. Introduction

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Abstract Syntax Trees Synthetic and Inherited Attributes

POLITECNICO DI TORINO. (01JEUHT) Formal Languages and Compilers. Laboratory N 3. Lab 3. Cup Advanced Use

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

CMSC 330: Organization of Programming Languages

Defining syntax using CFGs

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Semantic actions for declarations and expressions

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

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Context-Free Grammars

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

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

COP 3402 Systems Software Syntax Analysis (Parser)

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

Topic 3: Syntax Analysis I

Using an LALR(1) Parser Generator

CMSC 330: Organization of Programming Languages. Context Free Grammars

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

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

Programming Language Syntax and Analysis

Transcription:

Parsing Techniques C41/C413 Introduction to Compilers Tim Teitelbaum Lecture 11: yntax-irected efinitions February 16, 005 LL parsing Computes a Leftmost derivation Builds the derivation top-down LL parsing table indicates which production to use for expanding the rightmost non-terminal LR parsing Computes a Rightmost derivation Builds the derivation bottom-up Uses a set of LR states and a stack of symbols LR parsing table indicates, for each state, what action to perform (shift/reduce) and what state to go to next Use these techniques to construct an AT C 41/413 pring 005 Introduction to Compilers 1 C 41/413 pring 005 Introduction to Compilers AT Review erivation = sequence of applied productions E 1 1 E 1 Parse tree = graph representation of a derivation oesn t capture the order of applying the productions Abstract yntax Tree (AT) discards unnecessary information from the parse tree Parse Tree E 1 E E 3 AT 1 3 AT ata tructures abstract class Expr { class extends Expr { Expr left, right; (Expr L, Expr R) { left = L; right = R; class Num extends Expr { int value; Num (int v) { value = v; Num 1 Num Num 3 C 41/413 pring 005 Introduction to Compilers 3 C 41/413 pring 005 Introduction to Compilers 4 Implicit AT Construction LL/LR parsing techniques implicitly build the AT The parse tree is captured in the derivation LL parsing: AT is implicitly represented by the sequence of applied productions LR parsing: AT is implicitly represented by the sequence of applied reductions We want to explicitly construct the AT during the parsing phase: add code in the parser to explicitly build the AT AT Construction LL parsing: extend procedures for nonterminals Example: E' ' ε E num ( ) vo parse_() { switch (token) { case num: case ( : parse_e(); parse_ (); return; default: throw new ParseError(); Expr parse_() { switch (token) { case num: case ( : Expr left = parse_e(); Expr right = parse_ (); if (right == null) return left; else return new (left, right); default: throw new ParseError(); C 41/413 pring 005 Introduction to Compilers 5 C 41/413 pring 005 Introduction to Compilers 6

AT Construction LR parsing We need again to add code for explicit AT construction AT construction mechanism for LR Parsing tore parts of the tree on the stack For each nonterminal symbol B on stack, also store the sub-tree rooted at B on stack Whenever the parser performs a reduce operation for a production B γ, create an AT node for B AT Construction for LR Parsing Example stack E Before reduction E Num() Num(3) Num(1) E E num ( ) Num(1) After reduction E Num() Num(3) C 41/413 pring 005 Introduction to Compilers 7 C 41/413 pring 005 Introduction to Compilers 8 Problems Unstructured code: mixed parsing code with AT construction code Automatic parser generators The generated parser needs to contain AT construction code How to construct a customized AT data structure using an automatic parser generator? May want to perform other actions concurrently with the parsing phase E.g. semantic checks This can reduce the number of compiler passes yntax-irected efinition olution: syntax-directed definition Extends each grammar production with an associated semantic action (code): E { action The parser generator adds these actions into the generated parser Each action is executed when the corresponding production is reduced C 41/413 pring 005 Introduction to Compilers 9 C 41/413 pring 005 Introduction to Compilers 10 emantic Actions Actions = code in a programming language ame language as the automatically generated parser Examples: Yacc = actions written in C CUP = actions written in Java The actions access the parser stack! Parser generators extend the stack of states (corresponding to RH symbols) symbols with entries for user-defined structures (e.g., parse trees) The action code should be able to refer to the states (corresponding to the RH grammar symbols in the production Need a naming scheme C 41/413 pring 005 Introduction to Compilers 11 Naming cheme Need names for grammar symbols to use in the semantic action code Need to refer to multiple occurrences of the same nonterminal symbol E E 1 E istinguish the nonterminal on the LH E 0 E E C 41/413 pring 005 Introduction to Compilers 1

Naming cheme: CUP CUP: Name RH nonterminal occurrences using distinct, user-defined labels: expr ::= expr:e1 PLU expr:e Use keyword REULT for LH nonterminal CUP Example: expr ::= expr:e1 PLU expr:e {: REULT = e1 e; : Naming cheme: yacc Yacc: Uses keywords: $1 refers to the first RH symbol, $ refers to the second RH symbol, etc. Keyword $$ refers to the LH nonterminal YaccExample: expr ::= expr PLU expr { $$ = $1 $3; C 41/413 pring 005 Introduction to Compilers 13 C 41/413 pring 005 Introduction to Compilers 14 Building the AT Use semantic actions to build the AT AT is built bottom-up along with parsing non terminal Expr expr; User-defined type for semantic objects on the stack Nonterminal name expr ::= NUM:i {: REULT = new Num(i.val); : expr ::= expr:e1 PLU expr:e {: REULT = new (e1,e); : expr ::= expr:e1 MULT expr:e {: REULT = new Mul(e1,e); : expr ::= LPAR expr:e RPAR {: REULT = e; : Example E num (E) EE E*E Parser stack stores value of each nonterminal (1)*3 (1 )*3 (E Num(1) )*3 REULT=new Num(1) (E )*3 (EE Num() )*3 REULT=new Num() (E (, ) )*3 REULT=new (e1,e) (E) *3 E *3 REULT=e C 41/413 pring 005 Introduction to Compilers 15 C 41/413 pring 005 Introduction to Compilers 16 AT esign Keep the AT abstract o not introduce a tree node for every node in parse tree (not very abstract) E ( ) E E 3 1 E? 1 3 um Expr um LPar um RPar Expr Expr um 3 1 Expr C 41/413 pring 005 Introduction to Compilers 17 AT esign o not use one single class AT_node E.g., need information for if, while,, *, I, NUM class AT_node { int node_type; AT_node[ ] children; tring name; int value; etc Problem: must have fields for every different kind of node with attributes Not extensible, Java type checking no help C 41/413 pring 005 Introduction to Compilers 18

Use Class Hierarchy Can use subclassing to solve problem Use an abstract class for each interesting set of non-terminals in grammar (e.g. expressions) E EE E*E -E (E) abstract class Expr { class extends Expr { Expr left, right; class Mult extends Expr { Expr left, right; // or: class BinExpr extends Expr { Oper o; Expr l, r; class Minus extends Expr { Expr e; Another Example E ::= num (E) EE ::= E ; if (E) if (E) else = E ; ; abstract class Expr { class Num extends Expr { Num(int value) class extends Expr { (Expr e1, Expr e) class Id extends Expr { Id(tring name) abstract class tmt { class If extends tmt { If(Expr c, tmt s1, tmt s) class Empty extends tmt { Empty() class Assign extends tmt { Assign(tring, Expr e) C 41/413 pring 005 Introduction to Compilers 19 C 41/413 pring 005 Introduction to Compilers 0 Other yntax-irected efinitions Can use syntax-directed definitions to perform semantic checks during parsing E.g. type-checking Benefit = efficiency One single compiler pass for multiple tasks isadvantage = unstructured code Mixes parsing and semantic checking phases Perform checks while AT is changing Limited to one pass in bottom-up order C 41/413 pring 005 Introduction to Compilers 1 Type eclaration Example T 1, { Type(, T.type);.type = T.type; { Type(, 1.type);.type = 1.type; T int { T.type = inttype; T float { T.type = floattype; C 41/413 pring 005 Introduction to Compilers Propagation of Values Propagate type attributes while building the AT int a, b T.type.type T inttype int.type, Type(,.type) Type(,T.type) T L Another Example {.type = T.type; L.type = T.type; T int { T.type = inttype; T float { T.type = floattype; L { Type(,???); L L 1, { Type(, L 1.type);??? C 41/413 pring 005 Introduction to Compilers 3 C 41/413 pring 005 Introduction to Compilers 4

Propagation of Values Propagate values both bottom-up and top-down int a, b T.type T inttype int LR parsing: AT is built bottom-up! L.type L.type L L Type(,L.Type), Type(,L.type) tructured Approach eparate AT construction from semantic checking phase Traverse the AT and perform semantic checks (or other actions) only after the tree has been built and its structure is stable This approach is more flexible and less error-prone It is better when efficiency is not a critical issue C 41/413 pring 005 Introduction to Compilers 5 C 41/413 pring 005 Introduction to Compilers 6 Where We Are ummary ource code (character stream) Token stream Abstract syntax tree (AT) if (b == 0) a = b; if ( b == 0 ) a = b ; if == b 0 = a b Lexical Analysis yntax Analysis (Parsing) emantic Analysis yntax-directed definitions attach semantic actions to grammar productions Easy to construct the AT using syntax-directed definitions Can use syntax-directed definitions to perform semantic checks eparate AT construction from semantic checks or other actions which traverse the AT C 41/413 pring 005 Introduction to Compilers 7 C 41/413 pring 005 Introduction to Compilers 8