자바 를 이용 하여 간단 한 품사 분석 기 인 스 턴 스 코드 를 실현 합 니 다.
 
 출력 결 과 는 다음 과 같 습 니 다.
 
 출력 결과(a).PNG
 
 출력 결과(b).PNG
 
 출력 결과(c).PNG
괄호 안 은 이원 식 이다.(단어 유형 인 코딩,단어 위치 번호)
코드 는 다음 과 같 습 니 다:
package Yue.LexicalAnalyzer;
import java.io.*;
/*
 *    
 */
public class Main {
  public static void main(String[] args) throws IOException {
    Lexer lexer = new Lexer();
    lexer.printToken();
    lexer.printSymbolsTable();
  }
}
package Yue.LexicalAnalyzer;
import java.io.*;
import java.util.*;
/*
 *        
 */
public class Lexer {
  /*    */
  public static int line = 1;
  /*         */
  char character = ' ';
  /*   */
  Hashtable<String, KeyWord> keywords = new Hashtable<String, KeyWord>();
  /*token  */
  private ArrayList<Token> tokens = new ArrayList<Token>();
  /*   */
  private ArrayList<Symbol> symtable = new ArrayList<Symbol>();
  /*      */
  BufferedReader reader = null;
  /*               */
  private Boolean isEnd = false;
  /*            */
  public Boolean getReaderState() {
    return this.isEnd;
  }
  /*  tokens  */
  public void printToken() throws IOException {
    FileWriter writer = new FileWriter("E:\\lex.txt");
    System.out.println("        :");
    System.out.print("  -2015220201031\r
");
    writer.write("  -2015220201031\r
\r
");
    while (getReaderState() == false) {
      Token tok = scan();
      String str = "line " + tok.line + "\t(" + tok.tag + "," + tok.pos + ")\t\t"
          + tok.name + ": " + tok.toString() + "\r
";
      writer.write(str);
      System.out.print(str);
    }
    writer.flush();
  }
  /*     */
  public void printSymbolsTable() throws IOException {
    FileWriter writer = new FileWriter("E:\\symtab1.txt");
    System.out.print("\r
\r
   \r
");
    System.out.print("  \t  \t  \r
");
    writer.write("   \r
");
    writer.write("   " + "\t   " + "\t   \r
");
    Iterator<Symbol> e = symtable.iterator();
    while (e.hasNext()) {
      Symbol symbol = e.next();
      String desc = symbol.pos + "\t" + symbol.line + "\t" + symbol.toString();
      System.out.print(desc + "\r
");
      writer.write(desc + "\r
");
    }
    writer.flush();
  }
  /*    */
  public void printError(Token tok) throws IOException{
    FileWriter writer = new FileWriter("E:\\error.txt");
    System.out.print("\r
\r
      :\r
");
    writer.write("      :\r
");
    String str = "line " + tok.line + "\t(" + tok.tag + "," + tok.pos + ")\t\t"
        + tok.name + ": " + tok.toString() + "\r
";
    writer.write(str);
  }
  /*     */
  void reserve(KeyWord w) {
    keywords.put(w.lexme, w);
  }
  public Lexer() {
    /*         */
    try {
      reader = new BufferedReader(new FileReader("E:\\  .txt"));
    } catch (IOException e) {
      System.out.print(e);
    }
    /*     */
    this.reserve(KeyWord.begin);
    this.reserve(KeyWord.end);
    this.reserve(KeyWord.integer);
    this.reserve(KeyWord.function);
    this.reserve(KeyWord.read);
    this.reserve(KeyWord.write);
    this.reserve(KeyWord.aIf);
    this.reserve(KeyWord.aThen);
    this.reserve(KeyWord.aElse);
  }
  /*    */
  public void readch() throws IOException {
    character = (char) reader.read();
    if ((int) character == 0xffff) {
      this.isEnd = true;
    }
  }
  /*      */
  public Boolean readch(char ch) throws IOException {
    readch();
    if (this.character != ch) {
      return false;
    }
    this.character = ' ';
    return true;
  }
  /*     */
  public Boolean isDigit() throws IOException {
    if (Character.isDigit(character)) {
      int value = 0;
      while (Character.isDigit(character)) {
        value = 10 * value + Character.digit(character, 10);
        readch();
      }
      Num n = new Num(value);
      n.line = line;
      tokens.add(n);
      return true;
    } else
      return false;
  }
  /*   、      */
  public Boolean isLetter() throws IOException {
    if (Character.isLetter(character)) {
      StringBuffer sb = new StringBuffer();
      /*           */
      while (Character.isLetterOrDigit(character)) {
        sb.append(character);
        readch();
      }
      /*           */
      String s = sb.toString();
      KeyWord w = keywords.get(s);
      /*        ,w      */
      if (w != null) {
        w.line = line;
        tokens.add(w);
      } else {
        /*       ,              */
        Symbol sy = new Symbol(s);
        Symbol mark = sy;      //          
        Boolean isRepeat = false;
        sy.line = line;
        for (Symbol i : symtable) {
          if (sy.toString().equals(i.toString())) {
            mark = i;
            isRepeat = true;
          }
        }
        if (!isRepeat) {
          sy.pos = symtable.size() + 1;
          symtable.add(sy);
        } else if (isRepeat) {
          sy.pos = mark.pos;
        }
        tokens.add(sy);
      }
      return true;
    } else
      return false;
  }
  /*     */
  public Boolean isSign() throws IOException {
    switch (character) {
      case '#':
        readch();
        AllEnd.allEnd.line = line;
        tokens.add(AllEnd.allEnd);
        return true;
      case '\r':
        if (readch('
')) {
          readch();
          LineEnd.lineEnd.line = line;
          tokens.add(LineEnd.lineEnd);
          line++;
          return true;
        }
      case '(':
        readch();
        Delimiter.lpar.line = line;
        tokens.add(Delimiter.lpar);
        return true;
      case ')':
        readch();
        Delimiter.rpar.line = line;
        tokens.add(Delimiter.rpar);
        return true;
      case ';':
        readch();
        Delimiter.sem.line = line;
        tokens.add(Delimiter.sem);
        return true;
      case '+':
        readch();
        CalcWord.add.line = line;
        tokens.add(CalcWord.add);
        return true;
      case '-':
        readch();
        CalcWord.sub.line = line;
        tokens.add(CalcWord.sub);
        return true;
      case '*':
        readch();
        CalcWord.mul.line = line;
        tokens.add(CalcWord.mul);
        return true;
      case '/':
        readch();
        CalcWord.div.line = line;
        tokens.add(CalcWord.div);
        return true;
      case ':':
        if (readch('=')) {
          readch();
          CalcWord.assign.line = line;
          tokens.add(CalcWord.assign);
          return true;
        }
        break;
      case '>':
        if (readch('=')) {
          readch();
          CalcWord.ge.line = line;
          tokens.add(CalcWord.ge);
          return true;
        }
        break;
      case '<':
        if (readch('=')) {
          readch();
          CalcWord.le.line = line;
          tokens.add(CalcWord.le);
          return true;
        }
        break;
      case '!':
        if (readch('=')) {
          readch();
          CalcWord.ne.line = line;
          tokens.add(CalcWord.ne);
          return true;
        }
        break;
    }
    return false;
  }
  /*         ,      */
  public Token scan() throws IOException {
    Token tok;
    while (character == ' ')
      readch();
    if (isDigit() || isSign() || isLetter()) {
      tok = tokens.get(tokens.size() - 1);
    } else {
      tok = new Token(character);
      printError(tok);
    }
    return tok;
  }
}
package Yue.LexicalAnalyzer;
/*
 * Token  
 */
public class Token {
  public final int tag;
  public int line = 1;
  public String name = "";
  public int pos = 0;
  public Token(int t) {
    this.tag = t;
  }
  public String toString() {
    return "" + (char) tag;
  }
}
package Yue.LexicalAnalyzer;
/*
 *       
 */
public class Tag {
  public final static int
      BEGIN = 1,     //   
      END = 2,      //   
      INTEGER = 3,    //   
      FUNCTION = 4,    //   
      READ = 5,      //   
      WRITE = 6,     //   
      IF = 7,       //   
      THEN = 8,      //   
      ELSE = 9,      //   
      SYMBOL = 11,    //   
      CONSTANT = 12,   //  
      ADD = 13,      //    "+"
      SUB = 14,      //    "-"
      MUL = 15,      //    "*"
      DIV = 16,      //    "/"
      LE = 18,      //    "<="
      GE = 19,      //    ">="
      NE = 20,      //    "!="
      ASSIGN = 23,    //    ":="
      LPAR = 24,     //   "("
      RPAR = 25,     //   ")"
      SEM = 26,      //   ";"
      LINE_END = 27,   //   
      ALL_END = 28;    //    "#"
}
package Yue.LexicalAnalyzer;
/**
 *    
 */
public class KeyWord extends Token {
  public String lexme = "";
  public KeyWord(String s, int t) {
    super(t);
    this.lexme = s;
    this.name = "   ";
  }
  public String toString() {
    return this.lexme;
  }
  public static final KeyWord
      begin = new KeyWord("begin", Tag.BEGIN),
      end = new KeyWord("end", Tag.END),
      integer = new KeyWord("integer", Tag.INTEGER),
      function = new KeyWord("function", Tag.FUNCTION),
      read = new KeyWord("read", Tag.READ),
      write = new KeyWord("write", Tag.WRITE),
      aIf = new KeyWord("if", Tag.IF),
      aThen = new KeyWord("then", Tag.THEN),
      aElse = new KeyWord("else", Tag.ELSE);
}
package Yue.LexicalAnalyzer;
/*
 *    
 */
public class Symbol extends Token {
  public String lexme = "";
  public Symbol(String s) {
    super(Tag.SYMBOL);
    this.lexme = s;
    this.name = "   ";
  }
  public String toString() {
    return this.lexme;
  }
}
package Yue.LexicalAnalyzer;
/**
 *    
 */
public class CalcWord extends Token {
  public String lexme = "";
  public CalcWord(String s, int t) {
    super(t);
    this.lexme = s;
    this.name = "   ";
  }
  public String toString() {
    return this.lexme;
  }
  public static final CalcWord
      add = new CalcWord("+", Tag.ADD),
      sub = new CalcWord("-", Tag.SUB),
      mul = new CalcWord("*", Tag.MUL),
      div = new CalcWord("/", Tag.DIV),
      le = new CalcWord("<=", Tag.LE),
      ge = new CalcWord(">=", Tag.GE),
      ne = new CalcWord("!=", Tag.NE),
      assign = new CalcWord(":=", Tag.ASSIGN);
}
package Yue.LexicalAnalyzer;
/**
 *   
 */
public class Delimiter extends Token {
  public String lexme = "";
  public Delimiter(String s, int t) {
    super(t);
    this.lexme = s;
    this.name = "  ";
  }
  public String toString() {
    return this.lexme;
  }
  public static final Delimiter
      lpar = new Delimiter("(", Tag.LPAR),
      rpar = new Delimiter(")", Tag.RPAR),
      sem = new Delimiter(";", Tag.SEM);
}
package Yue.LexicalAnalyzer;
/*
 *   
 */
public class Num extends Token {
  public final int value;
  public Num(int v) {
    super(Tag.CONSTANT);
    this.value = v;
    this.name = "  ";
  }
  public String toString() {
    return "" + value;
  }
}
package Yue.LexicalAnalyzer;
/**
 *    
 */
public class LineEnd extends Token {
  public String lexme = "";
  public LineEnd(String s) {
    super(Tag.LINE_END);
    this.lexme = s;
    this.name = "   ";
  }
  public String toString() {
    return this.lexme;
  }
  public static final LineEnd lineEnd = new LineEnd("\r
");
}
package Yue.LexicalAnalyzer;
/**
 *    
 */
public class AllEnd extends Token {
  public String lexme = "";
  public AllEnd(String s) {
    super(Tag.ALL_END);
    this.lexme = s;
    this.name = "   ";
  }
  public String toString() {
    return this.lexme;
  }
  public static final AllEnd allEnd = new AllEnd("#");
}이상 은 이 글 의 모든 내용 을 잠 들 었 습 니 다.본 논문 의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 면 댓 글 을 남 겨 주 십시오.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 를 이용 하여 간단 한 품사 분석 기 인 스 턴 스 코드 를 실현 합 니 다.먼저 우리 가 분석 하고 자 하 는 코드 세그먼트 를 보면 다음 과 같다. 출력 결 과 는 다음 과 같 습 니 다. 출력 결과(a).PNG 출력 결과(b).PNG 출력 결과(c).PNG 괄호 안 은 이원 식 이다.(...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.