-
Notifications
You must be signed in to change notification settings - Fork 0
/
janiec_bylica.l
92 lines (69 loc) · 5.7 KB
/
janiec_bylica.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
%{
#include <string.h>
#include <stdio.h>
#include "janiec_bylica.tab.hpp"
//for easier string management. (I would rather like to use std::string in %union but it wont compile)
const char* newyytext(const char*);
int curly_bracket_counter=0;
void yyerror(const char *s);
%}
%option yylineno
WS [ \t]
NUM (0|0x)?[0-9]+
ID [A-Za-z_$][A-Za-z_$0-9]*
TYPE void|char|short|int|long|float
EXTYPE struct|union
ALL .|[\n]
POINTER [*]+
%x _BODY _STRING _LINE_COMMENT _C_COMMENT
%x _EXTYPE _OUTER_LINE_COMMENT _OUTER_C_COMMENT
%%
<INITIAL>{TYPE} {yylval.string=newyytext(yytext); return DECL_SPECIFIER; }
<INITIAL>{EXTYPE} {BEGIN(_EXTYPE); yymore(); }
<INITIAL>{NUM} {yylval.string=newyytext(yytext); return NUM; }
<INITIAL>{ID} {yylval.string=newyytext(yytext); return ID; }
<INITIAL>({WS}|\n)+ {} //eat-up white spaces
<_EXTYPE>{WS}+{ID} {yylval.string=newyytext(yytext); BEGIN(INITIAL); return DECL_SPECIFIER; }
<INITIAL>"(" {return LEFT_PARENTHESIS; }
<INITIAL>")" {return RIGHT_PARENTHESIS; }
<INITIAL>"[" {return LEFT_BRACKET; }
<INITIAL>"]" {return RIGHT_BRACKET; }
<INITIAL>"," {return COMMA; }
<INITIAL>";" {return SEMICOLON; }
<INITIAL>{POINTER} {yylval.string=newyytext(yytext); return POINTER; }
/* Comments outside a function (not necessery )*/
<INITIAL>"//" {BEGIN(_OUTER_LINE_COMMENT); }
<INITIAL>"/\*" {BEGIN(_OUTER_C_COMMENT); }
<_OUTER_C_COMMENT>{ALL} {; }
<_OUTER_C_COMMENT>"*/" {BEGIN(INITIAL);}
<_OUTER_LINE_COMMENT>. {; }
<_OUTER_LINE_COMMENT>\n {BEGIN(INITIAL);}
/*Body handling just cunting curly brackets. Excpet those one that are in comment or string */
<INITIAL>"}" {/* error */}
<INITIAL>"{" {++curly_bracket_counter; yymore(); BEGIN(_BODY); }
<_BODY>"{" {++curly_bracket_counter; yymore(); }
<_BODY>"}" {--curly_bracket_counter; yymore(); if(curly_bracket_counter==0){BEGIN(INITIAL); yylval.string=newyytext(yytext);return BODY; } }
<_BODY>"'\"'" {yymore(); }
<_BODY>"\"" {yymore(); BEGIN(_STRING); }
<_BODY>"//" {yymore(); BEGIN(_LINE_COMMENT); }
<_BODY>"/\*" {yymore(); BEGIN(_C_COMMENT); }
<_BODY>{ALL} {yymore(); }
<_STRING>"\"" {yymore(); BEGIN(_BODY); }
<_STRING>. {yymore(); }
<_STRING>"\\\"" {yymore(); }
<_C_COMMENT>{ALL} {yymore(); }
<_C_COMMENT>"*/" {yymore(); BEGIN(_BODY); }
<_LINE_COMMENT>. {yymore(); }
<_LINE_COMMENT>\n {yymore(); BEGIN(_BODY); }
/*ERROR*/
<*>{ALL} {
char buffer[1024];
sprintf(buffer,"ERROR: Lexing error at line: %d nears token \'%s\'.\n",yylineno,yytext);
yyerror(buffer);
}
%%
const char* newyytext(const char* txt){
char *t=new char[strlen(txt)+1];
strcpy(t,txt);
return t;
}