From b98a9799f15c8ad0275175a1d8d013c4f6e0176f Mon Sep 17 00:00:00 2001 From: Ralitsa Dardjonova Date: Wed, 28 Nov 2018 12:15:14 +0200 Subject: 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 --- homework/81350/parser/lexer.lex | 182 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 homework/81350/parser/lexer.lex (limited to 'homework/81350/parser/lexer.lex') 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 +#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; } + +. { unput(*yytext); indent_caller = normal; BEGIN(indent); } +\n { indent_caller = normal; BEGIN(indent); } + +" " { current_line_indent++;} +"\t" { current_line_indent = (current_line_indent+2) & ~1;} /* one tab is 2 spaces*/ +"\n" { current_line_indent = 0; return NEWLINE;} +<> { + 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(); + } + } +. { + 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); + } + } +"\n" { current_line_indent = 0; indent_caller = YY_START; BEGIN(indent); return NEWLINE;} + +{number}|{fnumber} { yylval.Number = atof(yytext); return NUMBER; } +{digit} { yylval.Number = atof(yytext); return NUMBER;} +{string} { strcpy(yylval.Lexeme, strdup(yytext)); return STRING;} + +"+" { return PLUS; } +"-" { return MINUS; } +"*" { return STAR; } +"/" { return SLASH; } +"//" { return DOUBLESLASH;} +"%" { return PERCENT; } +"**" { return DOUBLESTAR; } +">" { return BIGGER; } +"<" { return LESS; } +">=" { return BIGGEREQL; } +"<=" { return LESSEQL; } +"==" { return EQL; } +"!=" { return NOTEQL; } + + +"&" { return BOOLAND; } +"|" { return BOOLOR; } +"~" { return TILDA; } +"^" { return HAT; } +">>" { return SHIFTRIGHT; } +"<<" { return SHIFTLESS; } + + +"=" { return ASSIGN; } +"+=" { return PLUSASSIGN; } +"-=" { return MINUSASSIGN;} +"*=" { return STARASSIGN; } +"/=" { return SLASHASSIGN;} +"%=" { return PERCENTASSIGN;} +"**=" { return DOUBLESTARASSIGN;} +"//=" { return DOUBLESLASHASSIGN;} +"^=" { return HATASSIGN;} +"&=" { return BOOLANDASSIGN;} +"|=" { return BOOLORASSIGN;} +">>=" { return SHIFTRIGHTASSIGN;} +"<<=" { return SHIFTLEFTASSIGN;} +"@=" { return ATASSIGN; } + +"@" { return AT;} +"(" { return LPAREN; } +")" { return RPAREN; } +"[" { return LSQPAREN;} +"]" { return RSQPAREN;} +"{" { return LCURPAREN;} +"}" { return RCURPAREN;} +";" { return SEMICOLON; } +":" { return COLON; } +"," { return COMMA; } +"." { return DOT; } +"..." { return THREEDOTS;} + +"async" { return ASYNC;} +"def" { return DEF; } +"and" { return AND;} +"or" { return OR;} +"not" { return NOT;} +"in" { return IN;} +"not in" { return NOTIN;} +"is" { return IS;} +"is not" { return ISNOT;} +"await" { return AWAIT;} +"None" { return NONE;} +"True" { return TRUE;} +"False" { return FALSE;} +"class" { return CLASS;} +"for" { return FOR;} +"if" { return IF;} +"elif" { return ELIF;} +"else" { return ELSE;} +"except" { return EXCEPT;} +"from" { return FROM;} +"finally" { return FINALLY;} +"lambda" { return LAMBDA;} +"yield" { return YIELD;} +"as" { return AS;} +"assert" { return ASSERT;} +"break" { return BREAK;} +"continue" { return CONTINUE;} +"del" { return DEL;} +"global" { return GLOBAL;} +"import" { return IMPORT;} +"nonlocal" { return NONLOCAL;} +"pass" { return PASS;} +"raise" { return RAISE;} +"return" { return RETURN;} +"try" { return TRY;} +"while" { return WHILE;} +"with" { return WITH;} + +{letter}({letter}|{digit})* { + strcpy(yylval.Lexeme, strdup(yytext)); +return NAME; } -- cgit v1.3.1-26-gb76a