Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

Embed Size (px)

Citation preview

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    1/69

    ContextContext

    SensitiveSensitiveAnalysisAnalysis

    ContextContext

    SensitiveSensitiveAnalysisAnalysis

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    2/69

    2

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntaxvoid fie(int a, int b)

    { .... }

    void fee() {

    int f[3],g[1],h,i,j,k;char* p;

    fie(h, i, ab);

    k = f*i+j;

    h = g[17];

    p = 10;

    }

    what is wrong withthis program?

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    3/69

    3

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntaxvoid fie(int a, int b)

    { .... }

    void fee() {

    int f[3],g[1],h,i,j,k;char* p;

    fie(h, i, ab);

    k = f*i+j;

    h = g[17];

    p = 10;

    }

    dimension ofg is 1,

    index is 17

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    4/69

    4

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntaxvoid fie(int a, int b)

    { .... }

    void fee() {

    int f[3],g[1],h,i,j,k;char* p;

    fie(h, i, ab);

    k = f*i+j;

    h = g[17];

    p = 10;

    }

    wrong numberofargs to function fie

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    5/69

    5

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntaxvoid fie(int a, int b)

    { .... }

    void fee() {

    int f[3],g[1],h,i,j,k;char* p;

    fie(h, i, ab);

    k = f*i+j;

    h = g[17];

    p = 10;

    }

    f is an array;used withoutindex

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    6/69

    6

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntaxvoid fie(int a, int b)

    { .... }

    void fee() {

    int f[3],g[1],h,i,j,k;char* p;

    fie(h, i, ab);

    k = f*i+j;

    h = g[17];

    p = 10;

    }

    10 is not a

    characterstring

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    7/69

    7

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax

    To generate code, the

    compiler

    needs to answ

    er

    many questions

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    8/69

    8

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax

    Is x a scaler, an array or afunction?

    Is x declared before it isused?

    Is the expression x*y+ztype-consistent?

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    9/69

    9

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax

    Is x a scaler, an array or afunction?

    Is x declared before it isused?

    Is the expression x*y+ztype-consistent?

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    10/69

    10

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax

    Is x a scaler, an array or afunction?

    Is x declared before it isused?

    Is the expression x*y+ztype-consistent?

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    11/69

    11

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax In a[i,j,k] does a have

    three dimensions?

    Does *preference theresult of new?

    Do p and qrefer to thesame memory location?

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    12/69

    12

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax In a[i,j,k] does a have

    three dimensions?

    Does *preference theresult of new?

    Do p and qrefer to thesame memory location?

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    13/69

    13

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax In a[i,j,k] does a have

    three dimensions?

    Does *preference theresult of new?

    Do p and qrefer to thesame memory location?

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    14/69

    14

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax

    These questions are part ofcontext-sensitive analysis

    Answers depend on values,not parts of speech

    Answers may involvecomputation

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    15/69

    15

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax

    These questions are part ofcontext-sensitive analysis

    Answers depend on values,not parts of speech

    Answers may involvecomputation

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    16/69

    16

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax

    These questions are part ofcontext-sensitive analysis

    Answers depend on values,not parts of speech

    Answers may involvecomputation

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    17/69

    17

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax

    How can we answer thesequestions?

    Use formal methods Context-sensitive

    grammars Attribute grammars

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    18/69

    18

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax

    How can we answer thesequestions?

    Use formal methods Context-sensitive

    grammars Attribute grammars

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    19/69

    19

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax

    How can we answer thesequestions?

    Use formal methods Context-sensitive

    grammars Attribute grammars

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    20/69

    20

    Beyond SyntaxBeyond SyntaxBeyond SyntaxBeyond Syntax

    Use ad-hoc techniques

    Symbol tables

    ad-hoc code

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    21/69

    21

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars A CFG is augmented with a

    set of rules

    Each symbol in the derivationhas a set of values orattributes

    Rules specify how to computea value for each attribute

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    22/69

    22

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars A CFG is augmented with a

    set of rules

    Each symbol in the derivationhas a set of values orattributes

    Rules specify how to computea value for each attribute

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    23/69

    23

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars A CFG is augmented with a

    set of rules

    Each symbol in the derivationhas a set of values orattributes

    Rules specify how to computea value for each attribute

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    24/69

    24

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars

    grammar forsigned binary

    numbers (SBN)

    Number Sign List

    Sign +

    List List Bit | BitBit 01

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    25/69

    25

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars

    derivation for 1

    Number Sign List List Bit

    1

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    26/69

    26

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars

    For -101Number Sign List

    Sign List Bit

    Sign List1 Sign List Bit1 Sign List0 1

    Sign Bit0 1 Sign 1 0 1 1 0 1

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    27/69

    27

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars

    Foran attributed version ofSBN,the following attributes are needed

    Symbol AttributesNumber val

    Sign neg

    List pos, val

    Bit pos, val

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    28/69

    28

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars

    We will add rules tocompute decimal value of a

    signed binary number

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    29/69

    29

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars

    Productions Attribution Rules

    NumberSign List List.posn 0

    ifSign.neg thenNumber.valn List.valelse Number.valn List.val

    Sign + Sign.negn false

    Sign Sign.negn true

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    30/69

    30

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars

    Productions Attribution Rules

    NumberSign List List.posn 0

    ifSign.neg thenNumber.valn List.valelse Number.valn List.val

    Sign + Sign.negn false

    Sign Sign.negn true

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    31/69

    31

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute GrammarsProductions Attribution Rules

    List0 List1 Bit List1.posn List0.pos + 1Bit.posn List0.posList0.valn List1.val + Bit.val

    ListBit Bit.posn List.pos

    List.valn Bit.val

    Bit 0 Bit.valn 0

    Bit 1 Bit.valn 2Bit.pos

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    32/69

    32

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars

    Attributes are associated withnodes in parse tree

    Rules are value assignmentsassociated with productions

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    33/69

    33

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars

    Attributes are associated withnodes in parse tree

    Rules are value assignmentsassociated with productions

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    34/69

    34

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars

    Rules and parse tree definean attribute dependence

    gr

    aph Graph must be acyclic

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    35/69

    35

    Attribute GrammarsAttribute GrammarsAttribute GrammarsAttribute Grammars

    Number

    Sign List

    Bit

    1

    List.pos n 0List.valn Bit.val =1

    Number.valn List.val =1

    Bit.pos n 0

    Bit.valn 2Bit.pos =1

    Sign.neg

    n true

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    36/69

    36

    AttributesAttributesAttributesAttributes

    Attributes are distinguishedbased on the direction ofvalueflow

    1. Synthesized attributes

    2. Inherited attributes

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    37/69

    37

    Synthesized AttributesSynthesized AttributesSynthesized AttributesSynthesized Attributes

    Attributes of a node whose

    values are defined wholly in

    terms of attributes ofnodeschildren and from constants

    are called synthesized

    attributes

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    38/69

    38

    Synthesized AttributesSynthesized AttributesSynthesized AttributesSynthesized Attributes

    Values used to compute

    synthesized attributes flow

    bottom-up in the parse tree

    Good match to LR parsing

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    39/69

    39

    Inherited AttributesInherited AttributesInherited AttributesInherited Attributes

    Attributes whose values aredefined in terms of

    nodes own attributes,

    nodes siblings

    nodes parent

    Values flow top-down andlaterally in the parse tree

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    40/69

    40

    Inherited AttributesInherited AttributesInherited AttributesInherited Attributes

    Attributes whose values aredefined in terms of

    nodes own attributes,

    nodes siblings

    nodes parent

    Values flowtop-down andlaterally in the parse tree

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    41/69

    41

    Number

    SignList

    Bit

    1

    List

    List

    Bit

    1Bit

    0

    Parse tree for101

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    42/69

    42

    Number

    SignList

    Bit

    1

    val: -5

    List

    List

    Bit

    1Bit

    0

    attributed tree

    pos: 0val: 5

    pos: 0val: 1

    pos: 1

    val: 4

    pos: 1val: 0

    pos: 2val: 4

    pos: 2val: 4

    neg: true

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    43/69

    43

    Number

    SignList

    Bit

    1

    val: -5

    List

    List

    Bit

    1Bit

    0

    Inherited Attributes

    pos: 0val: 5

    pos: 0val: 1

    pos: 1

    val: 4

    pos: 1val: 0

    pos: 2val: 4

    pos: 2val: 4

    neg: true

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    44/69

    44

    Number

    SignList

    Bit

    1

    val: -5

    List

    List

    Bit

    1Bit

    0

    Synthesized Attributes

    pos: 0

    val: 5

    pos: 0

    val: 1

    pos: 1

    val: 4

    pos: 1

    val: 0

    pos: 2

    val: 4

    pos: 2

    val: 4

    neg: true

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    45/69

    45

    Number

    SignList

    Bit

    1

    val: -5

    List

    List

    Bit

    1Bit

    0

    Together

    pos: 0

    val: 5

    pos: 0

    val: 1

    pos: 1

    val: 4

    pos: 1

    val: 0

    pos: 2

    val: 4

    pos: 2

    val: 4

    neg: true

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    46/69

    46

    Number

    SignList

    Bit

    1

    val: -5

    List

    List

    Bit

    1Bit

    0

    peel away parse tree

    pos: 0

    val: 5

    pos: 0

    val: 1

    pos: 1

    val: 4

    pos: 1

    val: 0

    pos: 2

    val: 4

    pos: 2

    val: 4

    neg: true

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    47/69

    47

    1

    val: -5

    1

    0

    dependence graph

    pos: 0

    val: 5

    pos: 0

    val: 1

    pos: 1

    val: 4

    pos: 1

    val: 0

    pos: 2

    val: 4

    pos: 2

    val: 4

    neg: true

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    48/69

    48

    1

    val: -5

    1

    0

    dependence graph

    pos: 0

    val: 5

    pos: 0

    val: 1

    pos: 1

    val: 4

    pos: 1

    val: 0

    pos: 2

    val: 4

    pos: 2

    val: 4

    neg: true

    must be acyclic!

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    49/69

    49

    Evaluation MethodsEvaluation MethodsEvaluation MethodsEvaluation Methods

    Dynamic methods

    Build the parse tree

    Build the dependence graph

    Topological sort the graph

    Define attributes in topologicalorder

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    50/69

    50

    Evaluation MethodsEvaluation MethodsEvaluation MethodsEvaluation Methods

    Dynamic methods

    Build the parse tree

    Build the dependence graph

    Topological sort the graph

    Define attributes in topologicalorder

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    51/69

    51

    Evaluation MethodsEvaluation MethodsEvaluation MethodsEvaluation Methods

    Dynamic methods

    Build the parse tree

    Build the dependence graph

    Topological sort the graph

    Define attributes in topologicalorder

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    52/69

    52

    Evaluation MethodsEvaluation MethodsEvaluation MethodsEvaluation Methods

    Dynamic methods

    Build the parse tree

    Build the dependence graph

    Topological sort the graph

    Define attributes in topologicalorder

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    53/69

    53

    Evaluation MethodsEvaluation MethodsEvaluation MethodsEvaluation Methods

    Dynamic methods

    Build the parse tree

    Build the dependence graph

    Topological sort the graph

    Define attributes in topologicalorder

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    54/69

    54

    Evaluation MethodsEvaluation MethodsEvaluation MethodsEvaluation Methods

    Rule-based (treewalk)

    Analyze attribute rules at

    compiler-generation time Determine a fixed (static)

    ordering

    Evaluate nodes in that order

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    55/69

    55

    Evaluation MethodsEvaluation MethodsEvaluation MethodsEvaluation Methods

    Oblivious (passes, dataflow)

    Ignore rules and parse tree

    Pick a convenient order (atdesign time) and use it

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    56/69

    56

    ProblemsProblemsProblemsProblems

    Attribute grammars havenot achieved widespread

    use due to a myriad ofproblems

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    57/69

    57

    ProblemsProblemsProblemsProblems

    non-local computation

    traversing parse tree

    storage management forshort-lived attributes

    lack of high-qualityinexpensive tools

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    58/69

    58

    ProblemsProblemsProblemsProblems

    non-local computation

    traversing parse tree

    storage management forshort-lived attributes

    lack of high-qualityinexpensive tools

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    59/69

    59

    ProblemsProblemsProblemsProblems

    non-local computation

    traversing parse tree

    storage management forshort-lived attributes

    lack of high-qualityinexpensive tools

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    60/69

    60

    ProblemsProblemsProblemsProblems

    non-local computation

    traversing parse tree

    storage management forshort-lived attributes

    lack of high-qualityinexpensive tools

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    61/69

    61

    AdAd--Hoc AnalysisHoc AnalysisAdAd--Hoc AnalysisHoc Analysis

    In rule-based evaluators, asequence ofactions are

    associatedw

    ith gr

    ammar

    productions

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    62/69

    62

    AdAd--Hoc AnalysisHoc AnalysisAdAd--Hoc AnalysisHoc Analysis

    Organizing actions requiredfor context-sensitive analysis

    ar

    ound str

    uctur

    e of thegrammar leads to powerful,

    albeit ad-hoc, approach which

    is used on most parsers

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    63/69

    63

    AdAd--Hoc AnalysisHoc AnalysisAdAd--Hoc AnalysisHoc Analysis

    A snippet of code (action) isassociated with eachproduction that executes atparse time

    In top-down parsers, the

    snippet is added to theappropriate parsing routine

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    64/69

    64

    AdAd--Hoc AnalysisHoc AnalysisAdAd--Hoc AnalysisHoc Analysis

    A snippet of code (action) isassociated with eachproduction that executes atparse time

    In top-down parsers, the

    snippet is added to theappropriate parsing routine

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    65/69

    65

    AdAd--Hoc AnalysisHoc AnalysisAdAd--Hoc AnalysisHoc Analysis

    In a bottom-up shift-reduceparsers, the actions areperformed each time theparserperforms a reduction.

    LR(1) Sk l t PLR(1) Sk l t PLR(1) Sk l t PLR(1) Sk l t P

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    66/69

    66

    LR(1) Skeleton ParserLR(1) Skeleton ParserLR(1) Skeleton ParserLR(1) Skeleton Parserstack.push(dummy); stack.push(0);

    done = false; token = scanner.next();while (!done) {

    s = stack.top();

    if( Action[s,token] == reduce AF) {

    stack.pop(2v|F|);

    s = stack.top();

    stack.push(A);

    stack.push(Goto[s,A]);

    }

    else if( Action[s,token] == shifti){stack.push(token); stack.push(i);

    token = scanner.next();

    }

    LR(1) Sk l t PLR(1) Sk l t PLR(1) Sk l t PLR(1) Sk l t P

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    67/69

    67

    LR(1) Skeleton ParserLR(1) Skeleton ParserLR(1) Skeleton ParserLR(1) Skeleton Parserstack.push(dummy); stack.push(0);

    done = false; token = scanner.next();while (!done) {

    s = stack.top();

    if( Action[s,token] == reduce AF) {

    stack.pop(2v|F|);

    s = stack.top();

    stack.push(A);

    stack.push(Goto[s,A]);

    }

    else if( Action[s,token] == shifti){stack.push(token); stack.push(i);

    token = scanner.next();

    }

    LR(1) Sk l t PLR(1) Sk l t PLR(1) Sk l t PLR(1) Sk l t P

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    68/69

    68

    LR(1) Skeleton ParserLR(1) Skeleton ParserLR(1) Skeleton ParserLR(1) Skeleton Parser

    if( Action[s,token] == reduce AF) {

    stack.pop(2v|F|);

    s = stack.top();

    stack.push(A);

    stack.push(Goto[s,A]);}

    invoke the code snippet

  • 8/7/2019 Context Sensitive Analysis and Attribute Grammar - Compiler Design - Dr. D. P. Sharma - NIT Surathkal by wahid311

    69/69

    69

    Productions Code snippet

    NumberSign List Number.val Sign.valv List.val

    Sign + Sign.val 1

    Sign Sign.val 1

    List Bit List.val Bit.val

    List0 List1 Bit List0.val 2vList1.val + Bit.val

    Bit 0 Bit.val 0

    Bit 1 Bit.val 1