Skip to content

Commit

Permalink
Modified how the command works
Browse files Browse the repository at this point in the history
Added options:
-s, --show-states    Show all states of the finite state machine
-p, --optimize-csv   Optimize CSV file by removing ERROR actions
  • Loading branch information
NoaSenesi committed May 29, 2023
1 parent 1c067e5 commit 07fadad
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file>` for Windows or `sh g2t.sh <file>` for Linux.
Then you can use the command `g2t <file> [options]` for Windows or `sh g2t.sh <file> [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

```
Expand Down
3 changes: 1 addition & 2 deletions g2t.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
@echo off
set arg1=%1
java -cp ./bin fr.senesi.g2t.Grammar2Table %arg1%
java -cp ./bin fr.senesi.g2t.Grammar2Table %*
2 changes: 1 addition & 1 deletion g2t.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
java -cp ./bin fr.senesi.g2t.Grammar2Table $1
java -cp ./bin fr.senesi.g2t.Grammar2Table $@
23 changes: 18 additions & 5 deletions src/fr/senesi/g2t/Grammar2Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file> [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;
Expand All @@ -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");
}
}
9 changes: 8 additions & 1 deletion src/fr/senesi/g2t/table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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());
}
}

Expand Down

0 comments on commit 07fadad

Please sign in to comment.