Introduction to ShiftReduce Parsing - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Introduction to ShiftReduce Parsing

Description:

1. Initialize the parse stack to contain a single state s0, where s0 is the ... If the action table entry is accept, then terminate the parse with success. ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 24
Provided by: davidbru
Category:

less

Transcript and Presenter's Notes

Title: Introduction to ShiftReduce Parsing


1
Introduction to Shift-Reduce Parsing
  • uses parse stack
  • symbols from input shifted onto parse stack
  • if prefix symbol on top stack matches RHS of
    grammar rule, parser reduces RHS of rule to LHS
  • process continues until parser terminates
  • terminates when input is legal and accepted by
    parser

2
Control of the parser
  • Action Table
  • The action table is a table with rows indexed by
    states and columns indexed by terminal symbols
  • action taken by the parser depends on the
    contents of actionst, which can contain four
    different kinds of entries
  • Shift s' - Shift state s' onto the parse stack.
  • Reduce r - Reduce by rule r.
  • Accept - Terminate the parse with success,
    accepting the input.
  • Error - Signal a parse error.
  • Goto Table
  • When the parser is in state s immediately after
    reducing by rule N, then the next state to enter
    is given by gotosN.

3
Operation of a parser is as follows
  • 1. Initialize the parse stack to contain a single
    state s0, where s0 is the distinguished initial
    state of the parser.
  • 2. Use the state s on top of the parse stack and
    the current lookahead t to consult the action
    table entry actionst
  • If the action table entry is shift s then push
    state s onto the stack and advance the input so
    that the lookahead is set to the next token.
  • If the action table entry is reduce r and rule r
    has m symbols in its RHS, then pop m symbols off
    the parse stack. Let s be the state now revealed
    on top of the parse stack and N be the LHS
    nonterminal for rule r. Then consult the goto
    table and push the state given by gotosN
    onto the stack. The lookahead token is not
    changed by this step.
  • If the action table entry is accept, then
    terminate the parse with success.
  • If the action table entry is error, then signal
    an error.
  • 3. Repeat step (2) until the parser terminates.

4
consider the following simple grammar
  • 0) S stmt ltEOFgt
  • 1) stmt ID expr
  • 2) expr expr ID
  • 3) expr expr - ID
  • 4) expr ID
  • which describes assignment statements like a b
    c - d. (Rule 0 is a special augmenting
    production added to the grammar).

5
Parser Tables
6
A trace of the parser on input a b c - d
7
Construction of Shift-Reduce Parsing Tables
  • The general idea of bottom-up parsing is to
    repeatedly match the RHS of some rule and reduce
    it to the rules LHS.
  • To identify the matching RHSs, the parser needs
    to keep track of all possible rules which may
    match.
  • This is done by means of the parser state, where
    each state keeps track of the set of rules the
    parser may currently be in, and how far along the
    parser may be within each rule.

8
Consider the grammar
  • 0) S stmt ltEOFgt
  • 1) stmt ID expr
  • 2) expr expr ID
  • 3) expr expr - ID
  • 4) expr ID
  • denote an item as a rule with a dot . just before
    the next expected symbol.

9
Parser
  • 0) S . stmt ltEOFgt
  • State 0
  • 0) S . stmt ltEOFgt
  • 1) stmt . ID expr

10
  • State 1
  • 1) stmt ID . expr
  • State 2
  • 0) S stmt . ltEOFgt

11
  • State 3
  • 1) stmt ID . expr
  • the dot is before the nonterminal expr, the
    parser could be in any of the rules for expr.
  • State 3
  • 1) stmt ID . expr
  • 2) expr . expr ID
  • 3) expr . expr - ID

12
  • 4) expr . ID
  • Continue this process of following all possible
    transitions out of states until we cannot
    construct any new states.
  • Completing this construction results in an
    automaton called a LR(0) machine. The transitions
    on terminal symbols correspond to shift actions
    in the parser the transitions on nonterminal
    symbols correspond to goto actions in the parser.

13
For our example grammar, we get the following
LR(0) machine
  • State 0
  • 0) S . stmt
  • 1) stmt . ID expr
  • GOTO 2 on stmt
  • SHIFT 1 on ID
  • State 1
  • 1) stmt ID . expr
  • SHIFT 3 on
  • State 2
  • 0) S stmt .
  • SHIFT 4 on
  • State 3
  • 1) stmt ID . expr
  • 2) expr . expr ID
  • 3) expr . expr - ID
  • 4) expr . ID
  • GOTO 6 on expr
  • SHIFT 5 on ID

14
  • State 4
  • 0) S stmt .
  • State 5
  • 4) expr ID .
  • State 6
  • 1) stmt ID expr .
  • 2) expr expr . ID
  • 3) expr expr . - ID
  • SHIFT 7 on
  • SHIFT 8 on -
  • State 7
  • 2) expr expr . ID
  • SHIFT 9 on ID
  • State 8
  • 3) expr expr - . ID
  • SHIFT 10 on ID
  • State 9
  • 2) expr expr ID .
  • State 10
  • 3) expr expr - ID .

15
Computer Architecture
  • To generate efficient code, a translator needs to
    understand of the target architecture

16
Instruction Cycle
17
Memory Hierarchy
18
Data Representation
  • Instructions
  • Addresses
  • Binary Integers
  • Floating point numbers
  • Characters
  • BCD (binary-coded decimals)

19
Instructions
  • move
  • computation
  • control transfer
  • special operations
  • addressing modes
  • conditional branches

20
Integers
  • unsigned integers have values ranging from 0 to
    2n -1
  • signed integers have values in the range of
    -2n-1 to 2n-1
  • 2s complement arithmetic

21
Floating point arithmetic
  • 1985 IEEE Std 754

22
Architecture Evolution
  • 1960s - IBM microprogramming
  • mid-1970s - VLSI
  • early 1980s
  • 32 bit processor cold be implemented on a single
    chip
  • processor speeds increased at rate higher than
    memory speed
  • compilers could match speed of assembly language

23
RISC
  • low level instruction set
  • large number of registers
  • optimizing compilers
Write a Comment
User Comments (0)
About PowerShow.com