1、对算术表达式文法:
E→TE'
E'→+TE'| -TE' |ε
T→FT'
T'→*FT'| /FT' |ε
F→(E) | id |num
构造其递归下降分析程序。I*i/I;
2、实验步骤
(1)求出每个非终结符的FIRST和FOLLOW集(在练习本上求出即可,不要求程序实现);
FIRST(E)= {(,id,num }
FIRST(E‘)={+,-, ε}
FIRST(T)={(, id,num }
FIRST(T‘)={*, /,ε}
FIRST(F)= {(, i d,num}
FOLLOW(E)= {#, ) }
FOLLOW (E')={ #, ) }
FOLLOW (T)= { +,-,#, ) }
FOLLOW (T')={ +,-,#, ) }
FOLLOW (F)= { *,/,+,-,#, ) }
public class O {
private String[] f;
private String[] F2;
public String[] getF() {
return f;
}
public void setF(String[] f) {
this.f = f;
}
public String[] getF2() {
return F2;
}
public void setF2(String[] F2) {
this.F2 = F2;
}
public boolean followsContains(O o,String current){
boolean flag = false;
//o.F=o.follow(F);
for(int i=0;i<o.getF2().length;i++){
if(current.equals(o.getF2()[i])){
flag = true;
break;
}
}
return flag;
}
}
public class E extends O {
private String[] f;
//= new String[]{"(","id","num"};//first集
private String[] F2;
//= new String[]{"#",")"};//follow集
public String[] getF() {
return f;
}
public void setF(String[] f) {
this.f = f;
}
public String[] getF2() {
return F2;
}
public void setF2(String[] f2) {
F2 = f2;
}
public E(){
this.setF2(new String[]{"#",")"});
}
}
public class E1 extends O {
private String[] f ;
//= new String[]{"+","-","ε"};//first集
private String[] F2 ;
//= new String[]{"#",")"};//follow集
public String[] getF() {
return f;
}
public void setF(String[] f) {
this.f = f;
}
public String[] getF2() {
return F2;
}
public void setF2(String[] f2) {
F2 = f2;
}
public E1() {
this.setF2(new String[]{"#",")"} );
}
}
public class T extends O {
private String[] f ;
//= new String[]{"(","id","num"};//first集
private String[] F2;
//= new String[]{"+","-","#",")"};//follow集
public String[] getF() {
return f;
}
public void setF(String[] f) {
this.f = f;
}
public String[] getF2() {
return F2;
}
public void setF2(String[] f2) {
F2 = f2;
}
public T(){
this.setF2(new String[]{"+","-","#",")"});
}
}
public class T1 extends O {
private String[] f ;
private String[] F2;
//=new String[]{"+","-","#",")"} ;
public String[] getF() {
return f;
}
public void setF(String[] f) {
this.f = f;
}
public String[] getF2() {
return F2;
}
public void setF2(String[] f2) {
F2 = f2;
}
public T1(){
//this.f= new String[]{"*","/","$"};//first集
//this.F= new String[]{"+","-","#",")"};//follow集
this.setF2(new String[]{"+","-","#",")"});
}
}
public class F extends O {
private String[] f;
//= new String[]{"(","id","num"};//first集
private String[] F2;
//= new String[]{"*","/","+","-","#"};//follow集
public String[] getF() {
return f;
}
public void setF(String[] f) {
this.f = f;
}
public String[] getF2() {
return F2;
}
public void setF2(String[] f2) {
F2 = f2;
}
public F(){
this.setF2(new String[]{"*","/","+","-","#"} );
}
}
public class GrammerAnalysis {
WordsType wt;
WordAnalysis wa;
static O[] os = new O[]{new E(),new E1(),new T(),new T1(),new F()};
WordsType temp=null;
char[] chTemps;
public WordsType advance(char[] chs,int length){
wt = WordAnalysis.analysis(chs, length);
return wt;
}
public void E(){
T();
E1();
}
public void E1(){
if(WordAnalysis.length<chTemps.length)
temp = advance(chTemps,WordAnalysis.length);
if(temp!=null){
if(temp==WordsType.SYM_PLUS){
System.out.println("E1->"+temp);
T();
E1();
}else if(temp==WordsType.SYM_MINUS){
System.out.println("E1->"+temp);
T();
E1();
}
else if(os[1].followsContains(os[1],temp.toString(temp))){
System.out.println("E1->kong");
WordAnalysis.length-=temp.toString(wt).length();
}
}
}
public void T(){
F();
T1();
}
public void T1(){
if(WordAnalysis.length<chTemps.length)
temp = advance(chTemps,WordAnalysis.length);
if(temp!=null){
if(temp==WordsType.SYM_TIMES){
System.out.println("T1->"+temp);
F();
T1();
}else if(temp==WordsType.SYM_SLASH){
System.out.println("T1->"+temp);
F();
T1();
}else if(os[3].followsContains(os[3],temp.toString(temp))){
System.out.println("T1->kong");
WordAnalysis.length-=(temp.toString(temp).length());
}
}
}
public void F(){
if(WordAnalysis.length<chTemps.length)
temp = advance(chTemps,WordAnalysis.length);
if(temp!=null){
if(temp==WordsType.SYM_LPAREN){
System.out.println("F->"+temp);
E();
if(WordAnalysis.length>=chTemps.length)
{
System.out.println("error");
System.exit(1);
}else
temp = advance(chTemps,WordAnalysis.length);
}
else if(temp==WordsType.SYM_IDENTIFIER){
System.out.println("F->"+temp);
return;
}else if(temp==WordsType.SYM_NUMBER){
System.out.println("F->"+temp);
return;
}else{
System.out.println("error");
System.exit(1);
}
}else{
System.out.println("error");
System.exit(1);
}
}
public static void main(String[] args) {
String s="(a+b)*4#";
GrammerAnalysis ga = new GrammerAnalysis();
ga.chTemps = s.toCharArray();
ga.E();
}
}