From 07fadada9fc2c18f41bba5de270d5c5f82544566 Mon Sep 17 00:00:00 2001 From: Noa Senesi Date: Mon, 29 May 2023 22:13:26 +0200 Subject: [PATCH] Modified how the command works Added options: -s, --show-states Show all states of the finite state machine -p, --optimize-csv Optimize CSV file by removing ERROR actions --- README.md | 7 ++++++- g2t.bat | 3 +-- g2t.sh | 2 +- src/fr/senesi/g2t/Grammar2Table.java | 23 ++++++++++++++++++----- src/fr/senesi/g2t/table/Table.java | 9 ++++++++- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 203f93f..f7eaea2 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,17 @@ Java program that takes a grammar as an input and gives a table as an output. You will need Java to be installed in order to use the program. To run this program on a grammar file, you first need to compile it with `build` for Windows or `sh build.sh` for Linux. -Then you can use the command `g2t ` for Windows or `sh g2t.sh ` for Linux. +Then you can use the command `g2t [options]` for Windows or `sh g2t.sh [options]` for Linux. Terminals and non-terminals are automatically determined. They can be any symbol apart from the end of file symbol `$` Non-alphanumerical symbols are automatically determined to be terminals, and they are always separated. Empty words (epsilon) are written as `^` +### Options + +- `-s, --show-states`: Shows all states of the finite state machine +- `-p, --optimize-csv`: Optimizes CSV file by removing ERROR actions + ## Syntax ``` diff --git a/g2t.bat b/g2t.bat index a940a17..c0265d7 100644 --- a/g2t.bat +++ b/g2t.bat @@ -1,3 +1,2 @@ @echo off -set arg1=%1 -java -cp ./bin fr.senesi.g2t.Grammar2Table %arg1% \ No newline at end of file +java -cp ./bin fr.senesi.g2t.Grammar2Table %* \ No newline at end of file diff --git a/g2t.sh b/g2t.sh index 9b2f07b..593e21d 100644 --- a/g2t.sh +++ b/g2t.sh @@ -1 +1 @@ -java -cp ./bin fr.senesi.g2t.Grammar2Table $1 \ No newline at end of file +java -cp ./bin fr.senesi.g2t.Grammar2Table $@ \ No newline at end of file diff --git a/src/fr/senesi/g2t/Grammar2Table.java b/src/fr/senesi/g2t/Grammar2Table.java index b8e0cd3..7394a7f 100644 --- a/src/fr/senesi/g2t/Grammar2Table.java +++ b/src/fr/senesi/g2t/Grammar2Table.java @@ -10,15 +10,28 @@ import fr.senesi.g2t.tokenizer.Tokenizer; public class Grammar2Table { - public static final String VERSION = "2.0.0"; + public static final String VERSION = "2.1.0"; public static void main(String[] args) { - if (args.length != 1) { + if (args.length == 0) { + args = new String[]{"help"}; + } + + if (args[0].equals("help") || args[0].equals("?")) { System.out.println("Grammar2Table v" + VERSION); - System.out.println("Please input a file name"); + System.out.println("Usage: g2t [options]"); + System.out.println("Options:"); + System.out.println(" -s, --show-states Show all states of the finite state machine"); + System.out.println(" -p, --optimize-csv Optimize CSV file by removing ERROR actions"); System.exit(0); } + boolean showStates = false, optimizeCSV = false; + for (int i = 1; i < args.length; i++) { + if (args[i].equals("--show-states") || args[i].equals("-s")) showStates = true; + if (args[i].equals("--optimize-csv") || args[i].equals("-p")) optimizeCSV = true; + } + String[] read = Reader.getLines(args[0]); Tokenizer tokenizer = null; @@ -44,10 +57,10 @@ public static void main(String[] args) { FiniteStateMachine fsm = new FiniteStateMachine(grammar); fsm.createAllStates(); - for (State s : fsm.getStates()) s.print(); + if (showStates) for (State s : fsm.getStates()) s.print(); Table table = new Table(fsm); - table.saveCSV(args[0] + ".csv"); + table.saveCSV(args[0] + ".csv", optimizeCSV); table.save(args[0] + ".txt"); } } \ No newline at end of file diff --git a/src/fr/senesi/g2t/table/Table.java b/src/fr/senesi/g2t/table/Table.java index 2433bfc..47edd6b 100644 --- a/src/fr/senesi/g2t/table/Table.java +++ b/src/fr/senesi/g2t/table/Table.java @@ -214,6 +214,10 @@ public void save(String filename) { } public void saveCSV(String filename) { + saveCSV(filename, false); + } + + public void saveCSV(String filename, boolean optimizeCSV) { File file = new File(filename); if (file.exists()) file.delete(); @@ -230,7 +234,10 @@ public void saveCSV(String filename) { writer.write("\nI" + i); for (int j = 0; j < getTable()[i].length; j++) { - writer.write("," + getTable()[i][j].toString()); + Action action = getTable()[i][j]; + + writer.write(","); + if (!optimizeCSV || action.getType() != ActionType.ERROR) writer.write(action.toString()); } }