namespace YyNameSpace
{
using System;
using System.Text;
class Sample
{
public static void Main(String[] argv)
{
String [] args = Environment.GetCommandLineArgs();
System.IO.FileStream f = new System.IO.FileStream(args[1], System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read, 8192);
Yylex yy = new Yylex(f);
LexToken t;
while ((t = yy.yylex()) != null && t.token_type!="EOF")
Console.WriteLine(t.complete_info());
}
}
class Utility {
public static void assert
(
bool expr
)
{
if (!expr) Console.WriteLine("Assertion Failed");
}
private static String[] errorMsg = new String[]
{
"Error: Unmatched end-of-comment punctuation.",
"Error: Unmatched start-of-comment punctuation.",
"Error: Unclosed string.",
"Error: Illegal character."
};
public const int E_ENDCOMMENT = 0;
public const int E_STARTCOMMENT = 1;
public const int E_UNCLOSEDSTR = 2;
public const int E_UNMATCHED = 3;
public static void error
(
int code
)
{
Console.WriteLine(errorMsg[code]);
}
}
internal class Yylex
{
private const int YY_BUFFER_SIZE = 512;
private const int YY_F = -1;
private const int YY_NO_STATE = -1;
private const int YY_NOT_ACCEPT = 0;
private const int YY_START = 1;
private const int YY_END = 2;
private const int YY_NO_ANCHOR = 4;
delegate LexToken AcceptMethod();
AcceptMethod[] accept_dispatch;
private const int YY_BOL = 128;
private const int YY_EOF = 129;
private static int comment_count = 0;
private static int line_char = 0;
private System.IO.TextReader yy_reader;
private int yy_buffer_index;
private int yy_buffer_read;
private int yy_buffer_start;
private int yy_buffer_end;
private char[] yy_buffer;
private int yychar;
private int yyline;
private bool yy_at_bol;
private int yy_lexical_state;
internal Yylex(System.IO.TextReader reader) : this()
{
if (null == reader)
{
throw new System.ApplicationException("Error: Bad input stream initializer.");
}
yy_reader = reader;
}
internal Yylex(System.IO.FileStream instream) : this()
{
if (null == instream)
{
throw new System.ApplicationException("Error: Bad input stream initializer.");
}
yy_reader = new System.IO.StreamReader(instream);
}
private Yylex()
{
yy_buffer = new char[YY_BUFFER_SIZE];
yy_buffer_read = 0;
yy_buffer_index = 0;
yy_buffer_start = 0;
yy_buffer_end = 0;
yychar = 0;
yyline = 0;
yy_at_bol = true;
yy_lexical_state = YYINITIAL;
accept_dispatch = new AcceptMethod[]
{
null,
null,
new AcceptMethod(this.Accept_2),
new AcceptMethod(this.Accept_3),
new AcceptMethod(this.Accept_4),
new AcceptMethod(this.Accept_5),
new AcceptMethod(this.Accept_6),
new AcceptMethod(this.Accept_7),
new AcceptMethod(this.Accept_8),
new AcceptMethod(this.Accept_9),
new AcceptMethod(this.Accept_10),
new AcceptMethod(this.Accept_11),
new AcceptMethod(this.Accept_12),
new AcceptMethod(this.Accept_13),
new AcceptMethod(this.Accept_14),
new AcceptMethod(this.Accept_15),
new AcceptMethod(this.Accept_16),
new AcceptMethod(this.Accept_17),
new AcceptMethod(this.Accept_18),
new AcceptMethod(this.Accept_19),
new AcceptMethod(this.Accept_20),
null,
new AcceptMethod(this.Accept_22),
new AcceptMethod(this.Accept_23),
new AcceptMethod(this.Accept_24),
new AcceptMethod(this.Accept_25),
new AcceptMethod(this.Accept_26),
new AcceptMethod(this.Accept_27),
null,
new AcceptMethod(this.Accept_29),
new AcceptMethod(this.Accept_30),
new AcceptMethod(this.Accept_31),
null,
new AcceptMethod(this.Accept_33),
new AcceptMethod(this.Accept_34),
null,
new AcceptMethod(this.Accept_36),
null,
new AcceptMethod(this.Accept_38),
};
}
LexToken Accept_2()
{ { return null; }
}
LexToken Accept_3()
{ { line_char = yychar+yytext().Length; return null; }
}
LexToken Accept_4()
{ { return (new LexToken("COMMA",yytext(),yyline,yychar-line_char,yychar)); }
}
LexToken Accept_5()
{ { return new LexToken("SEMICOLON",yytext(),yyline,yychar-line_char,yychar); }
}
LexToken Accept_6()
{ { return new LexToken("LPREN",yytext(),yyline,yychar-line_char,yychar); }
}
LexToken Accept_7()
{ { return new LexToken("RPAREN",yytext(),yyline,yychar-line_char,yychar); }
}
LexToken Accept_8()
{ { return null; }
}
LexToken Accept_9()
{ {
StringBuilder sb = new StringBuilder("Illegal character: <");
String s = yytext();
for (int i = 0; i < s.Length; i++)
if (s[i] >= 32)
sb.Append(s[i]);
else
{
sb.Append("^");
sb.Append(Convert.ToChar(s[i]+'A'-1));
}
sb.Append(">");
Console.WriteLine(sb.ToString());
Utility.error(Utility.E_UNMATCHED);
return null;
}
}
LexToken Accept_10()
{ {
String str = yytext().Substring(1,yytext().Length);
Utility.error(Utility.E_UNCLOSEDSTR);
return new LexToken("Unclosed String",str,yyline,yychar-line_char,yychar);
}
}
LexToken Accept_11()
{ {
return new LexToken("Integer",Int64.Parse(yytext()),yyline,yychar-line_char,yychar);
}
}
LexToken Accept_12()
{ {
return new LexToken("Alphanumeric",yytext(),yyline,yychar-line_char,yychar);
}
}
LexToken Accept_13()
{ { yybegin(COMMENT); comment_count = comment_count + 1; return null;
}
}
LexToken Accept_14()
{ {
return new LexToken("StrLit",yytext(),yyline,yychar-line_char,yychar);
}
}
LexToken Accept_15()
{ {
return new LexToken("Hexnum",Convert.ToInt32(yytext(),16),yyline,yychar-line_char,yychar);
}
}
LexToken Accept_16()
{ {
return new LexToken("Float",Convert.ToDouble(yytext()),yyline,yychar-line_char,yychar);
}
}
LexToken Accept_17()
{ { line_char=yychar+yytext().Length; return null; }
}
LexToken Accept_18()
{ { return null; }
}
LexToken Accept_19()
{ { comment_count = comment_count + 1; return null;
}
}
LexToken Accept_20()
{ {
comment_count = comment_count - 1;
if (comment_count == 0) {
yybegin(YYINITIAL);
}
return null;
}
}
LexToken Accept_22()
{ { return null; }
}
LexToken Accept_23()
{ { line_char = yychar+yytext().Length; return null; }
}
LexToken Accept_24()
{ {
StringBuilder sb = new StringBuilder("Illegal character: <");
String s = yytext();
for (int i = 0; i < s.Length; i++)
if (s[i] >= 32)
sb.Append(s[i]);
else
{
sb.Append("^");
sb.Append(Convert.ToChar(s[i]+'A'-1));
}
sb.Append(">");
Console.WriteLine(sb.ToString());
Utility.error(Utility.E_UNMATCHED);
return null;
}
}
LexToken Accept_25()
{ {
return new LexToken("Integer",Int64.Parse(yytext()),yyline,yychar-line_char,yychar);
}
}
LexToken Accept_26()
{ {
return new LexToken("StrLit",yytext(),yyline,yychar-line_char,yychar);
}
}
LexToken Accept_27()
{ { return null; }
}
LexToken Accept_29()
{ { return null; }
}
LexToken Accept_30()
{ {
StringBuilder sb = new StringBuilder("Illegal character: <");
String s = yytext();
for (int i = 0; i < s.Length; i++)
if (s[i] >= 32)
sb.Append(s[i]);
else
{
sb.Append("^");
sb.Append(Convert.ToChar(s[i]+'A'-1));
}
sb.Append(">");
Console.WriteLine(sb.ToString());
Utility.error(Utility.E_UNMATCHED);
return null;
}
}
LexToken Accept_31()
{ { return null; }
}
LexToken Accept_33()
{ {
StringBuilder sb = new StringBuilder("Illegal character: <");
String s = yytext();
for (int i = 0; i < s.Length; i++)
if (s[i] >= 32)
sb.Append(s[i]);
else
{
sb.Append("^");
sb.Append(Convert.ToChar(s[i]+'A'-1));
}
sb.Append(">");
Console.WriteLine(sb.ToString());
Utility.error(Utility.E_UNMATCHED);
return null;
}
}
LexToken Accept_34()
{ { return null; }
}
LexToken Accept_36()
{ {
StringBuilder sb = new StringBuilder("Illegal character: <");
String s = yytext();
for (int i = 0; i < s.Length; i++)
if (s[i] >= 32)
sb.Append(s[i]);
else
{
sb.Append("^");
sb.Append(Convert.ToChar(s[i]+'A'-1));
}
sb.Append(">");
Console.WriteLine(sb.ToString());
Utility.error(Utility.E_UNMATCHED);
return null;
}
}
LexToken Accept_38()
{ {
String str = yytext().Substring(1,yytext().Length);
Utility.error(Utility.E_UNCLOSEDSTR);
return new LexToken("Unclosed String",str,yyline,yychar-line_char,yychar);
}
}
private const int YYINITIAL = 0;
private const int COMMENT = 1;
private static int[] yy_state_dtrans = new int[]
{ 0,
18
};
private void yybegin (int state)
{
yy_lexical_state = state;
}
private char yy_advance ()
{
int next_read;
int i;
int j;
if (yy_buffer_index < yy_buffer_read)
{
return yy_buffer[yy_buffer_index++];
}
if (0 != yy_buffer_start)
{
i = yy_buffer_start;
j = 0;
while (i < yy_buffer_read)
{
yy_buffer[j] = yy_buffer[i];
i++;
j++;
}
yy_buffer_end = yy_buffer_end - yy_buffer_start;
yy_buffer_start = 0;
yy_buffer_read = j;
yy_buffer_index = j;
next_read = yy_reader.Read(yy_buffer,yy_buffer_read,
yy_buffer.Length - yy_buffer_read);
if (next_read <= 0)
{
return (char) YY_EOF;
}
yy_buffer_read = yy_buffer_read + next_read;
}
while (yy_buffer_index >= yy_buffer_read)
{
if (yy_buffer_index >= yy_buffer.Length)
{
yy_buffer = yy_double(yy_buffer);
}
next_read = yy_reader.Read(yy_buffer,yy_buffer_read,
yy_buffer.Length - yy_buffer_read);
if (next_read <= 0)
{
return (char) YY_EOF;
}
yy_buffer_read = yy_buffer_read + next_read;
}
return yy_buffer[yy_buffer_index++];
}
private void yy_move_end ()
{
if (yy_buffer_end > yy_buffer_start &&
'\n' == yy_buffer[yy_buffer_end-1])
yy_buffer_end--;
if (yy_buffer_end > yy_buffer_start &&
'\r' == yy_buffer[yy_buffer_end-1])
yy_buffer_end--;
}
private bool yy_last_was_cr=false;
private void yy_mark_start ()
{
int i;
for (i = yy_buffer_start; i < yy_buffer_index; i++)
{
if (yy_buffer[i] == '\n' && !yy_last_was_cr)
{
yyline++;
}
if (yy_buffer[i] == '\r')
{
yyline++;
yy_last_was_cr=true;
}
else
{
yy_last_was_cr=false;
}
}
yychar = yychar + yy_buffer_index - yy_buffer_start;
yy_buffer_start = yy_buffer_index;
}
private void yy_mark_end ()
{
yy_buffer_end = yy_buffer_index;
}
private void yy_to_mark ()
{
yy_buffer_index = yy_buffer_end;
yy_at_bol = (yy_buffer_end > yy_buffer_start) &&
(yy_buffer[yy_buffer_end-1] == '\r' ||
yy_buffer[yy_buffer_end-1] == '\n');
}
internal string yytext()
{
return (new string(yy_buffer,
yy_buffer_start,
yy_buffer_end - yy_buffer_start)
);
}
private int yylength ()
{
return yy_buffer_end - yy_buffer_start;
}
private char[] yy_double (char[] buf)
{
int i;
char[] newbuf;
newbuf = new char[2*buf.Length];
for (i = 0; i < buf.Length; i++)
{
newbuf[i] = buf[i];
}
return newbuf;
}
private const int YY_E_INTERNAL = 0;
private const int YY_E_MATCH = 1;
private static string[] yy_error_string = new string[]
{
"Error: Internal error.\n",
"Error: Unmatched input.\n"
};
private void yy_error (int code,bool fatal)
{
System.Console.Write(yy_error_string[code]);
if (fatal)
{
throw new System.ApplicationException("Fatal Error.\n");
}
}
private static int[] yy_acpt = new int[]
{
YY_NOT_ACCEPT,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NOT_ACCEPT,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NOT_ACCEPT,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NOT_ACCEPT,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NOT_ACCEPT,
YY_NO_ANCHOR,
YY_NOT_ACCEPT,
YY_NO_ANCHOR
};
private static int[] yy_cmap = new int[]
{
11, 11, 11, 11, 11, 11, 11, 11,
7, 7, 2, 11, 11, 1, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11,
7, 11, 12, 11, 11, 11, 11, 11,
5, 6, 10, 11, 3, 11, 18, 9,
15, 14, 14, 14, 14, 14, 14, 14,
14, 14, 11, 4, 11, 11, 11, 8,
11, 17, 17, 17, 17, 17, 17, 19,
19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 11, 13, 11, 11, 19,
11, 17, 17, 17, 17, 17, 17, 19,
19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19,
16, 19, 19, 11, 8, 11, 11, 11,
0, 0
};
private static int[] yy_rmap = new int[]
{
0, 1, 2, 3, 1, 1, 1, 1,
4, 5, 6, 7, 8, 1, 1, 9,
10, 1, 11, 1, 1, 2, 1, 12,
1, 13, 6, 14, 15, 14, 10, 16,
10, 17, 18, 19, 20, 21, 22
};
private static int[,] yy_nxt = new int[,]
{
{ 1, 2, 3, 4, 5, 6, 7, 8,
22, 9, 24, 24, 10, 24, 11, 25,
12, 12, 30, 12 },
{ -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1 },
{ -1, -1, 23, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1 },
{ -1, 21, 3, -1, -1, -1, -1, 8,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1 },
{ -1, -1, 8, -1, -1, -1, -1, 8,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1,
-1, 28, 13, -1, -1, -1, -1, -1,
-1, -1, -1, -1 },
{ -1, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 14, 38, 10, 10,
10, 10, 10, 10 },
{ -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, 11, 11,
-1, -1, 32, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, 12, 12,
12, 12, -1, 12 },
{ -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, 15, 15,
-1, 15, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, 16, 16,
-1, -1, -1, -1 },
{ 1, 22, 22, 27, 27, 29, 29, 27,
29, 33, 36, 27, 27, 27, 27, 27,
27, 27, 27, 27 },
{ -1, 21, 23, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, 11, 11,
15, -1, 32, -1 },
{ -1, -1, -1, 27, 27, 27, 27, 27,
27, 35, 37, 27, 27, 27, 27, 27,
27, 27, 27, 27 },
{ -1, -1, 17, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28 },
{ -1, -1, -1, 27, 27, 27, 27, 27,
27, 31, 37, 27, 27, 27, 27, 27,
27, 27, 27, 27 },
{ -1, -1, -1, 27, 27, 27, 27, 27,
27, 31, 19, 27, 27, 27, 27, 27,
27, 27, 27, 27 },
{ -1, -1, -1, 27, 27, 27, 27, 27,
27, 35, 34, 27, 27, 27, 27, 27,
27, 27, 27, 27 },
{ -1, -1, -1, 27, 27, 27, 27, 27,
27, 31, -1, 27, 27, 27, 27, 27,
27, 27, 27, 27 },
{ -1, -1, -1, 27, 27, 27, 27, 27,
27, 20, 34, 27, 27, 27, 27, 27,
27, 27, 27, 27 },
{ -1, -1, -1, 27, 27, 27, 27, 27,
27, -1, 34, 27, 27, 27, 27, 27,
27, 27, 27, 27 },
{ -1, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 26, 38, 10, 10,
10, 10, 10, 10 }
};
public LexToken yylex()
{
char yy_lookahead;
int yy_anchor = YY_NO_ANCHOR;
int yy_state = yy_state_dtrans[yy_lexical_state];
int yy_next_state = YY_NO_STATE;
int yy_last_accept_state = YY_NO_STATE;
bool yy_initial = true;
int yy_this_accept;
yy_mark_start();
yy_this_accept = yy_acpt[yy_state];
if (YY_NOT_ACCEPT != yy_this_accept)
{
yy_last_accept_state = yy_state;
yy_mark_end();
}
while (true)
{
if (yy_initial && yy_at_bol)
yy_lookahead = (char) YY_BOL;
else
{
yy_lookahead = yy_advance();
}
yy_next_state = yy_nxt[yy_rmap[yy_state],yy_cmap[yy_lookahead]];
if (YY_EOF == yy_lookahead && yy_initial)
{
return new LexToken("EOF","EOF",yyline,yychar);
}
if (YY_F != yy_next_state)
{
yy_state = yy_next_state;
yy_initial = false;
yy_this_accept = yy_acpt[yy_state];
if (YY_NOT_ACCEPT != yy_this_accept)
{
yy_last_accept_state = yy_state;
yy_mark_end();
}
}
else
{
if (YY_NO_STATE == yy_last_accept_state)
{
throw new System.ApplicationException("Lexical Error: Unmatched Input.");
}
else
{
yy_anchor = yy_acpt[yy_last_accept_state];
if (0 != (YY_END & yy_anchor))
{
yy_move_end();
}
yy_to_mark();
if (yy_last_accept_state < 0)
{
if (yy_last_accept_state < 39)
yy_error(YY_E_INTERNAL, false);
}
else
{
AcceptMethod m = accept_dispatch[yy_last_accept_state];
if (m != null)
{
LexToken tmp = m();
if (tmp != null)
return tmp;
}
}
yy_initial = true;
yy_state = yy_state_dtrans[yy_lexical_state];
yy_next_state = YY_NO_STATE;
yy_last_accept_state = YY_NO_STATE;
yy_mark_start();
yy_this_accept = yy_acpt[yy_state];
if (YY_NOT_ACCEPT != yy_this_accept)
{
yy_last_accept_state = yy_state;
yy_mark_end();
}
}
}
}
}
}
}