From ccd71ee220093774a35708d8c1e31936b012930a Mon Sep 17 00:00:00 2001 From: Dimitar Milev Date: Sat, 3 Nov 2018 12:52:28 +0100 Subject: 81352 - homework 2 (#32) * fix(hw1): use yyextra instead global variable * hw2: extended the typescript syntax highlighter with collapsible block structures --- homework/81352/ts2html-hw2/parser.y | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 homework/81352/ts2html-hw2/parser.y (limited to 'homework/81352/ts2html-hw2/parser.y') diff --git a/homework/81352/ts2html-hw2/parser.y b/homework/81352/ts2html-hw2/parser.y new file mode 100644 index 0000000..53a56c4 --- /dev/null +++ b/homework/81352/ts2html-hw2/parser.y @@ -0,0 +1,86 @@ +%{ +#include +int yylex(); +int yyerror(const char* error); +int current_line = 1; +%} + +%code requires {#include } +%start input +%define api.value.type {std::string} + +%token KEYWORD +%token STRING +%token NUMBER +%token IDENTIFIER +%token OPERATOR +%token OTHER +%token NEWLINE +%token DECORATOR +%token COMMENT + + +%% + +input: %empty + | program { std::cout << " \ +
1. " << $1 << "
"; } + +program: expr + | program expr {$$ = $1 + $2;} + +expr: block + | nonblock + +nonblock: KEYWORD { $$ = "" + $1 + ""; } + | STRING { $$ = "" + $1 + ""; } + | NUMBER { $$ = "" + $1 + ""; } + | IDENTIFIER { $$ = "" + $1 + ""; } + | OPERATOR { $$ = "" + $1 + ""; } + | NEWLINE { $$ = $1 + "" + std::to_string(++current_line) + ". ";} + | COMMENT { $$ = "" + $1 + ""; } + | DECORATOR { $$ = "" + $1 + ""; } + | OTHER + +block: '{' program '}' {$$ = "{" + std::string("
") + $2 + "
}";} + +%% +int yyerror(const char* error) +{ + std::cerr << "error:\n" << error; + return 0; +} +int main() +{ + yyparse(); + return 0; +} -- cgit v1.3.1-26-gb76a