Grammars and Parsing - Object-Oriented Programming and Data Structures - Lecture Sl, Lecture notes of Object Oriented Programming

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

2012/2013

Uploaded on 08/20/2013

yumni
yumni 🇮🇳

5

(2)

25 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Grammars&Parsing
1
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Grammars and Parsing - Object-Oriented Programming and Data Structures - Lecture Sl and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Grammars

Parsing

Java

Tips

-^ 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;

Motivation

•^

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

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 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)

Detour

-^

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

.^.

7

Sentence

Sentence

Sentence

Sentences

with

Periods

-^

PunctuatedSentence

Sentence

-^

Sentence

Sentence

and

Sentence

-^

Sentence

Sentence

or

Sentence

-^

Sentence

Noun

Verb

Noun

-^

Noun

boys

-^

Noun

girls

-^

Noun

bunnies

-^

Verb

like

-^

Verb

see

8

^

Add a new rule that adds aperiod only at the end ofthe sentence.

^

The tokens here are the 7words plus the period (.)

^

This grammar isambiguous:

boys like girlsand girls like boysor girls like bunnies

Parsing

•^

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

10

^

Example: Show that((4+23) + 89)is a valid expression E bybuilding a

parse tree^ E

(^

E^

) E +

89

(^

E^

) E + 4

23

Recursive

Descent

Parsing

-^ 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!

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

Exceptions

•^

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

Defining

Your

Own

Exceptions

•^

An

exception

is

an

object

(like

everything

else

in

Java)

•^

You

can

define

your

own

exceptions

and

throw

them

16

class MyOwnException extends Exception {}... if (input == null) {

throw new MyOwnException(); }

Declaring

Exceptions

-^

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”

17

void foo(int input) throws MyOwnException {

if (input == null) {

throw new MyOwnException(); } ... }

Using

a

Parser

to

Generate

Code

•^ 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

19

^ Method parseE can generatecode in a recursive way: ^ For integer i, it returns string “PUSH ”+ i + “\n” ^ For (E1 + E2),

^ 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?

  • 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

20

^ For some constructs, recursivedescent is hard to use ^ Can use a more powerful parsingtechnique (there are several, butnot in this course)