Professional Documents
Culture Documents
LR Parsing Algorithm (Pseudocode) : Festin, Kerr Oliver Bscs 2A
LR Parsing Algorithm (Pseudocode) : Festin, Kerr Oliver Bscs 2A
LR Parsing Algorithm (Pseudocode) : Festin, Kerr Oliver Bscs 2A
BSCS 2A
Grammatical sequence: 3
S->BB;
B->aB;
B->b;
Calculated closure:
Clousure0:
s->.S
S->.BB
B->.aB
B->.b
Clousure1:
s->S.
Clousure2:
S->B.B
B->.aB
B->.b
Clousure3:
S->BB.
Clousure4:
B->a.B
B->.aB
B->.b
Clousure5:
B->aB.
Clousure6:
B->b.
Action table:
S4 S6 err
err err acc
S4 S6 err
R1 R1 R1
S4 S6 err
R2 R2 R2
R3 R3 R3
Goto table:
12
-1 -1
-1 3
-1 -1
-1 5
-1 -1
-1 -1
The parsing table is constructed...
Start grammar...
LL Parsing Algorithm
package ll1_parser;
import java.util.Stack;
/**
LL1
G -> E$
E -> TK
K-> +TK | empty
T -> FH
H-> *FH | empty
F -> (E) | a
=========================================
a + * ( ) $
--------------------------------------------------------------------
G| G->E$ | | | G->E$ | | |
--------------------------------------------------------------------
E| E->TK | | | E->TK | | |
--------------------------------------------------------------------
K| | K->+TK | | | K-> | K-> |
--------------------------------------------------------------------
T| T->FH | | | T->FH | | |
--------------------------------------------------------------------
H| | H-> | H->*FH | | H-> | H-> |
--------------------------------------------------------------------
F| F->a | | | F->(E) | | |
--------------------------------------------------------------------
==================================================================
=============
*/
public class LL1_Parser {
//input
public String input="";//"i*i$"
private int indexOfInput=-1;
//Stack
Stack <String> strack=new Stack<String>();
//Table of rules
String [][] table=
{
{"E$",null,null,"E$",null,null}
,
{"TK",null,null,"TK",null,""}
,
{null,"+TK",null,null,"",""}
,
{"FH",null,null,"FH",null,null}
,
{null,"","*FH",null,"",""}
,
{"a",null,null,"(E)",null,null}
};
String [] nonTers={"G","E","K","T","H","F"};
String [] terminals={"a","+","*","(",")","$"};
//algorithm
public void algorithm ()
{
push(this.input.charAt(0)+"");//
push("G");
//Read one token from input
String token=read();
String top=null;
do
{
top=this.pop();
//if top is non-terminal
if(isNonTerminal(top))
{
String rule=this.getRule(top, token);
this.pushRule(rule);
}
else if(isTerminal(top))
{
if(!top.equals(token))
{
error("this token is not corrent , By Grammer rule . Token : ("+token+")");
}
else
{
}
}
else
{
error("Never Happens , Because top : ( "+top+" )");
}
if(token.equals("$"))
{
break;
}
//if top is terminal
//accept
if(token.equals("$"))
{
System.out.println("Input is Accepted by LL1");
}
else
{
System.out.println("Input is not Accepted by LL1");
}
}
}
return false;
}
}
return false;
}
int row=getnonTermIndex(non);
int column=getTermIndex(term);
String rule=this.table[row][column];
if(rule==null)
{
error("There is no Rule by this , Non-Terminal("+non+") ,Terminal("+term+") ");
}
return rule;
}
//main
public static void main(String[] args) {
// TODO code application logic here