Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit5240ae0fff4cbff28e6a852606f1dc3096d8b8bf (patch)
Author
 YanaRG <alexkolbeim@gmail.com>
2018-10-17 21:54:55 +0300
Committer
 Martin Dobrev <martindobrev0@gmail.com>
2018-10-17 21:54:55 +0300
Files2dc1f0f4d5fb72a7ae812ee8d8db19f63949a10b
IPL-5240ae0fff4cbff28e6a852606f1dc3096d8b8bf.tar
IPL-5240ae0fff4cbff28e6a852606f1dc3096d8b8bf.tar.gz
IPL-5240ae0fff4cbff28e6a852606f1dc3096d8b8bf.zip
Parentf530ae1938494a31de7c6766021fa62525f10ef9 (diff)
Adding first HW to repo. (#7)
* Create README.md * Initial commit * Initial commit * Create README.md * Rename homework/81281/c2html.pl to homework/81281/c2html/c2html.pl * Rename homework/81281/test.c to homework/81281/c2html/test.c * Update README.md * Update README.md * Update c2html.pl * Update c2html.pl * Update c2html.pl * Update README.md * Update c2html.pl * Update test.c * Create executeProgram.sh * Update README.md
5 files changed, 478 insertions, 0 deletions
-rw-r--r--homework/81281/README.md1
-rw-r--r--homework/81281/c2html/README.md27
-rw-r--r--homework/81281/c2html/c2html.pl351
-rw-r--r--homework/81281/c2html/executeProgram.sh12
-rw-r--r--homework/81281/c2html/test.c87
diff --git a/homework/81281/README.md b/homework/81281/README.md
new file mode 100644
index 0000000..8327fbd
--- /dev/null
+++ b/homework/81281/README.md
@@ -0,0 +1 @@
+# Homework repo
diff --git a/homework/81281/c2html/README.md b/homework/81281/c2html/README.md
new file mode 100644
index 0000000..d5d6983
--- /dev/null
+++ b/homework/81281/c2html/README.md
@@ -0,0 +1,27 @@
+# Homework 1
+Create a xxx2html syntax highlighter for a language of your choice.
+
+### How to install and use SWI-Prolog
+
++ [How to install SWI-Prolog](https://wwu-pi.github.io/tutorials/lectures/lsp/010_install_swi_prolog.html)
+
++ [Quick start on how to use it](http://www.swi-prolog.org/pldoc/man?section=quickstart)
+
++ [Command line options](http://www.swi-prolog.org/pldoc/man?section=cmdline)
+
+### Added bash script for executing program after successful installation of SWI-Prolog on a Unix or Unix-like variants
+[Script](./executeProgram.sh)
+
+### If you are going to install SWI-Prolog on Windows
++ You can omit lines 4 to 16 from c2html.pl.
++ Afterward in order to execute the program you can call the main predicate and give it a valid string name of file.
+
+For example **main("test.c").**
+
+### For the curious who want to find about DCG Grammar rules :
+
++ [SWI-Prolog/DCG](http://www.swi-prolog.org/pldoc/man?section=DCG)
+
++ [Wikibooks](https://en.wikibooks.org/wiki/Prolog/Definite_Clause_Grammars)
+
++ [A tutorial](http://www.pathwayslms.com/swipltuts/dcg/)
diff --git a/homework/81281/c2html/c2html.pl b/homework/81281/c2html/c2html.pl
new file mode 100644
index 0000000..82e8840
--- /dev/null
+++ b/homework/81281/c2html/c2html.pl
@@ -0,0 +1,351 @@
+:- use_module(library(dcg/basics)).
+:- use_module(library(readutil)).
+
+:- set_prolog_flag(verbose, silent).
+
+:- initialization(main, main).
+
+main :-
+ current_prolog_flag(argv, Argv),
+ Argv = [H|_],
+ atom_string(H, File),
+ mainy(File),
+ format('You can see the result in the html file.~n'),
+ halt.
+main :-
+ halt(1).
+
+mainy(File):-
+ open(File, read, RFile),
+ read_stream_to_codes(RFile, CSource),
+ close(RFile),
+ phrase(tokens(Tokens), CSource),
+%% write(Tokens),
+ open('page.html', write, WFile),
+ write(WFile,"<!doctype>
+ <html>
+ <head>
+ <title>hello.c</title>
+ <style>
+ .keyword {
+ color: blue;
+ }
+ .number {
+ color: violet;
+ }
+ .identifier {
+ color: green;
+ }
+ .operator {
+ font-style: bold;
+ color: blue;
+ }
+ .comment {
+ color: lime;
+ }
+ .string {
+ color: orange;
+ }
+ .parenthese {
+ font-style: bold;
+ color: brown;
+ }
+ .quotation {
+ font-style: bold;
+ color: black;
+ }
+ .function {
+ color: purple;
+ }
+ </style>
+ </head>
+ <body>
+ <pre class=\"code\">"),
+ tohtmlfile(WFile,Tokens),
+ close(WFile).
+
+%% Definitions of tokens
+token([tauto, "auto" ]) --> "auto", !.
+token([tbreak, "break" ]) --> "break", !.
+token([tcase, "case" ]) --> "case", !.
+token([tchar, "char" ]) --> "char", !.
+token([tconst, "const" ]) --> "const", !.
+token([tcontinue, "continue" ]) --> "continue", !.
+token([tdefault, "default" ]) --> "default", !.
+token([tdo, "do" ]) --> "do", !.
+token([tdouble, "double" ]) --> "double", !.
+token([telse, "else" ]) --> "else", !.
+token([tenum, "enum" ]) --> "enum", !.
+token([textern, "extern" ]) --> "extern", !.
+token([tfloat, "float" ]) --> "float", !.
+token([tfor, "for" ]) --> "for", !.
+token([tgoto, "goto" ]) --> "goto", !.
+token([tif, "if" ]) --> "if", !.
+token([tint, "int" ]) --> "int", !.
+token([tlong, "long" ]) --> "long", !.
+token([tregister, "register" ]) --> "register", !.
+token([treturn, "return" ]) --> "return", !.
+token([tshort, "short" ]) --> "short", !.
+token([tsigned, "signed" ]) --> "signed", !.
+token([tsizeof, "sizeof" ]) --> "sizeof", !.
+token([tstatic, "static" ]) --> "static", !.
+token([tstruct, "struct" ]) --> "struct", !.
+token([tswitch, "switch"]) --> "switch", !.
+token([ttypedef, "switch" ]) --> "typedef", !.
+token([tunion, "union" ]) --> "union", !.
+token([tunsigned, "unsigned" ]) --> "unsigned", !.
+token([tvoid, "void" ]) --> "void", !.
+token([tvolatile, "volatile" ]) --> "volatile", !.
+token([twhile, "while" ]) --> "while", !.
+token([tinclude, "#include" ]) --> "#include", !.
+
+token([tcomment1, IA]) --> comment1(I), !,
+ {atom_chars(IA,I)}.
+
+%% Handling windows written files with "\r\n"
+token([[tcomment2, IA], [execNL, '\n']]) --> comment2(I), !,
+ {append(I1, [C1, C2], I),
+ ((char_code('\r', C1), char_code('\n', C2), atom_chars(IA,I1));
+ (\+ char_code('\r', C1), char_code('\n', C2),
+ append(I1, [C1], I2), atom_chars(IA,I2)))}.
+
+token([ttypesPrintF, IA]) --> prFT(I), !,
+ {atom_chars(IA,I)}.
+token([tstring, IA]) --> stringy(I), !,
+ {atom_chars(IA,I)}.
+token([theader, IA]) --> header(I), !,
+ {append(IO, [_], I), append([_], II, IO), atom_chars(IA,II)}.
+token([[tfunction, IA], [tleftParen, "("]]) --> func(I), !,
+ {append(IO, [_], I), IO \= [], atom_chars(IA, IO)}.
+
+token([tmultiply, "*"]) --> "*", !.
+token([tdivide, "/"]) --> "/", !.
+token([tmod, "%"]) --> "%", !.
+token([tadd, "+"]) --> "+", !.
+token([tsubtract, "-"]) --> "-", !.
+token([tless, "<"]) --> "<", !.
+token([tlessEqual, "<="]) --> "<=", !.
+token([tgreater, ">"]) --> ">", !.
+token([tgreaterEqual,">=" ]) --> ">=", !.
+token([tequal, "=="]) --> "==", !.
+token([tnotequal,"!=" ]) --> "!=", !.
+token([tnot, "!"]) --> "!", !.
+token([tassign,"="]) --> "=", !.
+token([tlogicalAnd, "&&"]) --> "&&", !.
+token([tlogicalOr, "||"]) --> "||", !.
+token([tbitwiseAnd, "&"]) --> "&", !.
+token([tbitwiseOr, "|"]) --> "|", !.
+
+token([tspace , " "]) --> "\s", !.
+token([ttab , "\t"]) --> "\t", !.
+token([execTAB , "\\t"]) --> "\\t", !.
+token([execNL, "\n"]) --> ( "\n"; "\r\n"), !.
+token([tnl, "\\n"]) --> "\\n", !.
+
+token([tleftParen,"("]) --> "(", !.
+token([trightParen, ")"]) --> ")", !.
+token([tleftBrace, "{"]) --> "{", !.
+token([trightBrace, "}"]) --> "}", !.
+token([tleftSqParen, "["]) --> "[", !.
+token([trightSqParen, "]" ]) --> "]", !.
+token([tsemicolon, ";"]) --> ";", !.
+token([ttwodots, ":"]) --> ":", !.
+token([tquestion, "?"]) --> "?", !.
+
+token([tcomma, "," ]) --> ",", !.
+token([tquot, "\'"]) --> "\'", !.
+token([tdoubleQuot, "\""]) --> "\"", !.
+token([tdot, "."]) --> ".", !.
+
+token([tidentifier, IA]) --> identifier(I), !,
+ {atom_chars(IA,I)}.
+token([tnumber, I]) --> number(I), !.
+
+token([tunknown, IA]) --> allThatIsNotAToken(I), !,
+ {I \= [], atom_chars(IA, I)}.
+
+
+%% Main predicate
+tokens([Token | Tail]) --> token(Token), !, tokens(Tail).
+tokens([]) --> !, [], !.
+
+
+%% Helper predicates
+identifier([C|Cs]) --> [C], {char_type(C, csymf)}, !,
+ identifierHelper(Cs).
+
+identifierHelper([C|Cs]) --> [C], {char_type(C, csym)}, !,
+ identifierHelper(Cs).
+identifierHelper([C]) --> [C], {char_type(C, csym), \+ char_code('_', C)}, !.
+identifierHelper([]) --> [], !.
+
+func([C|Cs]) --> [C], {char_type(C, csymf)}, !,
+ funkHelper(Cs).
+
+funkHelper([C|Cs]) --> [C], {char_type(C, csym)}, !,
+ funkHelper(Cs).
+funkHelper([C]) --> [C], {char_code("(", C)}, !.
+
+stringy([C|Cs]) --> [C], {char_code('\"', C)}, !,
+ stringyHelper(Cs).
+
+stringyHelper([C|Cs]) --> [C], {char_type(C, csymf); char_code('.', C)},
+ stringyHelper(Cs).
+stringyHelper([C]) --> [C], {char_code('\"', C)}.
+
+header([C|Cs]) --> [C], {char_code('<', C)}, !,
+ hederHelper(Cs).
+
+hederHelper([C|Cs]) --> [C],
+ {char_type(C, csymf); char_code('.', C); char_code('/', C); char_code('+', C)},
+ hederHelper(Cs).
+hederHelper([C]) --> [C], {char_code('>', C)}.
+
+prFT([C|Cs]) --> [C], {char_code('%', C)}, !,
+ pr(Cs).
+
+pr([C]) --> [C], {char_type(C, csym)}.
+
+comment1([C1,C2|Cs]) --> [C1,C2], {char_code('/', C1), char_code('*', C2)}, !,
+ anything(Cs),
+ {append(_,[C3,C4],Cs), char_code('*', C3), char_code('/', C4)}.
+
+comment2([C1,C2|Cs]) --> [C1,C2], {char_code('/', C1), char_code('/', C2)}, !,
+ anytingButNL(Cs).
+
+anything([C|Cs]) --> [C], anything(Cs).
+anything([]) --> [].
+
+allThatIsNotAToken([C|Cs]) --> [C|Cs], \+ tokens(C), \+ tokens([C|Cs]),
+ allThatIsNotAToken(Cs).
+allThatIsNotAToken([]) --> [].
+
+anytingButNL([C|Cs]) --> [C],
+ {\+ char_code('\r', C), \+ char_code('\n', C)}, anytingButNL(Cs).
+anytingButNL([C1,C2|_]) --> [C1,C2],
+ {(char_code('\r', C1), char_code('\n', C2));
+ (\+ char_code('\r', C1), char_code('\n', C2))}.
+
+
+%% Definitions of lists of different types of tokens
+is_keyWord([H,_]):- member(H, [tauto, tbreak, tcase, tchar, tconst, tcontinue,
+ tdefault, tdo, tdouble, telse, tenum, textern, tfloat, tfor, tgoto, tif,
+ tint, tlong, tregister, treturn, tshort, tsigned, tsizeof, tstatic, tstruct,
+ tswitch, ttypedef, tunion, tunsigned, twhile, tvoid, tvolatile, tinclude]).
+
+is_operator([H,_]):- member(H, [tmultiply, tdivide, tmod, tadd, tsubtract, tless,
+ tlessEqual, tgreater, tgreaterEqual, tequal, tnotequal, tnot, tassign, tlogicalOr,
+ tlogicalAnd, tbitwiseOr, tbitwiseAnd]).
+
+is_parenth([H,_]):- member(H, [tleftParen, trightParen, tleftBrace, trightBrace,
+ tleftSqParen, trightSqParen, tsemicolon, ttwodots]).
+
+is_quotation([H,_]):- member(H, [tquot, tdoubleQuot, tcomma, tdot, tquestion]).
+
+is_unknown([H,_]):- member(H, [tunknown]).
+
+is_number([H,_]):- member(H, [tnumber, ttypesPrintF]).
+
+is_function([[H,_],_]):- member(H, [tfunction]).
+
+is_identifier([H,_]):- member(H, [tidentifier, tnl, ttab]).
+
+is_blank([H,_]):- member(H, [execTAB, tspace, execNL]).
+
+is_comment1([H,_]):-member(H, [tcomment1]).
+
+is_comment2([[H,_],_]):-member(H, [tcomment2]).
+
+is_string([H,_]):- member(H, [tstring]).
+
+is_header([H,_]):- member(H, [theader]).
+
+
+%% Writing to html file the stream of tokens
+tohtmlfile(Stream, []):- !, write(Stream, '</pre></body></html>'), !.
+
+tohtmlfile(Stream, [H|T]) :- is_identifier(H), H = [_,Lexem],
+ write(Stream, '<span class=\"identifier\">'),
+ write(Stream, Lexem),
+ write(Stream,'</span>'),
+ !,
+ tohtmlfile(Stream, T).
+
+tohtmlfile(Stream, [H|T]) :- is_string(H), H = [_,Lexem],
+ write(Stream, '<span class=\"string\">'),
+ write(Stream, Lexem),
+ write(Stream,'</span>'),
+ !,
+ tohtmlfile(Stream, T).
+
+tohtmlfile(Stream, [H|T]) :- is_function(H), H = [[_, Lexem1],[_,Lexem2]],
+ write(Stream, '<span class=\"function\">'),
+ write(Stream, Lexem1),
+ write(Stream,'</span>'),
+ write(Stream, '<span class=\"parenthese\">'),
+ write(Stream, Lexem2),
+ write(Stream,'</span>'),
+ !,
+ tohtmlfile(Stream, T).
+
+tohtmlfile(Stream, [H|T]) :- is_header(H), H = [_,Lexem],
+ write(Stream, '<span class=\"string\">&lt;'),
+ write(Stream, Lexem),
+ write(Stream,'&gt;</span>'),
+ !,
+ tohtmlfile(Stream, T).
+
+tohtmlfile(Stream, [H|T]) :- is_keyWord(H), H = [_,Lexem],
+ write(Stream, '<span class=\"keyword\">'),
+ write(Stream, Lexem),
+ write(Stream,'</span>'),
+ !,
+ tohtmlfile(Stream, T).
+
+tohtmlfile(Stream, [H|T]) :- is_number(H), H = [_,Lexem],
+ write(Stream, '<span class=\"number\">'),
+ write(Stream, Lexem),
+ write(Stream,'</span>'),
+ !,
+ tohtmlfile(Stream, T).
+
+tohtmlfile(Stream, [H|T]) :- is_operator(H), H = [_,Lexem],
+ write(Stream, '<span class=\"operator\">'),
+ write(Stream, Lexem),
+ write(Stream,'</span>'),
+ !,
+ tohtmlfile(Stream, T).
+
+tohtmlfile(Stream, [H|T]) :- is_parenth(H), H = [_,Lexem],
+ write(Stream, '<span class=\"parenthese\">'),
+ write(Stream, Lexem),
+ write(Stream,'</span>'),
+ !,
+ tohtmlfile(Stream, T).
+
+tohtmlfile(Stream, [H|T]) :- (is_quotation(H); is_unknown(H)), H = [_,Lexem],
+ write(Stream, '<span class=\"quotation\">'),
+ write(Stream, Lexem),
+ write(Stream,'</span>'),
+ !,
+ tohtmlfile(Stream, T).
+
+tohtmlfile(Stream, [H|T]) :- is_comment1(H), H = [_,Lexem],
+ write(Stream, '<span class=\"comment\">'),
+ write(Stream, Lexem),
+ write(Stream,'</span>'),
+ !,
+ tohtmlfile(Stream, T).
+
+tohtmlfile(Stream, [H|T]) :- is_comment2(H), H = [[_, Lexem1],[_,Lexem2]],
+ write(Stream, '<span class=\"comment\">'),
+ write(Stream, Lexem1),
+ write(Stream,'</span>'),
+ write(Stream, Lexem2),
+ !,
+ tohtmlfile(Stream, T).
+
+tohtmlfile(Stream, [H|T]) :- is_blank(H), H = [_,Lexem],
+ write(Stream, Lexem),
+ !,
+ tohtmlfile(Stream, T).
diff --git a/homework/81281/c2html/executeProgram.sh b/homework/81281/c2html/executeProgram.sh
new file mode 100644
index 0000000..bc02714
--- /dev/null
+++ b/homework/81281/c2html/executeProgram.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+echo -n "Please enter the name of the C file you wish to highlight:"
+read fileName
+echo $fileName
+if ! [[ $fileName == *.c\n ]]; then
+ base=`pwd`"/c2html.pl"
+ PL=swipl
+ exec $PL -q -f "$base" "$fileName"
+else
+ echo "Not a correct C filename!"
+fi
diff --git a/homework/81281/c2html/test.c b/homework/81281/c2html/test.c
new file mode 100644
index 0000000..7176890
--- /dev/null
+++ b/homework/81281/c2html/test.c
@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+int N = 0;
+int a[750001] = {0};
+/*Problem Secrect DAA*/
+//// mauuu
+
+void swap(int* i, int* j)
+{
+ int t = *i;
+ *i = *j;
+ *j = t;
+}
+
+int partition(int l, int r)
+{
+ int randIdx = l + rand() % (r - l);
+
+ int pivot = a[randIdx];
+
+ swap(&a[l], &a[randIdx]);
+ int ind = l;
+
+ for (int i = l + 1; i < r; ++i)
+ {
+ if (a[i] <= pivot)
+ {
+ ind++;
+ swap(&a[i], &a[ind]);
+ }
+ }
+
+ swap(&a[l], &a[ind]);
+ return ind;
+}
+
+void quickSort(int l, int r)
+{
+ if (l < r)
+ {
+ int idx = partition(l, r);
+ quickSort(l, idx);
+ quickSort(idx + 1, r);
+ }
+}
+
+int binarySearch(int k)
+{
+ int middle, left = 0, right = N - 1;
+ if (k < a[left])
+ return (-1);
+ if (k == a[left])
+ return (left + 1);
+ if (k > a[right])
+ return (-1);
+
+ while (right - left > 1)
+ {
+ middle = (left + right) / 2;
+ if (k <= a[middle])
+ right = middle;
+ else
+ left = middle;
+ }
+ return ((right <= N - 1 && a[right] == k) ? (right + 1) : (-1));
+}
+
+int main()
+{
+ srand(time(0));
+ scanf("%d", &N);
+ for (int i = 0; i < N; ++i)
+ {
+ scanf("%d ", &a[i]);
+ }
+ quickSort(0, N);
+
+ int tmp = 0;
+ scanf("%d", &tmp);
+ while (tmp != 0)
+ {
+ printf("%d\n", binarySearch(tmp));
+ scanf("%d", &tmp);
+ }
+ return 0;
+}