06-Parsing

06-Parsing - 2/14/2011 Java Tips 2 Declare fields and...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
2/14/2011 1 Lecture 7 CS2110 Fall 2009 Java Tips 2 Declare fields and methods public if they are to be visible outside the class; helper methods and private data should be declared private Constants that will never be changed should be declared final Public classes should appear in a file of the same name Two kinds of boolean operators: : evaluate both and compute their conjunction : evaluate e1 ; don‟t evaluate e2 unless necessary instead of if (s.equals("")) { f = true; } else { f = false; } write f = s.equals(""); instead of if (s.equals("")) { f = a; } else { f = b; } write f = s.equals("")? a : b; Application of Recursion 3 So far, we have discussed recursion on integers Factorial, fibonacci, combinations, a n 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://java.sun.com/docs/books/jls/third_edition/html/syntax.html 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/05/2011 for the course CS 211 at Cornell.

Page1 / 4

06-Parsing - 2/14/2011 Java Tips 2 Declare fields and...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online