SlideShare a Scribd company logo
Understanding Lemon generated Parser.
       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: It is the parser index value.
State Number : it holds state number ,which is used for the calculating with next State.
Symbol Index: Symbol Index, The integer unique Symbol id.
Token Value: Token Value.
Initially Parser Stack is initializes by $ symbol

Parser Stack Index           State No                     Symbol Index                 Token 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
                 Do {
                    get action value
                 if(value > totalstate){
                 reduce rule of (value –totalstate)
                 }
                 else{
                 push current token with value on stack
                 do the Rule Action
                 }
                 While(push current token);



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

Rules Index values are follows :


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

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

Parser Stack 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)

Parser Stack 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)

Parser Stack 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
Parser Stack 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);

Parser Stack 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)

Parser Stack 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
Parser Stack 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




Parser Stack 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
Parser Stack 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)
Parser Stack 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);

Parser Stack 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

Parser Stack 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
Parser Stack 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
Parser Stack Index     State No             Symbol                         Value
1                      12                   program=INTEGER(5)             29
0                      0                    $                              0



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

More Related Content

What's hot

Programmation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScriptProgrammation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScriptLoïc Knuchel
 
An introduction to functional programming with go
An introduction to functional programming with goAn introduction to functional programming with go
An introduction to functional programming with goEleanor McHugh
 
Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)진성 오
 
Python 내장 함수
Python 내장 함수Python 내장 함수
Python 내장 함수용 최
 
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
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data typeMegha V
 
Property-Based Testing
Property-Based TestingProperty-Based Testing
Property-Based TestingShai Geva
 
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
 
Merge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughMerge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughYoshi Watanabe
 
Jquery 13 cheatsheet_v1
Jquery 13 cheatsheet_v1Jquery 13 cheatsheet_v1
Jquery 13 cheatsheet_v1ilesh raval
 
Jquery 1.3 cheatsheet_v1
Jquery 1.3 cheatsheet_v1Jquery 1.3 cheatsheet_v1
Jquery 1.3 cheatsheet_v1Sultan Khan
 
MySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZEMySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZENorvald Ryeng
 
Refactoring 메소드 호출의 단순화
Refactoring 메소드 호출의 단순화Refactoring 메소드 호출의 단순화
Refactoring 메소드 호출의 단순화HyeonSeok Choi
 
Optimizing Queries with Explain
Optimizing Queries with ExplainOptimizing Queries with Explain
Optimizing Queries with ExplainMYXPLAIN
 
PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007Damien Seguy
 
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
 

What's hot (20)

Programmation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScriptProgrammation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScript
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
An introduction to functional programming with go
An introduction to functional programming with goAn introduction to functional programming with go
An introduction to functional programming with go
 
Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)
 
Python 내장 함수
Python 내장 함수Python 내장 함수
Python 내장 함수
 
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
 
Into Clojure
Into ClojureInto Clojure
Into Clojure
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
 
Stacks
Stacks Stacks
Stacks
 
Property-Based Testing
Property-Based TestingProperty-Based Testing
Property-Based Testing
 
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
 
Merge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughMerge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk through
 
Jquery 13 cheatsheet_v1
Jquery 13 cheatsheet_v1Jquery 13 cheatsheet_v1
Jquery 13 cheatsheet_v1
 
Jquery 1.3 cheatsheet_v1
Jquery 1.3 cheatsheet_v1Jquery 1.3 cheatsheet_v1
Jquery 1.3 cheatsheet_v1
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
MySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZEMySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZE
 
Refactoring 메소드 호출의 단순화
Refactoring 메소드 호출의 단순화Refactoring 메소드 호출의 단순화
Refactoring 메소드 호출의 단순화
 
Optimizing Queries with Explain
Optimizing Queries with ExplainOptimizing Queries with Explain
Optimizing Queries with Explain
 
PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007
 
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
 

Viewers also liked

Akshar Corporate Profile
Akshar Corporate ProfileAkshar Corporate Profile
Akshar Corporate Profileanoopkumar
 
Museu do Vaticano
Museu do VaticanoMuseu do Vaticano
Museu do Vaticanoguestededef
 
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
 
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 (6)

Akshar Corporate Profile
Akshar Corporate ProfileAkshar Corporate Profile
Akshar Corporate Profile
 
TheRefCheck.com
TheRefCheck.comTheRefCheck.com
TheRefCheck.com
 
Web Presen
Web PresenWeb Presen
Web Presen
 
Museu do Vaticano
Museu do VaticanoMuseu do Vaticano
Museu do Vaticano
 
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
 
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
 

Similar to Understanding Lemon Generated Parser

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乐群 陈
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Frsa
FrsaFrsa
Frsa_111
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data StructureChan Shik Lim
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a ElixirSvet Ivantchev
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structureShakil Ahmed
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK Hari Shankar
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingPrudhviVuda
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Ra'Fat Al-Msie'deen
 
Brief tour of psp-std
Brief tour of psp-stdBrief tour of psp-std
Brief tour of psp-stdPaul Phillips
 
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
 
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
 
Groovy Refactoring Patterns
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring PatternsNaresha K
 
Pratt Parser in Python
Pratt Parser in PythonPratt Parser in Python
Pratt Parser in PythonPercolate
 
Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 

Similar to Understanding Lemon Generated Parser (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
 
Arrays
ArraysArrays
Arrays
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Frsa
FrsaFrsa
Frsa
 
Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data Structure
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Python
PythonPython
Python
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Brief tour of psp-std
Brief tour of psp-stdBrief tour of psp-std
Brief tour of psp-std
 
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
 
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
 
Groovy Refactoring Patterns
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring Patterns
 
Pratt Parser in Python
Pratt Parser in PythonPratt Parser in Python
Pratt Parser in Python
 
Array data structure
Array data structureArray data structure
Array data structure
 

Recently uploaded

Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringDenish Jangid
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxCeline George
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...Nguyen Thanh Tu Collection
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePedroFerreira53928
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticspragatimahajan3
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...Nguyen Thanh Tu Collection
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointELaRue0
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxCapitolTechU
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryEugene Lysak
 
IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff17thcssbs2
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxjmorse8
 

Recently uploaded (20)

Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 

Understanding Lemon Generated Parser

  • 1. Understanding Lemon generated Parser. 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: It is the parser index value. State Number : it holds state number ,which is used for the calculating with next State. Symbol Index: Symbol Index, The integer unique Symbol id. Token Value: Token Value. Initially Parser Stack is initializes by $ symbol Parser Stack Index State No Symbol Index Token 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 Do { get action value if(value > totalstate){ reduce rule of (value –totalstate) } else{ push current token with value on stack do the Rule Action } While(push current token); 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) .
  • 2. 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 Rules Index values are follows : 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[] = {
  • 3. /* 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 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 Parser Stack Index State No Symbol Value 1 6 Expr=INTEGER(5) 3
  • 4. 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) Parser Stack 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) Parser Stack 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 Parser Stack Index State No Symbol Value
  • 5. 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); Parser Stack 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) Parser Stack 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
  • 6. Parser Stack 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 Parser Stack 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
  • 7. Parser Stack 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) Parser Stack 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); Parser Stack 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 Parser Stack Index State No Symbol Value 3 5 Expr=INTEGER(5) 6 2 2 PLUS 0 1 6 Expr=INTEGER(5) 23
  • 8. 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 Parser Stack 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 Parser Stack Index State No Symbol Value 1 12 program=INTEGER(5) 29 0 0 $ 0 program accept = Totalstate+totalrule+1 =7 + 4 +1 =>12 Hence program accept