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
No related branches found
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")
// 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 def atomo(str:String) {
proximo match { //TODO: Pattern Matching val iter = str.iterator
case ' ' || '\t' || '\n' => while (iter.hasNext) {
case letra => def take(iter:Iterator[Char]) = iter.takeWhile{(c) => ! espacoLinTab(c)}.mkString
case numero => iter.dropWhile(espacoLinTab)
case simbolo_especial => 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.
Finish editing this message first!
Please register or to comment