CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

Similar documents
LR Parsing - Conflicts

Recursive Descent Parsers

Introduction to Compiler Design

Compilers and Language Processing Tools

LR Parsing - The Items

Last Time. What do we want? When do we want it? An AST. Now!

Fall Compiler Principles Lecture 5: Parsing part 4. Roman Manevich Ben-Gurion University

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Fall Compiler Principles Lecture 4: Parsing part 3. Roman Manevich Ben-Gurion University of the Negev

Minimal Spanning Trees

JFlex. Lecture 16 Section 3.5, JFlex Manual. Robb T. Koether. Hampden-Sydney College. Mon, Feb 23, 2015

Abstract Syntax Trees Synthetic and Inherited Attributes

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

JavaCUP. There are also many parser generators written in Java

The Critical-Path Algorithm

Building the Abstract Syntax Trees

JFlex Regular Expressions

Solving Recursive Sequences by Iteration

The Traveling Salesman Problem Brute Force Method

Properties of Regular Expressions and Finite Automata

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

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Scope and Parameter Passing

Scheduling and Digraphs

Stacks and their Applications

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Syntax Analysis The Parser Generator (BYacc/J)

The Class Construct Part 1

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Magnification and Minification

Operator Precedence a+b*c b*c E + T T * P ( E )

Rotations and Translations

Friends and Unary Operators

Basic PHP Lecture 17

Recursive Linked Lists

The Graphics Pipeline

Scope and Parameter Passing

Defining syntax using CFGs

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

A Simple Syntax-Directed Translator

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

CSE 401 Midterm Exam Sample Solution 11/4/11

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

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

The CYK Parsing Algorithm

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

The Decreasing-Time Algorithm

The Pairwise-Comparison Method

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

Lecture 14 Sections Mon, Mar 2, 2009

For example, we might have productions such as:

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

Programming Languages

The Plurality-with-Elimination Method

Function Definition Syntax Tree

Configuration Sets for CSX- Lite. Parser Action Table

Lecture 12: Parser-Generating Tools

Agenda. Previously. Tentative syllabus. Fall Compiler Principles Lecture 5: Parsing part 4 12/2/2015. Roman Manevich Ben-Gurion University

Sampling Distribution Examples Sections 15.4, 15.5

CSE 401 Midterm Exam 11/5/10

Bottom-Up Syntax Anaysis

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Downloaded from Page 1. LR Parsing

Using an LALR(1) Parser Generator

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Implementing Linked Lists

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

List Iterator Implementation

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Table-Driven Top-Down Parsers

LECTURE 11. Semantic Analysis and Yacc

Lexical and Syntax Analysis

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

The Graphics Pipeline

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

CSE 431S Final Review. Washington University Spring 2013

(01JEUHT) Formal Languages and Compilers

Relational Databases Lecture 2

How do LL(1) Parsers Build Syntax Trees?

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

XQuery FLOWR Expressions Lecture 35

Density Curves Sections

Syntactic Analysis. Syntactic analysis, or parsing, is the second phase of compilation: The token file is converted to an abstract syntax tree.

CSE 582 Autumn 2002 Exam Sample Solution

Programming Languages

COMPILER (CSE 4120) (Lecture 6: Parsing 4 Bottom-up Parsing )

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

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Compiler Passes. Syntactic Analysis. Context-free Grammars. Syntactic Analysis / Parsing. EBNF Syntax of initial MiniJava.

Lexical Analysis. Chapter 1, Section Chapter 3, Section 3.1, 3.3, 3.4, 3.5 JFlex Manual

Binary Tree Applications

Transcription:

CUP Lecture 18 CUP User s Manual (online) Robb T. Koether Hampden-Sydney College Fri, Feb 27, 2015 Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 1 / 31

1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 2 / 31

Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 3 / 31

The CUP Parser Generator The CUP parser generator creates Java source code for an LR parser that uses the LALR algorithm. CUP = Common Useful Parsers. LALR = Look-Ahead LR. LALR is similar to LR(1), whose tables are much larger than the LR(0) tables. LALR tables are compact, the same size as the LR(0) tables. A LALR grammar is an LR(1) grammar whose LR(1) tables may be compressed without creating conflicts. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 4 / 31

The CUP Parser Generator CUP reads a description of the grammar from a.cup file. From that description, it produces two files: parser.java and sym.java. The file parser.java contains Java source code for the parser. The sym class is a class that defines the terminals of the grammar as integer constants, e.g., sym.num. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 5 / 31

The CUP Parser Generator Using the -parser command-line argument, we can rename the parser class. Using the -symbols command-line argument, we can rename the sym class. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 6 / 31

CUP Usage Typically invocations of CUP: $ java java_cup.main < grammar.cup $ java java_cup.main -parser MyParser < grammar.cup $ java java_cup.main -symbols Token < grammar.cup Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 7 / 31

CUP and JFlex CUP may be used with or without JFlex. In Assignment 7, we will see exactly how to make the two programs work together. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 8 / 31

CUP and JFlex CUP may be used with or without JFlex. In Assignment 7, we will see exactly how to make the two programs work together. It is, by design, easy. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 8 / 31

Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 9 / 31

Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 10 / 31

CUP Input Parser Code parser code {: public static void main(string args[]) throws IOException { Reader r = InputStreamReader(System.in); parser myparser = new parser(new Yylex(r)); } :} The CUP file begins with optional parser code that the programmer may add to the parser class. Type the directive parser code, and then include the code within the delimiters {: and :}. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 11 / 31

Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 12 / 31

