-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgml.flex
152 lines (130 loc) · 4.73 KB
/
sgml.flex
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Alex Lapena
29 January 2017
File Name: sgml.flex
*/
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.String.*;
%%
%class Lexer
%type Token
%line
%column
LineTerminator = \r|\n|\r\n
delimeters = {LineTerminator} | [ \t\f]
openTag = <[a-zA-Z0-9 ]+[a-zA-Z0-9 \"=]*>
closeTag = <\/[a-zA-Z0-9 ]+>
word = [0-9]*[a-zA-Z]+[0-9]*(\b)*
number = \+?\-?[0-9]+\.?[0-9]*
apostrophized = [a-zA-Z]+("'"[a-zA-Z]*)+
hyphenated = [a-zA-Z0-9]+("-"[a-zA-Z0-9]+)+
punctuations = [^\w\s\n ]
%{
List<String> rTag = Arrays.asList("text", "date", "doc", "docno", "headline", "length", "p");
Stack<String> globalStack = new Stack<String>();
List<String> errs = new ArrayList<String>();
public String baseTag = "";
String str, str2;
public int relevency = 0;
private void stackManager(String closer, int line)
{
if (globalStack.isEmpty()) {
errs.add("Error: Closing tag \"" + closer + "\" at line " + line + " is unexpected.");
return;
}
if (closer.equalsIgnoreCase(globalStack.peek())) {
globalStack.pop();
}
else {
errs.add("Error: Expected closing tag \""+ globalStack.peek() + "\" at line " + line + " but recieved \"" + closer +"\".");
}
}
private boolean isRelevent(String tag) {
//find a way to filter out tags until you can pop everything off to be relevent
if (rTag.contains(tag.toLowerCase())) {
relevency = 0;
return true;
}
else {
baseTag = tag;
relevency = 1;
return false;
}
}
public void checkIrrNest(String closeBase)
{
if (closeBase.equalsIgnoreCase(baseTag)) {
baseTag = "";
relevency = 0;
}
}
%}
%eofval{
for (int i = 0; i < errs.size(); i++) {
System.err.println(errs.get(i));
}
return null;
%eofval};
%%
{openTag} {
// Identifies tag name and pushes onto global stack
str = new String(yytext());
Pattern pattern = Pattern.compile("<(.*?)>");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
//Remove tag attributes
str2 = new String(matcher.group(1).replaceAll("\\s+", ""));
if (relevency == 0) {
if (isRelevent(str2)) {
globalStack.push(str2);
return new Token(Token.OPEN, str2, yyline, yycolumn);
}
}
}
}
{closeTag} {
// Identifies closing tag name and sends to stackManager() to compare
str = new String(yytext());
Pattern pattern = Pattern.compile("<(.*?)>");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
str2 = new String(matcher.group(1).replaceAll("[/\\s+]", ""));
if(relevency == 0) {
if (isRelevent(str2)) {
stackManager(str2, yyline);
return new Token(Token.CLOSE, str2, yyline,yycolumn);
}
}
else {
checkIrrNest(str2);
}
}
}
{word} {
if (relevency == 0) {
return new Token(Token.WORD, yytext(), yyline, yycolumn);
}
}
{number} {
if (relevency == 0) {
return new Token(Token.NUMBER, yytext(), yyline, yycolumn);
}
}
{punctuations} {
if (relevency == 0) {
return new Token(Token.PUNCTUATIONS, yytext(), yyline, yycolumn);
}
}
{apostrophized} {
if (relevency == 0) {
return new Token(Token.APOSTROPHIZED, yytext(), yyline, yycolumn);
}
}
{hyphenated} {
if (relevency == 0) {
return new Token(Token.HYPHENATED, yytext(), yyline, yycolumn);
}
}
{delimeters}* { }