Repository navigation
Expand file tree
/
Copy pathLexer.l
More file actions
135 lines (113 loc) · 3.58 KB
/
Copy pathLexer.l
File metadata and controls
135 lines (113 loc) · 3.58 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
%{
#include <cstring>
#include <cstdio>
#include <vector>
class BisonRules_Rule;
class BisonRule_Rule;
class DerivationRules_Rule;
class Symbols_Rule;
class Symbol_Rule;
class BisonDeclarations_Rule;
class BisonDeclaration_Rule;
class SymTableDeclaration_Rule;
class SymTableInput_Rule;
class SymTableOutput_Rule;
class SymTableNotEqual_Rule;
class StartDeclaration_Rule;
class TokenDeclaration_Rule;
class TokenList_Rule;
class Token_Rule;
class TypeDeclaration_Rule;
class LexModes_Rule;
class LexMode_Rule;
class LexRules_Rule;
class LexRule_Rule;
class LexCommand_Rule;
#include "Parser.tab.hpp"
#include <string>
#include <vector>
#include <iostream>
#define COPY_STRING_BUFFER yylval.string = new char[stringBuffer.size()]; strcpy(yylval.string,stringBuffer.c_str());
#define COUNT character += yyleng;
#define LINE line++; character = 1;
#define RETURN(x) lastToken = x; return x;
using namespace std;
string stringBuffer;
int line = 1;
int character = 1;
int lastToken = -1;
int mode = 0;
int codeInsertionType = -1; // CODE_INSERTION or PCNT_CODE_INSERTION, or -1 for neither
void lexRulesMode();
extern "C"
{
int yywrap()
{
return 1;
}
}
%}
%x STRING_STATE
%x CODE_INSERTION_STATE
%x C_COMMENT_STATE
%x REGEX_MODE
%x REGEX_MODE_CHARACTER
%s LEX_MODES_STATE
%%
/*
Code insertion parsing
*/
<INITIAL>"%{" BEGIN(CODE_INSERTION_STATE); codeInsertionType = PCNT_CODE_INSERTION; stringBuffer.clear();COUNT
<CODE_INSERTION_STATE>\{ BEGIN(CODE_INSERTION_STATE); codeInsertionType = CODE_INSERTION; stringBuffer.clear();COUNT
<CODE_INSERTION_STATE>"%}" BEGIN(mode); COPY_STRING_BUFFER;COUNT RETURN(codeInsertionType);
<CODE_INSERTION_STATE>\} BEGIN(mode); COPY_STRING_BUFFER;COUNT RETURN(codeInsertionType);
<CODE_INSERTION_STATE>. stringBuffer.push_back(yytext[0]);COUNT
<CODE_INSERTION_STATE>\n stringBuffer.push_back('\n');LINE
/*
Literal string parsing
*/
<INITIAL>\" BEGIN(STRING_STATE);stringBuffer.clear();COUNT
<STRING_STATE>\" BEGIN(mode); COPY_STRING_BUFFER;COUNT RETURN (STRING);
<STRING_STATE>\\["\\"''] stringBuffer.push_back('\a');COUNT
<STRING_STATE>\\a stringBuffer.push_back('\a');COUNT
<STRING_STATE>\\b stringBuffer.push_back('\a');COUNT
<STRING_STATE>\\f stringBuffer.push_back('\a');COUNT
<STRING_STATE>[^\"\n\\]+ stringBuffer.append(yytext);COUNT
/*
C-style comment parsing
*/
<INITIAL>\/\* BEGIN(C_COMMENT_STATE);
<C_COMMENT_STATE>[^*/\n]
<C_COMMENT_STATE>\*
<C_COMMENT_STATE>\/
<C_COMMENT_STATE>\*\/ BEGIN(mode);
<C_COMMENT_STATE>\n LINE
<REGEX_MODE>;\n BEGIN(LEX_MODES_STATE); COUNT LINE RETURN(';');
<REGEX_MODE>^.+ lexRulesMode(); yylval.string = new char[yyleng]; strcpy(yylval.string,yytext);COUNT LINE RETURN(REGULAR_EXPRESSION);printf(yytext);
<REGEX_MODE>\n
\/\/.*\n LINE // C++-style comment
\'[^\n\\]\' yylval.character = yytext[1];COUNT RETURN (CHARACTER); // Character literal
"%%" COUNT RETURN (PCNT_PCNT);
"%token" COUNT RETURN (PCNT_TOKEN);
"%type" COUNT RETURN (PCNT_TYPE);
"%union" COUNT RETURN (PCNT_UNION);
"%start" COUNT RETURN (PCNT_START);
"%symtable" COUNT RETURN (PCNT_SYM_TABLE);
[a-zA-Z_][a-zA-Z0-9_]* yylval.string = new char[yyleng+5]; memcpy(yylval.string,yytext,yyleng); yylval.string[yyleng] = '\0'; COUNT RETURN (ID);
<INITIAL,LEX_MODES_STATE>[{}] COUNT RETURN (yytext[0]);
[;:|<>*{}] COUNT RETURN (yytext[0]);
\<< COUNT RETURN (LEFT_SHIFT);
>> COUNT RETURN (RIGHT_SHIFT);
!= COUNT RETURN (NOT_EQUAL);
\n LINE
[\ \t] COUNT
%%
void lexRulesMode()
{
BEGIN(LEX_MODES_STATE);
mode = LEX_MODES_STATE;
}
void regexMode()
{
BEGIN(REGEX_MODE);
}