java实现简单语法分析器

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


 

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值