Skip to content

Commit 31d10df

Browse files
committed
added nodes for defining relations, added grammar for it to, relation repo for lexer and parser to check if relation name is not the same as set name
1 parent 2a4b76f commit 31d10df

File tree

12 files changed

+337
-117
lines changed

12 files changed

+337
-117
lines changed

src/cup/parser.cup

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import java_cup.runtime.*;
22
import java.io.InputStreamReader;
33
//import javafx.util.Pair;
44
import java.util.HashMap;
5-
import java.util.List;
6-
import java.io.IOException;
7-
import java.io.InputStreamReader;
85
import java.util.Set;
96
import java.util.HashSet;
7+
import common.RelationRepository;
108

119
parser code {:
1210
private HashMap<String, Integer> variables_; //nazwa zmiennej -> jej wartość
@@ -39,7 +37,7 @@ init with {:
3937
scan with {: return lexer.next_token(); :}
4038

4139
/* terminals */
42-
terminal GT,LT,L_BRA,R_BRA,IS,SET_DELI,SEMI;
40+
terminal GT,LT,L_BRA,R_BRA,IS,SET_DELI,SEMI,REL_DEFINITION;
4341
terminal String SET_OPERATORS;
4442
terminal String REL;
4543
terminal List<Integer> SET;
@@ -48,7 +46,7 @@ terminal String VAR,SET_VAR;
4846

4947
/* non terminals */
5048
non terminal Integer expr_var,expr_rel;
51-
non terminal rel_def,statment_def,statment,expr_list,set;
49+
non terminal rel_def,statment_def_node,statment,expr_list,set;
5250
non terminal Integer set_content;
5351
non terminal Set<Integer> expr_set;
5452

@@ -64,6 +62,8 @@ expr_list ::= expr_list statment SEMI
6462
| expr_list expr_var:e SEMI
6563
| statment SEMI
6664
| expr_var:e SEMI
65+
| expr_list rel_def SEMI
66+
| rel_def SEMI
6767
;//rel_def |
6868

6969

@@ -95,20 +95,21 @@ expr_set ::= SET_VAR:v {: //System.out.println(""+
9595
set ::= L_BRA set_content R_BRA //{: System.out.println("set"); :}
9696
;
9797

98-
set_content ::= NUMBER:n1 SET_DELI set_content {: //System.out.println("set el "+n1);
99-
tmp_set_.add(n1);
98+
set_content ::= NUMBER:n1 SET_DELI set_content {: tmp_set_.add(n1);
10099
RESULT = n1; :}
101-
| NUMBER:n1 {: //System.out.println("set el "+n1);
102-
tmp_set_.add(n1);
100+
| NUMBER:n1 {: tmp_set_.add(n1);
103101
RESULT = n1; :}
104102
;
105-
//rel_def ::= VAR:v1 REL:r VAR:v2 L_BRA statment_def R_BRA {: System.out.println("znaleziono def relacji\n"); :}
106-
// ;
103+
rel_def ::= REL_DEFINITION VAR:v1 SET_VAR:r VAR:v2 L_BRA statment_def_node R_BRA
104+
{: System.out.println("znaleziono def relacji\n");
105+
//RelationRepository.addRelationName(r);
106+
:}
107+
;
107108

108-
//statment_def ::= VAR:v1 GT VAR:v2 {: System.out.println("ciało relacji\n"); :}
109-
// | VAR:v1 LT VAR:v2 {: System.out.println("ciało relacji\n"); :}
110-
// | VAR:v1 {: System.out.println("ciało relacji\n"); :}
111-
// ;
109+
statment_def_node ::= VAR:v1 GT VAR:v2 {: System.out.println("ciało relacji\n"); :}
110+
| VAR:v1 LT VAR:v2 {: System.out.println("ciało relacji\n"); :}
111+
| VAR:v1 {: System.out.println("ciało relacji\n"); :}
112+
;//DEF: a RRR b {a<b};
112113

113114

114115
//expr_rel ::= NUMBER:n1 REL:r NUMBER:n2 {: System.out.println("expr_rel\n"); :}

src/jflex/test.flex

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Text up to the first is copied verbatim into the created source file.
33
* Used for package and import statements. (we don't need any here)
44
*/
5-
import java_cup.runtime.Symbol;
5+
import common.RelationRepository;import java_cup.runtime.Symbol;
66
import java_cup.runtime.ComplexSymbolFactory;
77
import java_cup.runtime.ComplexSymbolFactory.Location;
88
import java.lang.*;
@@ -33,10 +33,7 @@ import java.lang.*;
3333
/* Code in the next section is copied into the generated lexer class.
3434
*/
3535
%{
36-
/* public TestLexer(ComplexSymbolFactory sf, java.io.InputStream is){
37-
this(is);
38-
symbolFactory = sf;
39-
}*/
36+
4037
public TestLexer(ComplexSymbolFactory sf, java.io.Reader reader){
4138
this(reader);
4239
symbolFactory = sf;
@@ -73,7 +70,7 @@ import java.lang.*;
7370
*/
7471

7572
VARIABLE = [a-z]+([a-z]|[0-9])*
76-
RELATION = "*"[A-Z]+"*"
73+
//RELATION = [A-Z]+([A-Z]|[0-9])*
7774
SET_VAR = [A-Z]+([A-Z]|[0-9])*
7875
BRACKET_L = "{"
7976
BRACKET_R = "}"
@@ -84,25 +81,32 @@ NUMBER = [1-9][0-9]*
8481
SET_OPERATORS = "+"|"-"|"\\"
8582
SET_DELI = ","
8683
SEMI = ";"
84+
DEF = "DEF:"
8785
//SET_LIST = (","{WHITESPACE}*{VARIABLE})+
8886

8987
%state STRING
9088

9189
%%
9290

9391
<YYINITIAL> "is" { return symbolFactory.newSymbol("IS",sym.IS);}
94-
<YYINITIAL> {VARIABLE} { return symbolFactory.newSymbol("VARIABLE",sym.VAR, yytext());}
95-
<YYINITIAL> {RELATION} { return symbolFactory.newSymbol("RELATION",sym.REL, yytext());}
92+
<YYINITIAL> {VARIABLE} {return symbolFactory.newSymbol("VARIABLE",sym.VAR, yytext());}
93+
//<YYINITIAL> {RELATION} { return symbolFactory.newSymbol("RELATION",sym.REL, yytext());}
9694
<YYINITIAL> {WHITESPACE} {/*ignore*/}
9795
<YYINITIAL> {BRACKET_L} {return symbolFactory.newSymbol("BRACKET_L",sym.L_BRA);}
9896
<YYINITIAL> {BRACKET_R} {return symbolFactory.newSymbol("BRACKET_R",sym.R_BRA);}
9997
<YYINITIAL> {GT} {return symbolFactory.newSymbol("GT",sym.GT);}
10098
<YYINITIAL> {LT} {return symbolFactory.newSymbol("LT",sym.LT);}
10199
<YYINITIAL> {NUMBER} {return symbolFactory.newSymbol("NUMBER",sym.NUMBER, Integer.parseInt(yytext()));}
102-
<YYINITIAL> {SET_VAR} {return symbolFactory.newSymbol("SET_VAR",sym.SET_VAR, yytext());}
100+
<YYINITIAL> {SET_VAR} {String capsName = yytext();
101+
if(RelationRepository.isThereRelation(capsName)){
102+
return symbolFactory.newSymbol("SET_VAR",sym.REL, yytext());
103+
}else{
104+
return symbolFactory.newSymbol("SET_VAR",sym.SET_VAR, capsName);
105+
}}
103106
<YYINITIAL> {SET_OPERATORS} {return symbolFactory.newSymbol("SET_OPERATORS",sym.SET_OPERATORS, yytext());}
104107
<YYINITIAL> {SET_DELI} {return symbolFactory.newSymbol("SET_DELI",sym.SET_DELI);}
105108
<YYINITIAL> {SEMI} {return symbolFactory.newSymbol("SEMI",sym.SEMI);}
109+
<YYINITIAL> {DEF} {return symbolFactory.newSymbol("DEF",sym.REL_DEFINITION);}
106110

107111
[^] { emit_warning("Unrecognized character '" +yytext()+"' -- ignored"); }
108112
//[^] { throw new RuntimeException("Illegal character <" + yytext() + ">"); }

src/main/java/TestLexer.java

Lines changed: 36 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package common;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class RelationRepository {
7+
8+
private static Set<String> allRelationsNames = new HashSet<>();
9+
10+
public static void addRelationName(String name) {
11+
allRelationsNames.add(name);
12+
}
13+
14+
public static boolean isThereRelation(String name) {
15+
return allRelationsNames.contains(name);
16+
}
17+
18+
}

src/main/java/node/NumberNode.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package node;
2+
3+
import node.common.BiContextNode;
4+
import node.common.BiNode;
5+
import node.common.NodeType;
6+
7+
public class NumberNode extends BiNode {
8+
@Override
9+
public Integer compute(BiContextNode ctx) {
10+
Integer toReturn = null;
11+
if (side == NodeType.LEFT_VAR) {
12+
toReturn = ctx.ctx[0];
13+
} else if (side == NodeType.RIGHT_VAR) {
14+
toReturn = ctx.ctx[1];
15+
}
16+
return toReturn;
17+
}
18+
19+
@Override
20+
public NodeType getNodeType() {
21+
return NodeType.NUMBER;
22+
}
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package node.common;
2+
3+
public class BiContextNode {
4+
public Integer[] ctx;
5+
6+
public BiContextNode() {
7+
ctx = new Integer[2];
8+
}
9+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package node.common;
2+
3+
public abstract class BiNode implements Computable {
4+
protected BiNode leftNode;
5+
protected BiNode rightNode;
6+
7+
protected NodeType side;
8+
9+
public void setLeftNode(BiNode leftNode) {
10+
leftNode.side = NodeType.LEFT_VAR;
11+
this.leftNode = leftNode;
12+
}
13+
14+
public void setRightNode(BiNode rightNode) {
15+
rightNode.side = NodeType.RIGHT_VAR;
16+
this.rightNode = rightNode;
17+
}
18+
19+
public Computable getLeftNode() {
20+
return leftNode;
21+
}
22+
23+
public Computable getRightNode() {
24+
return rightNode;
25+
}
26+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package node.common;
2+
3+
public interface Computable {
4+
5+
public Integer compute(BiContextNode ctx);
6+
public NodeType getNodeType();
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package node.common;
2+
3+
public enum NodeType {
4+
VAR,
5+
NUMBER,
6+
LEFT_VAR,
7+
RIGHT_VAR,
8+
SMALLER,
9+
GREATER,
10+
SMALLER_OR_EQUAL,
11+
GREATER_OR_EQUAL,
12+
EQUAL,
13+
NOT_EQUAL;
14+
}

0 commit comments

Comments
 (0)