Académique Documents
Professionnel Documents
Culture Documents
Bottom-up parsing can be defined as an attempt to reduce the input string w to the start symbol of a grammar by tracing out the right-most derivations of w in reverse. For example, if the right-most derivation sequence of some w is: A 1 2 3 . n1 w then the bottom-up parser starts with w and searches for the occurrence of a substring of w that matches the right side of some production A such that the replacement of by A will lead to the generation of n1. The parser replaces by A, then it searches for the occurrence of a substring of n1 that matches the right side of some production B such that replacement of by B will lead to the generation of n2. This process continues until the entire w substring is reduced to S, or until the parser encounters an error. Operator Precedence Relations Definition A grammar G is an operator grammar if it contains no rules of the form V aXY , where X, Y Vn ; that is, the occurrence of consecutive non terminals in the right part of any rule is forbidden. Also, empty rules are not permitted. We call the language generated by an operator grammar an operator language. In OPP ,we define three disjoint precedence relation between certain pairs of terminals. a < b b has higher precedence than a a = b b has same precedence as a a > b b has lower precedence than a Example:EE + T ET TT * F TF F( E ) Fid
//PARSE TABLE PROGRAM WITH LEADING & TRAILLING SET IN JAVA import java.io.*; class opp { public static int nt=0,t=0,top=0; public static char[]s=new char[50]; public static char [] NT=new char[10]; public static char [] T=new char[10]; public static char[] st=new char[50]; public static char [][] l=new char[10][10]; public static char [][] tr =new char[50][50]; static char[] lead= new char[30]; static char[] trail=new char[30]; String op1,op2,op3,str1; String[][] TABLE; public static BufferedReader in =new BufferedReader(new InputStreamReader(System.in)); static int searchnt(char a) { int count=-1,i; for(i=0;i<nt;i++) { if(NT[i]==a) return i;
} return count; }
static int searchter(char a) { int count=-1,i; for(i=0;i<t;i++) { if(T[i]==a) return i; } return count; } static void push(char a) { s[top]=a; top++; } static char pop() { top--; return s[top];
} static void installl(int a,int b) { if(l[a][b]=='f') { l[a][b]='t'; push(T[b]); push(NT[a]); } } static void installt(int a,int b) { if(tr[a][b]=='f') { tr[a][b]='t'; push(T[b]); push(NT[a]); } } public static void main(String agr[]) { int i,s,k,j,n=0; char [][] pr =new char[30][30];
try { n=Integer.parseInt(in.readLine()); } catch(Exception e) { System.out.println( +e); } System.out.println("Enter the productions one by one\n"); for(i=0;i<n;i++) { try { String temp=in.readLine(); pr[i]=temp.toCharArray(); } catch(Exception e) { System.out.println( +e);
} } nt=0; t=0; for(i=0;i<n;i++) { if((searchnt(pr[i][0]))==-1) NT[nt++]=pr[i][0]; } for(i=0;i<n;i++) { for(j=3;j<(pr[i].length);j++) { if(searchnt(pr[i][j])==-1) { if(searchter(pr[i][j])==-1) T[t++]=pr[i][j]; } } } for(i=0;i<nt;i++) { for(j=0;j<t;j++)
l[i][j]='f'; }
for(i=0;i<nt;i++) { for(j=0;j<t;j++) tr[i][j]='f'; } for(i=0;i<nt;i++) { for(j=0;j<n;j++) { if(NT[(searchnt(pr[j][0]))]==NT[i]) { if(searchter(pr[j][3])!=-1) installl(searchnt(pr[j][0]),searchter(pr[j][3])); else { for(k=3;k<(pr[j].length);k++) { if(searchnt(pr[j][k])==-1) {
installl(searchnt(pr[j][0]),searchter(pr[j][k]); break; } }}}}} while(top!=0) { b=pop(); c=pop(); for(s=0;s<n;s++) { if(pr[s][3]==b) installl(searchnt(pr[s][0]),searchter(c)); } } for(i=0;i<nt;i++) { System.out.println("Leading["+NT[i]+"]"+"\t{"); for(j=0;j<t;j++) { if(l[i][j]=='t') System.out.println(T[j]+","); } System.out.println("}\n");
} top=0; for(i=0;i<nt;i++) { for(j=0;j<n;j++) { if(NT[searchnt(pr[j][0])]==NT[i]) { if(searchter(pr[j][(pr[j]).length-1])!=-1) installt(searchnt(pr[j][0]),searchter(pr[j][(pr[j]).length-1])); else { for(k=((pr[j]).length-1);k>=3;k--) { if(searchnt(pr[j][k])==-1) { installt(searchnt(pr[j][0]),searchter(pr[j][k])); break; } } } }}} while(top!=0)
{ b=pop(); c=pop();
Output:-