• 大小: 18KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-07-05
  • 语言: Java
  • 标签: JAVA  

资源简介

用java写的LALR编译器,实现语法分析

资源截图

代码片段和文件信息

/**
 * @(#)LALR.java
 *
 *
 * @author Huachunya
 * @version 1.00 2009/6/15
 */
 import java.io.*;
 import java.lang.*;
 import java.util.*;

public class LALR {
    private Map mapt=new HashMap();//终结符
    private Map mapn=new HashMap();   //非终结符
    private char map[]=new char[9]; //非终结符
    private String pro[]=new String[20];
    private int len=0;  //产生式的个数
    private int cntn=0;  //非终结符个数
    private int cntt=0;  //终结符个数
    private char first[][]=new char[9][25];
    private char follow[][]=new char[9][25];
    private int firstlen[]=new int[9];
    private int followlen[]=new int[9];
    private String I[][]=new String[200][25];//LALR项目集
    private String It[][]=new String [200][25]; //LR后跟的集合
    private String Iori[]=new String [25];
    private String Itori[]=new String [25];
    private int Icnt[]=new int[200];//每个项目集中产生式的个数
    private int Ioricnt[]=new int[200];//每个项目集的原始产生式个数
    private int Inum=0;//项目集中的个数
    private int Taction[][]=new int[200][25];
    private int Tgoto[][]=new int[200][10];
    private String  mstr=null; //要分析的句子
    private int escape1[]=new int[25]; //终结符要跟着输出的空格数
    private int escape2[]=new int[25];//非终结符
    
    public void InitialProducer() throws NullPointerException{
     try{
     mapt.clear();
     mapt.put(‘a‘“;“); escape1[0]=5;
     mapt.put(‘b‘“var“);escape1[1]=3;
     mapt.put(‘c‘“id“);escape1[2]=4;
     mapt.put(‘d‘“$“);escape1[3]=5;
     mapt.put(‘e‘“if“);escape1[4]=4;
     mapt.put(‘f‘“then“);escape1[5]=2;
     mapt.put(‘g‘“else“);escape1[6]=2;
     mapt.put(‘h‘“while“);escape1[7]=1;
     mapt.put(‘i‘“do“);escape1[8]=4;
     mapt.put(‘j‘“begin“);escape1[9]=1;
     mapt.put(‘k‘“end“);escape1[10]=3;
     mapt.put(‘l‘“:=“);escape1[11]=4;
     mapt.put(‘m‘“:“);escape1[12]=5;
     mapt.put(‘n‘“n“);escape1[13]=5;
     mapt.put(‘o‘“*“);escape1[14]=5;
     mapt.put(‘p‘“(“);escape1[15]=5;
     mapt.put(‘q‘“)“);escape1[16]=5;
     mapt.put(‘r‘“true“);escape1[17]=2;
     mapt.put(‘s‘“false“);escape1[18]=1;
     mapt.put(‘t‘“==“);escape1[19]=4;
     mapt.put(‘u‘“+“);escape1[20]=5;
     mapt.put(‘v‘“#“);escape1[21]=5;
     cntt=mapt.size();
 
     mapn.clear();
     mapn.put(‘Q‘0);
     mapn.put(‘P‘1);
     mapn.put(‘D‘2);
     mapn.put(‘S‘3);
     mapn.put(‘L‘4);
     mapn.put(‘E‘5);
     mapn.put(‘T‘6);
     mapn.put(‘F‘7);
     mapn.put(‘B‘8);
     cntn=mapn.size();
     map[0]=‘Q‘;escape2[0]=5;
     map[1]=‘P‘;escape2[1]=5;
     map[2]=‘D‘;escape2[2]=5;
     map[3]=‘S‘;escape2[3]=5;
     map[4]=‘L‘;escape2[4]=5;
     map[5]=‘E‘;escape2[5]=5;
     map[6]=‘T‘;escape2[6]=5;
     map[7]=‘F‘;escape2[7]=5;
     map[8]=‘B‘;escape2[8]=5;

    
     len=0;
     pro[len++]=“Q-P“;
     pro[len++]=“P-DS“;//P-DaS?
     pro[len++]=“D-bcaD“;
     pro[len++]=“D-d“;
     pro[len++]=“S-eBfSgS“;
     pro[len++]=“S-hBiS“;
 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      19624  2009-06-25 12:39  LALR1\classes\LALR.class

     文件       1631  2009-06-25 12:39  LALR1\classes\LALR1.class

     文件        899  2009-06-25 12:39  LALR1\LALR1.jcp

     文件        284  2009-06-25 12:39  LALR1\LALR1.jcw

     文件       2125  2009-06-25 12:39  LALR1\LALR12.jcu

     文件        285  2009-06-25 12:39  LALR1\LALR12.jcw

     文件      25178  2009-06-25 12:39  LALR1\src\LALR.java

     文件       1117  2009-06-25 12:39  LALR1\src\LALR1.java

     文件        209  2009-06-25 12:39  LALR1\src_lalr1.txt

     目录          0  2009-06-25 12:39  LALR1\classes

     目录          0  2009-06-25 12:39  LALR1\src

     目录          0  2009-06-25 12:39  LALR1

----------- ---------  ---------- -----  ----

                51352                    12


评论

共有 条评论