You are on page 1of 12

school_51

F00k6547
?) ?)
, ?)
, , , \?))
, , ... , , ,
, ,
, .
?)
, ?
?
, ?
? ?)
? )
?
?
?
?
?)
. ?
?
, ?
?
? , ?
- , ?
?
?
?
-?)
- , ?
?
?
?
, ?
...?
, ? ,
?
" ,
?)
?
, ? ??
?
, ) , , ))
, ?) ?)
, ?)))
options {
LOOKAHEAD = 1;
STATIC = false;
IGNORE_CASE = true;
}
PARSER_BEGIN(SelectParser)
import java.io.Reader;
import java.io.StringReader;
public class SelectParser {
static int beginToken = 0;
static int endToken = 0;
static StringBuffer infoPrint = new StringBuffer();
public static void main(String args[]) throws ParseException {
String query = "select * from table";
System.out.println("Introduced by request: " + query + "chars");
Reader reader = new StringReader(query);
SelectParser parser = new SelectParser(reader);
System.out.println("The length of the entered query: " + parser.Input()
+ " characters.");
}
}
PARSER_END(SelectParser)
SKIP : { "\t"| "\n"| "\r" }
TOKEN : { <SELECT : ["S","s"]["E","e"]["L","l"]["E","e"]["C","c"]["T","t"]> }
TOKEN : { <FROM : ["F","f"]["R","r"]["O","o"]["M","m"]> }
TOKEN : { <IDENTIFIER: ["A"-"Z","a"-"z","_"](["A"-"Z","a"-"z","0"-"9","_"]){1,11
}> }
TOKEN : { <SPACE: (" ")+> }
TOKEN : { <STAR: ("*")> }
int Input() :
{
Token t;
int letterCounter = 0;
int temp = 0;
int countToken = 0;
}
{
////////////////////////////////////////////////////////////////////////////////
t = <SELECT> {
beginToken = letterCounter+1;
letterCounter += t.image.length();
endToken = letterCounter;
countToken+=1;
infoPrint.setLength(0);
infoPrint.append(countToken)
.append(": Found token <SELECT>, look: \"")
.append(t.image)
.append("\" in length: ")
.append(t.image.length())
.append(" characters. Token Is located ")
.append(beginToken)
.append(" to ")
.append(endToken)
.append(" position.");
System.out.println(infoPrint);
}
////////////////////////////////////////////////////////////////////////////////
t = <SPACE> {
beginToken = letterCounter+1;
letterCounter += t.image.length();
endToken = letterCounter;
countToken+=1;
infoPrint.setLength(0);
infoPrint.append(countToken)
.append(": Found token <SPACE>, look: \"")
.append(t.image)
.append("\" in length: ")
.append(t.image.length())
.append(" characters. Token Is located ")
.append(beginToken)
.append(" to ")
.append(endToken)
.append(" position.");
System.out.println(infoPrint);
}
////////////////////////////////////////////////////////////////////////////////
(t = <STAR>{
beginToken = letterCounter+1;
letterCounter += t.image.length();
endToken = letterCounter;
countToken+=1;
infoPrint.setLength(0);
infoPrint.append(countToken)
.append(": Found token <STAR>, look: \"")
.append(t.image)
.append("\" in length: ")
.append(t.image.length())
.append(" characters. Token Is located ")
.append(beginToken)
.append(" to ")
.append(endToken)
.append(" position.");
System.out.println(infoPrint);
} | temp = ListOfFields(){letterCounter += temp;})
////////////////////////////////////////////////////////////////////////////////
t = <SPACE>{
beginToken = letterCounter+1;
letterCounter += t.image.length();
endToken = letterCounter;
countToken+=1;
infoPrint.setLength(0);
infoPrint.append(countToken)
.append(": Found token <SPACE>, look: \"")
.append(t.image)
.append("\" in length: ")
.append(t.image.length())
.append(" characters. Token Is located ")
.append(beginToken)
.append(" to ")
.append(endToken)
.append(" position.");
System.out.println(infoPrint);
}
////////////////////////////////////////////////////////////////////////////////
t = <FROM>{
beginToken = letterCounter+1;
letterCounter += t.image.length();
endToken = letterCounter;
countToken+=1;
infoPrint.setLength(0);
infoPrint.append(countToken)
.append(": Found token <FROM>, look: \"")
.append(t.image)
.append("\" in length: ")
.append(t.image.length())
.append(" characters. Token Is located ")
.append(beginToken)
.append(" to ")
.append(endToken)
.append(" position.");
System.out.println(infoPrint);
}
////////////////////////////////////////////////////////////////////////////////
t = <SPACE>{
beginToken = letterCounter+1;
letterCounter += t.image.length();
endToken = letterCounter;
countToken+=1;
infoPrint.setLength(0);
infoPrint.append(countToken)
.append(": Found token <SPACE>, look: \"")
.append(t.image)
.append("\" in length: ")
.append(t.image.length())
.append(" characters. Token Is located ")
.append(beginToken)
.append(" to ")
.append(endToken)
.append(" position.");
System.out.println(infoPrint);
}
////////////////////////////////////////////////////////////////////////////////
t = <IDENTIFIER>{
beginToken = letterCounter+1;
letterCounter += t.image.length();
endToken = letterCounter;
countToken+=1;
infoPrint.setLength(0);
infoPrint.append(countToken)
.append(": Found token <IDENTIFIER>, look: \"")
.append(t.image)
.append("\" in length: ")
.append(t.image.length())
.append(" characters. Token Is located ")
.append(beginToken)
.append(" to ")
.append(endToken)
.append(" position.");
System.out.println(infoPrint);
}
////////////////////////////////////////////////////////////////////////////////
{return letterCounter;}
}
int ListOfFields():
{
Token t;
int letterCounter = 0;
}
{
t = <IDENTIFIER>{letterCounter += t.image.length();}
(
t = "," {letterCounter += t.image.length();}
(t = <SPACE>{letterCounter += t.image.length();})*
t = <IDENTIFIER>{letterCounter += t.image.length();}
)*
{return letterCounter;}
}
----------------------
t = <DELETE> { PrintInfo(t, "<DELETE>"); }
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
t = <FROM> { PrintInfo(t, "<FROM>"); }
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
t = <TABLENAME> { PrintInfo(t, "<TABLENAME>"); }
/// ()
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
t = <WHERE> { PrintInfo(t, "<WHERE>"); }
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
t = <COLUMNNAMEREAL> { PrintInfo(t, "<COLUMNNAMEREAL>"); }
/// t = <COLUMNNAME> { PrintInfo(t, "<COLUMNNAME>"); }

t = <SPACE> { PrintInfo(t, "<SPACE>"); }
t = <COMPARE> { PrintInfo(t, "<COMPARE>"); }
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
t = <NUMBER>{ PrintInfo(t, "<NUMBER>"); }
t = <ENDQ> { PrintInfo(t, "<ENDQ>"); }
----------------------------------------------------------------
options {
LOOKAHEAD = 1;
STATIC = false;
IGNORE_CASE = true;
}
PARSER_BEGIN(Parser)
import java.io.Reader;
import java.io.StringReader;
public class Parser {
static int letterCounter = 0;
static int temp = 0;
static int countToken = 0;
// static Token t;
public static void main(String args[]) throws ParseException {
String query = "DELETE FROM DYSCYPL WHERE DYSCYPL.NOMKAF = 3;";
System.out.println("Introduced by request: " + query );
Parser parser = new Parser(new StringReader(query));
parser.Input();
System.out.println("The length of the entered query: " + parser.Input()
+ " characters.");
}
public void PrintInfo(Token t, String tokenName){
int beginToken = 0;
int endToken = 0;
StringBuffer infoPrint = new StringBuffer();
beginToken = letterCounter+1;
letterCounter += t.image.length();
endToken = letterCounter;
countToken+=1;
infoPrint.setLength(0);
infoPrint.append(countToken)
.append(": Found token ")
.append(tokenName)
.append(", look: \"")
.append(t.image)
.append("\" in length: ")
.append(t.image.length())
.append(" characters. Token Is located ")
.append(beginToken)
.append(" to ")
.append(endToken)
.append(" position.");
System.out.println(infoPrint);
}
}
PARSER_END(Parser)
SKIP : { "\t"| "\n"| "\r" }
////////////////////////////////////////////////////////////////////////////////
TOKEN : { <DELETE : ["D","d"]["E","e"]["L","l"]["E","e"]["T","t"]["E","e"]> }
TOKEN : { <FROM : ["F","f"]["R","r"]["O","o"]["M","m"]> }
TOKEN : { <WHERE : ["W","w"]["H","h"]["E","e"]["R","r"]["E","e"]> }
TOKEN : { <AND : ["A","a"]["N","n"]["D","d"]> }
TOKEN : { <NOT : ["N","n"]["O","o"]["T","t"]> }
TOKEN : { <OR : ["O","o"]["R","R"]> }
TOKEN : { <SPACE: (" ")+> }
TOKEN : { <STAR: ("*")> }
TOKEN : { <COMMA: (",")> }
TOKEN : { <SEMICOLON: (";")> }
TOKEN : { <BRACKETOPEN: ("(")> }
TOKEN : { <BRACKETCLOSE: (")")> }
////////////////////////////////////////////////////////////////////////////////
TOKEN : { <DYSCYPL: ["D","d"]["Y","y"]["S","s"]["C","c"]["Y","y"]["P","p"]["L","
l"]> }
TOKEN : { <NOMKAF: ["N","n"]["O","o"]["M","m"]["K","k"]["A","a"]["F","f"]> }
TOKEN : { <KODBLOK: ["K","k"]["O","o"]["D","d"]["B","b"]["l","l"]["O","o"]["K","
k"]> }
TOKEN : { <KODDYSC: ["K","k"]["O","o"]["D","d"]["D","d"]["Y","y"]["S","s"]["C","
c"]> }
TOKEN : { <NAZVDYSC: ["N","n"]["A","a"]["Z","z"]["V","v"]["D","d"]["Y","y"]["S",
"s"]["C","c"]> }
TOKEN : { <OBSCRED: ["O","o"]["B","b"]["S","s"]["C","c"]["R","r"]["E","e"]["D","
D"]> }
TOKEN : { <COLUMNNAMETEXT: <DYSCYPL>["."]<NAZVDYSC>> }
TOKEN : { <COLUMNNAMEINT: <DYSCYPL>["."](<NOMKAF>|<KODBLOK>|<OBSCRED>)> }
TOKEN : { <COLUMNNAMEREAL: <DYSCYPL>["."]<KODDYSC>> }
////////////////////////////////////////////////////////////////////////////////
TOKEN : { < EQUAL: "=" > }
TOKEN : { < NOTEQUAL: "!=" > }
TOKEN : { < LESSTHAN: "<" > }
TOKEN : { < GREATERTHAN: ">" > }
TOKEN : { < LESSTHANOREQUALTO: "<=" > }
TOKEN : { < GREATERTHANOREQUALTO: ">=" > }
////////////////////////////////////////////////////////////////////////////////
TOKEN : { <NUMBER: (["-"])?["1"-"9"](["0"-"9"])*> }
TOKEN : { <SHOTNUMBER: (["-"])? ["0"]|["1"-"9"](["0"-"9"])*["."](["0"-"9"])+> }
TOKEN : { <STRING: (["'"]["A"-"Z","a"-"z"]["'"])+> }
TOKEN : { <STRINGORNUMBER: ["'"](<STRING>|<NUMBER>)+["'"]> }
///TOKEN : { <COMPARE: ["=",">","<"] > }
///TOKEN : { <TABLENAME: ["A"-"Z","a"-"z","_"](["A"-"Z","a"-"z","0"-"9"]){1,7}>
}
///TOKEN : { <COLUMNNAME: <TABLENAME>["."]["A"-"Z","a"-"z"](["A"-"Z","a"-"z","0"
-"9","-","_"]){1,8}> }
int Input() :
{ Token t; }
{
t = <DELETE> { PrintInfo(t, "<DELETE>"); }
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
t = <FROM> { PrintInfo(t, "<FROM>"); }
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
t = <DYSCYPL> { PrintInfo(t, "<TABLENAME: DYSCYPL>"); }
( t = <SEMICOLON> { PrintInfo(t, "<SEMICOLON>"); }
|
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
t = <WHERE> { PrintInfo(t, "<WHERE>"); }
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
(
StringField()
|
RealField()
|
IntField()
)
t = <SEMICOLON> { PrintInfo(t, "<SEMICOLON>"); }
)
{return letterCounter;}
}
void StringField():
{ }
{
t = <COLUMNNAMETEXT>{PrintInfo(t, "<COLUMNNAMETEXT>");}
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
(
t = <EQUAL> { PrintInfo(t, "<EQUAL>"); }
|
t = <NOTEQUAL> { PrintInfo(t, "<NOTEQUAL>"); }
)
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
t = <STRINGORNUMBER> { PrintInfo(t, "<STRINGORNUMBER>"); }
}
void RealField():
{ }
{
t = <COLUMNNAMEREAL>{PrintInfo(t, "<COLUMNNAMEREAL>");}
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
(
t = <EQUAL> { PrintInfo(t, "<EQUAL>"); }
|
t = <NOTEQUAL> { PrintInfo(t, "<NOTEQUAL>"); }
|
t = <LESSTHAN> { PrintInfo(t, "<LESSTHAN>"); }
|
t = <GREATERTHAN> { PrintInfo(t, "<GREATERTHAN>"); }
|
t = <LESSTHANOREQUALTO> { PrintInfo(t, "<LESSTHANOREQUALTO>"); }
|
t = <GREATERTHANOREQUALTO> { PrintInfo(t, "<GREATERTHANOREQUALTO>"); }
)
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
t = <SHOTNUMBER> { PrintInfo(t, "<SHOTNUMBER>"); }
}
void IntField():
{ }
{
t = <COLUMNNAMEINT>{PrintInfo(t, "<COLUMNNAMEINT>");}
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
(
t = <EQUAL> { PrintInfo(t, "<EQUAL>"); }
|
t = <NOTEQUAL> { PrintInfo(t, "<NOTEQUAL>"); }
|
t = <LESSTHAN> { PrintInfo(t, "<LESSTHAN>"); }
|
t = <GREATERTHAN> { PrintInfo(t, "<GREATERTHAN>"); }
|
t = <LESSTHANOREQUALTO> { PrintInfo(t, "<LESSTHANOREQUALTO>"); }
|
t = <GREATERTHANOREQUALTO> { PrintInfo(t, "<GREATERTHANOREQUALTO>"); }
)
t = <SPACE> { PrintInfo(t, "<SPACE>"); }
t = <NUMBER> { PrintInfo(t, "<NUMBER>"); }
}
-------------------------------------------------------
/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 5.0 *
/
/* JavaCCOptions:KEEP_LINE_COL=null */
package lab1;
/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 5.0 *
/
/* JavaCCOptions:KEEP_LINE_COL=null */
/**
* This exception is thrown when parse errors are encountered.
* You can explicitly create objects of this exception type by
* calling the method generateParseException in the generated
* parser.
*
* You can modify this class to customize your error reporting
* mechanisms so long as you retain the public fields.
*/
public class ParseException extends Exception {
/**
* The version identifier for this Serializable class.
* Increment only if the <i>serialized</i> form of the
* class changes.
*/
private static final long serialVersionUID = 1L;
/**
* This constructor is used by the method "generateParseException"
* in the generated parser. Calling this constructor generates
* a new object of this type with the fields "currentToken",
* "expectedTokenSequences", and "tokenImage" set.
*/
public ParseException(Token currentTokenVal,
int[][] expectedTokenSequencesVal,
String[] tokenImageVal
)
{
super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal))
;
currentToken = currentTokenVal;
expectedTokenSequences = expectedTokenSequencesVal;
tokenImage = tokenImageVal;
}
/**
* The following constructors are for use by you for whatever
* purpose you can think of. Constructing the exception in this
* manner makes the exception behave in the normal way - i.e., as
* documented in the class "Throwable". The fields "errorToken",
* "expectedTokenSequences", and "tokenImage" do not contain
* relevant information. The JavaCC generated code does not use
* these constructors.
*/
public ParseException() {
super();
}
/** Constructor with message. */
public ParseException(String message) {
super(message);
}
/**
* This is the last token that has been consumed successfully. If
* this object has been created due to a parse error, the token
* followng this token will (therefore) be the first error token.
*/
public Token currentToken;
/**
* Each entry in this array is an array of integers. Each array
* of integers represents a sequence of tokens (by their ordinal
* values) that is expected at this point of the parse.
*/
public int[][] expectedTokenSequences;
/**
* This is a reference to the "tokenImage" array of the generated
* parser within which the parse error occurred. This array is
* defined in the generated ...Constants interface.
*/
public String[] tokenImage;
/**
* It uses "currentToken" and "expectedTokenSequences" to generate a parse
* error message and returns it. If this object has been created
* due to a parse error, and you do not catch it (it gets thrown
* from the parser) the correct error message
* gets displayed.
*/
private static String initialise(Token currentToken,
int[][] expectedTokenSequences,
String[] tokenImage) {
String eol = System.getProperty("line.separator", "\n");
StringBuffer expected = new StringBuffer();

StringBuffer expectedPrint = new StringBuffer();
expectedPrint.append(" ");

int maxSize = 0;
for (int i = 0; i < expectedTokenSequences.length; i++) {
if (maxSize < expectedTokenSequences[i].length) {
maxSize = expectedTokenSequences[i].length;
}
for (int j = 0; j < expectedTokenSequences[i].length; j++) {
expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
expectedPrint.append(tokenImage[expectedTokenSequences[i][j]]);
}
if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0)
{
expected.append("...");
}
expected.append(eol).append(" ");
}
String retval = "Encountered \"";
Token tok = currentToken.next;
for (int i = 0; i < maxSize; i++) {
if (i != 0) retval += " ";
if (tok.kind == 0) {
retval += tokenImage[0];
break;
}
retval += " --" + tokenImage[tok.kind];
retval += " \"";
retval += add_escapes(tok.image);
retval += " \"-----";
expectedPrint.append(", ")
.append(tokenImage[tok.kind])
.append(" \"")
.append(add_escapes(tok.image))
.append("\"");
tok = tok.next;
}
retval += "\" at line " + currentToken.next.beginLine + ", column " + curren
tToken.next.beginColumn;
retval += "." + eol;
if (expectedTokenSequences.length == 1) {
retval += "Was expecting:" + eol + " ";
} else {
retval += "Was expecting one of:" + eol + " ";
}
retval += expected.toString();
System.out.println(expectedPrint.toString());
return retval;
}
/**
* The end of line string for this machine.
*/
protected String eol = System.getProperty("line.separator", "\n");
/**
* Used to convert raw characters to their escaped version
* when these raw version cannot be used as part of an ASCII
* string literal.
*/
static String add_escapes(String str) {
StringBuffer retval = new StringBuffer();
char ch;
for (int i = 0; i < str.length(); i++) {
switch (str.charAt(i))
{
case 0 :
continue;
case '\b':
retval.append("\\b");
continue;
case '\t':
retval.append("\\t");
continue;
case '\n':
retval.append("\\n");
continue;
case '\f':
retval.append("\\f");
continue;
case '\r':
retval.append("\\r");
continue;
case '\"':
retval.append("\\\"");
continue;
case '\'':
retval.append("\\\'");
continue;
case '\\':
retval.append("\\\\");
continue;
default:
if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
String s = "0000" + Integer.toString(ch, 16);
retval.append("\\u" + s.substring(s.length() - 4, s.length()));
} else {
retval.append(ch);
}
continue;
}
}
return retval.toString();
}
}
/* JavaCC - OriginalChecksum=e25b01bc55ed9d08151448657bb75b17 (do not edit this
line) */

You might also like