Related articles |
---|
JLex zumanoZ@hotmail.com (2002-05-12) |
Re: JLex jpeeler@tampabay.rr.com (JoAnn Peeler) (2002-05-13) |
JLex anton.vahcic@snet.fri.uni-lj.si (Anton Vahcic, ml.) (1999-05-22) |
From: | "Anton Vahcic, ml." <anton.vahcic@snet.fri.uni-lj.si> |
Newsgroups: | comp.compilers |
Date: | 22 May 1999 03:00:26 -0400 |
Organization: | RCU, University of Ljubljana, Slovenia |
Keywords: | lex, Java, question |
Question for JLex (lexer).
How do I use - in other words - what do I have for
<YYINITIAL>?
My code (it alfa version!):
-------------
import java.lang.System;
import java_cup.runtime.*;
import Common.*;
class Utility
{
public static void assert(boolean expr)
{
if (false == expr)
{
throw (new Error("Error: Assertion failed."));
}
}
private static final String errorMsg[] =
{
"Error: Unmatched end-of-comment punctuation.",
"Error: Unmatched start-of-comment punctuation.",
"Error: Unclosed string.",
"Error: Illegal character."
};
public static final int E_ENDCOMMENT = 0;
public static final int E_STARTCOMMENT = 1;
public static final int E_UNCLOSEDSTR = 2;
public static final int E_UNMATCHED = 3;
public static void error(int code)
{
System.out.println(errorMsg[code]);
}
}
class Yytoken
{
Yytoken(int index,String text,int line,int charBegin,int charEnd)
{
m_index = index;
m_text = new String(text);
m_line = line;
m_charBegin = charBegin;
m_charEnd = charEnd;
}
public int m_index;
public String m_text;
public int m_line;
public int m_charBegin;
public int m_charEnd;
}
%%
%{
private int comment_count = 0;
public String sourceFilename;
%}
%line
%char
%state COMMENT
%notunix
%cup
/* literal keyword tokens */
PROGRAM { return(new
Yytoken(Common.PROGRAM,yytext(),yyline,yychar,yychar+1)); };
PROCEDURE { return(new
Yytoken(Common.PROCEDURE,yytext(),yyline,yychar,yychar+1)); }
CONST { return(new
Yytoken(Common.CONST,yytext(),yyline,yychar,yychar+1)); }
VAR { return(new Yytoken(Common.VAR,yytext(),yyline,yychar,yychar+1)); }
IF { return(new Yytoken(Common.IF,yytext(),yyline,yychar,yychar+1)); }
CALL { return(new Yytoken(Common.CALL,yytext(),yyline,yychar,yychar+1));
}
THEN { return(new Yytoken(Common.THEN,yytext(),yyline,yychar,yychar+1));
}
BEGIN { return(new
Yytoken(Common.BEGIN,yytext(),yyline,yychar,yychar+1)); }
END { return(new Yytoken(Common.END,yytext(),yyline,yychar,yychar+1)); }
WHILE { return(new
Yytoken(Common.WHILE,yytext(),yyline,yychar,yychar+1)); }
DO { return(new Yytoken(Common.DO,yytext(),yyline,yychar,yychar+1)); }
ODD { return(new Yytoken(Common.ODD,yytext(),yyline,yychar,yychar+1)); }
NOT { return(new Yytoken(Common.NOT,yytext(),yyline,yychar,yychar+1)); }
/* punctuation */
"=" |
"#" |
"<" |
">" { return(new
Yytoken(Common.COMPARISON,yytext(),yyline,yychar,yychar+1)); }
/* names */
[A-Za-z][A-Za-z0-9_]* { return(new
Yytoken(Common.NAME,yytext(),yyline,yychar,yychar+1)); }
/* number */
[0-9]+ { return(new
Yytoken(Common.NUMBER,yytext(),yyline,yychar,yychar+1)); }
\n yyline++;
[ \n\r]+; /*white space */
/* anything else */
. { return(new Yytoken(Common.ERROR,yytext(),yyline,yychar,yychar+1)); }
%%
<YYINITIAL> { System.out.print("test"); }
--- what now? ---
Return to the
comp.compilers page.
Search the
comp.compilers archives again.