GRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013

Similar documents
If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014

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

Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS. Regrades 10/6/15. Prelim 1. Prelim 1. Expression trees. Pointers to material

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

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

syntax tree - * * * - * * * * * 2 1 * * 2 * (2 * 1) - (1 + 0)

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018

syntax tree - * * * * * *

Announcements. Java Graphics. Exceptions. Java Odds & Ends

CSE 3302 Programming Languages Lecture 2: Syntax

Lexical Scanning COMP360

Compiling Regular Expressions COMP360

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Compiler Design Concepts. Syntax Analysis

CPS 506 Comparative Programming Languages. Syntax Specification

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

School of Informatics, University of Edinburgh

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

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Syntactic Analysis. The Big Picture Again. Grammar. ICS312 Machine-Level and Systems Programming

CS2110 Assignment 2 Lists, Induction, Recursion and Parsing, Summer

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Object Oriented Programming Exception Handling

CS 3 Introduction to Software Engineering. 3: Exceptions

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

LECTURE 3. Compiler Phases

XC Total Max Score Grader

Exceptions in Java

Chapter 3 Parsing. time, arrow, banana, flies, like, a, an, the, fruit

announcements CSE 311: Foundations of Computing review: regular expressions review: languages---sets of strings

Theory and Compiling COMP360

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

ECE 122. Engineering Problem Solving with Java

Correctness and Robustness

CS 3410 Ch 7 Recursion

CS164: Midterm I. Fall 2003

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Java Errors and Exceptions. Because Murphy s Law never fails

CS 61B Discussion 5: Inheritance II Fall 2014

COE318 Lecture Notes Week 10 (Nov 7, 2011)

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

CSCI 1260: Compilers and Program Analysis Steven Reiss Fall Lecture 4: Syntax Analysis I

Lecture 11: Context-free grammars

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Introduction to Java. Handout-3a. cs402 - Spring

Exception-Handling Overview

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Chapter 9: Dealing with Errors

Syntax Intro and Overview. Syntax

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

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

CS159. Nathan Sprague

Syntax and Grammars 1 / 21

DM550 Introduction to Programming part 2. Jan Baumbach.

Java Bytecode (binary file)

Introduction to Parsing. Lecture 5

CS 2230 CS II: Data structures. Meeting 20: generic types, exceptions, higher order functions Brandon Myers University of Iowa

Lecture 6 CS2110 Spring 2013 RECURSION

Program Correctness and Efficiency. Chapter 2

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Principles of Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

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

BBM 102 Introduction to Programming II Spring Exceptions

Iterators. Lecture 24: Iterators & Exceptions. Implementing Iterators. When Good Programs Go Bad! - where! Abstract over control structures (in Clu)!

Chapter 10 Language Translation

CMSC 330: Organization of Programming Languages

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

Semantic Analysis. Lecture 9. February 7, 2018

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Principles of Programming Languages COMP251: Syntax and Grammars

Related Course Objec6ves

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Introduction to Parsing Ambiguity and Syntax Errors

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Homework & Announcements

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Project Compiler. CS031 TA Help Session November 28, 2011

AP CS Fall Semester Final

1. Consider the following program in a PCAT-like language.

ITEC2620 Introduction to Data Structures

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Introduction to Parsing Ambiguity and Syntax Errors

CMSC 330, Fall 2009, Practice Problem 3 Solutions

Exceptions and Design

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

BBM 102 Introduction to Programming II Spring 2017

Transcription:

1 GRAMMARS & PARSING Lecture 7 CS2110 Fall 2013

Pointers to the textbook 2 Parse trees: Text page 592 (23.34), Figure 23-31 Definition of Java Language, sometimes useful: http://docs.oracle.com/javase/specs/jls/se7/html/index.html Grammar for most of Java, for those who are curious: http://csci.csusb.edu/dick/samples/java.syntax.html Homework: Learn to use these Java string methods: s.length, s.charat(), s.indexof(), s.substring(), s.tochararray(), s = new string(char[] array). Hint: These methods will be useful on prelim1 (Oct 10)! (They can be useful for parsing too )

Application of Recursion 3 So far, we have discussed recursion on integers Factorial, fibonacci, a n, combinatorials Let us now consider a new application that shows off the full power of recursion: parsing Parsing has numerous applications: compilers, data retrieval, data mining,

