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 JavaScript
Loïc Knuchel
 
Python Homework Help
Python Homework HelpPython Homework Help
Python 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
Eleanor 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 184
Mahmoud 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 type
Megha V
 
Stacks
Stacks Stacks
Stacks
Ashish Sethi
 
Property-Based Testing
Property-Based TestingProperty-Based Testing
Property-Based Testing
Shai 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 javascript
jonathanfmills
 
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
Yoshi Watanabe
 
Jquery 13 cheatsheet_v1
Jquery 13 cheatsheet_v1Jquery 13 cheatsheet_v1
Jquery 13 cheatsheet_v1
ilesh raval
 
Jquery 1.3 cheatsheet_v1
Jquery 1.3 cheatsheet_v1Jquery 1.3 cheatsheet_v1
Jquery 1.3 cheatsheet_v1
Sultan Khan
 
MySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZEMySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZE
Norvald 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 2007
Damien 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 Cheatsheet
Isham 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 Profile
anoopkumar
 
TheRefCheck.com
TheRefCheck.comTheRefCheck.com
TheRefCheck.com
therefcheck
 
Museu do Vaticano
Museu do VaticanoMuseu do Vaticano
Museu do Vaticano
guestededef
 
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
Joel 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 2016
Joel 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 Algorithms
pppepito86
 
Frsa
FrsaFrsa
Frsa
_111
 
Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
JayeshGadhave1
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
ikdysfm
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data Structure
Chan Shik Lim
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
Svet Ivantchev
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
Ramachendran Logarajah
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
Shakil Ahmed
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
K Hari Shankar
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
PrudhviVuda
 
Python
PythonPython
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-std
Paul 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.pdf
Rahul04August
 
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
Sunil Yadav
 
Groovy Refactoring Patterns
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring Patterns
Naresha K
 
Pratt Parser in Python
Pratt Parser in PythonPratt Parser in Python
Pratt Parser in Python
Percolate
 
Array data structure
Array data structureArray data structure
Array data structure
maamir 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

Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 

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