


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A lecture note from cs2110 - fall 2008, focusing on grammars and parsing in java. It covers the basics of declaring fields and methods, the application of recursion in parsing, and the concept of a recursive grammar. The document also touches upon the topic of handling exceptions and defining custom exceptions.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



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: " e1 & e2 : evaluate both and compute their conjunction " e1 && e2 : 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; 3
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
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’ ! 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)
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
7
Sentence Sentence Sentence 8
9
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 + 4 ! The tokens in this grammar are (, +, ), and any integer 10
" 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 )
" This is much like diagramming a sentence
! 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; } (^) 12
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); }
19
" For integer i, it returns string “PUSH ” + i + “\n” " For (E1 + E2),
return code strings c1 and c2, respectively
c1 + c2 + “ADD\n” " Top-level method should tack on a STOP command after code received from parseE 20
" A trivial example (causes infinite recursion):
" Can use a more powerful parsing technique (there are several, but not in this course) 21
S! A | aaxB A! x | aAb B! b | bB " The string aaxbb can be parsed in two ways
" 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. 1 + 2 * 3 should always be parsed as 1 + (2 * 3), not (1 + 2) * 3 " Other methods (e.g., Python uses amount of indentation) 22
fog that obscures the underlying recursive algorithm
" mom " dad " i prefer pi " race car " murder for a jar of red rum " sex at noon taxes
" [ or ]0…N " j27, but not 2j