Commit a469ee5c authored by Ruben Terceiro's avatar Ruben Terceiro
Browse files

test

parent 21aabf28
package s03;
package s03;
import java.util.HashSet;
import java.util.Set;
public class FSM {
private int[][] transition; // copy of the transition table
// TODO - A COMPLETER
private Set<Integer> acceptingStates;
private int crtState;
public FSM(int[][] transitions, // also gives n and m
Set<Integer> acceptingStates) {
int nStates = transitions.length;
int nSymbols = transitions[0].length;
this.acceptingStates = acceptingStates;
private int[][] transition; // copy of the transition table
// TODO - A COMPLETER
}
private Set <Integer> acceptingStates;
private int crtState;
public FSM(int[][] transitions, // also gives n and m
Set <Integer> acceptingStates) {
int nStates = transitions.length;
int nSymbols = transitions[0].length;
this.acceptingStates = acceptingStates;
this.transition = transitions;
// TODO - A COMPLETER
}
public boolean accepts(int[] word) {
//TODO - A COMPLETER
// ------ Simulation pseudo-code :
//for each symbol s in word
//apply transition with s
//return true if crtState is accepting
crtState = 0;
for (int i = 0; i < word.length; i++) {
crtState = transition[crtState][word[i]];
}
return acceptingStates.contains(crtState);
public boolean accepts(int[] word) {
// ------ Simulation pseudo-code :
boolean accepts = false;
crtState = 0;
for (int s : word ) {
}
public int nStates() {
return transition.length;
}
public int nSymbols() {
return transition[0].length;
}
// for each symbol s in word
// apply transition with s
// return true if crtState is accepting
return accepts; // TODO - A COMPLETER
}
public int nStates() {
return transition.length;
}
public int nSymbols() {
return transition[0].length;
}
// -----------------------------------------------------------
public static void main(String [] args) {
if (args.length==0)
args = new String[] {"abbab", "baab", "bbabbb"};
int [][] transitions = {
{1, 0}, // <-- from state 0, with symbol a,b,c...
{2, 1}, // <-- from state 1, with symbol a,b,c...
{2, 2}
};
Set<Integer> acceptingStates = new HashSet<>();
acceptingStates.add(1);
FSM autom = new FSM(transitions, acceptingStates);
for (int i=0; i<args.length; i++) {
String letters = args[i];
int [] word = new int [letters.length()];
for (int j=0; j<letters.length(); j++)
word[j] = letters.charAt(j) - 'a';
boolean res = autom.accepts(word);
System.out.println(letters +" : "+res);
// -----------------------------------------------------------
public static void main(String[] args) {
if (args.length == 0)
args = new String[]{"abbab", "baab", "bbabbb", ""};
int[][] transitions = {
{1, 0}, // <-- from state 0, with symbol a,b,c...
{2, 1}, // <-- from state 1, with symbol a,b,c...
{2, 2}
};
Set <Integer> acceptingStates = new HashSet <>();
acceptingStates.add(0);
acceptingStates.add(1);
FSM autom = new FSM(transitions, acceptingStates);
for (int i = 0; i < args.length; i++) {
String letters = args[i];
int[] word = new int[letters.length()];
for (int j = 0; j < letters.length(); j++)
word[j] = letters.charAt(j) - 'a';
boolean res = autom.accepts(word);
System.out.println(letters + " : " + res);
}
}
}
}
package s04;
public class Lexer {
private String crtToken = "";
private String whole;
private int nextCharIndex=0; // index of the char following crtToken
public Lexer(String s) {
whole = s;
goToNextSymbol();
}
public String crtSymbol() { // returns "" if the end is reached
return crtToken;
}
private boolean noMoreChars() {
return nextCharIndex>=whole.length();
}
private void trimSpaces() {
if (noMoreChars()) return;
char c = whole.charAt(nextCharIndex);
while (Character.isWhitespace(c)) {
nextCharIndex++;
if (noMoreChars()) return;
c = whole.charAt(nextCharIndex);
private String crtToken = "";
private String whole;
private int nextCharIndex = 0; // index of the char following crtToken
public Lexer(String s) {
whole = s;
goToNextSymbol();
}
public String crtSymbol() { // returns "" if the end is reached
return crtToken;
}
private boolean noMoreChars() {
return nextCharIndex >= whole.length();
}
private void trimSpaces() {
if (noMoreChars()) return;
char c = whole.charAt(nextCharIndex);
while (Character.isWhitespace(c)) {
nextCharIndex++;
if (noMoreChars()) return;
c = whole.charAt(nextCharIndex);
}
return;
}
public void goToNextSymbol() {
crtToken = "";
trimSpaces();
if (noMoreChars()) return;
char c = whole.charAt(nextCharIndex);
// .....
// TODO - A COMPLETER...
// Selon que le prochain caractre est :
// - une lettre :
// grouper les lettres conscutives
// - un chiffre :
// grouper les chiffres conscutifs
// - autre chose :
// prendre juste ce caractre
if (isNumber()) {
while (isNumber())
crtToken += nextCharIndex++;
} else if (isIdent()) {
while (isIdent())
crtToken += nextCharIndex++;
} else {
crtToken += nextCharIndex++;
}
}
public boolean isNumber() {
if (crtToken.length() == 0) return false;
for (int j = 0; j < crtToken.length(); j++)
if (!Character.isDigit(crtToken.charAt(j)))
return false;
return true;
}
public boolean isIdent() {
if (crtToken.length() == 0) return false;
for (int j = 0; j < crtToken.length(); j++)
if (!Character.isLetter(crtToken.charAt(j)))
return false;
return true;
}
public boolean isOpeningParenth() {
return crtToken.equals("(");
}
public boolean isClosingParenth() {
return crtToken.equals(")");
}
public boolean isPlus() {
return crtToken.equals("+");
}
public boolean isMinus() {
return crtToken.equals("-");
}
public boolean isStar() {
return crtToken.equals("*");
}
public boolean isSlash() {
return crtToken.equals("/");
}
// returns 0 if crtSymbol is not a number
public int intFromSymbol() {
if (!isNumber())
return 0;
return Integer.parseInt(crtToken);
}
return;
}
public void goToNextSymbol() {
crtToken = "";
trimSpaces();
if (noMoreChars()) return;
char c = whole.charAt(nextCharIndex);
// .....
// TODO - A COMPLETER...
// Selon que le prochain caractre est :
// - une lettre :
// grouper les lettres conscutives
// - un chiffre :
// grouper les chiffres conscutifs
// - autre chose :
// prendre juste ce caractre
}
public boolean isNumber() {
if (crtToken.length()==0)return false;
for(int j=0; j<crtToken.length(); j++)
if (! Character.isDigit(crtToken.charAt(j)))
return false;
return true;
}
public boolean isIdent() {
if (crtToken.length()==0)return false;
for(int j=0; j<crtToken.length(); j++)
if (! Character.isLetter(crtToken.charAt(j)))
return false;
return true;
}
public boolean isOpeningParenth() { return crtToken.equals("("); }
public boolean isClosingParenth() { return crtToken.equals(")"); }
public boolean isPlus() { return crtToken.equals("+"); }
public boolean isMinus() { return crtToken.equals("-"); }
public boolean isStar() { return crtToken.equals("*"); }
public boolean isSlash() { return crtToken.equals("/"); }
// returns 0 if crtSymbol is not a number
public int intFromSymbol() {
if (! isNumber())
return 0;
return Integer.parseInt(crtToken);
}
}
......@@ -62,6 +62,7 @@ public class LexerTest {
}
return res;
}
// ------------------------------------------------------------
public static void main(String[] args) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment