LR Parsing Algorithm (Pseudocode) : Festin, Kerr Oliver Bscs 2A

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Festin, Kerr Oliver

BSCS 2A

LR Parsing Algorithm (Pseudocode)


Input sequence:
bab#

Grammatical sequence: 3
S->BB;
B->aB;
B->b;

Construct a parsing table...

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...

Move in: Move in b


The content of the status stack is:
06
The contents of the symbol stack are:
#b

Reduction operation: use the third production


The content of the status stack is:
0
The contents of the symbol stack are:
#B

State transition: transfer to state 2

Move in: Move in a


The content of the status stack is:
024
The contents of the symbol stack are:
#Ba

Move in: Move in b


The content of the status stack is:
0246
The contents of the symbol stack are:
#Bab

Reduction operation: use the third production


The content of the status stack is:
024
The contents of the symbol stack are:
#BaB

State transition: transfer to state 5

Reduction operation: use the second production


The content of the status stack is:
02
The contents of the symbol stack are:
#BB

State transition: move to the third state

Reduction operation: use the first production


The content of the status stack is:
0
The contents of the symbol stack are:
#S

State transition: transfer to the first state

Successfully completed the parsing!


End parsing...

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","+","*","(",")","$"};

public LL1_Parser(String in)


{
this.input=in;
}

private void pushRule(String rule)


{
for(int i=rule.length()-1;i>=0;i--)
{
char ch=rule.charAt(i);
String str=String.valueOf(ch);
push(str);
}
}

//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
{

System.out.println("Matching: Terminal 🙁 "+token+" )");


token =read();
//top=pop();

}
}
else
{
error("Never Happens , Because top : ( "+top+" )");
}
if(token.equals("$"))
{
break;
}
//if top is terminal

}while(true);//out of the loop when $

//accept
if(token.equals("$"))
{
System.out.println("Input is Accepted by LL1");
}
else
{
System.out.println("Input is not Accepted by LL1");
}
}

private boolean isTerminal(String s) {


for(int i=0;i<this.terminals.length;i++)
{
if(s.equals(this.terminals[i]))
{
return true;
}

}
return false;
}

private boolean isNonTerminal(String s) {


for(int i=0;i<this.nonTers.length;i++)
{
if(s.equals(this.nonTers[i]))
{
return true;
}

}
return false;
}

private String read() {


indexOfInput++;
char ch=this.input.charAt(indexOfInput);
String str=String.valueOf(ch);
return str;
}

private void push(String s) {


this.strack.push(s);
}
private String pop() {
return this.strack.pop();
}

private void error(String message) {


System.out.println(message);
throw new RuntimeException(message);
}
public String getRule(String non,String term)
{

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;
}

private int getnonTermIndex(String non) {


for(int i=0;i<this.nonTers.length;i++)
{
if(non.equals(this.nonTers[i]))
{
return i;
}
}
error(non +" is not NonTerminal");
return -1;
}

private int getTermIndex(String term) {


for(int i=0;i<this.terminals.length;i++)
{
if(term.equals(this.terminals[i]))
{
return i;
}
}
error(term +" is not Terminal");
return -1;
}

//main
public static void main(String[] args) {
// TODO code application logic here

LL1_Parser parser=new LL1_Parser("a+(a+a*(a)+a)$");//i*i+(i+i)$


parser.algorithm();

You might also like