Ornek Scanner Parser
Ornek Scanner Parser
Ornek Scanner Parser
//----------------------------------------------------
// The following code was generated by CUP v0.10g
// Mon Feb 15 15:20:24 CST 1999
//----------------------------------------------------
/*
*
* JLex lexical specification file for JUIC ("Java at UIC" ) language.
* (Class solution)
*/
import java.io.*;
import java.lang.*;
import java_cup.runtime.*;
import sym;
%%
/***** directives to JLEX *****/
// name of generated class will be "Lexer"
%class Lexer
return s;
}
// called by main program to init Lexer
public void init( )
{
prev_EOL_yychar = 0;
comment_nesting = 0;
}
%}
/***** macros: equate a name with a RE *****/
anybutnl .
digit [0-9]
char [a-zA-Z]
id ([a-zA-Z][a-zA-Z0-9_]*)
/***** Here are the RE token definitions and associated semantic actions... *****/
/***** NOTE: at this point, all tokens longer than 1 char must be prefixed with ****/
/***** the state <YYINITIAL> so as not to conflict with the COMMENT state.***/
%state COMMENT
%%
<COMMENT>"/*" { this.comment_nesting++; }
<COMMENT>"*/" {
this.comment_nesting--;
if (this.comment_nesting == 0)
yybegin(YYINITIAL);
}
<COMMENT>. { /* ignore comment char */ }
<COMMENT>\n|\r {
/* ignore, but count newlines for proper token locating */
newline();
}
"/*" {
this.comment_nesting = 1;
yybegin(COMMENT);
}
<YYINITIAL>"//"[^\n\r]* { /* one-line Java comment -- ignore everything to EOL */ }
/* strip off the leading "import ", return just the path... */
value = value.substring(6); // strip off "import"
value = value.replace('\t', ' '); // convert \t -> spaces for deletion
value = value.trim(); // trim spaces from front (and back)
<YYINITIAL>{id} { /* identifier */
String value;
:};
/*
* code to include in the generated Parser
*/
parser code {:
// custom constructor
public Parser(Lexer lexer)
{
this(); // apply default Parser constructor
this.lexer = lexer;
}
// override default syntax error reporting method
public void syntax_error(java_cup.runtime.Symbol token)
{
System.err.print("**Syntax error at line " + token.left
+ ", column " + token.right + ": ");
System.err.print("token #" + token.sym);
if (token.value != null)
System.err.print(" (value='" + token.value + "').");
System.err.println();
System.err.println();
System.err.println("Halting...");
}
:};
/*
* connect generated parser with our Lexer
*/
/*
* terminal symbols returned by Lexer...
*/
/* keywords */
terminal ABSTRACT, BOOLEAN, CLASS, ELSE, EXTENDS, EXTERN, FALSE, FINAL, IF, IN;
terminal INSTANCEOF, INTEGER, MAIN, NEW, NULL, PROTECTED, PUBLIC, RETURN, STATIC;
terminal SUPER, SYNCHRONIZED, THIS, TRUE, VOID, WHILE;
/* punctuation */
terminal SEMI, COMMA, DOT, LPAREN, RPAREN, LBRACE, RBRACE, LBRACKET, RBRACKET;
terminal EQUAL, OR, AND, NOT, EQEQ, NE, LT, LE, GT, GE, PLUS, MINUS, TIMES, DIV, MOD, EXP;
class ::= PUBLIC CLASS ID extension LBRACE fields constructor methods RBRACE
| PUBLIC FINAL CLASS ID extension LBRACE fields constructor methods RBRACE
| PUBLIC ABSTRACT CLASS ID extension LBRACE fields constructor methods RBRACE
;
type ::= ID
| BOOLEAN
| INTEGER
;
id_list ::= ID
| ID COMMA id_list
;
constructor ::= PUBLIC ID LPAREN formals RPAREN LBRACE super_call stmts RBRACE
;
postfixexpression ::= ID
| primaryexpression
;
receiver ::= ID
| primaryexpression
| SUPER
;
/*
* Name: Joe Hummel
* SS# (last 4 digits): Professor
*
* EECS 473, Compiler Design
* U. of Illinois, Chicago
* Spring 1999
*
* Main program for JUIC ("Java at UIC") compiler.
*
* To run: java Juic file1.juic [file2.juic file3.juic ...]
*/
import java.io.*;
import java.lang.*;
import Lexer;
import Parser;
// Note: use of "static" below enables access from other classes via the
// use of the class name, e.g. Juic.filename (this is necessary since
// other objects won't necessarily have a reference to the actual Juic
// object created at startup).
/*
* Make sure user supplied at least one file to compile
*/
if (argv.length == 0) {
System.err.println("**Error: usage is 'java Juic file1.juic [file2.juic ...]'.");
System.err.println();
System.err.println("Halting...");
return;
}//then
/*
* For each file, parse...
*/
syntax_error = false;
}//for
/*
* Done parsing --- if a syntax error occurred, exit now...
*/
if (syntax_error) {
try {
in.close(); // close that last file...
}
catch (IOException e) { /* ignore */ }
return;
}//then
/*
* That's it, we're done!
*/
System.out.println();
System.out.println("done.");
}
public static InputStream openInputFile(String filename)
{
InputStream in;
return in;
}
public static PrintStream openOutputFile(String filename)
{
PrintStream out;
int loc;
String outname;
return out;
}