diff options
| Commit | b98a9799f15c8ad0275175a1d8d013c4f6e0176f (patch) | |
|---|---|---|
| Author | 2018-11-28 12:15:14 +0200 | |
| Committer | 2018-11-28 12:15:14 +0200 | |
| Files | d60c012699647adbcd20065703b0e58abb236fed | |
| IPL-b98a9799f15c8ad0275175a1d8d013c4f6e0176f.tar IPL-b98a9799f15c8ad0275175a1d8d013c4f6e0176f.tar.gz IPL-b98a9799f15c8ad0275175a1d8d013c4f6e0176f.zip | ||
| Parent | 3e5cf5941db07bcd3527a73b62077d5277d57f2d (diff) | |
Python parser and collapsing blocks + brainfuck interpreter (#46)
* Initial commit
* Fix some typos
modified: README
* Delete index.html
* Simple parser
* add grammar for Python
* Delete lex.backup
* Delete 2
* Delete a
* Delete pretty_print.cpp
* Delete try1.lex
* Add colapser
* Delete 2
* Delete a
* Delete lex.backup
* Delete try1.lex
* Delete pretty_print.cpp
* Delete pretty_print.c
* Add colapser
* Fix grammar test _more cases
* add README
* Delete parser.tab.c
* Delete parser.tab.h
* Delete lex.yy.c
* Delete parser
* Update README
* delete parser.output
parser.output was generated project file
* Delete output.file
* Delete output.html
* Delete output.html
* Delete parser.output
* Add brainfuck interpreter
* Add README and Makefile
17 files changed, 2245 insertions, 0 deletions
| -rw-r--r-- | homework/81350/brainfuck_interpreter/Makefile | 7 | ||||
| -rw-r--r-- | homework/81350/brainfuck_interpreter/README | 9 | ||||
| -rw-r--r-- | homework/81350/brainfuck_interpreter/input.file | 1 | ||||
| -rw-r--r-- | homework/81350/brainfuck_interpreter/interpreter.cpp | 77 | ||||
| -rw-r--r-- | homework/81350/parser/Makefile | 13 | ||||
| -rw-r--r-- | homework/81350/parser/colapser/Makefile | 13 | ||||
| -rw-r--r-- | homework/81350/parser/colapser/README | 13 | ||||
| -rw-r--r-- | homework/81350/parser/colapser/input.file | 13 | ||||
| -rw-r--r-- | homework/81350/parser/colapser/lexer.lex | 236 | ||||
| -rw-r--r-- | homework/81350/parser/colapser/parser.y | 1107 | ||||
| -rw-r--r-- | homework/81350/parser/colapser/stack.c | 56 | ||||
| -rw-r--r-- | homework/81350/parser/colapser/stack.h | 16 | ||||
| -rw-r--r-- | homework/81350/parser/input.file | 3 | ||||
| -rw-r--r-- | homework/81350/parser/lexer.lex | 182 | ||||
| -rw-r--r-- | homework/81350/parser/parser.y | 441 | ||||
| -rw-r--r-- | homework/81350/parser/stack.c | 46 | ||||
| -rw-r--r-- | homework/81350/parser/stack.h | 12 |
diff --git a/homework/81350/brainfuck_interpreter/Makefile b/homework/81350/brainfuck_interpreter/Makefile new file mode 100644 index 0000000..5619881 --- /dev/null +++ b/homework/81350/brainfuck_interpreter/Makefile @@ -0,0 +1,7 @@ +all: brainfuck + +brainfuck: interpreter.cpp input.file + gcc interpreter.cpp -o brainfuck + +clean: + rm brainfuck diff --git a/homework/81350/brainfuck_interpreter/README b/homework/81350/brainfuck_interpreter/README new file mode 100644 index 0000000..f795c86 --- /dev/null +++ b/homework/81350/brainfuck_interpreter/README @@ -0,0 +1,9 @@ +The program in brainfuck is stored in "input.file". The program can be changed +to any valid program in brainfuck. +To use the interpreter: + +1. You need to build it: +$ make + +2. Run the interpreter: +$ ./brainfuck diff --git a/homework/81350/brainfuck_interpreter/input.file b/homework/81350/brainfuck_interpreter/input.file new file mode 100644 index 0000000..8fa0f72 --- /dev/null +++ b/homework/81350/brainfuck_interpreter/input.file @@ -0,0 +1 @@ +++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++. diff --git a/homework/81350/brainfuck_interpreter/interpreter.cpp b/homework/81350/brainfuck_interpreter/interpreter.cpp new file mode 100644 index 0000000..76fdd44 --- /dev/null +++ b/homework/81350/brainfuck_interpreter/interpreter.cpp @@ -0,0 +1,77 @@ +#include <stdio.h> +#include <vector> +char cells[1024] = {0}; +char* ptr = cells; +FILE* program; + + +void read_while_loop(){ + int position = ftell(program); + //printf("%d\n", position); + char c; + + while((c = fgetc(program)) != ']'){ + if(c == '['){ + read_while_loop(); + } + + switch (c) { + case '+': + (*ptr)++; + break; + case '-': + (*ptr)--; + break; + case '>': + ptr++; + break; + case '<': + ptr--; + break; + case '.': + putchar(*ptr); + break; + case ',': + *ptr = getchar(); + break; + } + } + + if((int)*ptr != 0){ + fseek(program, position, SEEK_SET); + read_while_loop(); + } +} + +int main(){ + program = fopen("input.file", "r"); + char c; + + while((c = fgetc(program)) != EOF ){ + + switch (c) { + case '+': + (*ptr)++; + break; + case '-': + (*ptr)--; + break; + case '>': + ptr++; + break; + case '<': + ptr--; + break; + case '.': + putchar(*ptr); + break; + case ',': + *ptr = getchar(); + break; + case '[': + read_while_loop(); + break; + } + } + // printf("%c\n", cells[0]); +} diff --git a/homework/81350/parser/Makefile b/homework/81350/parser/Makefile new file mode 100644 index 0000000..ceb9963 --- /dev/null +++ b/homework/81350/parser/Makefile @@ -0,0 +1,13 @@ +all: parser + +parser: parser.tab.c parser.tab.h stack.c stack.h lex.yy.c + gcc lex.yy.c parser.tab.c stack.c -lfl -o parser + +parser.tab.c parser.tab.h: parser.y + bison -v -d parser.y + +lex.yy.c: lexer.lex parser.tab.h stack.h + flex lexer.lex + +clean: + rm -f lex.yy.c parser.tab.h parser.tab.c parser diff --git a/homework/81350/parser/colapser/Makefile b/homework/81350/parser/colapser/Makefile new file mode 100644 index 0000000..ceb9963 --- /dev/null +++ b/homework/81350/parser/colapser/Makefile @@ -0,0 +1,13 @@ +all: parser + +parser: parser.tab.c parser.tab.h stack.c stack.h lex.yy.c + gcc lex.yy.c parser.tab.c stack.c -lfl -o parser + +parser.tab.c parser.tab.h: parser.y + bison -v -d parser.y + +lex.yy.c: lexer.lex parser.tab.h stack.h + flex lexer.lex + +clean: + rm -f lex.yy.c parser.tab.h parser.tab.c parser diff --git a/homework/81350/parser/colapser/README b/homework/81350/parser/colapser/README new file mode 100644 index 0000000..cb143ea --- /dev/null +++ b/homework/81350/parser/colapser/README @@ -0,0 +1,13 @@ +Използвала съм изгледа на човека с факултетен номер 81319, затова изглежда по същия начин. + +За да се изпробва как действа: +1. Трябва да се билдне: +$ make + +2. Да се пусне: +$ ./parser > <name of file> + +Напреимер: +$ ./parser > output.html + +3. Да се отвори html файла в браузъра. diff --git a/homework/81350/parser/colapser/input.file b/homework/81350/parser/colapser/input.file new file mode 100644 index 0000000..223392d --- /dev/null +++ b/homework/81350/parser/colapser/input.file @@ -0,0 +1,13 @@ +def func(): + num = 7 + + factorial = 1 + + if num < 0: + print("Sorry, factorial does not exist for negative numbers") + elif num == 0: + print("The factorial of 0 is 1") + else: + for i in range(1,num + 1): + factorial = factorial*i + print("The factorial of",num,"is",factorial) diff --git a/homework/81350/parser/colapser/lexer.lex b/homework/81350/parser/colapser/lexer.lex new file mode 100644 index 0000000..7174494 --- /dev/null +++ b/homework/81350/parser/colapser/lexer.lex @@ -0,0 +1,236 @@ + +%{ +#include "stack.h" +#include <stdio.h> +#include "parser.tab.h" + +int g_current_line_indent = 0; +int g_is_fake_outdent_symbol = 0; +int current_line = 0; +int is_read_indent = 0; +int is_preveous_unindent = 0; + +static const unsigned int TAB_WIDTH = 2; + +#define YY_USER_INIT { \ + push(0); \ + BEGIN(initial); \ + } + +int yycolumn = 1; +void set_yycolumn(int val) { + yycolumn = val; + yylloc.first_column = yycolumn; + yylloc.last_column = yycolumn + yyleng - 1; +} + +#define YY_USER_ACTION { \ + yylloc.first_line = yylloc.last_line = yylineno; \ + set_yycolumn(yycolumn); \ + yycolumn += yyleng; \ +} + +char* fixed_unindent(char* text){ + char* add_new_line = ""; + if(is_preveous_unindent){ + add_new_line ="<br>"; + is_preveous_unindent = 0; + } + return concatenate(add_new_line, text); +} + +%} + + /* This is a sub-parser (state) for indentation-sensitive scoping */ +%x initial +%x indent +%s normal + + /* %option 8bit reentrant bison-bridge */ +%option warn +%option nodefault +%option yylineno +%option noyywrap + + +digit [0-9] +number [1-9]{digit}* +fnumber {digit}+"."{digit}* +letter [a-zA-Z_] +stringdq \"([^\"]|(\\\"))*\" +stringsq \'([^\']|(\\\'))*\' + + /*%option debug*/ +%% + int indent_caller = normal; + + +<*>\n { set_yycolumn(0); yylineno--; REJECT; } + + +<initial>. { set_yycolumn(yycolumn-1); indent_caller = normal; yyless(0); BEGIN(indent); } +<initial>\n { indent_caller = normal; yyless(0); BEGIN(indent); } + + /* The following are the rules that keep track of indentation. */ +<indent>" " { g_current_line_indent++; } +<indent>\t { g_current_line_indent = (g_current_line_indent + TAB_WIDTH) & ~(TAB_WIDTH-1); } +<indent>\n { g_current_line_indent = 0; /* ignoring blank line */ } +<indent><<EOF>> { + if(peek() != 0) { + pop(); + + if(g_current_line_indent != peek()) { + unput('\n'); + for(size_t i = 0 ; i < peek() ; ++i) { + unput(' '); + } + } else { + BEGIN(indent_caller); + } + + return UNINDENT; + } else { + yyterminate(); + } + } + +<indent>. { + // if(!g_is_fake_outdent_symbol) { + unput(*yytext); + // } + set_yycolumn(yycolumn-1); + g_is_fake_outdent_symbol = 0; + + + if(g_current_line_indent > peek()) { + push(g_current_line_indent); + BEGIN(indent_caller); + return INDENT; + } else if(g_current_line_indent < peek()) { + pop(); + + if(g_current_line_indent != peek()) { + for(size_t i = 0 ; i < g_current_line_indent ; ++i) { + unput(' '); + } + unput('\n'); + + unput('$'); + + g_is_fake_outdent_symbol = 1; + for(size_t i = 0 ; i < peek() ; ++i) { + unput(' '); + } + unput('\n'); + } else { + + BEGIN(indent_caller); + } + is_preveous_unindent = 1; + return UNINDENT; + } else { + BEGIN(indent_caller); + } + } +<normal>" " { yylval.lexeme = " ";} +<normal>\n { g_current_line_indent = 0; indent_caller = YY_START; BEGIN(indent); yylval.lexeme = "<br>"; return NEWLINE; } + +<normal>{number}|{fnumber} { yylval.lexeme = fixed_unindent(yytext); return NUMBER; } +<normal>{digit} { yylval.lexeme = fixed_unindent(yytext); return NUMBER;} +<normal>{stringsq} { yylval.lexeme = fixed_unindent(yytext); return STRING;} +<normal>{stringdq} { yylval.lexeme = fixed_unindent(yytext); return STRING;} + +<normal>"+" { yylval.lexeme = fixed_unindent(yytext); return PLUS; } +<normal>"-" { yylval.lexeme = fixed_unindent(yytext); return MINUS; } +<normal>"*" { yylval.lexeme = fixed_unindent(yytext); return STAR; } +<normal>"/" { yylval.lexeme = fixed_unindent(yytext); return SLASH; } +<normal>"//" { yylval.lexeme = fixed_unindent(yytext); return DOUBLESLASH;} +<normal>"%" { yylval.lexeme = fixed_unindent(yytext); return PERCENT; } +<normal>"**" { yylval.lexeme = fixed_unindent(yytext); return DOUBLESTAR; } +<normal>">" { yylval.lexeme = fixed_unindent(yytext); return BIGGER; } +<normal>"<" { yylval.lexeme = fixed_unindent(yytext); return LESS; } +<normal>">=" { yylval.lexeme = fixed_unindent(yytext); return BIGGEREQL; } +<normal>"<=" { yylval.lexeme = fixed_unindent(yytext); return LESSEQL; } +<normal>"==" { yylval.lexeme = fixed_unindent(yytext); return EQL; } +<normal>"!=" { yylval.lexeme = fixed_unindent(yytext); return NOTEQL; } + + +<normal>"&" { yylval.lexeme = fixed_unindent(yytext); return BOOLAND; } +<normal>"|" { yylval.lexeme = fixed_unindent(yytext); return BOOLOR; } +<normal>"~" { yylval.lexeme = fixed_unindent(yytext); return TILDA; } +<normal>"^" { yylval.lexeme = fixed_unindent(yytext); return HAT; } +<normal>">>" { yylval.lexeme = fixed_unindent(yytext); return SHIFTRIGHT; } +<normal>"<<" { yylval.lexeme = fixed_unindent(yytext); return SHIFTLESS; } + + +<normal>"=" { yylval.lexeme = fixed_unindent(yytext); return ASSIGN; } +<normal>"+=" { yylval.lexeme = fixed_unindent(yytext); return PLUSASSIGN; } +<normal>"-=" { yylval.lexeme = fixed_unindent(yytext); return MINUSASSIGN;} +<normal>"*=" { yylval.lexeme = fixed_unindent(yytext); return STARASSIGN; } +<normal>"/=" { yylval.lexeme = fixed_unindent(yytext); return SLASHASSIGN;} +<normal>"%=" { yylval.lexeme = fixed_unindent(yytext); return PERCENTASSIGN;} +<normal>"**=" { yylval.lexeme = fixed_unindent(yytext); return DOUBLESTARASSIGN;} +<normal>"//=" { yylval.lexeme = fixed_unindent(yytext); return DOUBLESLASHASSIGN;} +<normal>"^=" { yylval.lexeme = fixed_unindent(yytext); return HATASSIGN;} +<normal>"&=" { yylval.lexeme = fixed_unindent(yytext); return BOOLANDASSIGN;} +<normal>"|=" { yylval.lexeme = fixed_unindent(yytext); return BOOLORASSIGN;} +<normal>">>=" { yylval.lexeme = fixed_unindent(yytext); return SHIFTRIGHTASSIGN;} +<normal>"<<=" { yylval.lexeme = fixed_unindent(yytext); return SHIFTLEFTASSIGN;} +<normal>"@=" { yylval.lexeme = fixed_unindent(yytext); return ATASSIGN; } + +<normal>"@" { yylval.lexeme = fixed_unindent(yytext); return AT;} +<normal>"(" { yylval.lexeme = fixed_unindent(yytext); return LPAREN; } +<normal>")" { yylval.lexeme = fixed_unindent(yytext); return RPAREN; } +<normal>"[" { yylval.lexeme = fixed_unindent(yytext); return LSQPAREN;} +<normal>"]" { yylval.lexeme = fixed_unindent(yytext); return RSQPAREN;} +<normal>"{" { yylval.lexeme = fixed_unindent(yytext); return LCURPAREN;} +<normal>"}" { yylval.lexeme = fixed_unindent(yytext); return RCURPAREN;} +<normal>";" { yylval.lexeme = fixed_unindent(yytext); return SEMICOLON; } +<normal>":" { yylval.lexeme = fixed_unindent(yytext); return COLON; } +<normal>"," { yylval.lexeme = fixed_unindent(yytext); return COMMA; } +<normal>"." { yylval.lexeme = fixed_unindent(yytext); return DOT; } +<normal>"..." { yylval.lexeme = fixed_unindent(yytext); return THREEDOTS;} + +<normal>"$" { return DOLAR; } + +<normal>"async" { yylval.lexeme = fixed_unindent(yytext); return ASYNC;} +<normal>"def" { yylval.lexeme = fixed_unindent(yytext); return DEF; } +<normal>"and" { yylval.lexeme = fixed_unindent(yytext); return AND;} +<normal>"or" { yylval.lexeme = fixed_unindent(yytext); return OR;} +<normal>"not" { yylval.lexeme = fixed_unindent(yytext); return NOT;} +<normal>"in" { yylval.lexeme = fixed_unindent(yytext); return IN;} +<normal>"not in" { yylval.lexeme = fixed_unindent(yytext); return NOTIN;} +<normal>"is" { yylval.lexeme = fixed_unindent(yytext); return IS;} +<normal>"is not" { yylval.lexeme = fixed_unindent(yytext); return ISNOT;} +<normal>"await" { yylval.lexeme = fixed_unindent(yytext); return AWAIT;} +<normal>"None" { yylval.lexeme = fixed_unindent(yytext); return NONE;} +<normal>"True" { yylval.lexeme = fixed_unindent(yytext); return TRUE;} +<normal>"False" { yylval.lexeme = fixed_unindent(yytext); return FALSE;} +<normal>"class" { yylval.lexeme = fixed_unindent(yytext); return CLASS;} +<normal>"for" { yylval.lexeme = fixed_unindent(yytext); return FOR;} +<normal>"if" { yylval.lexeme = fixed_unindent(yytext); return IF;} +<normal>"elif" { yylval.lexeme = fixed_unindent(yytext); return ELIF;} +<normal>"else" { yylval.lexeme = fixed_unindent(yytext); return ELSE;} +<normal>"except" { yylval.lexeme = fixed_unindent(yytext); return EXCEPT;} +<normal>"from" { yylval.lexeme = fixed_unindent(yytext); return FROM;} +<normal>"finally" { yylval.lexeme = fixed_unindent(yytext); return FINALLY;} +<normal>"lambda" { yylval.lexeme = fixed_unindent(yytext); return LAMBDA;} +<normal>"yield" { yylval.lexeme = fixed_unindent(yytext); return YIELD;} +<normal>"as" { yylval.lexeme = fixed_unindent(yytext); return AS;} +<normal>"assert" { yylval.lexeme = fixed_unindent(yytext); return ASSERT;} +<normal>"break" { yylval.lexeme = fixed_unindent(yytext); return BREAK;} +<normal>"continue" { yylval.lexeme = fixed_unindent(yytext); return CONTINUE;} +<normal>"del" { yylval.lexeme = fixed_unindent(yytext); return DEL;} +<normal>"global" { yylval.lexeme = fixed_unindent(yytext); return GLOBAL;} +<normal>"import" { yylval.lexeme = fixed_unindent(yytext); return IMPORT;} +<normal>"nonlocal" { yylval.lexeme = fixed_unindent(yytext); return NONLOCAL;} +<normal>"pass" { yylval.lexeme = fixed_unindent(yytext); return PASS;} +<normal>"raise" { yylval.lexeme = fixed_unindent(yytext); return RAISE;} +<normal>"return" { yylval.lexeme = fixed_unindent(yytext); return RETURN;} +<normal>"try" { yylval.lexeme = fixed_unindent(yytext); return TRY;} +<normal>"while" { yylval.lexeme = fixed_unindent(yytext); return WHILE;} +<normal>"with" { yylval.lexeme = fixed_unindent(yytext); return WITH;} + +<normal>{letter}({letter}|{digit})* { + yylval.lexeme = fixed_unindent(yytext); return NAME; } +%% diff --git a/homework/81350/parser/colapser/parser.y b/homework/81350/parser/colapser/parser.y new file mode 100644 index 0000000..030ae64 --- /dev/null +++ b/homework/81350/parser/colapser/parser.y @@ -0,0 +1,1107 @@ + +%{ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "stack.h" +int yylex(); +int yyerror(const char* error); +FILE *yyin; +#undef YYDEBUG +#define YYDEBUG 1 +/* #define YY_DECL int yylex(scanner) void* scanner; */ + + + + char* concatenate3(char* first, char* second, char* third){ + char* result1 = concatenate(first, second); + char* result2 = concatenate(result1, third); + } + + void printStyles() { + puts( + " <style>" + " .identifier {" + " color: black;" + " }" + " .keyword {" + " color: purple;" + " }" + " .number {" + " color: blue;" + " }" + " .string {" + " color: green;" + " }" + " .operator {" + " color: #db184c;" + " font-style: bold;" + " .constant {" + " color: purple;" + " }" + " .collapsible {" + " color: white;" + " cursor: pointer;" + " padding: 2px;" + " width: 2%;" + " border: none;" + " text-align: center;" + " outline: none;" + " font-size: 15px;" + " }" + " </style>" + ); + } + void printCollapsableScript() { + puts( + "<script>" + "var coll = document.getElementsByClassName(\"collapsible\");\n" + "var i;\n" + "\n" + "for (i = 0; i < coll.length; i++) {\n" + "coll[i].addEventListener(\"click\", function() {\n" + "this.classList.toggle(\"active\");\n" + "var content = this.nextElementSibling;\n" + "if (content.style.display === \"inline\") {\n" + "content.style.display = \"none\";\n" + "this.innerHTML=\"+\"" + "} else {\n" + "content.style.display = \"inline\";\n" + "this.innerHTML=\"-\"" + "}\n" + "});\n" + "}\n" + "for (i = 0; i < coll.length; i++) {\n" + "coll[i].click()\n" + "}\n" + "</script>" + ); + } + char* stylize(char* text, char* style) { + char* result = concatenate("<span class=\"", style); + result = concatenate(result, "\">"); + result = concatenate(result, text); + result = concatenate(result, " </span>"); + return result; + } + + char *str_replace(char *orig, char *rep, char *with) { + char *result; + char *ins; + char *tmp; + int len_rep; + int len_with; + int len_front; + int count; + + if (!orig || !rep) + return NULL; + len_rep = strlen(rep); + if (len_rep == 0) + return NULL; + if (!with) + with = ""; + len_with = strlen(with); + + // count the number of replacements needed + ins = orig; + for (count = 0; tmp = strstr(ins, rep); ++count) { + ins = tmp + len_rep; + } + + tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); + + if (!result) + return NULL; + + while (count--) { + ins = strstr(orig, rep); + len_front = ins - orig; + tmp = strncpy(tmp, orig, len_front) + len_front; + tmp = strcpy(tmp, with) + len_with; + orig += len_front + len_rep; // move to next "end of rep" + } + strcpy(tmp, orig); + return result; + } + + char* collapsableBlock(char* text) { + int len = strlen(text); + if(len>4 && text[len-1] == '>' && text[len-2] == 'r' && text[len-3] == 'b' && text[len-4] == '<'){ + text[len-4] = '\0'; + } + char* result = concatenate("<button class=\"collapsible\">+</button>", "<div class=\"content\">"); + char* new_text = str_replace(text, "<br>", "<br>  "); + result = concatenate(result, new_text); + result = concatenate(result, "</div>"); + return result; + } + +%} + +%start input_stmt +/* %define api.pure full */ +%define api.value.type {union name {char* lexeme;}} +%locations + +%type<lexeme> NUMBER PLUS MINUS STAR SLASH DOUBLESLASH PERCENT DOUBLESTAR BIGGER +%type<lexeme> LESS BIGGEREQL LESSEQL EQL NOTEQL +%type<lexeme> BOOLAND BOOLOR TILDA HAT SHIFTRIGHT SHIFTLESS +%type<lexeme> ASSIGN PLUSASSIGN MINUSASSIGN STARASSIGN SLASHASSIGN PERCENTASSIGN +%type<lexeme> DOUBLESTARASSIGN DOUBLESLASHASSIGN HATASSIGN BOOLANDASSIGN +%type<lexeme> BOOLORASSIGN SHIFTRIGHTASSIGN SHIFTLEFTASSIGN ATASSIGN +%type<lexeme> LPAREN RPAREN LSQPAREN RSQPAREN LCURPAREN RCURPAREN SEMICOLON +%type<lexeme> COLON COMMA DOT AT THREEDOTS NAME +%type<lexeme> DEF ASYNC AND OR NOT IN NOTIN IS ISNOT AWAIT NONE TRUE FALSE FOR +%type<lexeme> IF ELIF ELSE EXCEPT CLASS FROM FINALLY LAMBDA YIELD AS ASSERT +%type<lexeme> BREAK CONTINUE DEL GLOBAL IMPORT NONLOCAL PASS RAISE RETURN TRY WHILE WITH +%type<lexeme> INDENT UNINDENT NEWLINE STRING +%type<lexeme> input_stmt decorator decorator_more decorated obj_definition async_funcdef funcdef parameters typedargslist td_one td_one_arg td_more_arg td_one_optional td_two td_two_optional td_three tfpdef varargslist var_one var_one_arg var_more_arg var_one_optional var_two var_two_optional var_three vfpdef stmt simple_stmt simple_more_stmt small_stmt expr_stmt expr_stmt_two assign_more assign_expr aug_expr annassign testlist_star_expr testlist_se testlist_star_expr_more augassign del_stmt pass_stmt flow_stmt break_stmt continue_stmt return_stmt yield_stmt raise_stmt import_stmt import_name import_from import_from_name dots dots_more import_from_import import_as_name dotted_as_name import_as_names import_as_name_more dotted_as_names dotted_as_name_more dotted_name dotted_name_more global_stmt global_stmt_more nonlocal_stmt nonlocal_stmt_more assert_stmt compound_stmt async_stmt if_stmt elif_stmt else_stmt elif_stmt_more while_stmt for_stmt try_stmt try_stmt_two finally_stmt except_fin_stmt except_stmt_more except_stmt except_clause with_stmt with_item with_item_more suite stmt_more test test_nocond lambdadef lambdadef_nocond or_test or_test_more and_test and_test_more not_test comparison comp_more comp_op star_expr expr expr_more xor_expr xor_expr_more and_expr and_expr_more shift_expr shift_expr_more shift_op arith_expr arith_expr_more arith_op term term_more term_op factor unar_op power atom_expr atom testlist_comp testlist_comp_second testlist_more_arg trailer trailer_more subscriptlist subscipt_more subscript sliceop exprlist exprlist_more exprlist_op testlist testlist_more dictorsetmaker test_star_expr dsm_one test_star_expr_more test_star_expr_2 dsm_two test_star_expr_2_more classdef arglist arglist_more argument comp_iter sync_comp_for comp_for comp_if yield_expr yield_arg + + + +%token NUMBER +%token PLUS +%token MINUS +%token STAR +%token SLASH +%token DOUBLESLASH +%token PERCENT +%token DOUBLESTAR +%token BIGGER +%token LESS +%token BIGGEREQL +%token LESSEQL +%token EQL +%token NOTEQL + +%token BOOLAND +%token BOOLOR +%token TILDA +%token HAT +%token SHIFTRIGHT +%token SHIFTLESS + +%token ASSIGN +%token PLUSASSIGN +%token MINUSASSIGN +%token STARASSIGN +%token SLASHASSIGN +%token PERCENTASSIGN +%token DOUBLESTARASSIGN +%token DOUBLESLASHASSIGN +%token HATASSIGN +%token BOOLANDASSIGN +%token BOOLORASSIGN +%token SHIFTRIGHTASSIGN +%token SHIFTLEFTASSIGN +%token ATASSIGN + +%token LPAREN +%token RPAREN +%token LSQPAREN +%token RSQPAREN +%token LCURPAREN +%token RCURPAREN +%token SEMICOLON +%token COLON +%token COMMA +%token DOT +%token AT +%token THREEDOTS + +%token NAME + +%token DEF +%token ASYNC +%token AND +%token OR +%token NOT +%token IN +%token NOTIN +%token IS +%token ISNOT +%token AWAIT +%token NONE +%token TRUE +%token FALSE +%token FOR +%token IF +%token ELIF +%token ELSE +%token EXCEPT +%token CLASS +%token FROM +%token FINALLY +%token LAMBDA +%token YIELD +%token AS +%token ASSERT +%token BREAK +%token CONTINUE +%token DEL +%token GLOBAL +%token IMPORT +%token NONLOCAL +%token PASS +%token RAISE +%token RETURN +%token TRY +%token WHILE +%token WITH + +%token INDENT +%token UNINDENT +%token NEWLINE +%token EOFILE +%token STRING +%token DOLAR + +/* +Define both - precedence and associativity */ +%left PLUS MINUS +%left STAR SLASH + +%% + +input_stmt: NEWLINE { printf("\n");} + | simple_stmt { printf("%s", $$);} + | compound_stmt NEWLINE { printf("%s\n", $$);} + | compound_stmt { printf("%s", $$);} + + +decorator: AT dotted_name NEWLINE { + $$ = concatenate(stylize($1, "operator"), $2); + $$ = concatenate($$, "\n"); + } + | AT dotted_name LPAREN RPAREN NEWLINE { + $$ = concatenate(stylize($1, "operator"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + $$ = concatenate($$, "\n"); + } + | AT dotted_name LPAREN arglist RPAREN NEWLINE { + $$ = concatenate(stylize($1, "operator"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + $$ = concatenate($$, $5); + $$ = concatenate($$, "\n"); + } +decorator_more: decorator { $$ = $1; } + | decorator_more decorator { $$ = concatenate($1, $2);} +decorated: decorator_more obj_definition { $$ = concatenate($1, $2);} +obj_definition: classdef { $$ = $1; } + | funcdef { $$ = $1; } + | async_funcdef { $$ = $1; } + +/* gotovo do tuk */ + +async_funcdef: ASYNC funcdef { $$ = concatenate(stylize($1, "keyword"), $2);} +funcdef: DEF NAME parameters COLON suite { + $$ = concatenate(stylize($1, "keyword"), stylize($2, "identifier")); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + $$ = concatenate($$, $5); + } +parameters: LPAREN RPAREN { $$ = concatenate($1, $2);} + | LPAREN typedargslist RPAREN { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + +typedargslist: td_one { $$ = $1; } + | td_two { $$ = $1; } + | td_three { $$ = $1; } +td_one: td_one_arg td_more_arg td_one_optional { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | td_one_arg td_one_optional { $$ = concatenate($1, $2);} +td_one_arg: tfpdef ASSIGN test { + $$ = concatenate($1, stylize($2, "operator")); + $$ = concatenate($$, $3); + } + | tfpdef { $$ = $1; } +td_more_arg: COMMA td_one_arg { $$ = concatenate($1, $2);} + | td_more_arg COMMA td_one_arg { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +td_one_optional: COMMA td_two { $$ = concatenate($1, $2);} + | COMMA td_three { $$ = concatenate($1, $2);} + | COMMA { $$ = $1; } + | { $$ = ""; } +td_two: STAR tfpdef td_more_arg td_two_optional { + $$ = concatenate(stylize($1, "operator"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } + | STAR td_more_arg td_two_optional { + $$ = concatenate(stylize($1, "operator"), $2); + $$ = concatenate($$, $3); + } +td_two_optional: COMMA td_three { $$ = concatenate($1, $2);} + | COMMA { $$ = $1; } +td_three: DOUBLESTAR tfpdef COMMA { + $$ = concatenate(stylize($1, "operator"), $2); + $$ = concatenate($$, $3); + } + | DOUBLESTAR tfpdef { $$ = concatenate(stylize($1, "operator"), $2);} +tfpdef: NAME { $$ = $1; } + | NAME COLON test { + $$ = concatenate(stylize($1, "identifier"), $2); + $$ = concatenate($$, $3); + } + +varargslist: var_one { $$ = $1; } + | var_two { $$ = $1; } + | var_three { $$ = $1; } +var_one: var_one_arg var_more_arg var_one_optional { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | var_one_arg var_one_optional { $$ = concatenate($1, $2);} +var_one_arg: vfpdef ASSIGN test { + $$ = concatenate($1, stylize($2, "operator")); + $$ = concatenate($$, $3); + } + | vfpdef { $$ = $1; } +var_more_arg: COMMA var_one_arg { $$ = concatenate($1, $2);} + | var_more_arg COMMA var_one_arg { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +var_one_optional: COMMA var_two { $$ = concatenate($1, $2);} + | COMMA var_three { $$ = concatenate($1, $2);} + | COMMA { $$ = $1; } +var_two: STAR vfpdef var_more_arg var_two_optional { + $$ = concatenate(stylize($1, "operator"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } + | STAR var_more_arg var_two_optional { + $$ = concatenate(stylize($1, "operator"), $2); + $$ = concatenate($$, $3); + } +var_two_optional: COMMA var_three { $$ = concatenate($1, $2);} + | COMMA { $$ = $1; } +var_three: DOUBLESTAR vfpdef COMMA { + $$ = concatenate(stylize($1, "operator"), $2); + $$ = concatenate($$, $3); + } + | DOUBLESTAR vfpdef { $$ = concatenate(stylize($1, "operator"), $2);} +vfpdef: NAME { $$ = stylize($1, "identifier"); } + +/* do tuk e gotovo */ + +stmt: simple_stmt { $$ = $1; } + | compound_stmt { $$ = $1; } +simple_stmt: small_stmt simple_more_stmt SEMICOLON NEWLINE { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, "<br>"); + } + | small_stmt simple_more_stmt NEWLINE { + $$ = concatenate($1, $2); + $$ = concatenate($$, "<br>"); + } + | small_stmt NEWLINE { $$ = concatenate($1, "<br>");} + | small_stmt SEMICOLON NEWLINE { + $$ = concatenate($1, $2); + $$ = concatenate($$, "<br>"); + } +simple_more_stmt: SEMICOLON small_stmt { $$ = concatenate($1, $2);} + | simple_more_stmt SEMICOLON small_stmt { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +small_stmt: expr_stmt { $$ = $1; } + | del_stmt { $$ = $1; } + | pass_stmt { $$ = $1; } + | flow_stmt { $$ = $1; } + | import_stmt { $$ = $1; } + | global_stmt { $$ = $1; } + | nonlocal_stmt { $$ = $1; } + | assert_stmt { $$ = $1; } +expr_stmt: testlist_star_expr expr_stmt_two { $$ = concatenate($1, $2);} + | testlist_star_expr { $$ = $1; } +expr_stmt_two: annassign { $$ = $1; } + | augassign aug_expr { $$ = concatenate($1, $2);} + | assign_more { $$ = $1; } +assign_more: ASSIGN assign_expr { $$ = concatenate(stylize($1, "operator"), $2);} + | assign_more ASSIGN assign_expr { + $$ = concatenate($1, stylize($2, "operator")); + $$ = concatenate($$, $3); + } +assign_expr: yield_expr { $$ = $1; } + | testlist_star_expr { $$ = $1; } +aug_expr: yield_expr { $$ = $1; } + | testlist { $$ = $1; } +annassign: COLON test { $$ = concatenate($1, $2);} + | COLON test ASSIGN test { + $$ = concatenate($1, $2); + $$ = concatenate($$, stylize($3, "operator")); + $$ = concatenate($$, $4); + } +testlist_star_expr: testlist_se testlist_star_expr_more COMMA { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | testlist_se testlist_star_expr_more { $$ = concatenate($1, $2);} + | testlist_se COMMA { $$ = concatenate($1, $2);} + | testlist_se { $$ = $1; } +testlist_se: test { $$ = $1; } + | star_expr { $$ = $1; } +testlist_star_expr_more: COMMA testlist_se { $$ = concatenate($1, $2);} + | testlist_star_expr_more COMMA testlist_se { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +augassign: PLUSASSIGN { $$ = stylize($1, "operator");} + | MINUSASSIGN { $$ = stylize($1, "operator");} + | STARASSIGN { $$ = stylize($1, "operator");} + | ATASSIGN { $$ = stylize($1, "operator");} + | SLASHASSIGN { $$ = stylize($1, "operator");} + | PERCENTASSIGN { $$ = stylize($1, "operator");} + | BOOLANDASSIGN { $$ = stylize($1, "operator");} + | BOOLORASSIGN { $$ = stylize($1, "operator");} + | HATASSIGN { $$ = stylize($1, "operator");} + | SHIFTLEFTASSIGN { $$ = stylize($1, "operator");} + | SHIFTRIGHTASSIGN { $$ = stylize($1, "operator");} + | DOUBLESTARASSIGN { $$ = stylize($1, "operator");} + | DOUBLESLASHASSIGN { $$ = stylize($1, "operator");} + +del_stmt: DEL exprlist { $$ = concatenate(stylize($1, "keyword"), $2);} +pass_stmt: PASS { $$ = stylize($1, "keyword");} +flow_stmt: break_stmt { $$ = $1; } + | continue_stmt { $$ = $1; } + | return_stmt { $$ = $1; } + | raise_stmt { $$ = $1; } + | yield_stmt { $$ = $1; } +break_stmt: BREAK { $$ = stylize($1, "keyword");} +continue_stmt: CONTINUE { $$ = stylize($1, "keyword");} +return_stmt: RETURN { $$ = stylize($1, "keyword");} + | RETURN testlist { $$ = concatenate(stylize($1, "keyword"), $2);} +yield_stmt: yield_expr { $$ = $1; } +raise_stmt: RAISE { $$ = stylize($1, "keyword");} + | RAISE test { $$ = concatenate(stylize($1, "keyword"), $2);} + | RAISE test FROM test { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, stylize($3, "keyword")); + $$ = concatenate($$, $4); + } +import_stmt: import_name { $$ = $1; } + | import_from { $$ = $1; } +import_name: IMPORT dotted_as_names { $$ = concatenate(stylize($1, "keyword"), $2);} +import_from: FROM import_from_name IMPORT import_from_import { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, stylize($3, "keyword")); + $$ = concatenate($$, $4); + } +import_from_name: dotted_name { $$ = $1; } + | dots_more dotted_name { $$ = concatenate($1, $2);} + | dots_more { $$ = $1; } +dots: DOT { $$ = $1; } + | THREEDOTS { $$ = $1; } +dots_more: dots { $$ = $1; } + | dots_more dots { $$ = concatenate($1, $2);} +import_from_import: STAR { $$ = stylize($1, "operator");} + | LPAREN import_as_names RPAREN { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | import_as_names { $$ = $1; } +import_as_name: NAME { $$ = stylize($1, "identifier");} + | NAME AS NAME { + $$ = concatenate(stylize($1, "identifier"), stylize($2, "operator")); + $$ = concatenate($$, stylize($3, "identifier")); + } +dotted_as_name: dotted_name { $$ = $1; } + | dotted_name AS NAME { + $$ = concatenate($1, stylize($2, "operator")); + $$ = concatenate($$, stylize($3, "identifier")); + } +import_as_names: import_as_name COMMA { $$ = concatenate($1, $2);} + | import_as_name { $$ = $1; } + | import_as_name import_as_name_more COMMA { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | import_as_name import_as_name_more { $$ = concatenate($1, $2);} +import_as_name_more: COMMA import_as_name { $$ = concatenate($1, $2);} + | import_as_name_more COMMA import_as_name { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +dotted_as_names: dotted_as_name { $$ = $1; } + | dotted_as_name dotted_as_name_more { $$ = concatenate($1, $2);} +dotted_as_name_more: COMMA dotted_as_name { $$ = concatenate($1, $2);} + | dotted_as_name_more COMMA dotted_as_name { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +dotted_name: NAME { $$ = stylize($1, "identifier");} + | NAME dotted_name_more { $$ = concatenate(stylize($1, "identifier"), $2);} +dotted_name_more: DOT NAME { $$ = concatenate($1, stylize($2, "identifier"));} + | dotted_name_more DOT NAME { + $$ = concatenate($1, $2); + $$ = concatenate($$, stylize($3, "identifier")); + } +global_stmt: GLOBAL NAME { $$ = concatenate(stylize($1, "keyword"), stylize($2, "identifier"));} + | GLOBAL NAME global_stmt_more { + $$ = concatenate(stylize($1, "keyword"), stylize($2, "identifier")); + $$ = concatenate($$, $3); + } +global_stmt_more: COMMA NAME { $$ = concatenate($1, stylize($2, "identifier"));} + | global_stmt_more COMMA NAME { + $$ = concatenate($1, $2); + $$ = concatenate($$, stylize($3, "identifier")); + } +nonlocal_stmt: NONLOCAL NAME { $$ = concatenate(stylize($1, "keyword"), stylize($2, "identifier"));} + | NONLOCAL NAME nonlocal_stmt_more { + $$ = concatenate(stylize($1, "keyword"), stylize($2, "identifier")); + $$ = concatenate($$, $3); + } +nonlocal_stmt_more: COMMA NAME { $$ = concatenate($1, stylize($2, "identifier"));} + | nonlocal_stmt_more COMMA NAME { + $$ = concatenate($1, $2); + $$ = concatenate($$, stylize($3, "identifier"));; + } +assert_stmt: ASSERT test { $$ = concatenate(stylize($1, "keyword"), $2);} + | ASSERT COMMA test { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + } + +compound_stmt: if_stmt { $$ = $1; } + | while_stmt { $$ = $1; } + | for_stmt { $$ = $1; } + | try_stmt { $$ = $1; } + | with_stmt { $$ = $1; } + | funcdef { $$ = $1; } + | classdef { $$ = $1; } + | decorated { $$ = $1; } + | async_stmt { $$ = $1; } +async_stmt: ASYNC funcdef { $$ = concatenate(stylize($1, "keyword"), $2);} + | ASYNC with_stmt { $$ = concatenate(stylize($1, "keyword"), $2);} + | ASYNC for_stmt { $$ = concatenate(stylize($1, "keyword"), $2);} + +if_stmt: IF test COLON suite elif_stmt_more else_stmt { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + $$ = concatenate($$, $5); + $$ = concatenate($$, $6); + } + | IF test COLON suite elif_stmt_more { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + $$ = concatenate($$, $5); + } + | IF test COLON suite else_stmt { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + $$ = concatenate($$, $5); + } + | IF test COLON suite { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } +elif_stmt: ELIF test COLON suite { + $$ = $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } +else_stmt: ELSE COLON suite { + $$ = $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + } +elif_stmt_more: elif_stmt { $$ = $1; } + | elif_stmt_more elif_stmt { $$ = concatenate($1, $2);} + +while_stmt: WHILE test COLON suite { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } + | WHILE test COLON suite else_stmt { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + $$ = concatenate($$, $5); + } + +for_stmt: FOR exprlist IN testlist COLON suite { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, stylize($3, "operator")); + $$ = concatenate($$, $4); + $$ = concatenate($$, $5); + $$ = concatenate($$, $6); + } + | FOR exprlist IN testlist COLON suite else_stmt { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, stylize($3, "operator")); + $$ = concatenate($$, $4); + $$ = concatenate($$, $5); + $$ = concatenate($$, $6); + $$ = concatenate($$, $7); + } + +try_stmt: TRY COLON suite try_stmt_two { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } +try_stmt_two: except_fin_stmt { $$ = $1; } + | finally_stmt { $$ = $1; } +finally_stmt: FINALLY COLON suite { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + } +except_fin_stmt: except_stmt_more { $$ = $1; } + | except_stmt_more else_stmt { $$ = concatenate($1, $2);} + | except_stmt_more finally_stmt { $$ = concatenate($1, $2);} + | except_stmt_more else_stmt finally_stmt { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +except_stmt_more: except_stmt { $$ = $1; } + | except_stmt_more except_stmt { $$ = concatenate($1, $2);} +except_stmt: except_clause COLON suite { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +except_clause: EXCEPT { $$ = stylize($1, "keyword"); } + | EXCEPT test { $$ = concatenate(stylize($1, "keyword"), $2);} + | EXCEPT test AS NAME { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, stylize($3, "operator")); + $$ = concatenate($$, stylize($4, "identifier")); + } +with_stmt: WITH with_item with_item_more COLON suite { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + $$ = concatenate($$, $5); + } + | WITH with_item COLON suite { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } +with_item: test { $$ = $1; } + | test AS expr { + $$ = concatenate($1, stylize($2, "operator")); + $$ = concatenate($$, $3); + } +with_item_more: COMMA with_item { $$ = concatenate($1, $2);} + | with_item_more COMMA with_item { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +suite: simple_stmt { $$ = $1; } + | NEWLINE INDENT stmt_more UNINDENT { + $$ = concatenate("<br>", $2); + $$ = concatenate($2, $3); + $$ = concatenate($$, ""); + /* $$ = concatenate($$, $4); */ + $$ = collapsableBlock($$); + } +stmt_more: stmt { $$ = $1; } + | stmt_more stmt { $$ = concatenate($1, $2);} + + +test: or_test { $$ = $1; } + | or_test IF or_test ELSE test { + $$ = concatenate($1, stylize($2, "keyword")); + $$ = concatenate($$, $3); + $$ = concatenate($$, stylize($4, "keyword")); + $$ = concatenate($$, $5); + } + | lambdadef { $$ = $1; } +test_nocond: or_test { $$ = $1; } + | lambdadef_nocond { $$ = $1; } +lambdadef: LAMBDA COLON test { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + } + | LAMBDA varargslist COLON test { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } +lambdadef_nocond: LAMBDA COLON test_nocond { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + } + | LAMBDA varargslist COLON test { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } +or_test: and_test or_test_more { $$ = concatenate($1, $2);} +or_test_more: OR and_test or_test_more { + $$ = concatenate(stylize($1, "operator"), $2); + $$ = concatenate($$, $3); + } + | { $$ = ""; } +and_test: not_test and_test_more { $$ = concatenate($1, $2);} +and_test_more: AND not_test and_test_more { + $$ = concatenate(stylize($1, "operator"), $2); + $$ = concatenate($$, $3); + } + | { $$ = ""; } +not_test: NOT not_test { $$ = concatenate(stylize($1, "operator"), $2);} + | comparison { $$ = $1; } +comparison: expr { $$ = $1; } + | expr comp_more { $$ = concatenate($1, $2);} +comp_more: comp_op expr { $$ = concatenate($1, $2);} + | comp_more comp_op expr { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +comp_op: LESS { $$ = stylize($1, "operator");} + | BIGGER { $$ = stylize($1, "operator");} + | EQL { $$ = stylize($1, "operator");} + | LESSEQL { $$ = stylize($1, "operator");} + | BIGGEREQL { $$ = stylize($1, "operator");} + | NOTEQL { $$ = stylize($1, "operator");} + | IN { $$ = stylize($1, "operator");} + | NOTIN { $$ = stylize($1, "operator");} + | IS { $$ = stylize($1, "operator");} + | ISNOT { $$ = stylize($1, "operator");} +star_expr: STAR expr { $$ = concatenate(stylize($1, "operator"), $2);} +expr: xor_expr { $$ = $1; } + | xor_expr expr_more { $$ = concatenate($1, $2);} +expr_more: BOOLOR xor_expr { $$ = concatenate(stylize($1, "operator"), $2);} + | expr_more BOOLOR xor_expr { + $$ = concatenate($1, stylize($2, "operator")); + $$ = concatenate($$, $3); + } +xor_expr: and_expr { $$ = $1; } + | and_expr xor_expr_more { $$ = concatenate($1, $2);} +xor_expr_more: HAT and_expr { $$ = concatenate(stylize($1, "operator"), $2);} + | xor_expr_more HAT and_expr { + $$ = concatenate($1, stylize($2, "operator")); + $$ = concatenate($$, $3); + } +and_expr: shift_expr { $$ = $1; } + | shift_expr and_expr_more { $$ = concatenate($1, $2);} +and_expr_more: BOOLAND shift_expr { $$ = concatenate(stylize($1, "operator"), $2);} + | and_expr_more BOOLAND shift_expr { + $$ = concatenate($1, stylize($2, "operator")); + $$ = concatenate($$, $3); + } +shift_expr: arith_expr { $$ = $1; } + | arith_expr shift_expr_more { $$ = concatenate($1, $2);} +shift_expr_more: shift_op arith_expr { $$ = concatenate($1, $2);} + | shift_expr_more shift_op arith_expr { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +shift_op: SHIFTLESS { $$ = stylize($1, "operator");} + | SHIFTRIGHT { $$ = stylize($1, "operator");} +arith_expr: term { $$ = $1; } + | term arith_expr_more { $$ = concatenate($1, $2);} +arith_expr_more: arith_op term { $$ = concatenate($1, $2);} + | arith_expr_more arith_op term { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +arith_op: PLUS { $$ = stylize($1, "operator");} + | MINUS { $$ = stylize($1, "operator");} +term: factor { $$ = $1; } + | factor term_more { $$ = concatenate($1, $2);} +term_more: term_op factor { $$ = concatenate($1, $2);} + | term_more term_op factor { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +term_op: STAR { $$ = stylize($1, "operator");} + | AT { $$ = stylize($1, "operator");} + | SLASH { $$ = stylize($1, "operator");} + | PERCENT { $$ = stylize($1, "operator");} + | DOUBLESLASH { $$ = stylize($1, "operator");} +factor: unar_op factor { $$ = concatenate($1, $2); } + | power { $$ = $1; } +unar_op: PLUS { $$ = stylize($1, "operator");} + | MINUS { $$ = stylize($1, "operator");} + | TILDA { $$ = stylize($1, "operator");} +power: atom_expr { $$ = $1; } + | atom_expr DOUBLESTAR factor { $$ = concatenate3($1, stylize($2, "operator"), $3);} +atom_expr: AWAIT atom { $$ = concatenate(stylize($1, "operator"), $2);} + | atom { $$ = $1; } + | AWAIT atom trailer_more { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + } + | atom trailer_more { $$ = concatenate($1, $2);} +atom: LPAREN RPAREN { $$ = concatenate($1, $2);} + | LPAREN yield_expr RPAREN { $$ = concatenate3($1, $2, $3);} + | LPAREN testlist_comp RPAREN { $$ = concatenate3($1, $2, $3);} + | LSQPAREN RSQPAREN { $$ = concatenate($1, $2);} + | LSQPAREN testlist_comp RSQPAREN { $$ = concatenate3($1, $2, $3); } + | LCURPAREN RCURPAREN { $$ = concatenate($1, $2);} + | LCURPAREN dictorsetmaker RCURPAREN { $$ = concatenate3($1, $2, $3); } + | NAME { $$ = stylize($1, "identifier");} + | NUMBER { $$ = stylize($1, "number");} + | STRING { $$ = stylize($1, "string");} + | THREEDOTS { $$ = stylize($1, "operator");} + | NONE { $$ = stylize($1, "constant");} + | TRUE { $$ = stylize($1, "constant");} + | FALSE { $$ = stylize($1, "constant");} + | DOLAR { $$ = "";} +testlist_comp: test testlist_comp_second { $$ = concatenate($1, $2);} + | star_expr testlist_comp_second { $$ = concatenate($1, $2);} +testlist_comp_second: comp_for { $$ = $1; } + | testlist_more_arg { $$ = $1; } +testlist_more_arg: COMMA test COMMA { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | COMMA test { $$ = concatenate($1, $2);} + | testlist_more_arg COMMA test COMMA { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } + | testlist_more_arg COMMA test { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | testlist_more_arg COMMA star_expr COMMA { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } + | testlist_more_arg COMMA star_expr { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +trailer: LPAREN RPAREN { $$ = concatenate($1, $2);} + | LPAREN arglist RPAREN { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | LSQPAREN subscriptlist RSQPAREN { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | DOT NAME { $$ = concatenate($1, stylize($2, "identifier"));} +trailer_more: trailer { $$ = $1; } + | trailer_more trailer { $$ = concatenate($1, $2);} +subscriptlist: subscript COMMA { $$ = concatenate($1, $2);} + | subscript { $$ = $1; } + | subscript subscipt_more COMMA { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | subscript subscipt_more { $$ = concatenate($1, $2);} +subscipt_more: COMMA subscript { $$ = concatenate($1, $2);} + | subscipt_more COMMA subscript { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +subscript: test { $$ = $1; } + | COLON { $$ = $1; } + | COLON test { $$ = concatenate($1, $2);} + | COLON sliceop { $$ = concatenate($1, $2);} + | COLON test sliceop { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | test COLON { $$ = concatenate($1, $2);} + | test COLON test { $$ = concatenate($1, $2);} + | test COLON sliceop { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | test COLON test sliceop { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } +sliceop: COLON test { $$ = concatenate($1, $2);} + | COLON { $$ = $1; } +exprlist: exprlist_op COMMA { $$ = concatenate($1, $2);} + | exprlist_op { $$ = $1; } + | exprlist_op exprlist_more COMMA { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | exprlist_op exprlist_more { $$ = concatenate($1, $2);} +exprlist_more: COMMA exprlist_op { $$ = concatenate($1, $2);} + | exprlist_more COMMA exprlist_op { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +exprlist_op: expr { $$ = $1; } + | star_expr { $$ = $1; } +testlist: test { $$ = $1; } + | test COMMA { $$ = concatenate($1, $2);} + | test testlist_more { $$ = concatenate($1, $2);} + | test testlist_more COMMA { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +testlist_more: COMMA test { $$ = concatenate($1, $2);} + | testlist_more COMMA test { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +dictorsetmaker: test_star_expr dsm_one { $$ = concatenate($1, $2);} + | test_star_expr COMMA { $$ = concatenate($1, $2);} /* in the case test_star_expr is zero times */ + | test_star_expr { $$ = $1; } /* in the case test_star_expr is zero times */ + | test_star_expr_2 dsm_two { $$ = concatenate($1, $2);} + | test_star_expr_2 COMMA { $$ = concatenate($1, $2);}/* in the case test_star_expr_2 is zero times */ + | test_star_expr_2 { $$ = $1; } /* in the case test_star_expr_2 is zero times */ +test_star_expr: test COLON test { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | DOUBLESTAR expr { $$ = concatenate(stylize($1, "operator"), $2);} +dsm_one: comp_for { $$ = $1; } + | comp_for COMMA { $$ = concatenate($1, $2);} + | test_star_expr_more COMMA { $$ = concatenate($1, $2);} + | test_star_expr_more { $$ = $1; } +test_star_expr_more: COMMA test_star_expr { $$ = concatenate($1, $2);} + | test_star_expr_more COMMA test_star_expr { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +test_star_expr_2: test { $$ = $1; } + | star_expr { $$ = $1; } +dsm_two: comp_for { $$ = $1; } + | comp_for COMMA { $$ = concatenate($1, $2);} + | test_star_expr_2_more COMMA { $$ = concatenate($1, $2);} + | test_star_expr_2_more { $$ = $1; } +test_star_expr_2_more: COMMA test_star_expr_2 { $$ = concatenate($1, $2);} + | test_star_expr_2_more COMMA test_star_expr_2 { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + +classdef: CLASS NAME COLON suite { + $$ = concatenate(stylize($1, "keyword"), stylize($2, "identifier")); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + } + | CLASS NAME LPAREN RPAREN COLON suite { + $$ = concatenate(stylize($1, "keyword"), stylize($2, "identifier")); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + $$ = concatenate($$, $5); + $$ = concatenate($$, $6); + } + | CLASS NAME LPAREN arglist RPAREN COLON suite { + $$ = concatenate(stylize($1, "keyword"), stylize($2, "identifier")); + $$ = concatenate($$, $3); + $$ = concatenate($$, $4); + $$ = concatenate($$, $5); + $$ = concatenate($$, $6); + $$ = concatenate($$, $7); + } +arglist: argument COMMA { $$ = concatenate($1, $2);} + | argument { $$ = $1; } + | argument arglist_more COMMA { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } + | argument arglist_more { $$ = concatenate($1, $2);} +arglist_more: COMMA argument { $$ = concatenate($1, $2);} + | arglist_more COMMA argument { + $$ = concatenate($1, $2); + $$ = concatenate($$, $3); + } +argument: test comp_for { $$ = concatenate($1, $2);} + | test { $$ = $1; } + | test ASSIGN test { + $$ = concatenate($1, stylize($2, "operator")); + $$ = concatenate($$, $3); + } + | DOUBLESTAR test { $$ = concatenate(stylize($1, "operator"), $2);} + | STAR test { $$ = concatenate(stylize($1, "operator"), $2);} +comp_iter: comp_for { $$ = $1; } + | comp_if { $$ = $1; } +sync_comp_for: FOR exprlist IN or_test { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, stylize($3, "operator")); + $$ = concatenate($$, $4); + } + | FOR exprlist IN or_test comp_iter { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, stylize($3, "operator")); + $$ = concatenate($$, $4); + $$ = concatenate($$, $5); + } +comp_for: sync_comp_for { $$ = $1; } + | ASYNC comp_for { $$ = concatenate(stylize($1, "keyword"), $2);} +comp_if: IF test_nocond { $$ = concatenate(stylize($1, "keyword"), $2);} + | IF test_nocond comp_iter { + $$ = concatenate(stylize($1, "keyword"), $2); + $$ = concatenate($$, $3); + } + +/* encoding_decl: NAME */ + +yield_expr: YIELD { $$ = stylize($1, "keyword");} + | YIELD yield_arg { $$ = concatenate(stylize($1, "keyword"), $2);} +yield_arg: FROM test { $$ = concatenate(stylize($1, "keyword"), $2);} + | testlist { $$ = $1; } + +%% +int yyerror(const char* error) +{ + extern int current_line; + fprintf(stderr, "input:%d: error: %s\n", current_line, error); + return 0; +} + +void printHtmlHeader(){ + puts( + "<head>" + " <title>python-parse.html</title>" + ); + printStyles(); + puts( + "</head>" + ); +} +void printBody() { + puts( + "<body bgcolor=\"#e2cdb7\">" + " <pre class=\"code\">" + ); + yyparse(); + puts("</pre></body>"); +} +void printHTML() { + puts( + "<!doctype>" + "<html>" + ); + printHtmlHeader(); + printBody(); + printCollapsableScript(); + puts("</html>"); +} + + +int main(int argc, const char* argv[]) +{ + #if YYDEBUG + yydebug = 1; + #endif + //printHTML(); + FILE *myfile = fopen("input.file", "r"); +if (!myfile) { + printf("I can't open input.file!\n"); + return -1; +} +yyin = myfile; + printHTML(); + return 0; +} diff --git a/homework/81350/parser/colapser/stack.c b/homework/81350/parser/colapser/stack.c new file mode 100644 index 0000000..c6edd42 --- /dev/null +++ b/homework/81350/parser/colapser/stack.c @@ -0,0 +1,56 @@ +#include "stack.h" + +int stack[MAXSIZE]; +int top = -1; + +int isempty() { + + if(top == -1) + return 1; + else + return 0; +} + +int isfull() { + + if(top == MAXSIZE) + return 1; + else + return 0; +} + +int peek() { + return stack[top]; +} + +int pop() { + int data; + + if(!isempty()) { + data = stack[top]; + top = top - 1; + return data; + } else { + printf("Could not retrieve data, Stack is empty.\n"); + } +} + +int push(int data) { + + if(!isfull()) { + top = top + 1; + stack[top] = data; + } else { + printf("Could not insert data, Stack is full.\n"); + } +} + +char* concatenate(char* first, char* second) { + int firstlen = strlen(first); + int secondlen = strlen(second); + char* result; + result = malloc(firstlen + secondlen + 1); + strcpy(result, first); + strcat(result, second); + return result; +} diff --git a/homework/81350/parser/colapser/stack.h b/homework/81350/parser/colapser/stack.h new file mode 100644 index 0000000..83c2741 --- /dev/null +++ b/homework/81350/parser/colapser/stack.h @@ -0,0 +1,16 @@ +#pragma once +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#define MAXSIZE 1000 +extern int stack[MAXSIZE]; +extern int top; + +int isempty(); +int isfull(); +int peek(); +int pop(); +int push(int); + +char* concatenate(char*, char*); diff --git a/homework/81350/parser/input.file b/homework/81350/parser/input.file new file mode 100644 index 0000000..d39ec80 --- /dev/null +++ b/homework/81350/parser/input.file @@ -0,0 +1,3 @@ +func(): + a=2 + a=2+3 diff --git a/homework/81350/parser/lexer.lex b/homework/81350/parser/lexer.lex new file mode 100644 index 0000000..03ea391 --- /dev/null +++ b/homework/81350/parser/lexer.lex @@ -0,0 +1,182 @@ +%{ +/* need this for the call to atof() below */ +#include <math.h> +#include "parser.tab.h" +#include "stack.h" +#define YY_NO_UNISTD_H +static const unsigned int TAB_WIDTH = 2; + +#define YY_USER_INIT { push(0); BEGIN(initial); } +int current_line = 0; +int current_line_indent = 0; +int indent_level = 0; +int is_fake_sym = 0; +%} + +digit [0-9] +number [1-9]{digit}* +fnumber {digit}+"."{digit}* +letter [a-zA-Z] +string ["]{3}(["]{0,2}([^\\"]|\\(.|\n)))*["]{3} + +%x initial +%x indent +%s normal + +%% + int indent_caller = normal; +<*>"\n" { REJECT; } + +<initial>. { unput(*yytext); indent_caller = normal; BEGIN(indent); } +<initial>\n { indent_caller = normal; BEGIN(indent); } + +<indent>" " { current_line_indent++;} +<indent>"\t" { current_line_indent = (current_line_indent+2) & ~1;} /* one tab is 2 spaces*/ +<indent>"\n" { current_line_indent = 0; return NEWLINE;} +<indent><<EOF>> { + if(peek() != 0){ + pop(); + if(current_line_indent != peek()){ + unput('\n'); + for(int i=0; i< peek(); i++){ + unput(' '); + } + } else { + BEGIN(indent_caller); + } + return UNINDENT; + } else { + yyterminate(); + } + } +<indent>. { + if(!is_fake_sym){ + unput(*yytext); + } + is_fake_sym = 0; + if (current_line_indent > peek()) { + push(current_line_indent); + BEGIN(indent_caller); + return INDENT; + } else if (current_line_indent < peek()) { + pop(); + if(current_line_indent != peek()){ + for(int i=0; i< current_line_indent; i++){ + unput(' '); + } + unput('\n'); + + unput('.'); + is_fake_sym = 1; + for(int i=0; i < peek(); i++){ + unput(' '); + is_fake_sym = 1; + } + unput('\n'); + is_fake_sym = 1; + } else { + BEGIN(indent_caller); + } + return UNINDENT; + } else { + BEGIN(indent_caller); + } + } +<normal>"\n" { current_line_indent = 0; indent_caller = YY_START; BEGIN(indent); return NEWLINE;} + +<normal>{number}|{fnumber} { yylval.Number = atof(yytext); return NUMBER; } +<normal>{digit} { yylval.Number = atof(yytext); return NUMBER;} +<normal>{string} { strcpy(yylval.Lexeme, strdup(yytext)); return STRING;} + +<normal>"+" { return PLUS; } +<normal>"-" { return MINUS; } +<normal>"*" { return STAR; } +<normal>"/" { return SLASH; } +<normal>"//" { return DOUBLESLASH;} +<normal>"%" { return PERCENT; } +<normal>"**" { return DOUBLESTAR; } +<normal>">" { return BIGGER; } +<normal>"<" { return LESS; } +<normal>">=" { return BIGGEREQL; } +<normal>"<=" { return LESSEQL; } +<normal>"==" { return EQL; } +<normal>"!=" { return NOTEQL; } + + +<normal>"&" { return BOOLAND; } +<normal>"|" { return BOOLOR; } +<normal>"~" { return TILDA; } +<normal>"^" { return HAT; } +<normal>">>" { return SHIFTRIGHT; } +<normal>"<<" { return SHIFTLESS; } + + +<normal>"=" { return ASSIGN; } +<normal>"+=" { return PLUSASSIGN; } +<normal>"-=" { return MINUSASSIGN;} +<normal>"*=" { return STARASSIGN; } +<normal>"/=" { return SLASHASSIGN;} +<normal>"%=" { return PERCENTASSIGN;} +<normal>"**=" { return DOUBLESTARASSIGN;} +<normal>"//=" { return DOUBLESLASHASSIGN;} +<normal>"^=" { return HATASSIGN;} +<normal>"&=" { return BOOLANDASSIGN;} +<normal>"|=" { return BOOLORASSIGN;} +<normal>">>=" { return SHIFTRIGHTASSIGN;} +<normal>"<<=" { return SHIFTLEFTASSIGN;} +<normal>"@=" { return ATASSIGN; } + +<normal>"@" { return AT;} +<normal>"(" { return LPAREN; } +<normal>")" { return RPAREN; } +<normal>"[" { return LSQPAREN;} +<normal>"]" { return RSQPAREN;} +<normal>"{" { return LCURPAREN;} +<normal>"}" { return RCURPAREN;} +<normal>";" { return SEMICOLON; } +<normal>":" { return COLON; } +<normal>"," { return COMMA; } +<normal>"." { return DOT; } +<normal>"..." { return THREEDOTS;} + +<normal>"async" { return ASYNC;} +<normal>"def" { return DEF; } +<normal>"and" { return AND;} +<normal>"or" { return OR;} +<normal>"not" { return NOT;} +<normal>"in" { return IN;} +<normal>"not in" { return NOTIN;} +<normal>"is" { return IS;} +<normal>"is not" { return ISNOT;} +<normal>"await" { return AWAIT;} +<normal>"None" { return NONE;} +<normal>"True" { return TRUE;} +<normal>"False" { return FALSE;} +<normal>"class" { return CLASS;} +<normal>"for" { return FOR;} +<normal>"if" { return IF;} +<normal>"elif" { return ELIF;} +<normal>"else" { return ELSE;} +<normal>"except" { return EXCEPT;} +<normal>"from" { return FROM;} +<normal>"finally" { return FINALLY;} +<normal>"lambda" { return LAMBDA;} +<normal>"yield" { return YIELD;} +<normal>"as" { return AS;} +<normal>"assert" { return ASSERT;} +<normal>"break" { return BREAK;} +<normal>"continue" { return CONTINUE;} +<normal>"del" { return DEL;} +<normal>"global" { return GLOBAL;} +<normal>"import" { return IMPORT;} +<normal>"nonlocal" { return NONLOCAL;} +<normal>"pass" { return PASS;} +<normal>"raise" { return RAISE;} +<normal>"return" { return RETURN;} +<normal>"try" { return TRY;} +<normal>"while" { return WHILE;} +<normal>"with" { return WITH;} + +<normal>{letter}({letter}|{digit})* { + strcpy(yylval.Lexeme, strdup(yytext)); +return NAME; } diff --git a/homework/81350/parser/parser.y b/homework/81350/parser/parser.y new file mode 100644 index 0000000..97d7393 --- /dev/null +++ b/homework/81350/parser/parser.y @@ -0,0 +1,441 @@ + +%code requires { +#include <stdio.h> +#include <string.h> +int yylex(); +int yyerror(const char* error); +FILE *yyin; +#undef YYDEBUG +#define YYDEBUG 1 +/* #define YY_DECL int yylex(scanner) void* scanner; */ +} + +%start input_stmt +/* %define api.pure full */ +%define api.value.type {union name { double Number; char Lexeme[256];} } + +%token NUMBER +%token PLUS +%token MINUS +%token STAR +%token SLASH +%token DOUBLESLASH +%token PERCENT +%token DOUBLESTAR +%token BIGGER +%token LESS +%token BIGGEREQL +%token LESSEQL +%token EQL +%token NOTEQL + +%token BOOLAND +%token BOOLOR +%token TILDA +%token HAT +%token SHIFTRIGHT +%token SHIFTLESS + +%token ASSIGN +%token PLUSASSIGN +%token MINUSASSIGN +%token STARASSIGN +%token SLASHASSIGN +%token PERCENTASSIGN +%token DOUBLESTARASSIGN +%token DOUBLESLASHASSIGN +%token HATASSIGN +%token BOOLANDASSIGN +%token BOOLORASSIGN +%token SHIFTRIGHTASSIGN +%token SHIFTLEFTASSIGN +%token ATASSIGN + +%token LPAREN +%token RPAREN +%token LSQPAREN +%token RSQPAREN +%token LCURPAREN +%token RCURPAREN +%token SEMICOLON +%token COLON +%token COMMA +%token DOT +%token AT +%token THREEDOTS + +%token NAME + +%token DEF +%token ASYNC +%token AND +%token OR +%token NOT +%token IN +%token NOTIN +%token IS +%token ISNOT +%token AWAIT +%token NONE +%token TRUE +%token FALSE +%token FOR +%token IF +%token ELIF +%token ELSE +%token EXCEPT +%token CLASS +%token FROM +%token FINALLY +%token LAMBDA +%token YIELD +%token AS +%token ASSERT +%token BREAK +%token CONTINUE +%token DEL +%token GLOBAL +%token IMPORT +%token NONLOCAL +%token PASS +%token RAISE +%token RETURN +%token TRY +%token WHILE +%token WITH + +%token INDENT +%token UNINDENT +%token NEWLINE +%token EOFILE +%token STRING + +/* +Define both - precedence and associativity */ +%left PLUS MINUS +%left STAR SLASH + +%% +/* +input: + | expr { printf("%f\n", $1.Number); } + ; +expr: NUMBER { $$.Number = $1.Number; } + | '(' expr ')' { $$.Number = $2.Number; } + | expr PLUS expr { $$.Number = $1.Number + $3.Number; } + | expr MINUS expr { $$.Number = $1.Number - $3.Number; } + | expr STAR expr { $$.Number = $1.Number * $3.Number; } + | expr SLASH expr { $$.Number = $1.Number / $3.Number; } +*/ + +input_stmt: NEWLINE | simple_stmt | compound_stmt NEWLINE | compound_stmt + + +decorator: AT dotted_name NEWLINE + | AT dotted_name LPAREN RPAREN NEWLINE + | AT dotted_name LPAREN arglist RPAREN NEWLINE +decorator_more: decorator | decorator_more decorator +decorated: decorator_more obj_definition +obj_definition: classdef | funcdef | async_funcdef + + +async_funcdef: ASYNC funcdef +funcdef: DEF NAME parameters COLON suite /* | DEF NAME parameters '->' test COLON suite */ +parameters: LPAREN RPAREN | LPAREN typedargslist RPAREN + +typedargslist: td_one | td_two | td_three +td_one: td_one_arg td_more_arg td_one_optional | td_one_arg td_one_optional +td_one_arg: tfpdef ASSIGN test | tfpdef +td_more_arg: COMMA td_one_arg | td_more_arg COMMA td_one_arg +/* td_add_arg: COMMA td_one_arg td_more_arg */ +td_one_optional: COMMA td_two | COMMA td_three | COMMA | +td_two: STAR tfpdef td_more_arg td_two_optional | STAR td_more_arg td_two_optional +td_two_optional: COMMA td_three | COMMA +td_three: DOUBLESTAR tfpdef COMMA | DOUBLESTAR tfpdef +tfpdef: NAME | NAME COLON test + +varargslist: var_one | var_two | var_three +var_one: var_one_arg var_more_arg var_one_optional | var_one_arg var_one_optional +var_one_arg: vfpdef ASSIGN test | vfpdef +var_more_arg: COMMA var_one_arg | var_more_arg COMMA var_one_arg +var_one_optional: COMMA var_two | COMMA var_three | COMMA +var_two: STAR vfpdef var_more_arg var_two_optional | STAR var_more_arg var_two_optional +var_two_optional: COMMA var_three | COMMA +var_three: DOUBLESTAR vfpdef COMMA | DOUBLESTAR vfpdef +vfpdef: NAME + +stmt: simple_stmt + | compound_stmt +simple_stmt: small_stmt simple_more_stmt SEMICOLON NEWLINE + | small_stmt simple_more_stmt NEWLINE + | small_stmt NEWLINE + | small_stmt SEMICOLON NEWLINE +simple_more_stmt: SEMICOLON small_stmt + | simple_more_stmt SEMICOLON small_stmt +small_stmt: expr_stmt + | del_stmt + | pass_stmt + | flow_stmt + | import_stmt + | global_stmt + | nonlocal_stmt + | assert_stmt +expr_stmt: testlist_star_expr expr_stmt_two + | testlist_star_expr +expr_stmt_two: annassign + | augassign aug_expr + | assign_more +assign_more: ASSIGN assign_expr + | assign_more ASSIGN assign_expr +assign_expr: yield_expr + | testlist_star_expr +aug_expr: yield_expr + | testlist +annassign: COLON test | COLON test ASSIGN test +testlist_star_expr: testlist_se testlist_star_expr_more COMMA + | testlist_se testlist_star_expr_more + | testlist_se COMMA + | testlist_se +testlist_se: test + | star_expr +testlist_star_expr_more: COMMA testlist_se | testlist_star_expr_more COMMA testlist_se +augassign: PLUSASSIGN + | MINUSASSIGN + | STARASSIGN + | ATASSIGN + | SLASHASSIGN + | PERCENTASSIGN + | BOOLANDASSIGN + | BOOLORASSIGN + | HATASSIGN + | SHIFTLEFTASSIGN + | SHIFTRIGHTASSIGN + | DOUBLESTARASSIGN + | DOUBLESLASHASSIGN + +del_stmt: DEL exprlist +pass_stmt: PASS +flow_stmt: break_stmt + | continue_stmt + | return_stmt + | raise_stmt + | yield_stmt +break_stmt: BREAK +continue_stmt: CONTINUE +return_stmt: RETURN | RETURN testlist +yield_stmt: yield_expr +raise_stmt: RAISE | RAISE test | RAISE test FROM test +import_stmt: import_name | import_from +import_name: IMPORT dotted_as_names +import_from: FROM import_from_name IMPORT import_from_import +import_from_name: dotted_name | dots_more dotted_name | dots_more +dots: DOT | THREEDOTS +dots_more: dots | dots_more dots +import_from_import: STAR + | LPAREN import_as_names RPAREN + | import_as_names +import_as_name: NAME | NAME AS NAME +dotted_as_name: dotted_name | dotted_name AS NAME +import_as_names: import_as_name COMMA + | import_as_name + | import_as_name import_as_name_more COMMA + | import_as_name import_as_name_more +import_as_name_more: COMMA import_as_name + | import_as_name_more COMMA import_as_name +dotted_as_names: dotted_as_name + | dotted_as_name dotted_as_name_more +dotted_as_name_more: COMMA dotted_as_name + | dotted_as_name_more COMMA dotted_as_name +dotted_name: NAME | NAME dotted_name_more +dotted_name_more: DOT NAME | dotted_name_more DOT NAME +global_stmt: GLOBAL NAME | GLOBAL NAME global_stmt_more +global_stmt_more: COMMA NAME | global_stmt_more COMMA NAME +nonlocal_stmt: NONLOCAL NAME | NONLOCAL NAME nonlocal_stmt_more +nonlocal_stmt_more: COMMA NAME | nonlocal_stmt_more COMMA NAME +assert_stmt: ASSERT test | ASSERT COMMA test + +compound_stmt: if_stmt + | while_stmt + | for_stmt + | try_stmt + | with_stmt + | funcdef + | classdef + | decorated + | async_stmt +async_stmt: ASYNC funcdef | ASYNC with_stmt | ASYNC for_stmt +if_stmt: IF test COLON suite elif_stmt_more else_stmt + | IF test COLON suite elif_stmt_more + | IF test COLON suite else_stmt + | IF test COLON suite +elif_stmt: ELIF test COLON suite +else_stmt: ELSE COLON suite +elif_stmt_more: elif_stmt | elif_stmt_more elif_stmt +while_stmt: WHILE test COLON suite + | WHILE test COLON suite else_stmt +for_stmt: FOR exprlist IN testlist COLON suite + | FOR exprlist IN testlist COLON suite else_stmt +try_stmt: TRY COLON suite try_stmt_two +try_stmt_two: except_fin_stmt | finally_stmt +finally_stmt: FINALLY COLON suite +except_fin_stmt: except_stmt_more + | except_stmt_more else_stmt + | except_stmt_more finally_stmt + | except_stmt_more else_stmt finally_stmt +except_stmt_more: except_stmt | except_stmt_more except_stmt +except_stmt: except_clause COLON suite +except_clause: EXCEPT + | EXCEPT test + | EXCEPT test AS NAME +with_stmt: WITH with_item with_item_more COLON suite + | WITH with_item COLON suite +with_item: test | test AS expr +with_item_more: COMMA with_item | with_item_more COMMA with_item +suite: simple_stmt | NEWLINE INDENT stmt_more UNINDENT +stmt_more: stmt | stmt_more stmt + + +test: or_test + | or_test IF or_test ELSE test + | lambdadef +test_nocond: or_test | lambdadef_nocond +lambdadef: LAMBDA COLON test + | LAMBDA varargslist COLON test +lambdadef_nocond: LAMBDA COLON test_nocond + | LAMBDA varargslist COLON test +or_test: and_test or_test_more +or_test_more: | OR and_test or_test_more +and_test: not_test and_test_more +and_test_more: | AND not_test and_test_more +not_test: NOT not_test | comparison +comparison: expr | expr comp_more +comp_more: comp_op expr | comp_more comp_op expr +comp_op: LESS | BIGGER | EQL | LESSEQL | BIGGEREQL | NOTEQL | IN | NOTIN | IS | ISNOT +star_expr: STAR expr +expr: xor_expr | xor_expr expr_more +expr_more: BOOLOR xor_expr | expr BOOLOR xor_expr +xor_expr: and_expr | and_expr xor_expr_more +xor_expr_more: HAT and_expr | xor_expr HAT and_expr +and_expr: shift_expr | shift_expr and_expr_more +and_expr_more: BOOLAND shift_expr | and_expr BOOLAND shift_expr +shift_expr: arith_expr | arith_expr shift_expr_more +shift_expr_more: shift_op arith_expr | shift_expr shift_op arith_expr +shift_op: SHIFTLESS | SHIFTRIGHT +arith_expr: term | term arith_expr_more +arith_expr_more: arith_op term | arith_expr arith_op term +arith_op: PLUS | MINUS +term: factor | factor term_more +term_more: term_op factor | term term_op factor +term_op: STAR | AT | SLASH | PERCENT | DOUBLESLASH +factor: unar_op factor | power +unar_op: PLUS | MINUS | TILDA +power: atom_expr | atom_expr DOUBLESTAR factor +atom_expr: AWAIT atom | atom | AWAIT atom trailer_more | atom trailer_more +atom: LPAREN RPAREN + | LPAREN yield_expr RPAREN + | LPAREN testlist_comp RPAREN + | LSQPAREN RSQPAREN + | LSQPAREN testlist_comp RSQPAREN + | LCURPAREN RCURPAREN + | LCURPAREN dictorsetmaker RCURPAREN + | NAME + | NUMBER + | STRING + | THREEDOTS + | NONE + | TRUE + | FALSE +testlist_comp: test testlist_comp_second | star_expr testlist_comp_second +testlist_comp_second: comp_for | testlist_more_arg +testlist_more_arg: COMMA test COMMA + | COMMA test + | testlist_more_arg COMMA test COMMA + | testlist_more_arg COMMA test + | testlist_more_arg COMMA star_expr COMMA + | testlist_more_arg COMMA star_expr +trailer: LPAREN RPAREN + | LPAREN arglist RPAREN + | LSQPAREN subscriptlist RSQPAREN + | DOT NAME +trailer_more: trailer | trailer_more trailer +subscriptlist: subscript COMMA + | subscript + | subscript subscipt_more COMMA | + | subscript subscipt_more +subscipt_more: COMMA subscript | subscipt_more COMMA subscript +subscript: test + | COLON + | COLON test + | COLON sliceop + | COLON test sliceop + | test COLON + | test COLON test + | test COLON sliceop + | test COLON test sliceop +sliceop: COLON test | COLON +exprlist: exprlist_op COMMA | exprlist_op | exprlist_op exprlist_more COMMA | exprlist_op exprlist_more +exprlist_more: COMMA exprlist_op | exprlist_more COMMA exprlist_op +exprlist_op: expr | star_expr +testlist: test | test COMMA | test testlist_more | test testlist_more COMMA +testlist_more: COMMA test | testlist_more COMMA test +dictorsetmaker: test_star_expr dsm_one + | test_star_expr COMMA /* in the case test_star_expr is zero times */ + | test_star_expr /* in the case test_star_expr is zero times */ + | test_star_expr_2 dsm_two + | test_star_expr_2 COMMA /* in the case test_star_expr_2 is zero times */ + | test_star_expr_2 /* in the case test_star_expr_2 is zero times */ +test_star_expr: test COLON test | DOUBLESTAR expr +dsm_one: comp_for | comp_for COMMA | test_star_expr_more COMMA | test_star_expr_more +test_star_expr_more: COMMA test_star_expr | test_star_expr_more COMMA test_star_expr +test_star_expr_2: test | star_expr +dsm_two: comp_for | comp_for COMMA | test_star_expr_2_more COMMA | test_star_expr_2_more +test_star_expr_2_more: COMMA test_star_expr_2 | test_star_expr_2_more COMMA test_star_expr_2 + +classdef: CLASS NAME COLON suite + | CLASS NAME LPAREN RPAREN COLON suite + | CLASS NAME LPAREN arglist RPAREN COLON suite +arglist: argument COMMA + | argument + | argument arglist_more COMMA + | argument arglist_more +arglist_more: COMMA argument + | arglist_more COMMA argument +argument: test comp_for + | test + | test ASSIGN test + | DOUBLESTAR test + | STAR test +comp_iter: comp_for + | comp_if +sync_comp_for: FOR exprlist IN or_test + | FOR exprlist IN or_test comp_iter +comp_for: sync_comp_for + | ASYNC comp_for +comp_if: IF test_nocond | IF test_nocond comp_iter + +/* encoding_decl: NAME */ + +yield_expr: YIELD | YIELD yield_arg +yield_arg: FROM test | testlist + +%% +int yyerror(const char* error) +{ + extern int current_line; + fprintf(stderr, "input:%d: error: %s\n", current_line, error); + return 0; +} + int main() +{ +#if YYDEBUG + yydebug = 1; +#endif +FILE *myfile = fopen("input.file", "r"); +if (!myfile) { + printf("I can't open input.file!\n"); + return -1; +} +yyin = myfile; +yyparse(); + return 0; +} diff --git a/homework/81350/parser/stack.c b/homework/81350/parser/stack.c new file mode 100644 index 0000000..a051f7a --- /dev/null +++ b/homework/81350/parser/stack.c @@ -0,0 +1,46 @@ +#include "stack.h" + +int stack[MAXSIZE]; +int top = -1; + +int isempty() { + + if(top == -1) + return 1; + else + return 0; +} + +int isfull() { + + if(top == MAXSIZE) + return 1; + else + return 0; +} + +int peek() { + return stack[top]; +} + +int pop() { + int data; + + if(!isempty()) { + data = stack[top]; + top = top - 1; + return data; + } else { + printf("Could not retrieve data, Stack is empty.\n"); + } +} + +int push(int data) { + + if(!isfull()) { + top = top + 1; + stack[top] = data; + } else { + printf("Could not insert data, Stack is full.\n"); + } +} diff --git a/homework/81350/parser/stack.h b/homework/81350/parser/stack.h new file mode 100644 index 0000000..2fccd60 --- /dev/null +++ b/homework/81350/parser/stack.h @@ -0,0 +1,12 @@ +#pragma once +#include <stdio.h> + +#define MAXSIZE 1000 +extern int stack[MAXSIZE]; +extern int top; + +int isempty(); +int isfull(); +int peek(); +int pop(); +int push(int); |
