Advertisement
osipyonok

lab3 SysProg

Dec 1st, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. /////////////////////////// Tokens.java
  2. package LispRegexLab;
  3.  
  4. public enum Tokens {
  5.     Keyword,
  6.     Atom,
  7.     Int,
  8.     Hex,
  9.     Double,
  10.     Comment,
  11.     String,
  12.     Char,
  13.     Directive,
  14.     Operator,
  15.     Punctuation
  16. }
  17. /////////////////////////// Main.java
  18. package LispRegexLab;
  19.  
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24. import java.util.*;
  25. import java.util.regex.*;
  26.  
  27.  
  28. public class Main {
  29.     static Map<Tokens , Pattern> regular = new HashMap<Tokens , Pattern>();
  30.     static Map<Tokens , Integer> priority = new HashMap<Tokens , Integer>();
  31.    
  32.     static void InitRegex(){
  33.         regular.put(Tokens.Atom , Pattern.compile("^(_|[a-z])([a-z]|[0-9]|-)*"));
  34.         regular.put(Tokens.Keyword , Pattern.compile("^(eq|car|cdr|defun|defvar|setq|and|not|or|xor|atom|equal|nil|t|defmacro|cond|if|when|case|loop|do|return|let|list|do|length|setf|format|terpri|dotimes|write-char)"));       
  35.         regular.put(Tokens.Int , Pattern.compile("^(0|[1-9]\\d*)"));
  36.         regular.put(Tokens.String, Pattern.compile("^((\"\"([^\\\"\"\\n\\r\\\\]*|\\\\[\\\\0abfnrtuUxv]|\\\\x[\\da-fA-F]{1,4}|\\\\u[\\da-fA-F]{4}|\\\\U[\\da-fA-F]{8})*\"\")|(@\"\"([^\\\"\"]|\"\"\"\")*\"\"))"));
  37.         regular.put(Tokens.Comment, Pattern.compile("^(;+[^\n\r]*)"));
  38.         regular.put(Tokens.Punctuation, Pattern.compile("^[():]"));
  39.         regular.put(Tokens.Hex, Pattern.compile("^0[xX]((0|[1-9a-fA-F][\\da-fA-F]*))"));
  40.         regular.put(Tokens.Operator, Pattern.compile("^\\+ |\\- |\\* |mod |rem |incf |decf |= |> |< |>= |<= |max |min "));
  41.         regular.put(Tokens.Double, Pattern.compile("^(((0|[1-9]\\d*)?\\.\\d+([eE][+-]?\\d+)?[FfDdMm]?)|((0|[1-9]\\d*)([eE][+-]?\\d+)[FfDdMm]?)|((0|[1-9]\\d*)[FfDdMm]))"));
  42.         regular.put(Tokens.Directive, Pattern.compile("^mp:.*"));
  43.         regular.put(Tokens.Char , Pattern.compile("^'[a-z]*"));
  44.     }
  45.    
  46.     static void InitPriority(){
  47.         priority.put(Tokens.Operator, 1);
  48.         priority.put(Tokens.Punctuation, 2);
  49.         priority.put(Tokens.Double, 3);
  50.         priority.put(Tokens.Int, 4);
  51.         priority.put(Tokens.Hex, 4);
  52.         priority.put(Tokens.Atom, 5);
  53.         priority.put(Tokens.Keyword, 6);
  54.         priority.put(Tokens.String, 7);
  55.         priority.put(Tokens.Char, 7);
  56.         priority.put(Tokens.Directive, 8);
  57.         priority.put(Tokens.Comment, 9);
  58.     }
  59.    
  60.     static void Tokenize(String text){
  61.         String code = text;
  62.         while(code.length() > 0 && (code.charAt(0) == ' ' || code.charAt(0) == '\n' || code.charAt(0) == '\t'))code = code.substring(1);
  63.         while(code.length() > 0){
  64.             Tokens tokType = Tokens.Comment;
  65.             int len = -1;
  66.             for(Tokens tok : regular.keySet()){    
  67.                 Pattern trg = regular.get(tok);
  68.                 int t = 1 , tmax = 0;
  69.                 Matcher tmat = trg.matcher(code.substring(0, t));
  70.                 while(t <= code.length()){
  71.                     Matcher m = trg.matcher(code.substring(0, t));
  72.                     if(m.matches() == false){
  73.                         if(t > 20)break;
  74.                         ++t;
  75.                         continue;
  76.                     }
  77.                     tmat = m;
  78.                     tmax = t;
  79.                     ++t;
  80.                    
  81.                     if(t == code.length()){
  82.                         break;
  83.                     }
  84.                 }
  85.                 if(tmat.matches() == false)continue;   
  86.                 int tlen = tmax;
  87.                 if(tlen > len || (tlen == len && priority.get(tokType) < priority.get(tok))){
  88.                     tokType = tok;     
  89.                     len = tlen;
  90.                 }
  91.             }
  92.             if(len <= 0){
  93.                 System.out.println("Unknown lexeme " + code);
  94.                 return;
  95.             }
  96.             System.out.println(code.substring(0 , len) + " " + tokType.toString());
  97.             code = code.substring(len);
  98.             code = code.trim();
  99.         }
  100.     }
  101.    
  102.     public static void main(String[] args) {
  103.         InitRegex();
  104.         InitPriority();
  105.         File file = new File("input.txt");
  106.         try {
  107.             FileInputStream fis = new FileInputStream(file);
  108.             byte[] data = new byte[(int) file.length()];
  109.             fis.read(data);
  110.             fis.close();
  111.             fis.close();
  112.             String str = new String(data, "UTF-8");
  113.             Tokenize(str);
  114.         }catch(FileNotFoundException e){
  115.             System.out.print("File not found");
  116.         }catch(IOException e) {
  117.             e.printStackTrace();
  118.         }
  119.     }  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement