Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziocash committed Nov 24, 2021
0 parents commit d0f88cc
Show file tree
Hide file tree
Showing 15 changed files with 1,019 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"java.project.referencedLibraries": [
"lib/**/*.jar",
"c:\\Users\\Simone Gattini\\Downloads\\junit-platform-console-standalone-1.8.1.jar"
]
}
134 changes: 134 additions & 0 deletions CompilatoreAcDc/src/scanner/Scanner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package scanner;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PushbackReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import token.*;

public class Scanner {
final char EOF = (char) -1; // int 65535
private int row;
private PushbackReader buffer;
private String log;

private List<Character> skipChars; // ' ', '\n', '\t', '\r', EOF
private List<Character> letters; // 'a',...'z'
private List<Character> numbers; // '0',...'9'

private HashMap<Character, TokenType> operatorsMap; // '+', '-', '*', '/', '=', ';'
private HashMap<String, TokenType> keyWordsMap; // "print", "float", "int"

public Scanner(String fileName) throws FileNotFoundException {
this.buffer = new PushbackReader(new FileReader(fileName));
row = 1;
skipChars = Arrays.asList(' ', '\n', '\t', '\r', EOF);
letters = Arrays.asList('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
numbers = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
operatorsMap = new HashMap<Character, TokenType>() {
{
put('+', TokenType.PLUS);
put('-', TokenType.MINUS);
put('*', TokenType.TIMES);
put('/', TokenType.DIV);
put('=', TokenType.ASSIGN);
put(';', TokenType.SEMI);
}
};

keyWordsMap = new HashMap<String, TokenType>() {
{
put("print", TokenType.PRINT);
put("float", TokenType.TYFLOAT);
put("int", TokenType.TYINT);
}
};
}

/*
* private Token peekToken() throws IOException { ..... }
*/

public Token nextToken() throws IOException {

// nextChar contiene il prossimo carattere dell'input.
char nextChar = peekChar();

// Avanza nel buffer leggendo i carattere in skipChars
// incrementando riga se leggi '\n'.
// Se raggiungi la fine del file ritorna il Token EOF

while (nextChar != '\n') {
if (skipChars.contains(nextChar))
readChar();

if (numbers.contains(nextChar))
return scanNumber();

if (letters.contains(nextChar))
return scanId();

if (nextChar == EOF)
return new Token(TokenType.EOF, row);
}

// Se nextChar e' in numbers
// return scanNumber()
// che legge sia un intero che un float e ritorna il Token INUM o FNUM
// i caratteri che leggete devono essere accumulati in una stringa
// che verra' assegnata al campo valore del Token

// Se nextChar e' in letters
// return scanId()
// che legge tutte le lettere minuscole e ritorna un Token ID o
// il Token associato Parola Chiave (per generare i Token per le
// parole chiave usate l'HaskMap di corrispondenza

// Se nextChar e' in operators
// ritorna il Token associato con l'operatore o il delimitatore

// Altrimenti il carattere NON E' UN CARATTERE LEGALE

return null;

}

private Token scanNumber() throws IOException {
char nextChar = peekChar();
StringBuilder sb = new StringBuilder();
Token numberToken = new Token(TokenType.INT, row);
while (numbers.contains(nextChar)) {
if (nextChar == '.')
numberToken.setType(TokenType.FLOAT);
sb.append(readChar());
}
return numberToken;

}

private Token scanId() throws IOException {
char nextChar = peekChar();
StringBuilder sb = new StringBuilder();
Token idToken = new Token(TokenType.ID, row);
while (letters.contains(nextChar)) {
sb.append(readChar());
}
return null;
}

private char readChar() throws IOException {
return ((char) this.buffer.read());
}

private char peekChar() throws IOException {
char c = (char) buffer.read();
buffer.unread(c);
return c;
}
}
31 changes: 31 additions & 0 deletions CompilatoreAcDc/src/test/TestScanner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package test;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;

import org.junit.Test;

import scanner.Scanner;
import token.Token;
import token.TokenType;

public class TestScanner {

// @Test
// public void testScanEOF() throws IOException {
// String path = "C:\\Users\\Simone Gattini\\source\\repos\\UPO-Fondamenti-Linguaggi-Traduttori\\CompilatoreAcDc\\src\\test\\data\\testEOF.txt";
// Scanner scanner = new Scanner(path);
// assertNull(scanner.nextToken());
// fail("Unexpected value");

// }

@Test
public void testScanId() throws IOException {
String path = "src/test/data/testIdKw.txt";
Scanner scanner = new Scanner(path);
}

}
44 changes: 44 additions & 0 deletions CompilatoreAcDc/src/test/TestToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package test;

import static org.junit.Assert.*;

import org.junit.Test;

import token.Token;
import token.TokenType;

public class TestToken {

@Test
public void testToken() {

Token tok0 = new Token(TokenType.ASSIGN,1);
Token tok1 = new Token(TokenType.DIV,1);
Token tok2 = new Token(TokenType.EOF,5);
Token tok3 = new Token(TokenType.FLOAT,1,"1.0");
Token tok4 = new Token(TokenType.ID,1,"pippo");
Token tok5 = new Token(TokenType.INT,1,"1");
Token tok6 = new Token(TokenType.MINUS,1);
Token tok7 = new Token(TokenType.PLUS,1);
Token tok8 = new Token(TokenType.PRINT,11);
Token tok9 = new Token(TokenType.SEMI,1);
Token tok10 = new Token(TokenType.TIMES,1);
Token tok11 = new Token(TokenType.TYFLOAT,1);
Token tok12 = new Token(TokenType.TYINT,1);
assertEquals("<ASSIGN,r:1>",tok0.toString());
assertEquals("<DIV,r:1>",tok1.toString());
assertEquals("<EOF,r:5>",tok2.toString());
assertEquals("<FLOAT,r:1,1.0>",tok3.toString());
assertEquals("<ID,r:1,pippo>",tok4.toString());
assertEquals("<INT,r:1,1>",tok5.toString());
assertEquals("<MINUS,r:1>",tok6.toString());
assertEquals("<PLUS,r:1>",tok7.toString());
assertEquals("<PRINT,r:11>",tok8.toString());
assertEquals("<SEMI,r:1>",tok9.toString());
assertEquals("<TIMES,r:1>",tok10.toString());
assertEquals("<TYFLOAT,r:1>",tok11.toString());
assertEquals("<TYINT,r:1>",tok12.toString());

}

}
2 changes: 2 additions & 0 deletions CompilatoreAcDc/src/test/data/testEOF.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


7 changes: 7 additions & 0 deletions CompilatoreAcDc/src/test/data/testGenerale.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

int tempa;
tempa = 5.;

float tempb;
tempb = tempa + 3.2;
print tempb;
6 changes: 6 additions & 0 deletions CompilatoreAcDc/src/test/data/testIdKw.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int
float floata
print aprintf
nome
intnome
int nome
8 changes: 8 additions & 0 deletions CompilatoreAcDc/src/test/data/testNumbers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
30000

698.
13.454 098.895
45668
98.

89.99999999
10 changes: 10 additions & 0 deletions CompilatoreAcDc/src/test/data/testOperators.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
+
-*
/




=

;
53 changes: 53 additions & 0 deletions CompilatoreAcDc/src/token/Token.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package token;

public class Token {

private int row;
private TokenType type;
private String value;

public Token(TokenType type, int row, String value) {
this.type = type;
this.row = row;
this.value = value;
}

public Token(TokenType type, int row) {
this.type = type;
this.row = row;
}

public int getRow() {
return row;
}

public void setRow(int row) {
this.row = row;
}

public TokenType getType() {
return type;
}

public void setType(TokenType type) {
this.type = type;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("<").append(type).append(",").append("r:"+row);
builder = value != null ? builder.append(","+value+">") : builder.append(">");
return builder.toString();
}



}
17 changes: 17 additions & 0 deletions CompilatoreAcDc/src/token/TokenType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package token;

public enum TokenType {
TYFLOAT,
TYINT,
PRINT,
ID,
INT,
FLOAT,
ASSIGN,
PLUS,
MINUS,
TIMES,
DIV,
SEMI,
EOF;
}
Loading

0 comments on commit d0f88cc

Please sign in to comment.