Created
November 12, 2021 17:45
-
-
Save nmanumr/45027f16911b26afde9f634a8a873e44 to your computer and use it in GitHub Desktop.
CheatSheat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package practice; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.Scanner; | |
public class eliminate_lr { | |
private static final ArrayList<LinkedList<String>> lists = new ArrayList<>(); | |
public static void main(String[] args) { | |
File cfg = new File("src/samplecfg2.txt"); | |
try { | |
Scanner scanner = new Scanner(cfg); | |
while (scanner.hasNextLine()) { | |
String line = scanner.nextLine(); | |
String[] x = line.split("->"); | |
LinkedList<String> list = new LinkedList<>(); | |
list.add(x[0].trim()); | |
String[] rules = x[1].split("\\|"); | |
for (String rule : rules) { | |
rule = rule.trim(); | |
if (!rule.equals("")) | |
list.add(rule); | |
} | |
lists.add(list); | |
} | |
// Print Linked list | |
System.out.println(lists); | |
reduceCFG(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static LinkedList<String> getListFromNT(String nt) { | |
for (LinkedList<String> rules : lists) { | |
if (rules.get(0).equals(nt)) | |
return rules; | |
} | |
return null; | |
} | |
private static void eliminateLR(String nt, int i) { | |
LinkedList<String> list = getListFromNT(nt); | |
if (list == null) | |
return; | |
String alpha = list.get(i).replaceFirst(nt, ""); | |
ArrayList<String> betas = new ArrayList<>(); | |
for (String rule : list) if (!rule.startsWith(nt)) betas.add(rule); | |
// New Rules | |
StringBuilder nRule = new StringBuilder(nt).append(" -> "); | |
for (int j = 0, betasSize = betas.size(); j < betasSize; j++) { | |
String beta = betas.get(j); | |
nRule.append(beta).append(nt).append("'"); | |
if(j != betasSize - 1) | |
nRule.append(" | "); | |
} | |
System.out.println(nRule); | |
System.out.println(nt + "' -> " + alpha + nt + "' | ɛ "); | |
} | |
private static void reduceCFG() { | |
for (LinkedList<String> rules : lists) { | |
String nt = rules.get(0); | |
for (int i = 1; i < rules.size(); i++) { | |
if (rules.get(i).startsWith(nt)) { | |
System.out.println("Left Recursion Detected!\nPossible Elimination"); | |
eliminateLR(nt, i); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package practice; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.Scanner; | |
public class load_cfg { | |
private static final ArrayList<LinkedList<String>> lists = new ArrayList<>(); | |
public static void main(String[] args) { | |
File cfg = new File("src/samplecfg.txt"); | |
try { | |
Scanner scanner = new Scanner(cfg); | |
while (scanner.hasNextLine()){ | |
String line = scanner.nextLine(); | |
String[] x = line.split("->"); | |
LinkedList<String> list = new LinkedList<>(); | |
list.add(x[0]); | |
String[] rules = x[1].split("\\|"); | |
for(String rule: rules){ | |
rule = rule.trim(); | |
if(!rule.equals("")) | |
list.add(rule); | |
} | |
lists.add(list); | |
} | |
// Print Linked list | |
System.out.println(lists); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package practice; | |
public class recognize_identifiers { | |
public static boolean isIdentifier(String tok){ | |
return tok.matches("[_a-zA-Z][_a-zA-Z0-9]*"); | |
} | |
public static void main(String[] args) { | |
System.out.println(isIdentifier("123")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package practice; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Scanner; | |
public class recognize_keywords { | |
private static final String[] KEYWORDS = { | |
"int", | |
"String", | |
"char", | |
"static", | |
"if", | |
"boolean", | |
"private", | |
"public", | |
"protected", | |
"else", | |
"long", | |
"class", | |
"void", | |
"true", | |
"false", | |
"return", | |
"final", | |
"try", | |
"catch" | |
}; | |
private static boolean isKeyword(String tok){ | |
for (String keyword : KEYWORDS) if (keyword.equals(tok)) return true; | |
return false; | |
} | |
public static void main(String[] args) { | |
File codeFile = new File("src/practice/recognize_keywords.java"); | |
try { | |
Scanner reader = new Scanner(codeFile); | |
while (reader.hasNextLine()){ | |
String line = reader.nextLine(); | |
String[] tokens = line.split(" "); | |
for (String token: tokens) if (isKeyword(token)) System.out.println("Keyword: " + token); | |
} | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment