Commit c5b3ef2e authored by Frédéric Bapst's avatar Frédéric Bapst
Browse files

rename in s04 (Lexer, Interpreter)

parent 9aedb724
......@@ -2,12 +2,12 @@ package s04;
import java.util.HashMap;
public class ExprInterpreter {
private static ExprSymbolStream lexer;
public class Interpreter {
private static Lexer lexer;
// TODO - A COMPLETER (ex. 3)
public static int evaluate(String e) throws ExprException {
lexer = new ExprSymbolStream(e);
lexer = new Lexer(e);
int res = parseExpr();
// test if nothing follows the expression...
if (lexer.crtSymbol().length() > 0)
......
......@@ -2,7 +2,8 @@ package s04;
import java.util.ArrayList;
public class ExprInterpreterTest {
public class InterpreterTest {
//============================================================
static class TestData{
final String expr;
final int val; // expression correct value
......@@ -11,8 +12,8 @@ public class ExprInterpreterTest {
}
}
//============================================================
static ArrayList<TestData> corpusWithoutVars=new ArrayList<TestData>();
static ArrayList<TestData> corpusWithVars =new ArrayList<TestData>();
static ArrayList<TestData> corpusWithoutVars = new ArrayList<TestData>();
static ArrayList<TestData> corpusWithVars = new ArrayList<TestData>();
static {
corpusWithoutVars.add(new TestData(1, "1" ));
......@@ -35,7 +36,8 @@ public class ExprInterpreterTest {
corpusWithVars.add(new TestData(3, "(3+ (4-1)v) -v"));
corpusWithVars.add(new TestData(29, "3*(4-1)abc + (2)*abc + (4+abc)xy + xy"));
};
}
static String[] wrongExpressions = {
" ",
"2+",
......@@ -50,20 +52,20 @@ public class ExprInterpreterTest {
"3-aaa",
"(3+1)aaa -aab"
};
// ------------------------------------------------------------
public static void generateException(String msg) {
try { Thread.sleep(300); }
catch (InterruptedException ex) { }
throw new RuntimeException(msg);
}
// ------------------------------------------------------------
public static int testExpr(String expr) throws ExprException {
System.out.print("'"+ expr + "'");
int res = ExprInterpreter.evaluate(expr);
int res = Interpreter.evaluate(expr);
System.out.println(" gives result : " + res);
return res;
}
// ------------------------------------------------------------
public static void testCorrectExpr() {
for (TestData td:corpusWithoutVars) {
try {
......@@ -79,7 +81,7 @@ public class ExprInterpreterTest {
}
}
}
// ------------------------------------------------------------
public static void testExprWithVars() {
System.out.println();
for (TestData td:corpusWithVars) {
......@@ -89,21 +91,19 @@ public class ExprInterpreterTest {
generateException("'" + td.expr + "' returns " + res
+ " but the correct result is: " + td.val);
}
}
catch (ExprException e) {
} catch (ExprException e) {
e.printStackTrace();
generateException("'" + td.expr + "' is wrong due to " + e);
}
}
}
// ------------------------------------------------------------
public static void testWrongExpr() {
String expr;
boolean exprIsAccepted;
int i;
System.out.println();
for (i = 0; i < wrongExpressions.length; i++) {
for (int i = 0; i < wrongExpressions.length; i++) {
exprIsAccepted = true;
expr = wrongExpressions[i];
try {
......
package s04;
public class ExprSymbolStream {
public class Lexer {
private String crtToken = "";
private String whole;
private int nextCharIndex=0; // index of the char following crtToken
// ------------------------------------------------------------
public ExprSymbolStream(String s) {
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();
return nextCharIndex>=whole.length();
}
// ------------------------------------------------------------
private void trimSpaces() {
if (noMoreChars()) return;
char c = whole.charAt(nextCharIndex);
......@@ -28,7 +28,7 @@ public class ExprSymbolStream {
}
return;
}
// ------------------------------------------------------------
public void goToNextSymbol() {
crtToken = "";
trimSpaces();
......@@ -44,7 +44,7 @@ public class ExprSymbolStream {
// - autre chose :
// prendre juste ce caractre
}
// ------------------------------------------------------------
public boolean isNumber() {
if (crtToken.length()==0)return false;
for(int j=0; j<crtToken.length(); j++)
......@@ -52,7 +52,7 @@ public class ExprSymbolStream {
return false;
return true;
}
// ------------------------------------------------------------
public boolean isIdent() {
if (crtToken.length()==0)return false;
for(int j=0; j<crtToken.length(); j++)
......@@ -60,15 +60,16 @@ public class ExprSymbolStream {
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("/");}
// ------------------------------------------------------------
public int intFromSymbol(){ // returns 0 if crtSymbol is not a number
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);
......
......@@ -2,7 +2,7 @@ package s04;
import java.util.ArrayList;
public class ExprSymbolStreamTest {
public class LexerTest {
static class TestData{
final String expr;
final int size; // number of tokens
......@@ -24,12 +24,12 @@ public class ExprSymbolStreamTest {
corpus.add(new TestData(7, "(sqrt (4)))" ));
corpus.add(new TestData(6, "aa55bb cc 4 4"));
corpus.add(new TestData(11, "2+2+2+2+2+2"));
};
//------------------------------------------------------------
}
public static void testLexicalAnalyzer(String expr, int expectedSize) {
System.out.println("--------------------");
System.out.println("Symbol stream for : '" + expr + "'");
ExprSymbolStream s = new ExprSymbolStream(expr);
Lexer s = new Lexer(expr);
System.out.print(" gives : ");
int n=0;
String whole="";
......@@ -47,13 +47,13 @@ public class ExprSymbolStreamTest {
if (!whole.equals(withoutSpaces(expr)))
generateException("bad sequence of symbols");
}
// ------------------------------------------------------------
public static void generateException(String msg) {
try { Thread.sleep(300); }
catch (InterruptedException ex) { }
throw new RuntimeException(msg);
}
// ------------------------------------------------------------
static String withoutSpaces(String s) {
String res="";
for(int i=0; i<s.length(); i++) {
......@@ -62,9 +62,9 @@ public class ExprSymbolStreamTest {
}
return res;
}
// ------------------------------------------------------------
public static void main(String[] args) {
for (TestData e:corpus) {
testLexicalAnalyzer(e.expr, e.size);
}
......
<?include("list-dir.php");?>
<?include("list-dir.php");?>
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