Motivation 4 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 cat that sat in the hat ate the big rat on the mat slowly. The small cat that sat in the hat ate the big rat on the mat slowly, then got sick. Not all sequences of words are legal sentences The ate cat rat the How many legal sentences are there? How many legal programs are there? Are all Java programs that compile legal programs? How do we know what programs are legal? http://docs.oracle.com/javase/specs/jls/se7/html/index.html

A Grammar 5 Sentence Noun Verb Noun Noun Noun Noun Verb Verb boys girls bunnies like see Our sample grammar has these rules: A Sentence can be a Noun followed by a Verb followed by a Noun A Noun can be boys or girls or bunnies A Verb can be like or see Grammar: set of rules for generating sentences in a language Examples of Sentence: boys see bunnies bunnies like girls White space between words does not matter The words boys, girls, bunnies, like, see are called tokens or terminals The words Sentence, Noun, Verb are called nonterminals This is a very boring grammar because the set of Sentences is finite (exactly 18 sentences)

A Recursive Grammar 6 Sentence Sentence and Sentence Sentence Sentence or Sentence Sentence Noun Verb Noun Noun boys Noun girls Noun bunnies Verb like Verb see Examples of Sentences in this language: boys like girls boys like girls and girls like bunnies boys like girls and girls like bunnies and girls like bunnies boys like girls and girls like bunnies and girls like bunnies and girls like bunnies This grammar is more interesting than the last one because the set of Sentences is infinite What makes this set infinite? Answer: Recursive definition of Sentence

Detour 7 What if we want to add a period at the end of every sentence? Sentence Sentence and Sentence. Sentence Sentence or Sentence. Sentence Noun Verb Noun. Noun Does this work? No! This produces sentences like: girls like boys. and boys like bunnies.. Sentence Sentence Sentence

Sentences with Periods 8 PunctuatedSentence Sentence. Sentence Sentence and Sentence Sentence Sentence or Sentence Sentence Noun Verb Noun Noun boys Noun girls Noun bunnies Verb like Verb see Add a new rule that adds a period only at the end of the sentence. The tokens here are the 7 words plus the period (.) This grammar is ambiguous: boys like girls and girls like boys or girls like bunnies

Uses of Grammars 9 Grammar describes every possible legal expression You could use the grammar for Java to list every possible Java program. (It would take forever) Grammar tells the Java compiler how to understand a Java program

