3 лаба полуисправленная

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
class Pos private(val prog: String, val offs: Int, val line: Int, val col: Int) {
def this(prog: String) = this(prog, 0, 1, 1)
def ch = if (offs == prog.length) -1 else prog(offs)
def inc = ch match {
case '\n' => new Pos(prog, offs+1, line+1, 1)
case -1 => this
case _ => new Pos(prog, offs+1, line, col+1)
}
def inc(val step: int) = step match {
case 0 => this
case s => this.inc.inc(s - 1)
}
override def toString = "(" + line + ", " + col + ")"
}
object DomainTags extends Enumeration {
type Tag = Value
val WHITESPACE, IDENT, KEYWORD, COMMENT, END_OF_PROGRAM = Value
}
import DomainTags._
class Scanner {
def scan(start: Pos, list:List[String]): (Tag, Pos, List[String]) =
sys.error("syntax error at " + start)
}
class Token(val start: Pos, scanner: Scanner) {
val (tag, follow) = start.ch match {
case -1 => (END_OF_PROGRAM, start)
case _ => scanner.scan(start)
}
def image = start.prog.substring(start.offs, follow.offs)
def next = new Token(follow, scanner)
}
trait Whitespaces extends Scanner {
private def missWhitespace(pos: Pos): Pos = pos.ch match {
case ' ' => missWhitespace(pos.inc)
case '\t' => missWhitespace(pos.inc)
case '\n' => missWhitespace(pos.inc)
case _ => pos
}
override def scan(start: Pos, list:List[String]) = {
val follow = missWhitespace(start)
if (start != follow) (WHITESPACE, follow, list)
else super.scan(start, list)
}
}
trait Keywords extends Scanner {
private val words = list("for", "if", "m1")
private pass(start: Pos, word: List, err: List[String]): (Tag, Pos, List[String]) = (start, word) match{
case (start,Nil) => super.scan(start, err)
case (start, x :: xs) if (start.prog.startwith(x, start.offs)) => if (isSpace(start.inc(x.length()))) (KEYWORD, start.inc(x.length))
else super.scan(start, err)
case (start, x :: xs) => pass(start, xs, err)
}
override def scan(start: Pos, list: List[String]) = {
pass(start, words, list)
}
}
trait Comments extends Scanner {
private def getComments(start: Pos): Pos = {
if (start.prog.startswith("*/", start.offs)) start.inc(2)
else super.scan(start.inc)
if (start.ch == -1)
}
override def scan(start: Pos, list: List[String]) = {
if (start.prog.startswith("/*", start.offs))
(COMMENT, getComments(start))
else super.scan(start)
}
}
trait Idents extends Scanner {
private def isLetter(pos: Pos) = ((pos.ch >= 'a') && (pos.ch <= 'z')) || ((pos.ch >= 'A') && (pos.ch <= 'Z'))
private def isDigit(pos: Pos) = ((pos.ch >= '0') && (pos.ch <= '9'))
private def getIdent(Pos, int): (Tag, Pos) = {
case (s, i) if (i == 0 && isLetter(pos)) => getIdent(s.inc, 1)
case (s, i) if (i == 0 && isDigit(pos)) => getIdent(s.inc, -1)
case (s, i) if (i == -1 && isDigit(pos)) => (IDENT, s)
case (s, i) if (i == 1 && isDigit(pos)) => getIdent(s.inc, -1)
case (s, i) if (i == 1 && isLetter(pos)) => (IDENT, s)
case (s, i) if (i == -1 && isLetter(pos)) => getIdent(s.inc, -1)
}
override def scan(start: Pos) = {
getIdent(start, 0)
}
}
var t = new Token(
new Pos("1234\n alpha"),
new Scanner with Numbers with Whitespaces
)
while (t.tag != END_OF_PROGRAM) {
println(t.tag.toString + " " + t.start + "-" + t.follow + ": " + t.image)
t = t.next
}