GRAMMARS & PARSING Lecture 7 CS2110 – Fall 2013 1.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Parsing V: Bottom-up Parsing
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Semantics Static semantics Dynamic semantics attribute grammars
Yoshi
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
Grammars, Languages and Parse Trees. Language Let V be an alphabet or vocabulary V* is set of all strings over V A language L is a subset of V*, i.e.,
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
CS102--Object Oriented Programming
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
COMP 121 Week 5: Exceptions and Exception Handling.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
GRAMMARS & PARSING Lecture 7 CS2110 – Fall Java Tips 2  Declare fields and methods public if they are to be visible outside the class; helper methods.
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 12 CS2110 – Spring
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Grammars and Parsing. Sentence  Noun Verb Noun Noun  boys Noun  girls Noun  dogs Verb  like Verb  see Grammars Grammar: set of rules for generating.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
Parsing arithmetic expressions Reading material: These notes and an implementation (see course web page). The best way to prepare [to be a programmer]
The Java Programming Language
CS 280 Data Structures Professor John Peterson. How Does Parsing Work? You need to know where to start (“statement”) This grammar is constructed so that.
1 Top Down Parsing. CS 412/413 Spring 2008Introduction to Compilers2 Outline Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form.
COMP Parsing 2 of 4 Lecture 22. How do we write programs to do this? The process of getting from the input string to the parse tree consists of.
PART I: overview material
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 3, 09/11/2003 Prof. Roy Levow.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 12 CS2110 – Spring
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
CPS 506 Comparative Programming Languages Syntax Specification.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
GRAMMARS & PARSING Lecture 8 CS2110 – Spring If you are going to form a group for A2, please do it before tomorrow (Friday) noon.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Exceptions and Assertions Chapter 15 – CSCI 1302.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
CSE 3302 Programming Languages
ASTs, Grammars, Parsing, Tree traversals
Grammars and Parsing.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Chapter 10 – Exception Handling
ASTs, Grammars, Parsing, Tree traversals
ADTs, Grammars, Parsing, Tree traversals
ASTs, Grammars, Parsing, Tree traversals
Presentation transcript:

GRAMMARS & PARSING Lecture 7 CS2110 – Fall

Pointers to the textbook 2 Parse trees: Text page 592 (23.34), Figure  Definition of Java Language, sometimes useful:  Grammar for most of Java, for those who are curious: 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?

A Grammar  Sentence  Noun Verb Noun  Noun  boys  Noun  girls  Noun  bunnies  Verb  like  Verb  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’ 5  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  This grammar is more interesting than the last one because the set of Sentences is infinite  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  ………  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

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)  (( ) + (23 + (34+12)))  Here are some illegal expressions:  (3   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)  To parse a sentence is to build a parse tree  This is much like diagramming a sentence  Example: Show that ((4+23) + 89) is a valid expression E by building a parse tree E ( E ) E+ 89 ( E ) E+ 4 23

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  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” void foo(int input) throws MyOwnException { if (input == null) { throw new MyOwnException(); }... }

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  Can rewrite grammar S  b S  bA A  a A  aA  For some constructs, recursive descent is hard to use  Can use a more powerful parsing technique (there are several, but not in this course)

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 if- statement must end with a ‘fi’ )  Operator precedence (e.g * 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) + ( ))  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  [ or ] 0…N  j27, but not 2j7