语法分析代码

import java.util.List;

import test.Analysis;
import test.Grammar;


public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  String[][] table = {
    {"1", "2", "3", ":", "/", ".", "?", ";",  "//"},
    
    {"",  "1",  "",  "",  "",  "",  "",  "",  ""},
    {"",  "",   "",  "2", "",  "5", "",  "",  ""},
    {"",  "",   "",  "",  "",  "",  "",  "",  "3"},
    {"",  "4",  "",  "",  "",  "",  "",  "",  ""},
    {"",  "",   "",  "",  "",  "5", "",  "",  ""},
    {"",  "6",  "",  "",  "",  "",  "",  "",  ""},
    {"",  "",   "",  "7", "0", "5", "0", "0", ""},
    {"8", "",   "",  "",  "",  "",  "",  "",  ""},
    {"",  "",   "",  "",  "0", "",  "0", "0", ""}
  };
  
  Grammar grammar = new Grammar(table);
  
  Analysis an = new Analysis();
  
  List list = an.scan("https://www.google.w.cn:80/search?hl=zh-CN你好&newwindow=1&q=fy+%E5%88%A4%E6%96%AD&aq=f&oq=&aqi=");
  
  //an.print(list);
  
  String result = grammar.analysis2(list);
  
  System.out.println(result);
  
 }
}

 

 

 

 

package test;

import java.util.HashMap;
import java.util.List;

public class Grammar {
 private HashMap map = new HashMap();
 
 public Grammar(String[][] table) {
  String[] temp;
  String[] v;
  int s = table.length;
  int l = 0;
  
  temp = table[0];
  l = temp.length;
  
  for(int i=1;i<s;i++) {
   HashMap m = new HashMap();
   v = table[i];
   for(int j=0;j<l;j++) {
    String t = v[j];
    if(t == null || t.length() ==0) {
     t = "-1";
    }
    m.put(temp[j], t);
   }
   
   this.map.put(i-1, m);
  }
  
 }
 
 public String analysis2(List<Item> result) {
  StringBuilder builder = new StringBuilder();
  
  int state = 0;
  int s;
  
  s = result.size();
  
  for(int i=0;i<s;i++) {
   builder.append(result.get(i).getContent());
   Item item = result.get(i);
   String temp;
   if(item.getType() == 1 || item.getType() == 2 || item.getType() == 3) {
    HashMap t = (HashMap) map.get(state);
    temp = (String) t.get(String.valueOf(item.getType()));
   } else {
    HashMap t = (HashMap) map.get(state);
    temp = (String) t.get(item.getContent());
   }
   if(temp != null) {
    int v = 0;
    try{
     v = Integer.parseInt(temp);
    } catch(Exception e) {
     v = -1;
    }
    if(v == 0) {
     break;
    } else if(v == -1) {
     builder.delete(0, builder.length());
     break;
    } else {
     state = v;
    }
   } else {
    builder.delete(0, builder.length());
    break;
   }
  }
  
  return builder.toString();
 }
  
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
[说明] 本文件所在目录应该包含如下文件: icrEdit\ - 目录,icrEdit编辑器的源码 演示例程\ - 目录,演示CSynEditView使用方法的例程 语法编辑类\ - 目录,CSynEditView的头文件和实现文件 使用说明.txt - 本文件,说明书 riched20.dll - CSynEditView必须的支持文件,请留意其版本号5.30.23.1209,文件大小:431888 (如果看不到文件riched20.dll,请在文件夹选项对话框的查看TAB中将隐藏文件和文件夹选为 显示文件和文件夹) [简介] CSynEditView一个支持语法编辑的类,你只要简单的将该类引入你的工程,便可轻松的获得如VC++和VB编程环境中的语法编辑能力。 [使用方法] 1、新建一个工程,在第六步将视类的基类改为CRichEditView。 2、在VC环境中选 Project->Add to Project->Files,将SynEditView.h和SynEditView.cpp引入工程。 3、将你的视类中的CRichEditView字符串全部改为CSynEditView,在你的视类头文件中用#include引入SynEditView.h文件。 4、按F7编译该工程,然后将riched20.dll复制到可执行文件的同一目录下。也就是说,如果你用Debug模式编译,将riched20.dll复制到Debug\目录下,如果你用Release模式编译,将riched20.dll复制到Release\目录下。 5、按F6执行。 [注意] 1、为了使程序能正常执行,必须保证riched20.dll同执行文件在同一目录。 2、CSynEditViw暂不支持中文,如果文本中含有中文字符,可能会引起乱码,请自行更改代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值