Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yip-jm committed May 8, 2022
1 parent e67924e commit ecfcb59
Show file tree
Hide file tree
Showing 30 changed files with 5,103 additions and 0 deletions.
104 changes: 104 additions & 0 deletions HW2(Parser)/Code/LL1/ArithExp.jj
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Option块

options {
STATIC = false;
}

// Class声明块
PARSER_BEGIN(IsAriExp)
import java.io.PrintStream ;
class IsAriExp {
public static void main( String[] args )
throws ParseException, TokenMgrError, NumberFormatException {
IsAriExp parser = new IsAriExp( System.in ) ;
parser.Start( System.out ) ;
}
}
PARSER_END(IsAriExp)

SKIP: { " " }
TOKEN: { < EOL : "\n" | "\r" | "\r\n" > }
TOKEN: { < PLUS : "+" > }
TOKEN: { < MINUS : "-" > }
TOKEN: { < TIMES : "*" > }
TOKEN: { < DIVIDE : "/" > }
TOKEN: { < OPEN_PAR : "(" > }
TOKEN: { < CLOSE_PAR : ")" > }
TOKEN: { < ID : <DIGITS>
|<DIGITS>"."<DIGITS>
|<DIGITS>"."
|"."<DIGITS>
|<LETTER>(<LETTER>|<DIGITS>)* >}
TOKEN: { < #DIGITS : (["0"-"9"])+ > }
TOKEN: { < #LETTER : (["A"-"Z","a"-"z","_"])+ > }


//PROCEDURE MAIN
void Start(PrintStream printStream) throws NumberFormatException :
{}
{
(
E()
<EOL> { printStream.println( "TRUE" ) ; }
)*
<EOF>
}


// PROCEDURE E
void E() throws NumberFormatException :
{}
{
T()
E1()
}

// PROCEDURE E'
void E1() throws NumberFormatException :
{}
{
(
<PLUS> (
T()
E1()
)
| <MINUS> (
T()
E1()
)
)?
}

//PROCEDURE T
void T() throws NumberFormatException :
{}
{
F()
T1()
}

// PROCEDURE T1
void T1() throws NumberFormatException :
{}
{
(
<TIMES> (
F()
T1()
)
| <DIVIDE> (
F()
T1()
)
)?
}

// PROCEDURE F
void F() throws NumberFormatException :
{}
{
<ID>
| <OPEN_PAR> E() <CLOSE_PAR>
| <MINUS> F()

}
Binary file added HW2(Parser)/Code/LL1/IsAriExp.class
Binary file not shown.
289 changes: 289 additions & 0 deletions HW2(Parser)/Code/LL1/IsAriExp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
/* Generated By:JavaCC: Do not edit this line. IsAriExp.java */
import java.io.PrintStream ;
class IsAriExp implements IsAriExpConstants {
public static void main( String[] args )
throws ParseException, TokenMgrError, NumberFormatException {
IsAriExp parser = new IsAriExp( System.in ) ;
parser.Start( System.out ) ;
}

//PROCEDURE MAIN
final public void Start(PrintStream printStream) throws ParseException, NumberFormatException {
label_1:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case MINUS:
case OPEN_PAR:
case ID:
;
break;
default:
jj_la1[0] = jj_gen;
break label_1;
}
E();
jj_consume_token(EOL);
printStream.println( "TRUE" ) ;
}
jj_consume_token(0);
}

// PROCEDURE E
final public void E() throws ParseException, NumberFormatException {
T();
E1();
}

// PROCEDURE E'
final public void E1() throws ParseException, NumberFormatException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case PLUS:
case MINUS:
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case PLUS:
jj_consume_token(PLUS);
T();
E1();
break;
case MINUS:
jj_consume_token(MINUS);
T();
E1();
break;
default:
jj_la1[1] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
break;
default:
jj_la1[2] = jj_gen;
;
}
}

//PROCEDURE T
final public void T() throws ParseException, NumberFormatException {
F();
T1();
}

// PROCEDURE T1
final public void T1() throws ParseException, NumberFormatException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case TIMES:
case DIVIDE:
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case TIMES:
jj_consume_token(TIMES);
F();
T1();
break;
case DIVIDE:
jj_consume_token(DIVIDE);
F();
T1();
break;
default:
jj_la1[3] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
break;
default:
jj_la1[4] = jj_gen;
;
}
}