CUP Terminals Terminals terminal PLUS, TIMES, LPAREN, RPAREN, ASSIGN, SEMI; terminal Integer NUM; terminal String ID; Type terminal, followed by a list of symbolic names for the terminals (e.g., ID, NUM, PLUS). These are used to create the sym class. JFlex will use these for its tokens. Terminals may have an associated data type. For example, ID may have the type String. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 13 / 31

Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 14 / 31

CUP Nonterminals Nonterminals non terminal stmt; non terminal Integer expr; List the nonterminals of the grammar. Type non terminal, followed by a list of symbolic names for the nonterminals (e.g., stmt, expr). Nonterminals may have an associated data type. For example, expr may have the type Integer. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 15 / 31

Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 16 / 31

CUP Precedence Rules Precedence Rules precedence right ASSIGN; precedence left PLUS; precedence left TIMES; For each precedence rule, Type precedence, State the associativity (left, right, or nonassoc), Write the names of the terminals that represent the operators at that precedence level. The precedence rules are listed from lowest to highest precedence. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 17 / 31

Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 18 / 31

CUP Productions Productions stmt ::= id:i ASSIGN expr:e SEMI {: System.out.println(i + " = " + e); :}; expr ::= expr:e1 PLUS expr:e2 {: RESULT = e1 + e2; :}; The format of a production is head ::= body {: action :}; where head is a nonterminal. body is a string of grammar symbols. action is zero or more Java statements. If there is more than one production for a nonterminal, they are separated by. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 19 / 31

The Start Symbol The Start Symbol start with prog; The start symbol is assumed to be the nonterminal on the left side of the first production. Otherwise, specify it using the start with directive. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 20 / 31

Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 21 / 31

The Action and Goto Tables CUP produces the action and goto tables. We can use the command-line arguments -dump_grammar -dump_tables to see the numbered grammar symbols and productions and the action and goto tables. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 22 / 31

Example (The Grammar) Let the grammar be E + ( E, E) * ( E, E) num Let the semantic actions be + ( E, E) Return the sum of the expressions * ( E, E) Return the product of the expressions num Return the value of the token Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 23 / 31

Grammar Example (Grammar) ===== Terminals ===== [0]EOF [1]error [2]PLUS [3]TIMES [4]LPAREN [5]RPAREN [6]COMMA [7]NUM ===== Non terminals ===== [0]stmt [1]expr ===== Productions ===== [0] stmt ::= expr [1] $START ::= stmt EOF [2] expr ::= PLUS LPAREN expr COMMA expr RPAREN [3] expr ::= TIMES LPAREN expr COMMA expr RPAREN [4] expr ::= NUM Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 24 / 31

Action Table Example (Action Table) -------- ACTION_TABLE -------- From state #0 [term 2:SHIFT(to state 2)] [term 3:SHIFT(to state 5)] [term 7:SHIFT(to state 1)] From state #1 [term 0:REDUCE(with prod 4)] [term 5:REDUCE(with prod 4)] [term 6:REDUCE(with prod 4)] From state #2 [term 4:SHIFT(to state 12)] From state #3 [term 0:SHIFT(to state 11)] From state #4 [term 0:REDUCE(with prod 0)] From state #5 [term 4:SHIFT(to state 6)] Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 25 / 31

Action Table Example (Action Table) From state #6 [term 2:SHIFT(to state 2)] [term 3:SHIFT(to state 5)] [term 7:SHIFT(to state 1)] From state #7 [term 6:SHIFT(to state 8)] From state #8 [term 2:SHIFT(to state 2)] [term 3:SHIFT(to state 5)] [term 7:SHIFT(to state 1)] From state #9 [term 5:SHIFT(to state 10)] From state #10 [term 0:REDUCE(with prod 3)] [term 5:REDUCE(with prod 3)] [term 6:REDUCE(with prod 3)] From state #11 [term 0:REDUCE(with prod 1)] Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 26 / 31

Action Table Example (Action Table) From state #12 [term 2:SHIFT(to state 2)] [term 3:SHIFT(to state 5)] [term 7:SHIFT(to state 1)] From state #13 [term 6:SHIFT(to state 14)] From state #14 [term 2:SHIFT(to state 2)] [term 3:SHIFT(to state 5)] [term 7:SHIFT(to state 1)] From state #15 [term 5:SHIFT(to state 16)] From state #16 [term 0:REDUCE(with prod 2)] [term 5:REDUCE(with prod 2)] [term 6:REDUCE(with prod 2)] ------------------------------ Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 27 / 31

Goto Table Example (Goto Table) -------- REDUCE_TABLE -------- From state #0 [non term 0->state 3] [non term 1->state 4] From state #1 From state #2 From state #3 From state #4 From state #5 From state #6 [non term 1->state 7] From state #7 From state #8 [non term 1->state 9] Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 28 / 31

Goto Table Example (Goto Table) From state #9 From state #10 From state #11 From state #12 [non term 1->state 13] From state #13 From state #14 [non term 1->state 15] From state #15 From state #16 ----------------------------- Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 29 / 31

Outline 1 The CUP Parser Generator 2 The CUP File Parser Code Terminals Nonterminals Precedence and Associativity Rules Productions 3 The Action and Goto Tables 4 Assignment Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 30 / 31

Assignment Assignment Let the grammar be E& E E + E E * num Create a JFlex file and a CUP file that will parse this grammar. Let the actions be to display the productions that were matched and to compute the value of the expression as a postfix expression. Robb T. Koether (Hampden-Sydney College) CUP Fri, Feb 27, 2015 31 / 31