Commit 21aabf28 authored by Ruben Terceiro's avatar Ruben Terceiro
Browse files

S03 -> 2.5

parent 6b3955cc
package s03;
package s03;
public class DiagSynt {
//========================================================================
static class ParseException extends Exception {
public ParseException() { super(); }
public ParseException(String msg) { super(msg); }
}
//========================================================================
private static String theWord;
private static int crtIndex;
// ----------------------------------------------------------------------
private static void initSymbolStream(String w) {
theWord = w; crtIndex=0;
}
private static void goToNextSymbol() {
crtIndex++;
}
private static boolean isEndOfSymbolStream() {
return crtIndex >= theWord.length();
}
private static char crtSymbol() {
if (isEndOfSymbolStream())
return '\0';
return theWord.charAt(crtIndex);
}
// ----------------------------------------------------------------------
private static void parse_S() throws ParseException {
// TODO - A COMPLETER
}
private static void parse_A() throws ParseException {
// TODO - A COMPLETER
}
private static void parse_B() throws ParseException {
// TODO - A COMPLETER
}
private static void parse_C() throws ParseException {
// TODO - A COMPLETER
}
public static boolean isAccepted(String w) {
initSymbolStream(w);
try {
parse_S();
} catch (ParseException e) {
return false;
}
return (isEndOfSymbolStream());
}
// ----------------------------------------------------------------------
public static void main(String [] args) {
if (args.length != 1) {
System.out.println("Usage: java DiagSynt word");
System.exit(-1);
}
System.out.println(isAccepted(args[0]));
}
//========================================================================
static class ParseException extends Exception {
public ParseException() {
super();
}
public ParseException(String msg) {
super(msg);
}
}
//========================================================================
private static String theWord;
private static int crtIndex;
// ----------------------------------------------------------------------
private static void initSymbolStream(String w) {
theWord = w;
crtIndex = 0;
}
private static void goToNextSymbol() {
crtIndex++;
}
private static boolean isEndOfSymbolStream() {
return crtIndex >= theWord.length();
}
private static char crtSymbol() {
if (isEndOfSymbolStream())
return '\0';
return theWord.charAt(crtIndex);
}
// ----------------------------------------------------------------------
private static void parse_S() throws ParseException {
// TODO - A COMPLETER
if (crtSymbol() != 'i')
throw new ParseException();
goToNextSymbol();
parse_A();
if (crtSymbol() != 'i')
throw new ParseException();
goToNextSymbol();
}
private static void parse_A() throws ParseException {
// TODO - A COMPLETER
if (crtSymbol() == '+') {
goToNextSymbol();
parse_B();
} else parse_S();
}
private static void parse_B() throws ParseException {
// TODO - A COMPLETER
if (crtSymbol() != 'i')
throw new ParseException();
goToNextSymbol();
parse_C();
if (crtSymbol() != 'i')
throw new ParseException();
goToNextSymbol();
}
private static void parse_C() throws ParseException {
// TODO - A COMPLETER
if (crtSymbol() == '=')
goToNextSymbol();
else parse_B();
}
public static boolean isAccepted(String w) {
initSymbolStream(w);
try {
parse_S();
} catch (ParseException e) {
return false;
}
return (isEndOfSymbolStream());
}
// ----------------------------------------------------------------------
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java DiagSynt word");
System.exit(-1);
}
System.out.println(isAccepted(args[0]));
}
}
......@@ -5,22 +5,29 @@ 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;
// TODO - A COMPLETER
}
public boolean accepts(int[] word) {
// ------ Simulation pseudo-code :
// crtState = 0;
boolean accepts = false;
crtState = 0;
for (int s : word ) {
}
// for each symbol s in word
// apply transition with s
// return true if crtState is accepting
return false; // TODO - A COMPLETER
return accepts; // TODO - A COMPLETER
}
public int nStates() {
......
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