
















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
Major points from this lecture are: Grammars and Parsing, Grammers, Parsing, Application of Recursion, Recursive Grammar, Sentences with Periods, Recursive Descent Parsing, Java Code for Parsing E, Error Handling with Exceptions, Exceptions, Syntactic Ambiguity . Object-Oriented Programming and Data Structures course includes program structure and organization, object oriented programming, graphical user interfaces, algorithm analysis, recursion, data structures (lists, trees, stacks, queues, he
Typology: Lecture notes
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















-^ 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:
-^ e
&
e
:^ evaluate
both
and
compute
their
conjunction
-^ e
&&
e
:^ evaluate
e
;^ don’t
evaluate
e
unless
necessary
2
^ 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;
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.
-^
4
Not all sequences of words arelegal sentences ^
The ate cat rat the ^
How many legal sentences arethere? ^
How many legal programs arethere? ^
Are all Java programs thatcompile legal programs? ^
How do we know what programsare 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’
5
^ Grammar: set of rules for generatingsentences in a language ^ Examples of Sentence: ^ boys see bunnies ^ bunnies like girls ^ … ^ White space between words doesnot matter ^ The words boys, girls, bunnies, like,see are called
tokens
or
terminals
^ The words Sentence, Noun, Verb arecalled
nonterminals
^ This is a very boring grammarbecause the set of Sentences is finite(exactly 18 sentences)
-^
-^
-^
-^
-^
-^
-^
girls
like
boys
.^ and
boys
like
bunnies
.^.
7
Sentence
Sentence
Sentence
-^
-^
-^
-^
-^
-^
-^
-^
-^
8
-^
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
10
(^
E^
) E +
89
(^
E^
) E + 4
23
-^ Idea:
Use
the
grammar
to
design
a^
recursive
program
to
check
if^
a^ sentence
is^
in^
the
language • To^
parse
an
expression
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!
11
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;
}
Detour:
Error
Handling
with
Exceptions
-^
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
-^
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
16
class MyOwnException extends Exception {}... if (input == null) {
throw new MyOwnException(); }
-^
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
)
-^
17
void foo(int input) throws MyOwnException {
if (input == null) {
throw new MyOwnException(); } ... }
2
PUSH
2
STOP (2^ +
3)^
PUSH
2 PUSH
3 ADD STOP
19
^ Recursive calls for E1 and E2 returncode strings c1 and c2, respectively ^ Then to compile (E1 + E2), returnc1 + c2 + “ADD\n” ^ Top-level method should tack on aSTOP command after code receivedfrom parseE
Does
Recursive
Descent
Always
Work?
are
some
grammars
that
cannot
be
used
as
the
basis
for
recursive
descent
-^ A
trivial
example
(causes
infinite
recursion):^ •
S^
^
b
-^ S
Sa
rewrite
grammar
-^ S
b
-^ S
bA
-^ A
a
-^ A
aA
20