// PROCEDURE F
final public void F() throws ParseException, NumberFormatException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case ID:
jj_consume_token(ID);
break;
case OPEN_PAR:
jj_consume_token(OPEN_PAR);
E();
jj_consume_token(CLOSE_PAR);
break;
case MINUS:
jj_consume_token(MINUS);
F();
break;
default:
jj_la1[5] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}

/** Generated Token Manager. */
public IsAriExpTokenManager token_source;
SimpleCharStream jj_input_stream;
/** Current token. */
public Token token;
/** Next token. */
public Token jj_nt;
private int jj_ntk;
private int jj_gen;
final private int[] jj_la1 = new int[6];
static private int[] jj_la1_0;
static {
jj_la1_init_0();
}
private static void jj_la1_init_0() {
jj_la1_0 = new int[] {0x290,0x18,0x18,0x60,0x60,0x290,};
}

/** Constructor with InputStream. */
public IsAriExp(java.io.InputStream stream) {
this(stream, null);
}
/** Constructor with InputStream and supplied encoding */
public IsAriExp(java.io.InputStream stream, String encoding) {
try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source = new IsAriExpTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 6; i++) jj_la1[i] = -1;
}

/** Reinitialise. */
public void ReInit(java.io.InputStream stream) {
ReInit(stream, null);
}
/** Reinitialise. */
public void ReInit(java.io.InputStream stream, String encoding) {
try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 6; i++) jj_la1[i] = -1;
}

/** Constructor. */
public IsAriExp(java.io.Reader stream) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new IsAriExpTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 6; i++) jj_la1[i] = -1;
}

/** Reinitialise. */
public void ReInit(java.io.Reader stream) {
jj_input_stream.ReInit(stream, 1, 1);
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 6; i++) jj_la1[i] = -1;
}

/** Constructor with generated Token Manager. */
public IsAriExp(IsAriExpTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 6; i++) jj_la1[i] = -1;
}

/** Reinitialise. */
public void ReInit(IsAriExpTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 6; i++) jj_la1[i] = -1;
}

private Token jj_consume_token(int kind) throws ParseException {
Token oldToken;
if ((oldToken = token).next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
if (token.kind == kind) {
jj_gen++;
return token;
}
token = oldToken;
jj_kind = kind;
throw generateParseException();
}


/** Get the next Token. */
final public Token getNextToken() {
if (token.next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
jj_gen++;
return token;
}

/** Get the specific Token. */
final public Token getToken(int index) {
Token t = token;
for (int i = 0; i < index; i++) {
if (t.next != null) t = t.next;
else t = t.next = token_source.getNextToken();
}
return t;
}

private int jj_ntk() {
if ((jj_nt=token.next) == null)
return (jj_ntk = (token.next=token_source.getNextToken()).kind);
else
return (jj_ntk = jj_nt.kind);
}

private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
private int[] jj_expentry;
private int jj_kind = -1;

/** Generate ParseException. */
public ParseException generateParseException() {
jj_expentries.clear();
boolean[] la1tokens = new boolean[12];
if (jj_kind >= 0) {
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 6; i++) {
if (jj_la1[i] == jj_gen) {
for (int j = 0; j < 32; j++) {
if ((jj_la1_0[i] & (1<<j)) != 0) {
la1tokens[j] = true;
}
}
}
}
for (int i = 0; i < 12; i++) {
if (la1tokens[i]) {
jj_expentry = new int[1];
jj_expentry[0] = i;
jj_expentries.add(jj_expentry);
}
}
int[][] exptokseq = new int[jj_expentries.size()][];
for (int i = 0; i < jj_expentries.size(); i++) {
exptokseq[i] = jj_expentries.get(i);
}
return new ParseException(token, exptokseq, tokenImage);
}

/** Enable tracing. */
final public void enable_tracing() {
}

/** Disable tracing. */
final public void disable_tracing() {
}

}
Binary file added HW2(Parser)/Code/LL1/IsAriExpConstants.class
Binary file not shown.
Loading

0 comments on commit ecfcb59

Please sign in to comment.