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

Lexer novo, ainda em versão script.

parent 7eec21ff
Branches
No related tags found
No related merge requests found
import io.{Source,BufferedSource}
#!/usr/bin/scala
!#
println("started...")
case class Token(val value:String, val func:String)
case class Atomo(val valor:String, val token:String)
val palavrasReservadas = List("begin", "end", "function", "procedure", "var", "program")
object Lexer {
def getTokens(stream:Iterator[Char]):List[atomo] = {
var proximo: Char = ''
var acc = ""
// Conjuntos
val num = (c:Char) => ('0' to '9').contains(c)
val op = (c:Char) => "+-*/=:".contains(c)
val letra = (c:Char) => ('a' to 'z').contains(c) || ('A' to 'Z').contains(c) || c=='_'
val espacoLinTab = (c:Char) => c==' ' || c=='\t' || c=='\n'
val palavraReservada = (s:String) => palavrasReservadas.contains(s)
def PROX = if (stream.hasNext) proximo = stream.next
def ADD = acc + proximo
//Acoes
val erro = (msg:String) => {println("Erro: "+msg); sys.exit(1)}
PROX
proximo match { //TODO: Pattern Matching
case ' ' || '\t' || '\n' =>
case letra =>
case numero =>
case simbolo_especial =>
}
def atomo(str:String) {
val iter = str.iterator
while (iter.hasNext) {
def take(iter:Iterator[Char]) = iter.takeWhile{(c) => ! espacoLinTab(c)}.mkString
iter.dropWhile(espacoLinTab)
val c = iter.next
var acc = ""
println(
c match {
case c if num(c) =>
acc = c + take(iter)
if ( acc.exists{(c) => !num(c)} ) erro(acc+" nao eh numero")
(acc,"numero")
case c if op(c) => (c, "op")
case c if letra(c) => (c, "letra")
acc = c + take(iter)
if ( acc.exists{(c) => !(num(c) || letra(c)) } ) erro(acc+" nao eh identificador valido")
if (palavraReservada(acc)) (acc,"reservado")
else (acc, "identificador")
case c if espacoLinTab(c) => /*pula*/
case erroCh => erro(erroCh+"")
}
)
}
}
while (true)
atomo(readLine)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment