Advertisement
osipyonok

SP Lab3 Lexer

May 9th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.55 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.util.regex.*;
  6.  
  7. enum Tokens {
  8.     Keyword,
  9.     Atom,
  10.     Int,
  11.     Hex,
  12.     Double,
  13.     Comment,
  14.     String,
  15.     Char,
  16.     Directive,
  17.     Operator,
  18.     Punctuation,
  19.     CONST_NAME,
  20.     DIGIT,
  21.     IDN,
  22.     LITER_STR,
  23.     PROC_NAME,
  24.     FUNC_NAME,
  25.     TYPE_NAME
  26. }
  27.  
  28. class LexPascal{
  29.     private String path = "";
  30.     private static Map<Tokens , Pattern> regular = new HashMap<Tokens , Pattern>();
  31.     private static Map<Tokens , Integer> priority = new HashMap<Tokens , Integer>();
  32.  
  33.     public LexPascal(String _path){
  34.         path = _path.trim();
  35.         InitRegex();
  36.         InitPriority();
  37.     }
  38.  
  39.     private void InitRegex(){
  40.         regular.put(Tokens.IDN , Pattern.compile("^(_|[a-z])([a-z]|[0-9]|_)*"));
  41.         regular.put(Tokens.Keyword , Pattern.compile("^(uses|program|var|const|type|begin|repeat|until|end|if|then|else|while|do|for|of|record|with|procedure|function|case|in|set|array|nil|true|false)"));
  42.         regular.put(Tokens.DIGIT , Pattern.compile("^(0|[1-9]\\d*)"));
  43.         regular.put(Tokens.LITER_STR, Pattern.compile("^'(?:[^']+|(''))*'$"));
  44.         regular.put(Tokens.Comment, Pattern.compile("^(//(.*?)//|\\{(.*?)\\}|\\(\\*(.*?)\\*\\))"));
  45.         regular.put(Tokens.Punctuation, Pattern.compile("^(\\(|\\)|;|,|\\.|:|\\[|\\]|\\^|\\.\\.)"));
  46. //        regular.put(Tokens.Hex, Pattern.compile("^0[xX]((0|[1-9a-fA-F][\\da-fA-F]*))"));
  47.         regular.put(Tokens.Operator, Pattern.compile("^\\+|\\-|\\*|/|:=|<>|=|>|<|>=|<=|!=|div|mod|and|not|~"));
  48.         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]))"));
  49. //        regular.put(Tokens.Directive, Pattern.compile("^mp:.*"));
  50.         regular.put(Tokens.TYPE_NAME , Pattern.compile("^(byte|integer|real|char|boolean|string)"));
  51.      //   regular.put(Tokens.PROC_NAME , Pattern.compile("^(randomize)"));
  52.     }
  53.  
  54.     private void InitPriority(){
  55.         priority.put(Tokens.Operator, 10);
  56.         priority.put(Tokens.Punctuation, 2);
  57.         priority.put(Tokens.Double, 3);
  58.         priority.put(Tokens.DIGIT, 4);
  59.         priority.put(Tokens.Hex, 4);
  60.         priority.put(Tokens.IDN, 5);
  61.         priority.put(Tokens.Keyword, 6);
  62.         priority.put(Tokens.LITER_STR, 7);
  63.         priority.put(Tokens.TYPE_NAME, 8);
  64.         priority.put(Tokens.Comment, 9);
  65.     }
  66.  
  67.     private String Prepare(String text){
  68.         text = text.toLowerCase();
  69.         String code = "";
  70.         boolean ck = false;
  71.  
  72.       //  text = text.replace(";",";;");
  73.         text = text.replace("clrscr;","clrscr();");
  74.         text = text.replace("writeln;","writeln();");
  75.         text = text.replace("write;","write();");
  76.         text = text.replace("readln;","readln();");
  77.         text = text.replace("read;","read();");
  78.         text = text.replace("randomize;","randomize();");
  79.         text = text.replace("function","func");
  80.         text = text.replace("procedure","proc");
  81.         text = text.replace("true","1");
  82.         text = text.replace("false","0");
  83.  
  84.  
  85.         for(int i = 0 ; i < text.length() ; ++i){
  86.             char ch = text.charAt(i);
  87.             String small = "йцукенгшщзхъфывапролджэёячсмитьбю";
  88.             String big = "ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЁЯЧСМИТЬБЮ";
  89.             if(small.indexOf(ch) != -1 && !ck){
  90.                 code += 'a';
  91.                 ck = true;
  92.             }else if(big.indexOf(ch) != -1 && !ck){
  93.                 code += 'A';
  94.                 ck = true;
  95.             }else if(small.indexOf(ch) == -1 && big.indexOf(ch) == -1){
  96.                 code += ch;
  97.                 ck = false;
  98.             }
  99.         }
  100.         String temp = "";
  101.         boolean add = false;
  102.         int st = 0;
  103.         for(int i = 0 ; i < code.length() ; ++i){
  104.             char ch = code.charAt(i);
  105.             if(st == 0 && ch == '\''){
  106.                 add = false;
  107.                 temp += ch;
  108.                 st = 1;
  109.                 continue;
  110.             }
  111.             if(st == 1 && ch == '\''){
  112.                 st = 0;
  113.                 temp += ch;
  114.                 continue;
  115.             }
  116.             if(st == 1 && ch == '\\'){
  117.                 st = 2;
  118.                 continue;
  119.             }
  120.             if(st == 2){
  121.                 st = 1;
  122.                 continue;
  123.             }
  124.             if(st == 1) {
  125.                 if(add == false){
  126.                     temp += ch;
  127.                     add = true;
  128.                 }
  129.                 continue;
  130.             }
  131.             temp += ch;
  132.         }
  133.         return temp.toLowerCase();
  134.     }
  135.  
  136.     public ArrayList<Map<Tokens , String>> Tokenize() throws Exception{
  137.         int wasbegin = 0;
  138.         Set<String> consts = new HashSet<>();
  139.         String last_key = "";
  140.  
  141.         File file = new File(path);
  142.  
  143.         String text = "";
  144.         try {
  145.             FileInputStream fis = new FileInputStream(new File(path));
  146.             byte[] data = new byte[(int) file.length()];
  147.             fis.read(data);
  148.             fis.close();
  149.             text = new String(data, "UTF-8");
  150.         }catch(FileNotFoundException e){
  151.             System.out.print(path + " File not found");
  152.         }catch(IOException e) {
  153.             e.printStackTrace();
  154.         }
  155.  
  156.         ArrayList<Map<Tokens , String>> ans = new ArrayList<>();
  157.         String code = Prepare(text);
  158.         //    System.out.println(code);
  159.         while(code.length() > 0 && (code.charAt(0) == ' ' || code.charAt(0) == '\n' || code.charAt(0) == '\t'))code = code.substring(1);
  160.         while(code.length() > 0){
  161.             Tokens tokType = Tokens.Comment;
  162.             int len = -1;
  163.             for(Tokens tok : regular.keySet()){
  164.                 Pattern trg = regular.get(tok);
  165.  
  166.                 String tmp = code;
  167.                 for(int j = code.length() ; j > 0 ; --j){
  168.                     Matcher tmat = trg.matcher(tmp);
  169.                     if(tmat.matches()){
  170.                         if(j > len || (j == len && priority.get(tok) > priority.get(tokType))){
  171.                             len = j;
  172.                             tokType = tok;
  173.                             break;
  174.                         }
  175.                     }
  176.                     tmp = tmp.substring(0 , tmp.length() - 1);
  177.                 }
  178.  
  179.  
  180.                 Matcher tmat = trg.matcher(code);
  181.  
  182.                 MatchResult res1 = tmat.toMatchResult();
  183.                 try{
  184.                     System.out.println(res1.end());
  185.                 }catch(java.lang.IllegalStateException e){}
  186.                 if(tmat.matches()){
  187.                     MatchResult res = tmat.toMatchResult();
  188.                     if(res.start() == 0){
  189.                         if(res.end() > len){
  190.                             len = res.end();
  191.                             tokType = tok;
  192.                         }
  193.                     }
  194.                 }
  195.             }
  196.             if(len <= 0){
  197.                 throw new Exception("Unknown lexeme " + code);
  198.             }
  199.  
  200.             if(tokType == Tokens.IDN){
  201.                 if(ans.size() > 0) {
  202.                     for (Tokens tk : ans.get(ans.size() - 1).keySet()) {
  203.                         if (tk == Tokens.Keyword && (ans.get(ans.size() - 1).get(tk).equals("procedure")
  204.                                 || ans.get(ans.size() - 1).get(tk).equals("function"))) {
  205.                             tokType = ans.get(ans.size() - 1).get(tk).equals("function") ? Tokens.FUNC_NAME : Tokens.PROC_NAME;
  206.                         }
  207.                     }
  208.                 }
  209.             }
  210.  
  211.          /*   if(tokType == Tokens.Keyword && code.substring(0 , 2).equals("if")){
  212.                 ++opif;
  213.             }
  214.             if(opif > 0 && tokType == Tokens.Keyword && code.substring(0 , 4).equals("then")){
  215.                 ++opth;
  216.             }*/
  217.             if(wasbegin == 0 && tokType == Tokens.Keyword && code.substring(0 , 5).equals("begin")){
  218.                 ++wasbegin;
  219.             }
  220.             if(tokType == Tokens.Double){
  221.                 tokType = Tokens.DIGIT;
  222.             }
  223.             if(wasbegin > 0 && tokType == Tokens.Punctuation && code.substring(0 , 1).equals("(")){
  224.                 if(ans.size() > 1){
  225.                     for (Tokens tk : ans.get(ans.size() - 1).keySet()) {
  226.                         if(tk == Tokens.IDN){
  227.                             boolean ck = false;
  228.                             String str = "";
  229.                             str = ans.get(ans.size() - 1).get(tk);
  230.  
  231.                             for(Tokens tk2 : ans.get(ans.size() - 2).keySet()){
  232.                                 if(tk2 == Tokens.Operator || (tk2 == Tokens.Punctuation && (",(").contains(ans.get(ans.size() - 2).get(tk2)))){
  233.                                     ck = true;//function
  234.                                 }
  235.                             }
  236.                         //    System.out.println(ck);
  237.                             ans.get(ans.size() - 1).clear();
  238.                             if(ck){
  239.                                 ans.get(ans.size() - 1).put(Tokens.FUNC_NAME , str);
  240.                             }else{
  241.                                 ans.get(ans.size() - 1).put(Tokens.PROC_NAME , str);
  242.                             }
  243.                         }
  244.                     }
  245.                 }
  246.             }
  247.  
  248.             if(tokType == Tokens.Keyword){
  249.                 last_key = code.substring(0 , len);
  250.             }
  251.             if(last_key.equals("const") && tokType == Tokens.IDN){
  252.                 consts.add(code.substring(0 , len));
  253.             //    tokType = Tokens.CONST_NAME;
  254.             }
  255.             if(!last_key.equals("const") && tokType == Tokens.IDN && consts.contains(code.substring(0 , len))){
  256.                 tokType = Tokens.CONST_NAME;
  257.             }
  258.  
  259.             Map<Tokens , String> curans = new HashMap<>();
  260.             curans.put(tokType , code.substring(0 , len));
  261.         //    System.out.println(curans);
  262.          //   if(tokType != Tokens.Comment)
  263.             int repeat = 1;
  264.             if(wasbegin > 0 && tokType == Tokens.Punctuation && code.substring(0 , len).equals(";")){
  265.                 ++repeat;
  266.             }
  267.             for(int kk = 0 ; kk < repeat ; ++kk)
  268.                 ans.add(curans);
  269.             //      System.out.println(code.substring(0 , len) + " " + tokType.toString());
  270.             code = code.substring(len);
  271.             code = code.trim();
  272.         }
  273.  
  274.         int sz = ans.size() - 1;
  275.         int st = 0;
  276.         lab:
  277.         while(true){
  278.             for(Tokens tk : ans.get(sz).keySet()){
  279.                 if(tk == Tokens.Comment) {
  280.                     --sz;
  281.                     continue lab;
  282.                 }
  283.             }
  284.  
  285.             for(Tokens tk : ans.get(sz).keySet()) {
  286.                 if (st == 0 && tk == Tokens.Punctuation && ans.get(sz).get(tk).equals(".")) {
  287.                     st = 1;
  288.                     --sz;
  289.                     continue lab;
  290.                 }
  291.                 if (st == 1 && tk == Tokens.Keyword && ans.get(sz).get(tk).equals("end")) {
  292.                     st = 2;
  293.                     --sz;
  294.                     continue lab;
  295.                 }
  296.  
  297.                 if (st == 2 && (tk != Tokens.Punctuation || !ans.get(sz).get(tk).equals(";"))) {
  298.                     Map<Tokens, String> curans = new HashMap<>();
  299.                     curans.put(Tokens.Punctuation, ";");
  300.                     ans.add(sz + 1, curans);ans.add(sz + 1, curans);
  301.                 }
  302.                 return ans;
  303.             }
  304.         }
  305.  
  306.      //   return ans;
  307.     }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement