SlideShare a Scribd company logo
1 of 8
Download to read offline
Understanding Lemon generated Parser.
      Lemon uses LALR (1) parsers means Look Ahead exactly one token from Left to Right. When the parser
receive the input Token, it also reads previous State number from the Parser stack. Using this Token and State
number Parser process with the pre calculated Tables like yy_action, yy_shift_ofst, yy_reduce_ofst ,
Depends upon the values the corresponding action shift or Reduce is performed.

        Current Token with Previous State Number => Action

Parser Action :
          Shift : The Current Token with value is pushed at the top of the stack
         Reduce : The corresponding rule is applied & Parser Stack is poped. Depends upon the rule number of
The number symbols is poped from stack.
Parser Stack:
         it is associated with 4 elements :
Parser Stack Index(Index): It is the parser index value.
State Number(State No) : it holds state number ,which is used for the calculating with next State.
Symbol Index(Symbol): Symbol Index, The integer unique Symbol id.
Token Value(Value): Token Value.
Initially Parser Stack is initializes by $ symbol

Index                        State No                     Symbol                       Value
0                            0                            $                            0

Parser Tables:
        yy_action:      A single table containing all the possible state number
        yy_shift_ofst:  For each state, the offset into yy_action for shifting terminals into the parser stack
        yy_reduce_ofst: For each state, the offset into yy_action holds for the reducing the parse Stack
        yyRuleInfo : From the Rule Index value , we can map this array index value
            a. LHS Symbol Index : Symbol of the LHS of the given Rule Index
            b. NRHS: Number of RHS in that Rule

Algorthim:



        Get current token ,token value till end of token
                Do {
                   get action value
                if(value > totalstate){
                reduce rule of (value –totalstate)
                tokenpushedtostack=true
                }
                else{
                push current token with value on stack
                do the Rule Action
                }
                While(tokenpushedtostack);

              If(ActionValue== Totalstate+totalrule+1) {
                 Success =>program accept
               }
             If(ActionValue== Totalstate+totalrule) {
                 Syntax error=> The input is not subset of given grammar
              }
We will take a simple example of Context free Grammar(CFG) arithmetic calculator
Example1.y
program ::= expr(A) . { printf ("Result=%dn", A); }
expr(A) ::= INTEGER(B) PLUS INTEGER(C). { A = B + C ; }
expr(A) ::= INTEGER(B) TIMES INTEGER(C).{ A = B * C ; }
expr(A) ::= INTEGER(B) .




TotalState = YYNState = 7 (more info can be seen in .out file )
TotalRules = YYNRulee= 4
This CFG rules and symbol index vales as follows

Rule Index Value      Rules
0                     program    ::= expr
1                     expr ::=   expr PLUS expr
2                     expr ::=   expr TIMES expr
3                     expr ::=   INTEGER



Symbol Index      Symbol Name
0                 $
1                 INTEGER
2                 PLUS
3                 TIMES
4                 error:
5                 expr: INTEGER
6                 program: INTEGER
The Expression are evaluates as follows
=3+4*5+6
=3 +20+6
=23+6
=29;




This Expression is given in code of 8 steps as follows
           1. Parse(p,INTEGER,3);
           2. Parse(p,PLUS,0);
           3. Parse(p,INTEGER,4);
           4. Parse(p,TIMES,0);
           5. Parse(p,INTEGER,5);
           6. Parse(p,PLUS,0);
           7. Parse(p,INTEGER,6);
           8. Parse(p,0,0);

static const YYACTIONTYPE yy_action[] = {
 /*    0 */    7, 5, 2, 1, 10, 13, 10, 10,          8, 13,
 /* 10 */      8, 1, 9, 13, 9, 9, 6, 12,            4, 13,
/* 20 */       3,
};
static const signed char yy_shift_ofst[] = {
 /*    0 */ 17, 17, 17, 12, 4, 8, 0,
};

static const signed char yy_reduce_ofst[] = {
 /*    0 */ 11, 15, -4, 1, 1, 1, 1,
};

}
yyRuleInfo[] = {
  { 6, 1 }, => program = INTEGER (Total RHS =1)
  { 5, 3 }, => expr = expr +expr (Total RHS =3)
  { 5, 3 },=> expr =expr *expr (Total RHS =3)
  { 5, 1 },   expr = INTEGER(Total RHS =1)
};




1.Parse(p,INTEGER,3);
   Calculate Action :
        Current Token = INTEGER = 1, Previous State = 0

        Action = yy_action[ yy_shift_ofst[Previous State]+Current Token ]
               = yy_action[yy_shift_ofst[0]+1]
               = yy_action[17+1] => 4

4 < 7 then shift Shift this token in Parser stack

Index                   State No                Symbol                           Value
1                       4                       INTEGER                          3
0                       0                       $                                0

2.Parse(p,PLUS,0);

1.Calculate Action
Current Token = PLUS = 2, Previous State = 4
         Action = yy_action[ yy_shift_ofst[4]+2 ]
                 = yy_action[4+2] =>10
Action value > YYNState then Reduce
10 > 7

1.Reduce Action:
   Reduce Rule Index =action value – TotalState => 10-7= 3 => expr ::= INTEGER
        ReduceToken = yyRuleinfo[3].lhs => 5 (expr : integer)
        TotalRHSSymbol = yyRuleinfo[3].nrhs => 1
   Stateno :
        get Previous Stack Index = Current Stack Index – TotalRHSSymbol
                                 = 1-1 => 0
        PreviousStateno = stack[Previous Stack Index].stateno => 0
=yy_action[yy_reduce_ofst[PreviousStateno] +Token]
                         =yy_action[yy_reduce_ofst[0] +Token]
                         = yy_action[11+5] => 6

Index                  State No                Symbol                         Value
1                      6                       Expr=INTEGER(5)                3
0                      0                       $                              0

2.Calculate Action
Current Token = PLUS = 2, Previous State = 6

        Action = yy_action[ yy_shift_ofst[6]+2 ]
               = yy_action[0+2] =>2



Action value< yyNState then push to stack push(2,PLUS,0)

Index                  State No                Symbol                         Value
2                      2                       PLUS(2)                        0
1                      6                       Expr=INTEGER(5)                3
0                      0                       $                              0




3.Parse(p,INTEGER,4);

Calculate Action:
         Current Token = INTEGER =1, Previous State = 2
                          =yy_action[ yy_shift_ofst[2]+1 ]
                          = yy_action[17+1] =>4
Action value< yyNState then push to stack push(4,INTEGER,4)

Index                  State No                Symbol                         Value
3                      4                       INTEGER(1)                     4
2                      2                       PLUS(2)                        0
1                      6                       Expr=INTEGER(5)                3
0                      0                       $                              0

4.Parse(p,TIMES,0);

Calculate Action
Current Token = TIMES =3, Previous State = 4
         Action = yy_action[ yy_shift_ofst[4]+3 ]
                = yy_action[4+3] =>10
1.Reduce Action :
         Reduce Rule Index =action value – TotalState => 10-7= 3 => expr ::= INTEGER
ReduceToken = yyRuleinfo[3].lhs => 5(expr : integer)
TotalRHSSymbol = yyRuleinfo[3].nrhs => 1
Stateno :
get Previous Stack Index: Current Stack Index – TotalRHSSymbol
                              =3-1 => 2
PreviousStateno= stack[2].stateno => 2
        =yy_action[yy_reduce_ofst[PreviousStateno] +Token]
       =yy_action[yy_reduce_ofst[2] +5]
        = yy_action[-4+5] => 5
Reduce stack index 3 & apply rule 3
Index                State No           Symbol                                Value
3                    5                  Expr=INTEGER(5)                       4
2                    2                  PLUS(2)                               0
1                    6                  Expr=INTEGER(5)                       3
0                    0                  $                                     0

Calculate Action
Current Token = TIMES =3, Previous State = 5
         =yy_action[ yy_shift_ofst[5]+3 ]
         = yy_action[8+3] =>1
Push stack (1,Times,0);

Index                 State No                 Symbol                         Value
4                     1                        TIMES                          0
3                     5                        Expr=INTEGER(5)                4
2                     2                        PLUS(2)                        0
1                     6                        Expr=INTEGER(5)                3
0                     0                        $                              0

5.Parse(p,INTEGER,5);

Calculate Action
Current Token = INTEGER =1, Previous State = 1
          =yy_action[ yy_shift_ofst[1]+1 ]
          = yy_action[17+1] =>4
Action value< yyNState then push to stack push(4,INTEGER,5)

Index                 State No                 Symbol                         Value
5                     4                        INTEGER                        5
4                     1                        TIMES                          0
3                     5                        Expr=INTEGER(5)                4
2                     2                        PLUS(2)                        0
1                     6                        Expr=INTEGER(5)                3
0                     0                        $                              0

6.Parse(p,PLUS,0);
1.Calculate Action
Current Token = PLUS =2, Previous State = 4
         =yy_action[ yy_shift_ofst[4]+2 ]
         = yy_action[4+1] =>10
10 > 7 then reduce
1.Reduce Action :
Reduce Rule Index =action value – TotalState => 10-7= 3 => expr ::= INTEGER
ReduceToken = yyRuleinfo[3].lhs => 5
TotalRHSSymbol = yyRuleinfo[3].nrhs => 1
Stateno :
get Previous Stack Index: Current Stack Index – TotalRHSSymbol
                             =5-1 => 4
PreviousStateno= stack[4].stateno => 1
=yy_action[yy_reduce_ofst[PreviousStateno] +Token]
        =yy_action[yy_reduce_ofst[1] +5]
         = yy_action[15+5] => 3



apply rule 3 at stack index 5

Index                  State No              Symbol                         Value
5                      3                     Expr=INTEGER(5)                5
4                      1                     TIMES                          0
3                      5                     Expr=INTEGER(5)                4
2                      2                     PLUS(2)                        0
1                      6                     Expr=INTEGER(5)                3
0                      0                     $                              0



2. Calculate Action
Current Token = PLUS =2, Previous State = 3
                  =yy_action[ yy_shift_ofst[3]+2 ]
                 = yy_action[12+2] =>9
9> 7 then
2.Reduce Action :
Reduce Rule Index =action value – TotalState => 9-7= 2 => expr ::= expr TIMES expr
ReduceToken = yyRuleinfo[2].lhs => 5
TotalRHSSymbol = yyRuleinfo[2].nrhs => 3
Stateno :
get Previous Stack Index: Current Stack Index – TotalRHSSymbol
                             =5-3 => 2
PreviousStateno= stack[2].stateno => 2
          =yy_action[yy_reduce_ofst[PreviousStateno] +Token]
         =yy_action[yy_reduce_ofst[2] +5]
          = yy_action[-4+5] => 5



Index                  State No              Symbol                         Value
3                      5                     Expr=INTEGER(5)                20
2                      2                     PLUS(2)                        0
1                      6                     Expr=INTEGER(5)                3
0                      0                     $                              0

3.Calculate Action

Current Token = PLUS =2,Previous State = 5
                 =yy_action[ yy_shift_ofst[5]+2 ]
                 = yy_action[8+2] =>8
8> 7 then

3.Reduce Action :
Reduce Rule Index =action value – TotalState => 8-7= 1=> expr ::= exprPLUS expr
ReduceToken = yyRuleinfo[2].lhs => 5
TotalRHSSymbol = yyRuleinfo[2].nrhs => 3
Stateno :
get Previous Stack Index: Current Stack Index – TotalRHSSymbol
                             =3-3 => 0
PreviousStateno= stack[2].stateno => 2
        =yy_action[yy_reduce_ofst[PreviousStateno] +Token]
       =yy_action[yy_reduce_ofst[0] +5]
        = yy_action[11+5] => 6
Index                 State No             Symbol                            Value
1                     6                    Expr=INTEGER(5)                   23
0                     0                    $                                 0



3.Calculate Action
Current Token = PLUS =2
Previous State = 6
                 =yy_action[ yy_shift_ofst[6]+2 ]
                = yy_action[0+2] =>2
2> 7 then push stack (2,PLUS,0)
Index                   State No              Symbol                         Value
2                       2                     PLUS                           0
1                       6                     Expr=INTEGER(5)                23
0                       0                     $                              0

7.Parse(p,INTEGER,6);
Calculate Action :
Current Token = INTEGER =1, Previous State =2
                 =yy_action[ yy_shift_ofst[2]+1 ]
                  = yy_action[17+1] =>4
         Push stack(4,INTEGER,6);

Index                  State No               Symbol                         Value
3                      4                      INTEGER                        6
2                      2                      PLUS                           0
1                      6                      Expr=INTEGER(5)                23
0                      0                      $                              0



8.Parse(p,0,0);
Calculate Action :
Current Token = $ =0,Previous State =4
                =yy_action[ yy_shift_ofst[4]+0 ]
               = yy_action[4+0] =>10

1.Reduce Action :
Reduce Rule Index =action value – TotalState => 10-7= 3=> expr ::= INTEGER
ReduceToken = yyRuleinfo[3].lhs => 5
TotalRHSSymbol = yyRuleinfo[3].nrhs => 1
Stateno :
get Previous Stack Index: Current Stack Index – TotalRHSSymbol
                             =3-1 => 2
PreviousStateno= stack[2].stateno => 2
         =yy_action[yy_reduce_ofst[PreviousStateno] +Token]
        =yy_action[yy_reduce_ofst[2] +5]
         = yy_action[-4+5] => 5

Index                  State No               Symbol                         Value
3                      5                      Expr=INTEGER(5)                6
2                      2                      PLUS                          0
1                      6                      Expr=INTEGER(5)               23
0                      0                      $                             0

1.Calculate Action :
Current Token = $ =0 , Previous State =5
                 =yy_action[ yy_shift_ofst[5]+0 ]
                = yy_action[8+0] =>8
         8-7 => rule 1
Reduce Rule Index =action value – TotalState =>8-7= 1=> expr ::= expr PLUS expr
ReduceToken = yyRuleinfo[1].lhs => 5
TotalRHSSymbol = yyRuleinfo[1].nrhs => 3
Stateno :
get Previous Stack Index: Current Stack Index – TotalRHSSymbol
                             =3-3 => 0
PreviousStateno= stack[0].stateno => 0
          =yy_action[yy_reduce_ofst[PreviousStateno] +Token]
         =yy_action[yy_reduce_ofst[0] +5]
          = yy_action[11+5] => 6
Reduce Rule 1
Index                   State No              Symbol                         Value
1                       6                     Expr=INTEGER(5)                29
0                       0                     $                              0

2. Calculate Action :
Current Token = $ =0 , Previous State =6
                 =yy_action[ yy_shift_ofst[6]+0 ]
               = yy_action[0+0] =>7
         7>7 =>

1.Reduce Action :
Reduce Rule Index =action value – TotalState => 7-7= 0=> program ::= INTEGER
ReduceToken = yyRuleinfo[0].lhs => 6
TotalRHSSymbol = yyRuleinfo[0].nrhs => 1
Stateno :
get Previous Stack Index: Current Stack Index – TotalRHSSymbol
                             =1-1 => 0
PreviousStateno= stack[0].stateno => 0
         =yy_action[yy_reduce_ofst[PreviousStateno] +Token]
        =yy_action[yy_reduce_ofst[0] +6]
         = yy_action[11+6] =>12
Index                  State No             Symbol                         Value
1                      12                   program=INTEGER(6)             29
0                      0                    $                              0



program accept = Totalstate+totalrule+1
                 =7 + 4 +1 =>12
               Hence program accept

More Related Content

What's hot

Property-Based Testing
Property-Based TestingProperty-Based Testing
Property-Based TestingShai Geva
 
The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184Mahmoud Samir Fayed
 
Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)진성 오
 
A Skeptics guide to functional style javascript
A Skeptics guide to functional style javascriptA Skeptics guide to functional style javascript
A Skeptics guide to functional style javascriptjonathanfmills
 
Python 내장 함수
Python 내장 함수Python 내장 함수
Python 내장 함수용 최
 
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...Flink Forward
 
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...The1 Uploader
 
Jquery 1.3 cheatsheet_v1
Jquery 1.3 cheatsheet_v1Jquery 1.3 cheatsheet_v1
Jquery 1.3 cheatsheet_v1Sultan Khan
 
Jquery 13 cheatsheet_v1
Jquery 13 cheatsheet_v1Jquery 13 cheatsheet_v1
Jquery 13 cheatsheet_v1ilesh raval
 
Computation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine MatricesComputation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine MatricesLossian Barbosa Bacelar Miranda
 
Functions
FunctionsFunctions
FunctionsJJkedst
 
Evolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and EffectiveEvolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and EffectiveNaresha K
 
Python 3.x Dictionaries and Sets Cheatsheet
Python 3.x Dictionaries and Sets CheatsheetPython 3.x Dictionaries and Sets Cheatsheet
Python 3.x Dictionaries and Sets CheatsheetIsham Rashik
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersTikal Knowledge
 

What's hot (20)

Property-Based Testing
Property-Based TestingProperty-Based Testing
Property-Based Testing
 
The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184
 
Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)
 
A Skeptics guide to functional style javascript
A Skeptics guide to functional style javascriptA Skeptics guide to functional style javascript
A Skeptics guide to functional style javascript
 
Stacks
Stacks Stacks
Stacks
 
Python 내장 함수
Python 내장 함수Python 내장 함수
Python 내장 함수
 
Into Clojure
Into ClojureInto Clojure
Into Clojure
 
Talk Delivered In Seminar Lisbon 2007
Talk Delivered In Seminar Lisbon  2007Talk Delivered In Seminar Lisbon  2007
Talk Delivered In Seminar Lisbon 2007
 
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
 
Jquery 1.3 cheatsheet_v1
Jquery 1.3 cheatsheet_v1Jquery 1.3 cheatsheet_v1
Jquery 1.3 cheatsheet_v1
 
Jquery 13 cheatsheet_v1
Jquery 13 cheatsheet_v1Jquery 13 cheatsheet_v1
Jquery 13 cheatsheet_v1
 
Computation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine MatricesComputation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine Matrices
 
Promise
PromisePromise
Promise
 
Functions
FunctionsFunctions
Functions
 
Evolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and EffectiveEvolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and Effective
 
Python 3.x Dictionaries and Sets Cheatsheet
Python 3.x Dictionaries and Sets CheatsheetPython 3.x Dictionaries and Sets Cheatsheet
Python 3.x Dictionaries and Sets Cheatsheet
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
 

Viewers also liked

Akshar Corporate Profile
Akshar Corporate ProfileAkshar Corporate Profile
Akshar Corporate Profileanoopkumar
 
Precipitation Reaction Screencast
Precipitation Reaction ScreencastPrecipitation Reaction Screencast
Precipitation Reaction Screencasteversolls
 
Social Media Week agency2. Be A Leader in Social Media
Social Media Week agency2. Be A Leader in Social MediaSocial Media Week agency2. Be A Leader in Social Media
Social Media Week agency2. Be A Leader in Social MediaJoel Davis
 
Independence Day Packages
Independence Day PackagesIndependence Day Packages
Independence Day Packageskaurgurpreet
 
Museu do Vaticano
Museu do VaticanoMuseu do Vaticano
Museu do Vaticanoguestededef
 
TheRefCheck_ Old Friends
TheRefCheck_ Old FriendsTheRefCheck_ Old Friends
TheRefCheck_ Old Friendstherefcheck
 
The Colonel Find Outers
The Colonel Find OutersThe Colonel Find Outers
The Colonel Find Outersguestb872a7
 
Agency2 social data performance social media trends 2016
Agency2 social data performance social media trends 2016Agency2 social data performance social media trends 2016
Agency2 social data performance social media trends 2016Joel Davis
 

Viewers also liked (14)

Akshar Corporate Profile
Akshar Corporate ProfileAkshar Corporate Profile
Akshar Corporate Profile
 
Precipitation Reaction Screencast
Precipitation Reaction ScreencastPrecipitation Reaction Screencast
Precipitation Reaction Screencast
 
Social Media Week agency2. Be A Leader in Social Media
Social Media Week agency2. Be A Leader in Social MediaSocial Media Week agency2. Be A Leader in Social Media
Social Media Week agency2. Be A Leader in Social Media
 
TheRefCheck.com
TheRefCheck.comTheRefCheck.com
TheRefCheck.com
 
Independence Day Packages
Independence Day PackagesIndependence Day Packages
Independence Day Packages
 
Museu do Vaticano
Museu do VaticanoMuseu do Vaticano
Museu do Vaticano
 
TheRefCheck_ Old Friends
TheRefCheck_ Old FriendsTheRefCheck_ Old Friends
TheRefCheck_ Old Friends
 
The Colonel Find Outers
The Colonel Find OutersThe Colonel Find Outers
The Colonel Find Outers
 
TheRefCheck.com
TheRefCheck.comTheRefCheck.com
TheRefCheck.com
 
Web Presen
Web PresenWeb Presen
Web Presen
 
How To Use
How To UseHow To Use
How To Use
 
Product Brochure1
Product Brochure1Product Brochure1
Product Brochure1
 
Agency2 social data performance social media trends 2016
Agency2 social data performance social media trends 2016Agency2 social data performance social media trends 2016
Agency2 social data performance social media trends 2016
 
Library Alwyn Brochure
Library Alwyn BrochureLibrary Alwyn Brochure
Library Alwyn Brochure
 

Similar to Understanding Lemon Generated Parser Final

An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL乐群 陈
 
Hitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional ProgrammingHitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional ProgrammingSergey Shishkin
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
Frsa
FrsaFrsa
Frsa_111
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structureShakil Ahmed
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingSaurabh Singh
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingPrudhviVuda
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsSunil Yadav
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdfsowmya koneru
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a ElixirSvet Ivantchev
 
Groovy Refactoring Patterns
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring PatternsNaresha K
 
LeetCode April Coding Challenge
LeetCode April Coding ChallengeLeetCode April Coding Challenge
LeetCode April Coding ChallengeSunil Yadav
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfRahul04August
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK Hari Shankar
 
Comparison market implied volatilities with implied volatilities computed by ...
Comparison market implied volatilities with implied volatilities computed by ...Comparison market implied volatilities with implied volatilities computed by ...
Comparison market implied volatilities with implied volatilities computed by ...Yuan Jing
 

Similar to Understanding Lemon Generated Parser Final (20)

An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Hitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional ProgrammingHitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional Programming
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Arrays
ArraysArrays
Arrays
 
Frsa
FrsaFrsa
Frsa
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
Groovy Refactoring Patterns
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring Patterns
 
LeetCode April Coding Challenge
LeetCode April Coding ChallengeLeetCode April Coding Challenge
LeetCode April Coding Challenge
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
Python
PythonPython
Python
 
