From dfc1f89bfa4161fac1c022ba758956640f730763 Mon Sep 17 00:00:00 2001 From: Lior Spach <ls12@inf.ufpr.br> Date: Mon, 27 Apr 2015 15:13:59 -0300 Subject: [PATCH] =?UTF-8?q?Lexer=20novo,=20ainda=20em=20vers=C3=A3o=20scri?= =?UTF-8?q?pt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compiler/Lexer.scala | 58 +++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/compiler/Lexer.scala b/compiler/Lexer.scala index 6590f57..d36d6e0 100644 --- a/compiler/Lexer.scala +++ b/compiler/Lexer.scala @@ -1,23 +1,47 @@ -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") + +// 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) + +//Acoes +val erro = (msg:String) => {println("Erro: "+msg); sys.exit(1)} -object Lexer { - def getTokens(stream:Iterator[Char]):List[atomo] = { - var proximo: Char = '' - var acc = "" - - def PROX = if (stream.hasNext) proximo = stream.next - def ADD = acc + proximo - 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 -- GitLab