7 Parsing - 9/12/2010 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
9/12/2010 1 GRAMMARS & PARSING 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 hd h l d b d l d y instead of if (s.equals("")) { f = true; } else { f = false; } it changed should be declared final ± Public classes should appear in a file of the same name ± Two kinds of boolean operators: ± e1 & e2 : evaluate both and compute their conjunction ± e1 && e2 : evaluate e1 ; don’t evaluate e2 unless necessary write f = s.equals(""); y 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. y Not all sequences of words are legal sentences ² The ate cat rat the y How many legal sentences are there? How many legal programs are ± 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. ± y How many legal programs are there? y Are all Java programs that compile legal programs? y 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 5 y Grammar : set of rules for generating sentences in a language y Examples of Sentence: ² boys see bunnies ² bunnies like girls ² ± 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’ y White space between words does not matter y The words boys, girls, bunnies, like, see are called tokens or terminals y The words Sentence, Noun, Verb are called nonterminals y 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 y 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 ± Noun girls ± Noun bunnies ± Verb like ± Verb see ± This grammar is more interesting than the last one because the set of Sentences is infinite ²
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.

Page1 / 4

7 Parsing - 9/12/2010 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