From ad4eba0796ef68e7c6df8cb41946e57b8d64e396 Mon Sep 17 00:00:00 2001 From: "aljazbrodar." Date: Sun, 28 Dec 2025 14:52:19 +0100 Subject: [PATCH] regexp --- ass3/zbirnik/parsing/input.asm | 42 ++++++++++++++++++++ ass3/zbirnik/parsing/pregledovalnik.go | 55 ++++++++++++++++++++++++++ ass3/zbirnik/parsing/test.go | 17 ++++++++ 3 files changed, 114 insertions(+) create mode 100644 ass3/zbirnik/parsing/input.asm create mode 100644 ass3/zbirnik/parsing/pregledovalnik.go create mode 100644 ass3/zbirnik/parsing/test.go diff --git a/ass3/zbirnik/parsing/input.asm b/ass3/zbirnik/parsing/input.asm new file mode 100644 index 0000000..17eb80f --- /dev/null +++ b/ass3/zbirnik/parsing/input.asm @@ -0,0 +1,42 @@ +arithr START 0 .komentar1 + LDS #11 .komentar2 + LDT #5 + .komentar3 + LDA ZERO + ADDR S, A + ADDR T, A + STA sum + + .komentar4 + LDA ZERO + ADDR S, A + SUBR T, A + STA diff + + LDA ZERO + ADDR S, A + MULR T, A + STA prod + + LDA ZERO + ADDR S, A + DIVR T, A + STA quot + + LDA ZERO + ADDR S, A +LOOP SUBR T, A .komentar5 + COMPR A, T + JGT LOOP + JEQ LOOP + STA mod +HALT J HALT + END arithr + +ZERO WORD 0 +sum RESW 1 +diff RESW 1 +prod RESW 1 +quot RESW 1 .komentar6 +mod RESW 1 +.komentar7 diff --git a/ass3/zbirnik/parsing/pregledovalnik.go b/ass3/zbirnik/parsing/pregledovalnik.go new file mode 100644 index 0000000..384936e --- /dev/null +++ b/ass3/zbirnik/parsing/pregledovalnik.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "os" + "regexp" +) + +/* +type ukaz struct { + opcode int + st_operandov int + format int +} + +var ukazna_tabela = map[string]ukaz{ + "LDA": +}*/ + +func main() { + inputbyte, err := os.ReadFile("input.asm") + if err != nil { + fmt.Println("Error reading input file: ", err) + return + } + input := string(inputbyte) + pattern := regexp.MustCompile(`.*\n`) + matches := pattern.FindAllString(input, -1) + //var cleared_matches []string + for _, el := range matches { + match_pure_comment, err := regexp.MatchString(`(?m)^[\t ]*\..*$\n`, el) + if err != nil { + fmt.Println("Error while matching string.") + } + if match_pure_comment { + continue + } + match_empty, err := regexp.MatchString(`(?m)^[\t ]*$\n`, el) + if err != nil { + fmt.Println("Error while matching string.") + } + if match_empty { + continue + } + match_command, err := regexp.MatchString(``, el) + fmt.Print(el) + } + //Zbirnik mora opraviti naslednje naloge: + //1) Pregledati in razčleniti zbirniški stavek (pregledovalnik) + //2) Mnemonike nadomestiti z operacijskimi kodami + //3) Simbolična imena nadomestiti z ustreznimi številskimi ekvivalenti + //4) Razrešiti simbolične operande + //... + // ::== [] { } [] +} diff --git a/ass3/zbirnik/parsing/test.go b/ass3/zbirnik/parsing/test.go new file mode 100644 index 0000000..7d7ed38 --- /dev/null +++ b/ass3/zbirnik/parsing/test.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "regexp" +) + +func main() { + pattern := regexp.MustCompile(`o\w*g`) + fmt.Println(pattern.FindString("oogenesis")) // returns "" + fmt.Println(pattern.FindString("ck")) // returns "ook" + fmt.Println(pattern.FindString("oocyst")) // returns "" + fmt.Println(pattern.FindString("book")) // returns "ook"} + match, what := regexp.MatchString("p([a-z]+)ch", "peach") + fmt.Println(match) + fmt.Println(what) +}