CS 461 - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

CS 461

Description:

Phases of a compiler source code scanner stream of tokens parser parse tree Scanning Scanner needs to know what to expect when eating your program. identifiers ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 12
Provided by: fur102
Learn more at: http://cs.furman.edu
Category:
Tags: compiler | phases

less

Transcript and Presenter's Notes

Title: CS 461


1
CS 461 Oct. 7
  • Applications of CFLs Compiling
  • Scanning vs. parsing
  • Expression grammars
  • Associativity
  • Precedence
  • Programming language (handout)

2
Compiling
  • Grammars are used to define programming language
    and check syntax.
  • Phases of a compiler

source code
scanner
stream of tokens
parser
parse tree
3
Scanning
  • Scanner needs to know what to expect when eating
    your program.
  • identifiers
  • numbers
  • strings
  • comments
  • Specifications for tokens can be expressed by
    regular expression (or regular grammar).
  • While scanning, we can be in different states,
    such as inside a comment.

4
Parser
  • Purpose is to understand structure of program.
  • All programming structures can be expressed as
    CFG.
  • Simple example for and
  • expr ? expr digit expr digit digit
  • digit ? 0 1 2 3 4 5 6 7 8 9
  • How would we derive the string 9 5 2 ?

5
9 5 2
  • expr ? expr digit expr digit digit
  • digit ? 0 1 2 3 4 5 6 7 8 9
  • expr
  • expr digit
  • expr - digit 2
  • digit 5
  • 9
  • Leftmost derivation expr ? expr digit
  • ? expr digit digit
  • ? digit digit digit
  • ? 9 digit digit
  • ? 9 5 digit
  • ? 9 5 2

parse tree
6
Left right recursion
  • What is the difference between these 2 grammars?
    Which one is better?
  • expr ? expr digit expr digit digit
  • digit ? 0 1 2 3 4 5 6 7 8 9
  • expr ? digit expr digit expr digit
  • digit ? 0 1 2 3 4 5 6 7 8 9
  • Lets try 9 5 2 on both of these. The
    grammar must convey the order of operations!
  • Operators may be left associative or right
    associative.

7
- /
  • Question
  • How do we write grammar for all 4 operators? Can
    we do it this way
  • expr ? expr digit expr digit
  • expr digit expr / digit
  • digit
  • digit ? 0 1 2 3 4 5 6 7 8 9
  • BTW, we can ignore digits and from now on just
    replace them with num, and understand its any
    single number.

8
Precedence
  • ( /) bind stronger than ( -)
  • ( -) separate better than ( /)
  • Need to break up expression into terms
  • Ex. 9 8 2 4 / 5
  • We want to say that an expression consists of
    terms separated by and
  • And each term consists of numbers separated by
    and /
  • But which should we define first, expr or term?

9
Precedence (2)
  • Which grammar is right?
  • expr ? expr term expr term term
  • term ? term num term / num num
  • Or this one
  • expr ? expr term expr / term term
  • term ? term num term num num
  • Lets try examples 1 2 3 and 1 2
    3

10
Moral
  • If a grammar is defining something hierarchical,
    like an expression, define large groupings first.
  • Lower precedence operators appear first in
    grammar. (They separate better)
  • Ex. appears lower in parse tree than because
    it gets evaluated first.
  • In a real programming language, there can be more
    than 10 levels of precedence. C has 15!

11
C language ?
  • Handout
  • How does the grammar begin?
  • Where are the mathematical expressions?
  • Do you agree with the precedence?
  • Do you see associativity?
  • What else is defined in grammar?
  • Where are the terminals?
Write a Comment
User Comments (0)
About PowerShow.com