first commit :D

This commit is contained in:
Wittmann Simon 2023-12-06 13:10:50 +01:00
commit 74b3ffab27
8 changed files with 633 additions and 0 deletions

BIN
JLex2.jar Normal file

Binary file not shown.

16
Main.java Normal file
View File

@ -0,0 +1,16 @@
public class Main {
public static void main(String[] args) {
browserscanner scanner;
try {
scanner = new browserscanner(new java.io.FileReader("calctest.txt"));
} catch (Exception e) {
scanner = new browserscanner(new java.io.InputStreamReader(System.in));
}
browserparser parser = new browserparser();
try {
parser.yyparse(scanner);
} catch (Exception e) {
e.printStackTrace();
}
}
}

16
Makefile Normal file
View File

@ -0,0 +1,16 @@
JAVAC = javac
JAVA = java
JAY = jaooy
Main.class: Main.java yyTokenclass.class browserscanner.class
$(JAVAC) Main.java
yyTokenclass.class: browserparser.java
$(JAVAC) browserparser.java
browserscanner.class: browserlexer.java
$(JAVAC) browserlexer.java
browserlexer.java: browserlexer
$(JAVA) -cp JLex2.jar JLex2.Main browserlexer
browserparser.java: browserparser.jay skeleton.jaooy
$(JAY) -v browserparser.jay < skeleton.jaooy > browserparser.java
clean:
rm -f *.class browserlexer.java browserparser.java

22
browserlexer Normal file
View File

@ -0,0 +1,22 @@
%%
%jayscanner browserscanner
%jayparser browserparser
%class browserlexer
%eofval{
return new EOF();
%eofval}
ws = [ \t\r\n\b \015]+
%%
[0-9]+ { return new NUM(yytext()) ; }
"+" { return new PLS(); }
"-" { return new MIN(); }
"*" { return new MUL(); }
"/" { return new DIV(); }
"(" { return new BROP(); }
")" { return new BRCL(); }
{ws} { System.out.println(yytext()); }
. { System.out.println("Falsch"); }

18
browserparser.jay Normal file
View File

@ -0,0 +1,18 @@
%{
class browserparser {
%}
%token NUM
%token PLS
%token MIN
%token MUL
%token DIV
%token BROP
%token BRCL
%%
S : Exp Op Exp | BROP Exp Op Exp BRCL { }
Num : NUM | BROP MIN NUM BRCL { }
Exp : S | Num { }
Op : PLS | MIN | MUL | DIV { }
%%
}

1
calctest.txt Normal file
View File

@ -0,0 +1 @@
5+(2-3)/12

350
skeleton.jaooy Normal file
View File

