Skip to content
Snippets Groups Projects
Commit a1a9aeb7 authored by Lior Spach's avatar Lior Spach
Browse files

Reconhece ; e retorna lista de tokens

parent 8feaf533
No related branches found
No related tags found
No related merge requests found
......@@ -7,12 +7,14 @@ case class Token(val value:String, val func:String)
val palavrasReservadas = List("begin", "end", "function", "procedure", "var", "program")
val letras = List.concat('a' to 'z','A' to 'Z',"_")
val numeros = ('0' to '9').toList
val separadores = List(' ','\t','\n',';')
//Funcoes booleanas
val num = (c:Char) => numeros.contains(c)
val op = (c:Char) => "+-*/=:".contains(c)
val letra = (c:Char) => letras.contains(c)
val espacoLinTab = (c:Char) => c==' ' || c=='\t' || c=='\n'
val separador = (c:Char) => separadores.contains(c)
val palavraReservada = (s:String) => palavrasReservadas.contains(s)
//Verificadores
......@@ -25,42 +27,47 @@ val erro = (msg:String) => {println("Erro: "+msg); sys.exit(1)}
def take(iter:BufferedIterator[Char]) = {
var acc=""
while (iter.hasNext && ! espacoLinTab(iter.head) && ! op(iter.head))
while (iter.hasNext && ! separador(iter.head) && ! op(iter.head))
acc += iter.next
acc
}
def atomo(str:String) {
def atomo(str:String):List[Token] = {
var tokenList = List[Token]()
val iter = str.iterator.buffered
while (iter.hasNext) {
val c = iter.next
println( c match {
tokenList = (c match {
case c if num(c) => //NUMERO
val acc = c + take(iter)
verificaNumero(acc)
(acc,"numero")
Token(acc,"numero")
case c if op(c) => //OPERADOR
if ((c==':') && (iter.head == '='))
(c+""+iter.next, "op")
Token(c+""+iter.next, "op")
else
(c, "op")
Token(c+"", "op")
case c if letra(c) => //IDENTIFICADOR
val acc = c + take(iter)
verificaIdentificador(acc)
if (palavraReservada(acc))
(acc,"reservado")
Token(acc,"reservado")
else
(acc, "identificador")
Token(acc, "identificador")
case c if espacoLinTab(c) => /*pula*/
iter.dropWhile(espacoLinTab)
""
null;// continue
case c if c == ';' =>
Token(";", "pontoevirgula")
case erroCh => erro(erroCh+"")//TODO:which line error occurred?
})
}) :: tokenList
}
tokenList = tokenList.filterNot(_==null).reverse
println(tokenList)
tokenList
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment