From 7e8bcc75b6ae00248ad4f9ff9753be251d88a4ff Mon Sep 17 00:00:00 2001 From: dtagarev <92181714+dtagarev@users.noreply.github.com> Date: Thu, 3 Nov 2022 20:51:40 +0200 Subject: solution to first homework (#132) * Delete lex.yy.c * adding homework1 * Delete output * correction to the location of the folders Co-authored-by: dtrendafilov --- homework/9MI0800164/Homework1_9MI0800164/lua_lex.l | 77 ++++++++++++++++++++++ .../Homework1_9MI0800164/markov_chain.lua | 59 +++++++++++++++++ .../9MI0800164/Homework1_9MI0800164/result.html | 61 +++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 homework/9MI0800164/Homework1_9MI0800164/lua_lex.l create mode 100644 homework/9MI0800164/Homework1_9MI0800164/markov_chain.lua create mode 100644 homework/9MI0800164/Homework1_9MI0800164/result.html diff --git a/homework/9MI0800164/Homework1_9MI0800164/lua_lex.l b/homework/9MI0800164/Homework1_9MI0800164/lua_lex.l new file mode 100644 index 0000000..e51f53c --- /dev/null +++ b/homework/9MI0800164/Homework1_9MI0800164/lua_lex.l @@ -0,0 +1,77 @@ +%{ + +%} + +DIGIT [0-9] +ID [a-zA-Z_0-9]* +KEYWORD and|break|do|else|elseif|end|false|for|function|goto|if|in|local|nil|not|or|repeat|return|then|true|until|while +OPERATOR "+"|"-"|"*"|"/"|"%"|"^"|"#"|"=="|"~="|"<="|">="|"<"|">"|"="|"("|")"|"{"|"}"|"["|"]"|"::"|";"|":"|","|"."|".."|"..."|"<<"|">>"|"&"|"|"|"//"|"~"|"'"|"--"|"?"|"\""|"\\n" + + +%% + +{DIGIT}+ { + printf("%s", yytext); + } +{KEYWORD} printf("%s", yytext); + +(".") printf("%s", yytext); + +{ID} printf("%s", yytext); + +(--.) printf("%s", yytext); + +{OPERATOR} printf("%s", yytext); + +[ \t\n]+ printf("%s", yytext); + +. printf( "Unrecognized character: %s\n", yytext ); + +%% +int yywrap() +{ + return 1; +} +int main(int argc, const char* argv[]) +{ + if ( argc > 0 ) + yyin = fopen( "markov_chain.lua", "r" ); + else + yyin = stdin; + puts( + "" + "" + "" + " markov_chain.lua" + " " + "" + "" + "
"
+        );
+    yylex();
+    puts("
"); + return 0; +} diff --git a/homework/9MI0800164/Homework1_9MI0800164/markov_chain.lua b/homework/9MI0800164/Homework1_9MI0800164/markov_chain.lua new file mode 100644 index 0000000..2750c97 --- /dev/null +++ b/homework/9MI0800164/Homework1_9MI0800164/markov_chain.lua @@ -0,0 +1,59 @@ +-- Markov Chain Program in Lua + +function allwords () + local line = io.read() -- current line + local pos = 1 -- current position in the line + return function () -- iterator function + while line do -- repeat while there are lines + local s, e = string.find(line, "%w+", pos) + if s then -- found a word? + pos = e + 1 -- update next position + return string.sub(line, s, e) -- return the word + else + line = io.read() -- word not found; try next line + pos = 1 -- restart from first position + end + end + return nil -- no more lines: end of traversal + end +end + +function prefix (w1, w2) + return w1 .. ' ' .. w2 +end + +local statetab + +function insert (index, value) + if not statetab[index] then + statetab[index] = {n=0} + end + table.insert(statetab[index], value) +end + +local N = 2 +local MAXGEN = 10000 +local NOWORD = "\n" + +-- build table +statetab = {} +local w1, w2 = NOWORD, NOWORD +for w in allwords() do + insert(prefix(w1, w2), w) + w1 = w2; w2 = w; +end +insert(prefix(w1, w2), NOWORD) + + +-- generate text +w1 = NOWORD; w2 = NOWORD -- reinitialize +for i=1,MAXGEN do + local list = statetab[prefix(w1, w2)] + -- choose a random item from list + local r = math.random(table.getn(list)) + local nextword = list[r] + if nextword == NOWORD then return end + io.write(nextword, " ") + w1 = w2; w2 = nextword +end + diff --git a/homework/9MI0800164/Homework1_9MI0800164/result.html b/homework/9MI0800164/Homework1_9MI0800164/result.html new file mode 100644 index 0000000..f84c673 --- /dev/null +++ b/homework/9MI0800164/Homework1_9MI0800164/result.html @@ -0,0 +1,61 @@ + markov_chain.lua
+-- Markov Chain Program in Lua
+
+function allwords ()
+  local line = io.read()    -- current line
+  local pos = 1             -- current position in the line
+  return function ()        -- iterator function
+    while line do           -- repeat while there are lines
+      local s, e = string.find(line, "%w+", pos)
+      if s then      -- found a word?
+        pos = e + 1  -- update next position
+        return string.sub(line, s, e)   -- return the word
+      else
+        line = io.read()    -- word not found; try next line
+        pos = 1             -- restart from first position
+      end
+    end
+    return nil            -- no more lines: end of traversal
+  end
+end
+
+function prefix (w1, w2)
+  return w1 .. ' ' .. w2
+end
+
+local statetab
+
+function insert (index, value)
+  if not statetab[index] then
+    statetab[index] = {n=0}
+  end
+  table.insert(statetab[index], value)
+end
+
+local N  = 2
+local MAXGEN = 10000
+local NOWORD = "\n"
+
+-- build table
+statetab = {}
+local w1, w2 = NOWORD, NOWORD
+for w in allwords() do
+  insert(prefix(w1, w2), w)
+  w1 = w2; w2 = w;
+end
+insert(prefix(w1, w2), NOWORD)
+
+
+-- generate text
+w1 = NOWORD; w2 = NOWORD     -- reinitialize
+for i=1,MAXGEN do
+  local list = statetab[prefix(w1, w2)]
+  -- choose a random item from list
+  local r = math.random(table.getn(list))
+  local nextword = list[r]
+  if nextword == NOWORD then return end
+  io.write(nextword, " ")
+  w1 = w2; w2 = nextword
+end
+
+
-- cgit v1.3.1-23-gd82d