Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ravixr committed Jun 16, 2024
1 parent ce85c84 commit 26a5188
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 65 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
*.zip
*.tar.gz
*.rar
*.jff

# Virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

# Test files
*.txt
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

# Regex to DFA Converter

Este projeto converte expressões regulares em Autômatos Finitos Determinísticos (DFA) e permite testar sentenças com o DFA gerado.

## Arquivos no Projeto

- `FA.java`: Implementa a classe Finite Automaton (FA) que manipula a conversão de expressões regulares para autômatos finitos e outras operações relacionadas.
- `RegexToDFA.java`: Contém a lógica principal para ler a expressão regular e as sentenças a partir de arquivos, converter a expressão para DFA, testar as sentenças e salvar o DFA em formato JFLAP.
- `run_tests.sh`: Script de shell para criar arquivos de teste e executar testes automaticamente.

## Pré-requisitos

- JDK 11 ou superior.

## Compilação

Para compilar o projeto, use o comando `javac`:

```sh
javac FA.java RegexToDFA.java
```

Este comando irá gerar os arquivos `.class` necessários para executar o programa.

## Execução

### Ajuda

Para ver a ajuda e os detalhes do uso:

```sh
java RegexToDFA --help
```

### Uso

Para executar o programa, forneça o caminho para o arquivo de sentenças e o arquivo de regex:

```sh
java RegexToDFA <arquivo-de-sentencas> <arquivo-de-regex>
```

### Exemplo

```sh
java RegexToDFA sentences.txt regex.txt
```

## Testes Automáticos

Um script `run_tests.sh` está incluído para criar arquivos de teste e executar testes automaticamente. Para usá-lo:

1. Navegue até a pasta do script.
2. Execute o script:

```sh
./run_tests.sh
```

## Estrutura dos Arquivos de Entrada

### Arquivo de Regex

O arquivo de regex deve conter uma única linha com a expressão regular a ser convertida.

Exemplo (`regex.txt`):

```plaintext
(a+b)c
```

### Arquivo de Sentenças

O arquivo de sentenças deve conter uma sentença por linha.

Exemplo (`sentences.txt`):

```plaintext
ac
bc
abac
```

## Saída

O programa exibirá no console se cada sentença foi aceita ou rejeitada pelo DFA gerado a partir da expressão regular. Além disso, um arquivo `.jff` (JFLAP) será gerado contendo a definição do DFA.

## Releases

Você pode baixar o arquivo JAR na seção de releases do repositório no GitHub. [Seção de Releases](https://github.com/ravixr/regex-to-dfa/releases)
44 changes: 1 addition & 43 deletions instances/regex-test-DFA.jff
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created by https://github.com/ravixr/regex-to-dfa -->
<structure>&#13;
<type>fa</type>&#13;
<automaton>&#13;
<state id="0" name="q0">&#13;
<x>0</x>&#13;
<y>0</y>&#13;
<initial/>&#13;
<final/>&#13;
</state>&#13;
<state id="1" name="q1">&#13;
<x>0</x>&#13;
<y>0</y>&#13;
<final/>&#13;
</state>&#13;
<state id="2" name="q2">&#13;
<x>0</x>&#13;
Expand All @@ -22,10 +19,6 @@
<state id="3" name="q3">&#13;
<x>0</x>&#13;
<y>0</y>&#13;
</state>&#13;
<state id="4" name="q4">&#13;
<x>0</x>&#13;
<y>0</y>&#13;
<final/>&#13;
</state>&#13;
<transition>&#13;
Expand All @@ -36,47 +29,12 @@
<transition>&#13;
<from>0</from>&#13;
<to>2</to>&#13;
<read>b</read>&#13;
</transition>&#13;
<transition>&#13;
<from>0</from>&#13;
<to>3</to>&#13;
<read>c</read>&#13;
</transition>&#13;
<transition>&#13;
<from>0</from>&#13;
<to>4</to>&#13;
<read>d</read>&#13;
</transition>&#13;
<transition>&#13;
<from>1</from>&#13;
<to>1</to>&#13;
<read>a</read>&#13;
</transition>&#13;
<transition>&#13;
<from>1</from>&#13;
<to>2</to>&#13;
<read>b</read>&#13;
</transition>&#13;
<transition>&#13;
<from>2</from>&#13;
<to>1</to>&#13;
<read>a</read>&#13;
</transition>&#13;
<transition>&#13;
<from>2</from>&#13;
<to>2</to>&#13;
<read>b</read>&#13;
</transition>&#13;
<transition>&#13;
<from>3</from>&#13;
<to>3</to>&#13;
<read>c</read>&#13;
</transition>&#13;
<transition>&#13;
<from>3</from>&#13;
<to>4</to>&#13;
<read>d</read>&#13;
<read>b</read>&#13;
</transition>&#13;
</automaton>&#13;
</structure>
6 changes: 5 additions & 1 deletion src/FA.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ public static FA fromRegex(String regex) throws IllegalArgumentException {
}
j = 0;
continue;
}
} else if (j >= s.length()) {
break;
}
if (pos == -1 || calcPriority(s.charAt(j)) > calcPriority(op)) {
pos = j;
if (s.charAt(j) != '+' && s.charAt(j) != '*') {
Expand All @@ -132,7 +134,9 @@ public static FA fromRegex(String regex) throws IllegalArgumentException {
s = getExpandableTransition(q);
}
}
// nfa.SaveJFLAPXML("nfa-λ.jff");
nfa.removeLambdaTransitions();
// nfa.SaveJFLAPXML("nfa.jff");
return nfa;
}

