Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cup.example;
- import java_cup.runtime.ComplexSymbolFactory;
- import java_cup.runtime.ComplexSymbolFactory.Location;
- import java_cup.runtime.Symbol;
- import java.lang.*;
- import java.io.InputStreamReader;
- %%
- %class Lexer
- %implements sym
- %public
- %unicode
- %line
- %column
- %cup
- %char
- %{
- public Lexer(ComplexSymbolFactory sf, java.io.InputStream is){
- this(is);
- symbolFactory = sf;
- }
- public Lexer(ComplexSymbolFactory sf, java.io.Reader reader){
- this(reader);
- symbolFactory = sf;
- }
- private StringBuffer sb;
- private ComplexSymbolFactory symbolFactory;
- private int csline,cscolumn;
- public Symbol symbol(String name, int code){
- return symbolFactory.newSymbol(name, code,
- new Location(yyline+1,yycolumn+1, yychar), // -yylength()
- new Location(yyline+1,yycolumn+yylength(), yychar+yylength())
- );
- }
- public Symbol symbol(String name, int code, String lexem){
- return symbolFactory.newSymbol(name, code,
- new Location(yyline+1, yycolumn +1, yychar),
- new Location(yyline+1,yycolumn+yylength(), yychar+yylength()), lexem);
- }
- protected void emit_warning(String message){
- System.out.println("scanner warning: " + message + " at : 2 "+
- (yyline+1) + " " + (yycolumn+1) + " " + yychar);
- }
- protected void emit_error(String message){
- System.out.println("scanner error: " + message + " at : 2" +
- (yyline+1) + " " + (yycolumn+1) + " " + yychar);
- }
- %}
- /* comments */
- Comment = {TraditionalComment} | {EndOfLineComment}
- TraditionalComment = "/*" {CommentContent} \*+ "/"
- EndOfLineComment = "//" [^\r\n]* {Newline}
- CommentContent = ( [^*] | \*+[^*/] )*
- ident = ([:jletter:] | "_" ) ([:jletterdigit:] | [:jletter:] | "_" )*
- %eofval{
- return symbolFactory.newSymbol("EOF",sym.EOF);
- %eofval}
- %state CODESEG
- Newline = \r | \n | \r\n
- Whitespace = [ \t\f] | {Newline}
- Number = [0-9]+
- Boolean = (true)|(false)
- String = \"[^\"\n]*\"|\'[^\'\n]*\'
- Identifier = [a-zA-Z]+
- %%
- <YYINITIAL> {
- {Number} { return symbolFactory.newSymbol("NUMBER", NUMBER, Integer.parseInt(yytext())); }
- {Boolean} { return symbolFactory.newSymbol("BOOLEAN", BOOLEAN, Boolean.parseBoolean(yytext())); }
- {String} { return symbolFactory.newSymbol("STRING", STRING, yytext()); }
- {Identifier} { return symbolFactory.newSymbol("IDENTIFIER", IDENTIFIER, yytext()); }
- "{" { return symbolFactory.newSymbol("LCB", LCB); }
- "}" { return symbolFactory.newSymbol("RCB", RCB); }
- "[" { return symbolFactory.newSymbol("LSQB", LSQB); }
- "]" { return symbolFactory.newSymbol("RSQB", RSQB); }
- "," { return symbolFactory.newSymbol("COMMA", COMMA); }
- ":" { return symbolFactory.newSymbol("COLON", COLON); }
- }
- // error fallback
- .|\n { emit_warning("Unrecognized character '" +yytext()+"' -- ignored"); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement