Parsing arithmetic expressions - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Parsing arithmetic expressions

Description:

Parsing arithmetic expressions Reading material: These notes and an implementation (see course web page). The best way to prepare [to be a programmer] is to write ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 16
Provided by: Tria154
Category:

less

Transcript and Presenter's Notes

Title: Parsing arithmetic expressions


1
Parsing arithmetic expressions
  • Reading material These notes and an
    implementation (see course web page).
  • The best way to prepare to be a programmer is
    to write programs, and to study great programs
    that other people have written. In my case, I
    went to the garbage cans at the Computer Science
    Center and fished out listings of their operating
    system. (Bill Gates)
  • First learn computer science and all the theory.
    Next develop a programming style. Then forget all
    that and just hack. (George Carrette)
  • Look at this website

2
Grammar
  • A grammar set of rules for generating sentences
    of a language.
  • Sentence Noun Verb Noun
  • Noun boys
  • Noun girls
  • Verb like
  • Verb see
  • This grammar has 5 rules. The first says
  • A Sentence can be a Noun followed by a Verb
    followed by a Noun.
  • Note Whitespace between words doesnt matter.
  • Grammar is boring because the set of Sentences
    (8) is finite.

Example of Sentence boys see girls girls like
boys
3
Recursive grammar
  • Sentence Sentence and Sentence
  • Sentence Noun Verb Noun
  • Noun boys
  • Noun girls
  • Verb like
  • Verb see

Grammar has an infinite number of Sentences,
because Sentence is defined recursively
Example of Sentence boys see girls girls like
boys and boys see girls girls like boys and boys
see girls and boys see girls girls like boys and
boys see girls and boys like girls and boys
like girls
4
Notations used in grammars
  • Notation used to make grammars easier to write
  • stands for zero or more occurrences of
  • Example Noun phrase Adjective Noun
  • Meaning A Noun phrase is zero or more
    Adjectives followed by a Noun.
  • ltb cgt stands for either a b or a c.
  • Example Expression Term lt gt Term
  • Meaning An Expression is a Term followed by
    (either or ) followed by a Term
  • Alternative Expression is Term Term or Term
    Term

5
Grammar for arithmetic expressions
  • Expression E -- marks the end of the
    Expression
  • E T lt gt T
  • T F lt /gt F
  • F Integer
  • F F
  • F ( E )

An E is a T followed by any number of things of
the form lt gt T Here are four Es T T
T T T T T T - T - T T
6
Syntax trees
  • Expression E F Integer
  • E T lt gt T F F
  • T F lt /gt F F ( E )

Expression
Expression
E
E
T
T T
F
F
F
2
2 3
7
Trees
root of tree
Expression
E
The node labeled E is the parent of the nodes
labeled T and . These nodes are the children of
node E. Nodes labeled T and are siblings.
T T
F
F
2 3
leaf of tree
8
Grammar gives precedence to over
  • Expression E F Integer
  • E T lt gt T F F
  • T F lt /gt F F ( E )

E
T
After doing the plus first, the tree cannot be
completed
T
T
E
T T
F
F
F
F
F
F
2 3 4 2
3 4
9
Grammar gives precedence to over
  • Expression E F Integer
  • E T lt gt T F F
  • T F lt /gt F F ( E )

Mutual recursion Defined E in terms of T, T in
terms of F, F in terms of E.
E
E
T
F
T
T
E
F
F
F
T T
F
F
F
2 3 4 ( 2
3 ) 4
10
Writing a parser for the language
  • Expression E F Integer
  • E T lt gt T F F
  • T F lt /gt F F ( E )

Parser for a language is a program that reads in
a string of characters and tells whether it is a
sentence of the language or not. In addition, it
might construct a syntax tree for the
sentence. We will write a parser for the
language of expressions that appears above.
11
Writing a parser for the language
  • Expression E F Integer
  • E T lt gt T F F
  • T F lt /gt F F ( E )

The scanner is the part of the program that reads
in characters and produces tokens from them,
deleting all whitespace. 22 35
46 / 2

12
Writing a parser for the language
  • Expression E F Integer
  • E T lt gt T F F
  • T F lt /gt F F ( E )

Scan.getToken() is always you the token being
processed. Scan.scan() deletes the token being
processed, making the next one in the input the
one being processed, and returns the new one.
22 35
46 / 2
35 46 / 2
35 46 / 2

46 / 2
46 / 2
13
Writing a parser for the language
  • Expression E F Integer
  • E T lt gt T F F
  • T F lt /gt F F ( E )

For E, T, F, write a method with this spec (we
show only E) / Token Scan.getToken() is
first token of a sentence for E. Parse it,
giving error mess. if there are mistakes. After
the parse, Scan.getToken should be the symbol
following the parsed E. / public static void
parseE(). 2 ( 3 4
5 ) 6 after call parse(), situation is
this 2 ( 3 4 5 )
6
Scan.getToken()
Scan.getToken()
14
Writing a parser for the language
  • E T lt gt T

/ Token Scan.getToken() is first token of a
sentence for E. Parse it, giving error mess.
if there are mistakes. After the parse,
Scan.getToken should be the symbol following the
parsed E. / public static void parseE()
parseT() while
(Scan.getToken() is or - ) Scan.scan() pa
rse(T)
15
Writing a parser for the language
  • Expression E F Integer
  • E T lt gt T F F
  • T F lt /gt F F ( E )

We now use the blackboard. You should look at the
final program, which is on the course website.
Download it and play with it. Parts of it will be
discussed in recitation this week.
Write a Comment
User Comments (0)
About PowerShow.com