Expand Down
16 changes: 11 additions & 5 deletions src/RegexToDFA.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ public static void main(String[] args) throws Exception {
}

public static void usage() {
System.out.println("Usage: java RegexToDfa.jar [options] <sentence-file> <regex-file>");
System.out.println("Usage: java -jar RegexToDfa.jar [options] <sentence-file> <regex-file>");
System.out.println("Options:");
System.out.println("\t--help\t\tPrint this message");
System.out.println("Details:");
System.out.println("\t<sentence-file>\tFile containing the sentences to be tested (one per line)");
System.out.println("\t<regex-file>\tFile containing the regex to be converted to DFA (one line)");
// Specify the format of the regex
System.out.println("Regex format:");
System.out.println("\t- Use '" + FA.LAMBDA + "' for lambda transitions ('blank' character input transitions)");
System.out.println("\t- Use '()' for grouping (e.g. '(a+b)*' to group 'a+b'");
Expand All @@ -50,15 +49,22 @@ public static void usage() {
System.out.println("\t- Use '\\' followed by an operator character to use it as a literal (e.g. '\\" + FA.LAMBDA + "' to use '\" + FA.LAMBDA + \"' as a literal)");
}

public static String ANSI_RESET = "\u001B[0m";
public static String ANSI_RED = "\u001B[31m";
public static String ANSI_GREEN = "\u001B[32m";

public static void testDFA(FA dfa) throws Exception {
if (System.getProperty("os.name").toLowerCase().contains("win")) {
ANSI_RESET = ANSI_RED = ANSI_GREEN = "";
}
System.out.println("Testing DFA from regex: " + regex);
while (sentenceReader.ready()) {
String sentence = sentenceReader.readLine();
if (dfa.runDFA(sentence)) {
System.out.println("[ACCEPTED]: " + sentence);
System.out.println(ANSI_GREEN + "[ACCEPTED]" + ANSI_RESET + ": " + sentence);
} else {
System.out.println("[REJECTED]: " + sentence);
System.out.println(ANSI_RED + "[REJECTED]" + ANSI_RESET + ": " + sentence);
}
}
}
}
}
1 change: 0 additions & 1 deletion tests/regex-test.txt

This file was deleted.

47 changes: 47 additions & 0 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# Array of regular expressions
expressions=(
"(a+b)*c"
"a(b+c)d"
"(ab)*+(cd)*"
"a(bc)*d+ef"
"a*b*+c"
"(a+b+c)*"
"a(b+c)*d"
"(a+b)*a(a+b)*"
"ab+c(de+f)"
"((a+b)(c+d))*"
)

# Array of sentences
sentences=(
"c\nac\nbc\nabac\nbabc\n"
"abd\nacd\nabcd\nad\naabdc\n"
"\nab\ncd\nababcd\ncdcdcd\n"
"ad\nabcd\nabcbd\nef\na\n"
"\na\nb\naaabbb\nc\n"
"\nabc\naabbcc\ncab\nbcaacb\n"
"ad\nabcd\nabbbd\nabcbcbcbcd\nacd\n"
"a\nba\nabaa\nbaba\nbbbbba\n"
"abcde\nabcf\nabcdef\nab\ncde\n"
"ac\nbd\nacbd\nacacac\nbdac\n"
)

# Create and populate the test files
for i in {0..9}; do
regex_file="regex-$((i+1)).txt"
sentence_file="sentence-$((i+1)).txt"

# Write the regular expression to the regex file
echo -e "${expressions[i]}" > "$regex_file"

# Write the sentences to the sentence file
echo -e "${sentences[i]}" > "$sentence_file"

# Run the tests
cd ..

for i in {0..9}; do
java -jar RegexToDFA ../tests/sentence-$((i+1)).txt ../tests/regex-$((i+1)).txt
done
15 changes: 0 additions & 15 deletions tests/sentences-test.txt

This file was deleted.

0 comments on commit 26a5188

Please sign in to comment.