compiler construction · 6 precedence these two derivations point out a problem with the grammar it...

38
Compiler Construction LECTURE # 7

Upload: others

Post on 21-Oct-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

  • Compiler Construction

    LECTURE # 7

  • The CourseCourse Code: CS-4141

    Course Title: Compiler Construction

    Instructor: JAWAD AHMAD◦ Email Address: [email protected]

    Web Address: http://csandituoslahore.weebly.com/cc.html

    Term (Semester): FALL 2017

    Duration: 15/16 Weeks

    mailto:[email protected]

  • 3

    Parse TreesG

    E

    E op E

    E op Ex –

    2 * y

    Leftmost derivation

  • 4

    Parse TreesG

    E

    E op E

    E op Ex –

    2 * y

    evaluation order

    x – ( 2 * y )

  • 5

    Parse TreesG

    E

    op

    evaluation order

    (x – 2 ) * y

    E

    x –

    E

    E op E

    2

    * y

    Rightmost

    derivation

  • 6

    Precedence

    These two derivations pointout a problem with thegrammar

    It has no notion of precedence,or implied order of evaluation

  • 7

    Precedence

    To add precedence

    Create a non-terminal for each level of precedence

    Isolate corresponding part of grammar

  • 8

    Precedence

    To add precedence

    Force parser to recognize high precedence subexpressions first.

  • 9

    Precedence

    For algebraic expressions

    Multiplication and division, first. (level one)

    Subtraction and addition, next (level two)

  • 10

    1 Goal → expr2 expr → expr + term3 | expr – term4 | term5 term → term factor6 | term / factor7 | factor8 factor → number9 | Id

  • 11

    1 Goal → expr

    2 expr → expr + term

    3 | expr – term

    4 | term

    5 term → term factor

    6 | term / factor

    7 | factor

    8 factor → number

    9 | Id

    leveltwo

    levelone

  • 12

    Precedence

    This grammar is larger

    Takes more rewriting to reach some of the terminal symbols

    But it encodes expected precedence

  • 13

    Precedence

    Produces same parse tree under leftmost and rightmost derivations

    Let’s see how it parses x – 2 * y

  • 14

    Precedence

    Rule Sentential Form

    - Goal

    1 expr

    3 expr – term

    5 expr – term factor

    9 expr – term

    7 expr – factor

  • 15

    Derivations & Precedence

    Rule Sentential Form

    8 expr –

    4 term –

    7 factor –

    9 –

    The rightmost derivation

  • 16

    Parse Trees G

    E

    F

    T

    T F

    *

    T

    E

    T

    evaluation orderx – ( 2 * y )

  • 17

    Precedence

    Both leftmost and rightmost derivations give the sameexpression

    Because the grammar directly encodes the desired precedence.

  • 18

    Ambiguous Grammars

    If a grammar has more than one leftmost derivation for a single sentential form, the grammar is ambiguous

  • 19

    Ambiguous Grammars

    If a grammar has more than one rightmost derivation for a single sentential form, the grammar is ambiguous

  • 20

    Ambiguous Grammars

    The leftmost and rightmost derivations for a sentential form may differ, even in an unambiguous grammar

    Let’s consider the classic if-then-else example

  • 21

    Ambiguous Grammars

    if-then-else problem

    Stmt → if Expr then Stmt

    | if Expr then Stmt else Stmt

    | … other stmts ….

  • 22

    Ambiguous Grammars

    The following sentential form has two derivations:

    if E1 then if E2 then S else S2

  • 23

    Ambiguity

    Production 1,

    then

    Production 2:

    if E1 thenif E2 then S1

    else S2

    E1

    if

    then

    if

    then

    else

    S1

    S2

    E2

  • 24

    Ambiguity

    E1

    if

    then

    if

    then else

    S1 S2

    E2

    Production 2,

    then

    Production 1:

    if E1 thenif E2 then S1else S2

  • 25

    Ambiguity

    E1

    if

    then

    if

    then

    else

    S1

    S2

    E2

    E1

    if

    then

    if

    then else

    S1 S2

    E2

  • 26

    Removing Ambiguity

    Must rewrite grammar to avoid generating the problem

    Match each else to innermost umatched if

  • 27

    Removing Ambiguity

    1. Stmt → If E then Stmt

    2. | If E then WithElse else Stmt

    3. | Assignment

    4. WithElse → If E then WithElse else WithElse

    5. | Assignment

  • 28

    Removing Ambiguity

    Let derive the following using the rewritten grammar:

    if E1 then if E2 then A1 else A2

  • 29

    E1

    Stmt

    then

    else

    A1 A2E2

    if Expr Stmt

    Stmtthenif Expr Withelse

    This binds the else controlling A2 to inner if

  • 30

    Context-Free Grammars

    We have been using the term context-free without explaining why such rules are in fact “free of context”.

  • 31

    Context-Free Grammars

    The simple reason is that nonterminals appear by themselves to the left of the arrow in context-free rules:

    A → a

  • 32

    Context-Free Grammars

    The rule A → a says that A may be replaced by a anywhere, regardless of where A occurs.

  • 33

    Context-Free Grammars

    On the other hand, we could define a context as pair of strings b, g, such that a rule would apply only if b occurs before and g occurs after the nonterminal A.

  • 34

    Context-Sensitive Grammars

    We would write this as

    b A g → b a g

    Such a rule in which a ≠ e is called a context-sensitive grammar rule

  • Parsing Techniques

  • 36

    Parsing Techniques

    Top-down parsers

    Start at the root of the parse tree and grow towards leaves.

    Pick a production and try to match the input

  • 37

    Parsing Techniques

    Top-down parsers

    Bad “pick” may need to backtrack

    Some grammars are backtrack-free.

  • 38

    Parsing Techniques

    Bottom-up parsers

    Start at the leaves and grow toward root

    As input is consumed, encode possibilities in an internal state.