Grammar for Simple Expressions 10 E integer E ( E + E ) Simple expressions: An E can be an integer. An E can be ( followed by an E followed by + followed by an E followed by ) Set of expressions defined by this grammar is a recursively-defined set Is language finite or infinite? Do recursive grammars always yield infinite languages? Here are some legal expressions: 2 (3 + 34) ((4+23) + 89) ((89 + 23) + (23 + (34+12))) Here are some illegal expressions: (3 3 + 4 The tokens in this grammar are (, +, ), and any integer

Parsing 11 Grammars can be used in two ways A grammar defines a language (i.e., the set of properly structured sentences) A grammar can be used to parse a sentence (thus, checking if the sentence is in the language) Example: Show that ((4+23) + 89) is a valid expression E by building a parse tree E ( E + E ) To parse a sentence is to build a parse tree This is much like diagramming a sentence ( E + E ) 4 23 89

Recursive Descent Parsing 12 Idea: Use the grammar to design a recursive program to check if a sentence is in the language To parse an expression E, for instance We look for each terminal (i.e., each token) Each nonterminal (e.g., E) can handle itself by using a recursive call The grammar tells how to write the program! boolean parsee() { } if (first token is an integer) return true; if (first token is ( ) { } parsee(); Make sure there is a + token; parsee( ); Make sure there is a ) token; return true; return false;

Java Code for Parsing E 13 public static Node parsee(scanner scanner) { if (scanner.hasnextint()) { int data = scanner.nextint(); return new Node(data); } check(scanner, ( ); left = parsee(scanner); check(scanner, + ); right = parsee(scanner); check(scanner, ) ); return new Node(left, right); }

Detour: Error Handling with Exceptions 14 Parsing does two things: It returns useful data (a parse tree) It checks for validity (i.e., is the input a valid sentence?) How should we respond to invalid input? Exceptions allow us to do this without complicating our code unnecessarily

Exceptions 15 Exceptions are usually thrown to indicate that something bad has happened IOException on failure to open or read a file ClassCastException if attempted to cast an object to a type that is not a supertype of the dynamic type of the object NullPointerException if tried to dereference null ArrayIndexOutOfBoundsException if tried to access an array element at index i < 0 or the length of the array In our case (parsing), we should throw an exception when the input cannot be parsed

Handling Exceptions 16 Exceptions can be caught by the program using a try-catch block catch clauses are called exception handlers Integer x = null; try { x = (Integer)y; System.out.println(x.intValue()); } catch (ClassCastException e) { System.out.println("y was not an Integer"); } catch (NullPointerException e) { } System.out.println("y was null");

Defining Your Own Exceptions 17 An exception is an object (like everything else in Java) You can define your own exceptions and throw them class MyOwnException extends Exception {}... if (input == null) { throw new MyOwnException(); }

Declaring Exceptions 18 In general, any exception that could be thrown must be either declared in the method header or caught void foo(int input) throws MyOwnException { if (input == null) { throw new MyOwnException(); }... } Note: throws means can throw, not does throw Subtypes of RuntimeException do not have to be declared (e.g., NullPointerException, ClassCastException) These represent exceptions that can occur during normal operation of the Java Virtual Machine

How Exceptions are Handled 19 If the exception is thrown from inside the try clause of a try-catch block with a handler for that exception (or a superclass of the exception), then that handler is executed Otherwise, the method terminates abruptly and control is passed back to the calling method If the calling method can handle the exception (i.e., if the call occurred within a try-catch block with a handler for that exception) then that handler is executed Otherwise, the calling method terminates abruptly, etc. If none of the calling methods handle the exception, the entire program terminates with an error message

Using a Parser to Generate Code 20 We can modify the parser so that it generates stack code to evaluate arithmetic expressions: 2 PUSH 2 STOP (2 + 3) PUSH 2 PUSH 3 ADD STOP Goal: Method parsee should return a string containing stack code for expression it has parsed Method parsee can generate code in a recursive way: For integer i, it returns string PUSH + i + \n For (E1 + E2), Recursive calls for E1 and E2 return code strings c1 and c2, respectively Then to compile (E1 + E2), return c1 + c2 + ADD\n Top-level method should tack on a STOP command after code received from parsee

Does Recursive Descent Always Work? 21 There are some grammars that cannot be used as the basis for recursive descent A trivial example (causes infinite recursion): S b S Sa For some constructs, recursive descent is hard to use Can use a more powerful parsing technique (there are several, but not in this course) Can rewrite grammar S b S ba A a A aa

Syntactic Ambiguity 22 Sometimes a sentence has more than one parse tree S A aaxb A x aab B b bb The string aaxbb can be parsed in two ways This kind of ambiguity sometimes shows up in programming languages if E1 then if E2 then S1 else S2 Which then does the else go with? This ambiguity actually affects the program s meaning How do we resolve this? Provide an extra non-grammar rule (e.g., the else goes with the closest if) Modify the language (e.g., an ifstatement must end with a fi ) Operator precedence (e.g. 1 + 2 * 3 should be parsed as 1 + (2 * 3), not (1 + 2) * 3 Other methods (e.g., Python uses amount of indentation)

Conclusion 23 Recursion is a very powerful technique for writing compact programs that do complex things Common mistakes: Incorrect or missing base cases Subproblems must be simpler than top-level problem Try to write description of recursive algorithm and reason about base cases before writing code Why? Syntactic junk such as type declarations, etc. can create mental fog that obscures the underlying recursive algorithm Best to separate the logic of the program from coding details

Exercises 24 Think about recursive calls made to parse and generate code for simple expressions 2 (2 + 3) ((2 + 45) + (34 + -9)) Derive an expression for the total number of calls made to parsee for parsing an expression Hint: think inductively Derive an expression for the maximum number of recursive calls that are active at any time during the parsing of an expression (i.e. max depth of call stack)

Exercises 25 Write a grammar and recursive program for sentence palindromes that ignores white spaces & punctuation Was it Eliot's toilet I saw? No trace; not one carton Go deliver a dare, vile dog! Madam, in Eden I'm Adam Write a grammar and recursive program for strings A n B n AB AABB AAAAAAABBBBBBB Write a grammar and recursive program for Java identifiers <letter> [<letter> or <digit>] 0 N j27, but not 2j7