Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81350 / parser / lexer.lex
diff options
context:
space:
mode:
Commitb98a9799f15c8ad0275175a1d8d013c4f6e0176f (patch)
Author
 Ralitsa Dardjonova <dardzhonov@uni-sofia.bg>
2018-11-28 12:15:14 +0200
Committer
 dtrendafilov <trendafilov.dn@gmail.com>
2018-11-28 12:15:14 +0200
Filesd60c012699647adbcd20065703b0e58abb236fed /homework/81350/parser/lexer.lex
IPL-b98a9799f15c8ad0275175a1d8d013c4f6e0176f.tar
IPL-b98a9799f15c8ad0275175a1d8d013c4f6e0176f.tar.gz
IPL-b98a9799f15c8ad0275175a1d8d013c4f6e0176f.zip
Parent3e5cf5941db07bcd3527a73b62077d5277d57f2d (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
1 files changed, 182 insertions, 0 deletions
-rw-r--r--homework/81350/parser/lexer.lex182
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; }