Comparison market implied volatilities with implied volatilities computed by ...
Comparison market implied volatilities with implied volatilities computed by ...Comparison market implied volatilities with implied volatilities computed by ...
Comparison market implied volatilities with implied volatilities computed by ...
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Recently uploaded (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

Understanding Lemon Generated Parser Final

  • 1. Understanding Lemon generated Parser. Lemon uses LALR (1) parsers means Look Ahead exactly one token from Left to Right. When the parser receive the input Token, it also reads previous State number from the Parser stack. Using this Token and State number Parser process with the pre calculated Tables like yy_action, yy_shift_ofst, yy_reduce_ofst , Depends upon the values the corresponding action shift or Reduce is performed. Current Token with Previous State Number => Action Parser Action : Shift : The Current Token with value is pushed at the top of the stack Reduce : The corresponding rule is applied & Parser Stack is poped. Depends upon the rule number of The number symbols is poped from stack. Parser Stack: it is associated with 4 elements : Parser Stack Index(Index): It is the parser index value. State Number(State No) : it holds state number ,which is used for the calculating with next State. Symbol Index(Symbol): Symbol Index, The integer unique Symbol id. Token Value(Value): Token Value. Initially Parser Stack is initializes by $ symbol Index State No Symbol Value 0 0 $ 0 Parser Tables: yy_action: A single table containing all the possible state number yy_shift_ofst: For each state, the offset into yy_action for shifting terminals into the parser stack yy_reduce_ofst: For each state, the offset into yy_action holds for the reducing the parse Stack yyRuleInfo : From the Rule Index value , we can map this array index value a. LHS Symbol Index : Symbol of the LHS of the given Rule Index b. NRHS: Number of RHS in that Rule Algorthim: Get current token ,token value till end of token Do { get action value if(value > totalstate){ reduce rule of (value –totalstate) tokenpushedtostack=true } else{ push current token with value on stack do the Rule Action } While(tokenpushedtostack); If(ActionValue== Totalstate+totalrule+1) { Success =>program accept } If(ActionValue== Totalstate+totalrule) { Syntax error=> The input is not subset of given grammar }
  • 2. We will take a simple example of Context free Grammar(CFG) arithmetic calculator Example1.y program ::= expr(A) . { printf ("Result=%dn", A); } expr(A) ::= INTEGER(B) PLUS INTEGER(C). { A = B + C ; } expr(A) ::= INTEGER(B) TIMES INTEGER(C).{ A = B * C ; } expr(A) ::= INTEGER(B) . TotalState = YYNState = 7 (more info can be seen in .out file ) TotalRules = YYNRulee= 4 This CFG rules and symbol index vales as follows Rule Index Value Rules 0 program ::= expr 1 expr ::= expr PLUS expr 2 expr ::= expr TIMES expr 3 expr ::= INTEGER Symbol Index Symbol Name 0 $ 1 INTEGER 2 PLUS 3 TIMES 4 error: 5 expr: INTEGER 6 program: INTEGER The Expression are evaluates as follows =3+4*5+6 =3 +20+6 =23+6 =29; This Expression is given in code of 8 steps as follows 1. Parse(p,INTEGER,3); 2. Parse(p,PLUS,0); 3. Parse(p,INTEGER,4); 4. Parse(p,TIMES,0); 5. Parse(p,INTEGER,5); 6. Parse(p,PLUS,0); 7. Parse(p,INTEGER,6); 8. Parse(p,0,0); static const YYACTIONTYPE yy_action[] = { /* 0 */ 7, 5, 2, 1, 10, 13, 10, 10, 8, 13, /* 10 */ 8, 1, 9, 13, 9, 9, 6, 12, 4, 13,
  • 3. /* 20 */ 3, }; static const signed char yy_shift_ofst[] = { /* 0 */ 17, 17, 17, 12, 4, 8, 0, }; static const signed char yy_reduce_ofst[] = { /* 0 */ 11, 15, -4, 1, 1, 1, 1, }; } yyRuleInfo[] = { { 6, 1 }, => program = INTEGER (Total RHS =1) { 5, 3 }, => expr = expr +expr (Total RHS =3) { 5, 3 },=> expr =expr *expr (Total RHS =3) { 5, 1 }, expr = INTEGER(Total RHS =1) }; 1.Parse(p,INTEGER,3); Calculate Action : Current Token = INTEGER = 1, Previous State = 0 Action = yy_action[ yy_shift_ofst[Previous State]+Current Token ] = yy_action[yy_shift_ofst[0]+1] = yy_action[17+1] => 4 4 < 7 then shift Shift this token in Parser stack Index State No Symbol Value 1 4 INTEGER 3 0 0 $ 0 2.Parse(p,PLUS,0); 1.Calculate Action Current Token = PLUS = 2, Previous State = 4 Action = yy_action[ yy_shift_ofst[4]+2 ] = yy_action[4+2] =>10 Action value > YYNState then Reduce 10 > 7 1.Reduce Action: Reduce Rule Index =action value – TotalState => 10-7= 3 => expr ::= INTEGER ReduceToken = yyRuleinfo[3].lhs => 5 (expr : integer) TotalRHSSymbol = yyRuleinfo[3].nrhs => 1 Stateno : get Previous Stack Index = Current Stack Index – TotalRHSSymbol = 1-1 => 0 PreviousStateno = stack[Previous Stack Index].stateno => 0
  • 4. =yy_action[yy_reduce_ofst[PreviousStateno] +Token] =yy_action[yy_reduce_ofst[0] +Token] = yy_action[11+5] => 6 Index State No Symbol Value 1 6 Expr=INTEGER(5) 3 0 0 $ 0 2.Calculate Action Current Token = PLUS = 2, Previous State = 6 Action = yy_action[ yy_shift_ofst[6]+2 ] = yy_action[0+2] =>2 Action value< yyNState then push to stack push(2,PLUS,0) Index State No Symbol Value 2 2 PLUS(2) 0 1 6 Expr=INTEGER(5) 3 0 0 $ 0 3.Parse(p,INTEGER,4); Calculate Action: Current Token = INTEGER =1, Previous State = 2 =yy_action[ yy_shift_ofst[2]+1 ] = yy_action[17+1] =>4 Action value< yyNState then push to stack push(4,INTEGER,4) Index State No Symbol Value 3 4 INTEGER(1) 4 2 2 PLUS(2) 0 1 6 Expr=INTEGER(5) 3 0 0 $ 0 4.Parse(p,TIMES,0); Calculate Action Current Token = TIMES =3, Previous State = 4 Action = yy_action[ yy_shift_ofst[4]+3 ] = yy_action[4+3] =>10 1.Reduce Action : Reduce Rule Index =action value – TotalState => 10-7= 3 => expr ::= INTEGER ReduceToken = yyRuleinfo[3].lhs => 5(expr : integer) TotalRHSSymbol = yyRuleinfo[3].nrhs => 1 Stateno : get Previous Stack Index: Current Stack Index – TotalRHSSymbol =3-1 => 2
  • 5. PreviousStateno= stack[2].stateno => 2 =yy_action[yy_reduce_ofst[PreviousStateno] +Token] =yy_action[yy_reduce_ofst[2] +5] = yy_action[-4+5] => 5 Reduce stack index 3 & apply rule 3 Index State No Symbol Value 3 5 Expr=INTEGER(5) 4 2 2 PLUS(2) 0 1 6 Expr=INTEGER(5) 3 0 0 $ 0 Calculate Action Current Token = TIMES =3, Previous State = 5 =yy_action[ yy_shift_ofst[5]+3 ] = yy_action[8+3] =>1 Push stack (1,Times,0); Index State No Symbol Value 4 1 TIMES 0 3 5 Expr=INTEGER(5) 4 2 2 PLUS(2) 0 1 6 Expr=INTEGER(5) 3 0 0 $ 0 5.Parse(p,INTEGER,5); Calculate Action Current Token = INTEGER =1, Previous State = 1 =yy_action[ yy_shift_ofst[1]+1 ] = yy_action[17+1] =>4 Action value< yyNState then push to stack push(4,INTEGER,5) Index State No Symbol Value 5 4 INTEGER 5 4 1 TIMES 0 3 5 Expr=INTEGER(5) 4 2 2 PLUS(2) 0 1 6 Expr=INTEGER(5) 3 0 0 $ 0 6.Parse(p,PLUS,0); 1.Calculate Action Current Token = PLUS =2, Previous State = 4 =yy_action[ yy_shift_ofst[4]+2 ] = yy_action[4+1] =>10 10 > 7 then reduce 1.Reduce Action : Reduce Rule Index =action value – TotalState => 10-7= 3 => expr ::= INTEGER ReduceToken = yyRuleinfo[3].lhs => 5 TotalRHSSymbol = yyRuleinfo[3].nrhs => 1 Stateno : get Previous Stack Index: Current Stack Index – TotalRHSSymbol =5-1 => 4 PreviousStateno= stack[4].stateno => 1
  • 6. =yy_action[yy_reduce_ofst[PreviousStateno] +Token] =yy_action[yy_reduce_ofst[1] +5] = yy_action[15+5] => 3 apply rule 3 at stack index 5 Index State No Symbol Value 5 3 Expr=INTEGER(5) 5 4 1 TIMES 0 3 5 Expr=INTEGER(5) 4 2 2 PLUS(2) 0 1 6 Expr=INTEGER(5) 3 0 0 $ 0 2. Calculate Action Current Token = PLUS =2, Previous State = 3 =yy_action[ yy_shift_ofst[3]+2 ] = yy_action[12+2] =>9 9> 7 then 2.Reduce Action : Reduce Rule Index =action value – TotalState => 9-7= 2 => expr ::= expr TIMES expr ReduceToken = yyRuleinfo[2].lhs => 5 TotalRHSSymbol = yyRuleinfo[2].nrhs => 3 Stateno : get Previous Stack Index: Current Stack Index – TotalRHSSymbol =5-3 => 2 PreviousStateno= stack[2].stateno => 2 =yy_action[yy_reduce_ofst[PreviousStateno] +Token] =yy_action[yy_reduce_ofst[2] +5] = yy_action[-4+5] => 5 Index State No Symbol Value 3 5 Expr=INTEGER(5) 20 2 2 PLUS(2) 0 1 6 Expr=INTEGER(5) 3 0 0 $ 0 3.Calculate Action Current Token = PLUS =2,Previous State = 5 =yy_action[ yy_shift_ofst[5]+2 ] = yy_action[8+2] =>8 8> 7 then 3.Reduce Action : Reduce Rule Index =action value – TotalState => 8-7= 1=> expr ::= exprPLUS expr ReduceToken = yyRuleinfo[2].lhs => 5 TotalRHSSymbol = yyRuleinfo[2].nrhs => 3 Stateno : get Previous Stack Index: Current Stack Index – TotalRHSSymbol =3-3 => 0
  • 7. PreviousStateno= stack[2].stateno => 2 =yy_action[yy_reduce_ofst[PreviousStateno] +Token] =yy_action[yy_reduce_ofst[0] +5] = yy_action[11+5] => 6 Index State No Symbol Value 1 6 Expr=INTEGER(5) 23 0 0 $ 0 3.Calculate Action Current Token = PLUS =2 Previous State = 6 =yy_action[ yy_shift_ofst[6]+2 ] = yy_action[0+2] =>2 2> 7 then push stack (2,PLUS,0) Index State No Symbol Value 2 2 PLUS 0 1 6 Expr=INTEGER(5) 23 0 0 $ 0 7.Parse(p,INTEGER,6); Calculate Action : Current Token = INTEGER =1, Previous State =2 =yy_action[ yy_shift_ofst[2]+1 ] = yy_action[17+1] =>4 Push stack(4,INTEGER,6); Index State No Symbol Value 3 4 INTEGER 6 2 2 PLUS 0 1 6 Expr=INTEGER(5) 23 0 0 $ 0 8.Parse(p,0,0); Calculate Action : Current Token = $ =0,Previous State =4 =yy_action[ yy_shift_ofst[4]+0 ] = yy_action[4+0] =>10 1.Reduce Action : Reduce Rule Index =action value – TotalState => 10-7= 3=> expr ::= INTEGER ReduceToken = yyRuleinfo[3].lhs => 5 TotalRHSSymbol = yyRuleinfo[3].nrhs => 1 Stateno : get Previous Stack Index: Current Stack Index – TotalRHSSymbol =3-1 => 2 PreviousStateno= stack[2].stateno => 2 =yy_action[yy_reduce_ofst[PreviousStateno] +Token] =yy_action[yy_reduce_ofst[2] +5] = yy_action[-4+5] => 5 Index State No Symbol Value 3 5 Expr=INTEGER(5) 6
  • 8. 2 2 PLUS 0 1 6 Expr=INTEGER(5) 23 0 0 $ 0 1.Calculate Action : Current Token = $ =0 , Previous State =5 =yy_action[ yy_shift_ofst[5]+0 ] = yy_action[8+0] =>8 8-7 => rule 1 Reduce Rule Index =action value – TotalState =>8-7= 1=> expr ::= expr PLUS expr ReduceToken = yyRuleinfo[1].lhs => 5 TotalRHSSymbol = yyRuleinfo[1].nrhs => 3 Stateno : get Previous Stack Index: Current Stack Index – TotalRHSSymbol =3-3 => 0 PreviousStateno= stack[0].stateno => 0 =yy_action[yy_reduce_ofst[PreviousStateno] +Token] =yy_action[yy_reduce_ofst[0] +5] = yy_action[11+5] => 6 Reduce Rule 1 Index State No Symbol Value 1 6 Expr=INTEGER(5) 29 0 0 $ 0 2. Calculate Action : Current Token = $ =0 , Previous State =6 =yy_action[ yy_shift_ofst[6]+0 ] = yy_action[0+0] =>7 7>7 => 1.Reduce Action : Reduce Rule Index =action value – TotalState => 7-7= 0=> program ::= INTEGER ReduceToken = yyRuleinfo[0].lhs => 6 TotalRHSSymbol = yyRuleinfo[0].nrhs => 1 Stateno : get Previous Stack Index: Current Stack Index – TotalRHSSymbol =1-1 => 0 PreviousStateno= stack[0].stateno => 0 =yy_action[yy_reduce_ofst[PreviousStateno] +Token] =yy_action[yy_reduce_ofst[0] +6] = yy_action[11+6] =>12 Index State No Symbol Value 1 12 program=INTEGER(6) 29 0 0 $ 0 program accept = Totalstate+totalrule+1 =7 + 4 +1 =>12 Hence program accept