@ -0,0 +1,350 @@
# jay skeleton
# character in column 1 determines outcome...
# # is a comment
# . is copied
# t is copied as //t if -t is set
# other lines are interpreted to call jay procedures
.// created by jay 0.8 (c) 1998 Axel.Schreiner@informatik.uni-osnabrueck.de
.
prolog ## %{ ... %} prior to the first %%
. yyError yytemperror = new yyError();
. public final int yyErrorCode = yytemperror.tokennr;
.
. /** thrown for irrecoverable syntax errors and stack overflow.
. */
. public static class yyException extends java.lang.Exception {
. public yyException (String message) {
. super(message);
. }
. }
.
. /** must be implemented by a scanner object to supply input to the parser.
. */
. public interface yyInput {
. /** move on to next token.
. @return false if positioned beyond tokens.
. @throws IOException on input error.
. */
. /** modified by diho, 10.04.2004 */
. yyTokenclass advance () throws java.io.IOException;
. /** classifies current token.
. Should not be called if advance() returned false.
. @return current %token or single character.
. */
. /** modified by diho, 10.04.2004 token () is not longer necessary,
. advance () supplies token or null
. int token (); */
.
.
. /** associated with current token.
. Should not be called if advance() returned false.
. @return value for token().
. */
. /** modified by diho, 10.04.2004 value () is not longer necessary,
. Token value can be retrieved from Token itself.
. Object value ();*/
. }
.
. /** simplified error message.
. @see <a href="#yyerror(java.lang.String, java.lang.String[])">yyerror</a>
. */
. public void yyerror (String message) {
. yyerror(message, null);
. }
.
. /** (syntax) error message.
. Can be overwritten to control message format.
. @param message text to be displayed.
. @param expected vector of acceptable tokens, if available.
. */
. public void yyerror (String message, String[] expected) {
. if (expected != null && expected.length > 0) {
. System.err.print(message+", expecting");
. for (int n = 0; n < expected.length; ++ n)
. System.err.print(" "+expected[n]);
. System.err.println();
. } else
. System.err.println(message);
. }
.
. /** debugging support, requires the package jay.yydebug.
. Set to null to suppress debugging messages.
. */
t protected jay.yydebug.yyDebug yydebug;
.
final_rule ## constant storing final rule
.
. /** index-checked interface to yyName[].
. @param token single character or %token value.
. @return token name or [illegal] or [unknown].
. */
t public static final String yyname (int token) {
t if (token < 0 || token > YyNameClass.yyName.length) return "[illegal]";
t String name;
t if ((name = YyNameClass.yyName[token]) != null) return name;
t return "[unknown]";
t }
.
. /** computes list of expected tokens on error by tracing the tables.
. @param state for which to compute the list.
. @return list of token names.
. */
. protected String[] yyExpecting (int state) {
. int token, n, len = 0;
. boolean[] ok = new boolean[YyNameClass.yyName.length];
.
. if ((n = YySindexClass.yySindex[state]) != 0)
. for (token = n < 0 ? -n : 0;
. token < YyNameClass.yyName.length && n+token < YyTableClass.yyTable.length; ++ token)
. if (YyCheckClass.yyCheck[n+token] == token && !ok[token] && YyNameClass.yyName[token] != null) {
. ++ len;
. ok[token] = true;
. }
. if ((n = YyRindexClass.yyRindex[state]) != 0)
. for (token = n < 0 ? -n : 0;
. token < YyNameClass.yyName.length && n+token < YyTableClass.yyTable.length; ++ token)
. if (YyCheckClass.yyCheck[n+token] == token && !ok[token] && YyNameClass.yyName[token] != null) {
. ++ len;
. ok[token] = true;
. }
.
. String result[] = new String[len];
. for (n = token = 0; n < len; ++ token)
. if (ok[token]) result[n++] = YyNameClass.yyName[token];
. return result;
. }
.
. /** the generated parser, with debugging messages.
. Maintains a state and a value stack, currently with fixed maximum size.
. @param yyLex scanner.
. @param yydebug debug message writer implementing yyDebug, or null.
. @return result of the last reduction, if any.
. @throws yyException on irrecoverable parse error.
. */
. public Object yyparse (yyInput yyLex, Object yydebug)
. throws java.io.IOException, yyException {
t this.yydebug = (jay.yydebug.yyDebug)yydebug;
. return yyparse(yyLex);
. }
.
. /** initial size and increment of the state/value stack [default 256].
. This is not final so that it can be overwritten outside of invocations
. of yyparse().
. */
. protected int yyMax;
.
. /** executed at the beginning of a reduce action.
. Used as $$ = yyDefault($1), prior to the user-specified action, if any.
. Can be overwritten to provide deep copy, etc.
. @param first value for $1, or null.
. @return first.
. */
. protected Object yyDefault (Object first) {
. return first;
. }
.
. /** the generated parser.
. Maintains a state and a value stack, currently with fixed maximum size.
. @param yyLex scanner.
. @return result of the last reduction, if any.
. @throws yyException on irrecoverable parse error.
. */
. public Object yyparse (yyInput yyLex)
. throws java.io.IOException, yyException {
. YyCheckClass.yyCheckInit(); // initial yyCheck eingefuegt PL 05-03-08
. YyTableClass.yyTableInit(); // initial yyCheck eingefuegt PL 05-03-08
. if (yyMax <= 0) yyMax = 256; // initial size
. int yyState = 0, yyStates[] = new int[yyMax]; // state stack
. Object yyVal = null, yyVals[] = new Object[yyMax]; // value stack
. /*modified by diho, 14.04.2004
. orig: int yyToken = -1 was replaced by "empty Token"*/
. yyTokenclass yyToken = new yyTokenclass(); // current input
. int yyErrorFlag = 0; // #tks to shift
.
local ## %{ ... %} after the first %%
. yyLoop: for (int yyTop = 0;; ++ yyTop) {
. if (yyTop >= yyStates.length) { // dynamically increase
. int[] i = new int[yyStates.length+yyMax];
. System.arraycopy(yyStates, 0, i, 0, yyStates.length);
. yyStates = i;
. Object[] o = new Object[yyVals.length+yyMax];
. System.arraycopy(yyVals, 0, o, 0, yyVals.length);
. yyVals = o;
. }
. yyStates[yyTop] = yyState;
. yyVals[yyTop] = yyVal;
t if (yydebug != null) yydebug.push(yyState, yyVal);
.
. yyDiscarded: for (;;) { // discarding a token does not change stack
. int yyN;
. if ((yyN = YyDefRedClass.yyDefRed[yyState]) == 0) { // else [default] reduce (yyN)
. /* modified by diho, 14.04.2004
. if "empty token", get next token */
. if (yyToken.tokennr <0) {
. /* modified by diho, 10.04.2004
. call yyLex.advance in any case, check if yyToken is EOF */
. yyToken = yyLex.advance(); /*? yyLex.token() : 0;*/
. /* modified by diho, 27.04.2004
. if "null", create EOF token */
. if (yyToken == null) {yyToken = new EOF();}
.
t if (yydebug != null)
t /* modified by diho, 14.04.2004
t orig.: yydebug.lex(yyState, yyToken, yyname(yyToken), yyLex.value()); */
t yydebug.lex(yyState, yyToken.tokennr, yyname(yyToken.tokennr), yyToken.value);
. }
. /* modified by diho, 14.04.2004
. replaced yyToken by yyToken.tokennr*/
. if ((yyN = YySindexClass.yySindex[yyState]) != 0 && (yyN += yyToken.tokennr) >= 0
. /* modified by diho, 14.04.2004
. replaced yyToken by yyToken.tokennr*/
. && yyN < YyTableClass.yyTable.length && YyCheckClass.yyCheck[yyN] == yyToken.tokennr) {
t if (yydebug != null)
t yydebug.shift(yyState, YyTableClass.yyTable[yyN], yyErrorFlag-1);
. yyState = YyTableClass.yyTable[yyN]; // shift to yyN
. /* modified by diho, 14.04.2004
. replaced yyLex.value() by yyToken.value*/
. yyVal = yyToken.value;
. /* modified by diho, 14.04.2004
. orig: yyToken = -1 */
. yyToken = new yyTokenclass();
. if (yyErrorFlag > 0) -- yyErrorFlag;
. continue yyLoop;
. }
. /* modified by diho, 14.04.2004
. replaced yyToken by yyToken.tokennr*/
. if ((yyN = YyRindexClass.yyRindex[yyState]) != 0 && (yyN += yyToken.tokennr) >= 0
. /* modified by diho, 14.04.2004
. replaced yyToken by yyToken.tokennr*/
. && yyN < YyTableClass.yyTable.length && YyCheckClass.yyCheck[yyN] == yyToken.tokennr)
. yyN = YyTableClass.yyTable[yyN]; // reduce (yyN)
. else
. switch (yyErrorFlag) {
.
. case 0:
. yyerror("syntax error", yyExpecting(yyState));
t if (yydebug != null) yydebug.error("syntax error");
.
. case 1: case 2:
. yyErrorFlag = 3;
. do {
. if ((yyN = YySindexClass.yySindex[yyStates[yyTop]]) != 0
. && (yyN += yyErrorCode) >= 0 && yyN < YyTableClass.yyTable.length
. && YyCheckClass.yyCheck[yyN] == yyErrorCode) {
t if (yydebug != null)
t yydebug.shift(yyStates[yyTop], YyTableClass.yyTable[yyN], 3);
. yyState = YyTableClass.yyTable[yyN];
. /* modified by diho, 14.04.2004
. replaced yyLex.value by yyToken.value*/
. yyVal = yyToken.value;
. continue yyLoop;
. }
t if (yydebug != null) yydebug.pop(yyStates[yyTop]);
. } while (-- yyTop >= 0);
t if (yydebug != null) yydebug.reject();
. throw new yyException("irrecoverable syntax error");
.
. case 3:
. /* modified by diho, 14.04.2004
. orig.: if (yyToken == 0)*/
. if (yyToken == null) {
t if (yydebug != null) yydebug.reject();
. throw new yyException("irrecoverable syntax error at end-of-file");
. }
t if (yydebug != null)
. /* modified by diho, 14.04.2004
. replaced yyToken by yyToken.tokennr, yyLex.value by yyToken.value*/
t yydebug.discard(yyState, yyToken.tokennr, yyname(yyToken.tokennr),
t yyToken.value);
. /* modified by diho, 14.04.2004
. orig: yyToken = -1*/
. yyToken = new yyTokenclass();
. continue yyDiscarded; // leave stack alone
. }
. }
. int yyV = yyTop + 1-YyLenClass.yyLen[yyN];
t if (yydebug != null)
t yydebug.reduce(yyState, yyStates[yyV-1], yyN, YyRuleClass.yyRule[yyN], YyLenClass.yyLen[yyN]);
. yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]);
. switch (yyN) {
actions ## code from the actions within the grammar
. }
. yyTop -= YyLenClass.yyLen[yyN];
. yyState = yyStates[yyTop];
. int yyM = YyLhsClass.yyLhs[yyN];
. if (yyState == 0 && yyM == 0) {
t if (yydebug != null) yydebug.shift(0, yyFinal);
. yyState = yyFinal;
. /* modified by diho, 14.04.2004
. replaced yyToken by yyToken.tokennr*/
. if (yyToken.tokennr < 0) {
. /** modified by diho, 10.04.2004
. */
. yyToken = yyLex.advance(); /* ? yyLex.token() : 0;*/
. /* modified by diho, 14.04.2004
. if "empty token", get next token */
. if (yyToken == null) {yyToken = new EOF();}
t if (yydebug != null)
. /* modified by diho, 14.04.2004
. replaced yyToken by yyToken.tokennr, yyLex.value() by yyToken.value*/
t yydebug.lex(yyState, yyToken.tokennr,yyname(yyToken.tokennr), yyToken.value);
. }
. /* modified by diho, 14.04.2004
. orig.: if (yyToken == 0) */
. if (yyToken.tokennr == 0) {
t if (yydebug != null) yydebug.accept(yyVal);
. return yyVal;
. }
. continue yyLoop;
. }
. if ((yyN = YyGindexClass.yyGindex[yyM]) != 0 && (yyN += yyState) >= 0
. && yyN < YyTableClass.yyTable.length && YyCheckClass.yyCheck[yyN] == yyState)
. yyState = YyTableClass.yyTable[yyN];
. else
. yyState = YyDgotoClass.yyDgoto[yyM];
t if (yydebug != null) yydebug.shift(yyStates[yyTop], yyState);
. continue yyLoop;
. }
. }
. }
.
tables ## tables for rules, default reduction, and action calls
.
debug ## tables for debugging support
.
epilog ## text following second %%
# modified by diho, 11.02.2004
.class yyTokenclass {
. public int tokennr;
. public Object value;
.
. yyTokenclass () {
. this.tokennr=-1;
. }
. yyTokenclass (Object o) {
. this.value = o;
. }
.}
.class EOF extends yyTokenclass {
.
. EOF () {
. super();
. this.tokennr=0;
. }
.}
# end of modification
.// %token constants
.
# modified by diho, 11.02.2004
tokens final class
# end of modification

210
y.output Normal file
View File

@ -0,0 +1,210 @@
0 $accept : S $end
1 S : Exp Op Exp
2 | BROP Exp Op Exp BRCL
3 Num : NUM
4 | BROP MIN NUM BRCL
5 Exp : S
6 | Num
7 Op : PLS
8 | MIN
9 | MUL
10 | DIV
state 0
$accept : . S $end (0)
NUM shift 1
BROP shift 2
. error
S goto 3
Exp goto 4
Num goto 5
state 1
Num : NUM . (3)
. reduce 3
state 2
S : BROP . Exp Op Exp BRCL (2)
Num : BROP . MIN NUM BRCL (4)
NUM shift 1
MIN shift 6
BROP shift 2
. error
S goto 7
Exp goto 8
Num goto 5
state 3
$accept : S . $end (0)
Exp : S . (5)
$end accept
. reduce 5
state 4
S : Exp . Op Exp (1)
PLS shift 9
MIN shift 10
MUL shift 11
DIV shift 12
. error
Op goto 13
state 5
Exp : Num . (6)
. reduce 6
state 6
Num : BROP MIN . NUM BRCL (4)
NUM shift 14
. error
state 7
Exp : S . (5)
. reduce 5
state 8
S : Exp . Op Exp (1)
S : BROP Exp . Op Exp BRCL (2)
PLS shift 9
MIN shift 10
MUL shift 11
DIV shift 12
. error
Op goto 15
state 9
Op : PLS . (7)
. reduce 7
state 10
Op : MIN . (8)
. reduce 8
state 11
Op : MUL . (9)
. reduce 9
state 12
Op : DIV . (10)
. reduce 10
state 13
S : Exp Op . Exp (1)
NUM shift 1
BROP shift 2
. error
S goto 7
Exp goto 16
Num goto 5
state 14
Num : BROP MIN NUM . BRCL (4)
BRCL shift 17
. error
state 15
S : Exp Op . Exp (1)
S : BROP Exp Op . Exp BRCL (2)
NUM shift 1
BROP shift 2
. error
S goto 7
Exp goto 18
Num goto 5
16: shift/reduce conflict (shift 9, reduce 1) on PLS
16: shift/reduce conflict (shift 10, reduce 1) on MIN
16: shift/reduce conflict (shift 11, reduce 1) on MUL
16: shift/reduce conflict (shift 12, reduce 1) on DIV
state 16
S : Exp . Op Exp (1)
S : Exp Op Exp . (1)
PLS shift 9
MIN shift 10
MUL shift 11
DIV shift 12
$end reduce 1
BRCL reduce 1
Op goto 13
state 17
Num : BROP MIN NUM BRCL . (4)
. reduce 4
18: shift/reduce conflict (shift 9, reduce 1) on PLS
18: shift/reduce conflict (shift 10, reduce 1) on MIN
18: shift/reduce conflict (shift 11, reduce 1) on MUL
18: shift/reduce conflict (shift 12, reduce 1) on DIV
state 18
S : Exp . Op Exp (1)
S : Exp Op Exp . (1)
S : BROP Exp Op Exp . BRCL (2)
PLS shift 9
MIN shift 10
MUL shift 11
DIV shift 12
BRCL shift 19
Op goto 13
state 19
S : BROP Exp Op Exp BRCL . (2)
. reduce 2
State 16 contains 4 shift/reduce conflicts.
State 18 contains 4 shift/reduce conflicts.
9 terminals, 5 nonterminals
11 grammar rules, 20 states