Rewrote grammar, implemented two visitors

This commit is contained in:
Marvin Kaiser
2019-10-29 09:51:43 +01:00
parent ea248733d0
commit 64c41122d0
11 changed files with 539 additions and 103 deletions

View File

@@ -1,20 +1,33 @@
token literal names: token literal names:
null null
null 'print'
';'
'*' '*'
'+'
'-'
'%'
null
null null
token symbolic names: token symbolic names:
null null
INTEGER_LITERAL PRINT
SCOL
MULT MULT
ADD
SUB
MOD
INTEGER_LITERAL
WS WS
rule names: rule names:
parse parse
multiplicativeExpr block
unaryExpression statement
print
expression
atom
atn: atn:
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 5, 21, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 7, 3, 14, 10, 3, 12, 3, 14, 3, 17, 11, 3, 3, 4, 3, 4, 3, 4, 2, 2, 5, 2, 4, 6, 2, 2, 2, 18, 2, 8, 3, 2, 2, 2, 4, 10, 3, 2, 2, 2, 6, 18, 3, 2, 2, 2, 8, 9, 5, 4, 3, 2, 9, 3, 3, 2, 2, 2, 10, 15, 5, 6, 4, 2, 11, 12, 7, 4, 2, 2, 12, 14, 5, 6, 4, 2, 13, 11, 3, 2, 2, 2, 14, 17, 3, 2, 2, 2, 15, 13, 3, 2, 2, 2, 15, 16, 3, 2, 2, 2, 16, 5, 3, 2, 2, 2, 17, 15, 3, 2, 2, 2, 18, 19, 7, 3, 2, 2, 19, 7, 3, 2, 2, 2, 3, 15] [3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 10, 47, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 3, 2, 3, 2, 3, 3, 7, 3, 18, 10, 3, 12, 3, 14, 3, 21, 11, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 5, 6, 43, 10, 6, 3, 7, 3, 7, 3, 7, 2, 2, 8, 2, 4, 6, 8, 10, 12, 2, 3, 3, 2, 6, 7, 2, 44, 2, 14, 3, 2, 2, 2, 4, 19, 3, 2, 2, 2, 6, 22, 3, 2, 2, 2, 8, 24, 3, 2, 2, 2, 10, 42, 3, 2, 2, 2, 12, 44, 3, 2, 2, 2, 14, 15, 5, 4, 3, 2, 15, 3, 3, 2, 2, 2, 16, 18, 5, 6, 4, 2, 17, 16, 3, 2, 2, 2, 18, 21, 3, 2, 2, 2, 19, 17, 3, 2, 2, 2, 19, 20, 3, 2, 2, 2, 20, 5, 3, 2, 2, 2, 21, 19, 3, 2, 2, 2, 22, 23, 5, 8, 5, 2, 23, 7, 3, 2, 2, 2, 24, 25, 7, 3, 2, 2, 25, 26, 5, 10, 6, 2, 26, 27, 7, 4, 2, 2, 27, 9, 3, 2, 2, 2, 28, 29, 5, 12, 7, 2, 29, 30, 7, 5, 2, 2, 30, 31, 5, 12, 7, 2, 31, 43, 3, 2, 2, 2, 32, 33, 5, 12, 7, 2, 33, 34, 9, 2, 2, 2, 34, 35, 5, 12, 7, 2, 35, 43, 3, 2, 2, 2, 36, 37, 5, 12, 7, 2, 37, 38, 7, 8, 2, 2, 38, 39, 5, 12, 7, 2, 39, 43, 3, 2, 2, 2, 40, 41, 7, 7, 2, 2, 41, 43, 5, 12, 7, 2, 42, 28, 3, 2, 2, 2, 42, 32, 3, 2, 2, 2, 42, 36, 3, 2, 2, 2, 42, 40, 3, 2, 2, 2, 43, 11, 3, 2, 2, 2, 44, 45, 7, 9, 2, 2, 45, 13, 3, 2, 2, 2, 4, 19, 42]

View File

@@ -1,4 +1,14 @@
INTEGER_LITERAL=1 PRINT=1
MULT=2 SCOL=2
WS=3 MULT=3
'*'=2 ADD=4
SUB=5
MOD=6
INTEGER_LITERAL=7
WS=8
'print'=1
';'=2
'*'=3
'+'=4
'-'=5
'%'=6

View File

@@ -1,18 +1,33 @@
token literal names: token literal names:
null null
null 'print'
';'
'*' '*'
'+'
'-'
'%'
null
null null
token symbolic names: token symbolic names:
null null
INTEGER_LITERAL PRINT
SCOL
MULT MULT
ADD
SUB
MOD
INTEGER_LITERAL
WS WS
rule names: rule names:
INTEGER_LITERAL PRINT
SCOL
MULT MULT
ADD
SUB
MOD
INTEGER_LITERAL
WS WS
channel names: channel names:
@@ -23,4 +38,4 @@ mode names:
DEFAULT_MODE DEFAULT_MODE
atn: atn:
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 5, 20, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 3, 2, 6, 2, 11, 10, 2, 13, 2, 14, 2, 12, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3, 4, 2, 2, 5, 3, 3, 5, 4, 7, 5, 3, 2, 4, 3, 2, 50, 59, 5, 2, 11, 12, 15, 15, 34, 34, 2, 20, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 3, 10, 3, 2, 2, 2, 5, 14, 3, 2, 2, 2, 7, 16, 3, 2, 2, 2, 9, 11, 9, 2, 2, 2, 10, 9, 3, 2, 2, 2, 11, 12, 3, 2, 2, 2, 12, 10, 3, 2, 2, 2, 12, 13, 3, 2, 2, 2, 13, 4, 3, 2, 2, 2, 14, 15, 7, 44, 2, 2, 15, 6, 3, 2, 2, 2, 16, 17, 9, 3, 2, 2, 17, 18, 3, 2, 2, 2, 18, 19, 8, 4, 2, 2, 19, 8, 3, 2, 2, 2, 4, 2, 12, 3, 8, 2, 2] [3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 10, 44, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 6, 8, 37, 10, 8, 13, 8, 14, 8, 38, 3, 9, 3, 9, 3, 9, 3, 9, 2, 2, 10, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 3, 2, 4, 3, 2, 50, 59, 5, 2, 11, 12, 15, 15, 34, 34, 2, 44, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 3, 19, 3, 2, 2, 2, 5, 25, 3, 2, 2, 2, 7, 27, 3, 2, 2, 2, 9, 29, 3, 2, 2, 2, 11, 31, 3, 2, 2, 2, 13, 33, 3, 2, 2, 2, 15, 36, 3, 2, 2, 2, 17, 40, 3, 2, 2, 2, 19, 20, 7, 114, 2, 2, 20, 21, 7, 116, 2, 2, 21, 22, 7, 107, 2, 2, 22, 23, 7, 112, 2, 2, 23, 24, 7, 118, 2, 2, 24, 4, 3, 2, 2, 2, 25, 26, 7, 61, 2, 2, 26, 6, 3, 2, 2, 2, 27, 28, 7, 44, 2, 2, 28, 8, 3, 2, 2, 2, 29, 30, 7, 45, 2, 2, 30, 10, 3, 2, 2, 2, 31, 32, 7, 47, 2, 2, 32, 12, 3, 2, 2, 2, 33, 34, 7, 39, 2, 2, 34, 14, 3, 2, 2, 2, 35, 37, 9, 2, 2, 2, 36, 35, 3, 2, 2, 2, 37, 38, 3, 2, 2, 2, 38, 36, 3, 2, 2, 2, 38, 39, 3, 2, 2, 2, 39, 16, 3, 2, 2, 2, 40, 41, 9, 3, 2, 2, 41, 42, 3, 2, 2, 2, 42, 43, 8, 9, 2, 2, 43, 18, 3, 2, 2, 2, 4, 2, 38, 3, 8, 2, 2]

View File

@@ -1,4 +1,4 @@
// Generated from /home/nitrix/Development/hsrm/cb/klang/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 by ANTLR 4.7.1 // Generated from /home/marvin/Documents/university/compiler/klang/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 by ANTLR 4.7.1
import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
@@ -16,7 +16,7 @@ public class KlangLexer extends Lexer {
protected static final PredictionContextCache _sharedContextCache = protected static final PredictionContextCache _sharedContextCache =
new PredictionContextCache(); new PredictionContextCache();
public static final int public static final int
INTEGER_LITERAL=1, MULT=2, WS=3; PRINT=1, SCOL=2, MULT=3, ADD=4, SUB=5, MOD=6, INTEGER_LITERAL=7, WS=8;
public static String[] channelNames = { public static String[] channelNames = {
"DEFAULT_TOKEN_CHANNEL", "HIDDEN" "DEFAULT_TOKEN_CHANNEL", "HIDDEN"
}; };
@@ -26,14 +26,15 @@ public class KlangLexer extends Lexer {
}; };
public static final String[] ruleNames = { public static final String[] ruleNames = {
"INTEGER_LITERAL", "MULT", "WS" "PRINT", "SCOL", "MULT", "ADD", "SUB", "MOD", "INTEGER_LITERAL", "WS"
}; };
private static final String[] _LITERAL_NAMES = { private static final String[] _LITERAL_NAMES = {
null, null, "'*'" null, "'print'", "';'", "'*'", "'+'", "'-'", "'%'"
}; };
private static final String[] _SYMBOLIC_NAMES = { private static final String[] _SYMBOLIC_NAMES = {
null, "INTEGER_LITERAL", "MULT", "WS" null, "PRINT", "SCOL", "MULT", "ADD", "SUB", "MOD", "INTEGER_LITERAL",
"WS"
}; };
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
@@ -93,13 +94,18 @@ public class KlangLexer extends Lexer {
public ATN getATN() { return _ATN; } public ATN getATN() { return _ATN; }
public static final String _serializedATN = public static final String _serializedATN =
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\5\24\b\1\4\2\t\2"+ "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\n,\b\1\4\2\t\2\4"+
"\4\3\t\3\4\4\t\4\3\2\6\2\13\n\2\r\2\16\2\f\3\3\3\3\3\4\3\4\3\4\3\4\2\2"+ "\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\3\2\3\2\3\2\3\2"+
"\5\3\3\5\4\7\5\3\2\4\3\2\62;\5\2\13\f\17\17\"\"\2\24\2\3\3\2\2\2\2\5\3"+ "\3\2\3\2\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3\6\3\7\3\7\3\b\6\b%\n\b\r\b\16\b"+
"\2\2\2\2\7\3\2\2\2\3\n\3\2\2\2\5\16\3\2\2\2\7\20\3\2\2\2\t\13\t\2\2\2"+ "&\3\t\3\t\3\t\3\t\2\2\n\3\3\5\4\7\5\t\6\13\7\r\b\17\t\21\n\3\2\4\3\2\62"+
"\n\t\3\2\2\2\13\f\3\2\2\2\f\n\3\2\2\2\f\r\3\2\2\2\r\4\3\2\2\2\16\17\7"+ ";\5\2\13\f\17\17\"\"\2,\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2"+
",\2\2\17\6\3\2\2\2\20\21\t\3\2\2\21\22\3\2\2\2\22\23\b\4\2\2\23\b\3\2"+ "\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2\3\23\3\2\2\2\5\31"+
"\2\2\4\2\f\3\b\2\2"; "\3\2\2\2\7\33\3\2\2\2\t\35\3\2\2\2\13\37\3\2\2\2\r!\3\2\2\2\17$\3\2\2"+
"\2\21(\3\2\2\2\23\24\7r\2\2\24\25\7t\2\2\25\26\7k\2\2\26\27\7p\2\2\27"+
"\30\7v\2\2\30\4\3\2\2\2\31\32\7=\2\2\32\6\3\2\2\2\33\34\7,\2\2\34\b\3"+
"\2\2\2\35\36\7-\2\2\36\n\3\2\2\2\37 \7/\2\2 \f\3\2\2\2!\"\7\'\2\2\"\16"+
"\3\2\2\2#%\t\2\2\2$#\3\2\2\2%&\3\2\2\2&$\3\2\2\2&\'\3\2\2\2\'\20\3\2\2"+
"\2()\t\3\2\2)*\3\2\2\2*+\b\t\2\2+\22\3\2\2\2\4\2&\3\b\2\2";
public static final ATN _ATN = public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray()); new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static { static {

View File

@@ -1,4 +1,14 @@
INTEGER_LITERAL=1 PRINT=1
MULT=2 SCOL=2
WS=3 MULT=3
'*'=2 ADD=4
SUB=5
MOD=6
INTEGER_LITERAL=7
WS=8
'print'=1
';'=2
'*'=3
'+'=4
'-'=5
'%'=6

View File

@@ -1,4 +1,4 @@
// Generated from /home/nitrix/Development/hsrm/cb/klang/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 by ANTLR 4.7.1 // Generated from /home/marvin/Documents/university/compiler/klang/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 by ANTLR 4.7.1
import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.*;
@@ -16,18 +16,20 @@ public class KlangParser extends Parser {
protected static final PredictionContextCache _sharedContextCache = protected static final PredictionContextCache _sharedContextCache =
new PredictionContextCache(); new PredictionContextCache();
public static final int public static final int
INTEGER_LITERAL=1, MULT=2, WS=3; PRINT=1, SCOL=2, MULT=3, ADD=4, SUB=5, MOD=6, INTEGER_LITERAL=7, WS=8;
public static final int public static final int
RULE_parse = 0, RULE_multiplicativeExpr = 1, RULE_unaryExpression = 2; RULE_parse = 0, RULE_block = 1, RULE_statement = 2, RULE_print = 3, RULE_expression = 4,
RULE_atom = 5;
public static final String[] ruleNames = { public static final String[] ruleNames = {
"parse", "multiplicativeExpr", "unaryExpression" "parse", "block", "statement", "print", "expression", "atom"
}; };
private static final String[] _LITERAL_NAMES = { private static final String[] _LITERAL_NAMES = {
null, null, "'*'" null, "'print'", "';'", "'*'", "'+'", "'-'", "'%'"
}; };
private static final String[] _SYMBOLIC_NAMES = { private static final String[] _SYMBOLIC_NAMES = {
null, "INTEGER_LITERAL", "MULT", "WS" null, "PRINT", "SCOL", "MULT", "ADD", "SUB", "MOD", "INTEGER_LITERAL",
"WS"
}; };
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
@@ -79,8 +81,8 @@ public class KlangParser extends Parser {
_interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
} }
public static class ParseContext extends ParserRuleContext { public static class ParseContext extends ParserRuleContext {
public MultiplicativeExprContext multiplicativeExpr() { public BlockContext block() {
return getRuleContext(MultiplicativeExprContext.class,0); return getRuleContext(BlockContext.class,0);
} }
public ParseContext(ParserRuleContext parent, int invokingState) { public ParseContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState); super(parent, invokingState);
@@ -94,8 +96,8 @@ public class KlangParser extends Parser {
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(6); setState(12);
multiplicativeExpr(); block();
} }
} }
catch (RecognitionException re) { catch (RecognitionException re) {
@@ -109,45 +111,37 @@ public class KlangParser extends Parser {
return _localctx; return _localctx;
} }
public static class MultiplicativeExprContext extends ParserRuleContext { public static class BlockContext extends ParserRuleContext {
public List<UnaryExpressionContext> unaryExpression() { public List<StatementContext> statement() {
return getRuleContexts(UnaryExpressionContext.class); return getRuleContexts(StatementContext.class);
} }
public UnaryExpressionContext unaryExpression(int i) { public StatementContext statement(int i) {
return getRuleContext(UnaryExpressionContext.class,i); return getRuleContext(StatementContext.class,i);
} }
public List<TerminalNode> MULT() { return getTokens(KlangParser.MULT); } public BlockContext(ParserRuleContext parent, int invokingState) {
public TerminalNode MULT(int i) {
return getToken(KlangParser.MULT, i);
}
public MultiplicativeExprContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState); super(parent, invokingState);
} }
@Override public int getRuleIndex() { return RULE_multiplicativeExpr; } @Override public int getRuleIndex() { return RULE_block; }
} }
public final MultiplicativeExprContext multiplicativeExpr() throws RecognitionException { public final BlockContext block() throws RecognitionException {
MultiplicativeExprContext _localctx = new MultiplicativeExprContext(_ctx, getState()); BlockContext _localctx = new BlockContext(_ctx, getState());
enterRule(_localctx, 2, RULE_multiplicativeExpr); enterRule(_localctx, 2, RULE_block);
int _la; int _la;
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(8); setState(17);
unaryExpression();
setState(13);
_errHandler.sync(this); _errHandler.sync(this);
_la = _input.LA(1); _la = _input.LA(1);
while (_la==MULT) { while (_la==PRINT) {
{ {
{ {
setState(9); setState(14);
match(MULT); statement();
setState(10);
unaryExpression();
} }
} }
setState(15); setState(19);
_errHandler.sync(this); _errHandler.sync(this);
_la = _input.LA(1); _la = _input.LA(1);
} }
@@ -164,21 +158,225 @@ public class KlangParser extends Parser {
return _localctx; return _localctx;
} }
public static class UnaryExpressionContext extends ParserRuleContext { public static class StatementContext extends ParserRuleContext {
public TerminalNode INTEGER_LITERAL() { return getToken(KlangParser.INTEGER_LITERAL, 0); } public PrintContext print() {
public UnaryExpressionContext(ParserRuleContext parent, int invokingState) { return getRuleContext(PrintContext.class,0);
}
public StatementContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState); super(parent, invokingState);
} }
@Override public int getRuleIndex() { return RULE_unaryExpression; } @Override public int getRuleIndex() { return RULE_statement; }
} }
public final UnaryExpressionContext unaryExpression() throws RecognitionException { public final StatementContext statement() throws RecognitionException {
UnaryExpressionContext _localctx = new UnaryExpressionContext(_ctx, getState()); StatementContext _localctx = new StatementContext(_ctx, getState());
enterRule(_localctx, 4, RULE_unaryExpression); enterRule(_localctx, 4, RULE_statement);
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(16); setState(20);
print();
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static class PrintContext extends ParserRuleContext {
public TerminalNode PRINT() { return getToken(KlangParser.PRINT, 0); }
public ExpressionContext expression() {
return getRuleContext(ExpressionContext.class,0);
}
public TerminalNode SCOL() { return getToken(KlangParser.SCOL, 0); }
public PrintContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_print; }
}
public final PrintContext print() throws RecognitionException {
PrintContext _localctx = new PrintContext(_ctx, getState());
enterRule(_localctx, 6, RULE_print);
try {
enterOuterAlt(_localctx, 1);
{
setState(22);
match(PRINT);
setState(23);
expression();
setState(24);
match(SCOL);
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static class ExpressionContext extends ParserRuleContext {
public ExpressionContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_expression; }
public ExpressionContext() { }
public void copyFrom(ExpressionContext ctx) {
super.copyFrom(ctx);
}
}
public static class AdditiveExpressionContext extends ExpressionContext {
public Token op;
public List<AtomContext> atom() {
return getRuleContexts(AtomContext.class);
}
public AtomContext atom(int i) {
return getRuleContext(AtomContext.class,i);
}
public TerminalNode ADD() { return getToken(KlangParser.ADD, 0); }
public TerminalNode SUB() { return getToken(KlangParser.SUB, 0); }
public AdditiveExpressionContext(ExpressionContext ctx) { copyFrom(ctx); }
}
public static class ModuloExpressionContext extends ExpressionContext {
public List<AtomContext> atom() {
return getRuleContexts(AtomContext.class);
}
public AtomContext atom(int i) {
return getRuleContext(AtomContext.class,i);
}
public TerminalNode MOD() { return getToken(KlangParser.MOD, 0); }
public ModuloExpressionContext(ExpressionContext ctx) { copyFrom(ctx); }
}
public static class UnaryNegateExpressionContext extends ExpressionContext {
public TerminalNode SUB() { return getToken(KlangParser.SUB, 0); }
public AtomContext atom() {
return getRuleContext(AtomContext.class,0);
}
public UnaryNegateExpressionContext(ExpressionContext ctx) { copyFrom(ctx); }
}
public static class MultiplicationExpressionContext extends ExpressionContext {
public List<AtomContext> atom() {
return getRuleContexts(AtomContext.class);
}
public AtomContext atom(int i) {
return getRuleContext(AtomContext.class,i);
}
public TerminalNode MULT() { return getToken(KlangParser.MULT, 0); }
public MultiplicationExpressionContext(ExpressionContext ctx) { copyFrom(ctx); }
}
public final ExpressionContext expression() throws RecognitionException {
ExpressionContext _localctx = new ExpressionContext(_ctx, getState());
enterRule(_localctx, 8, RULE_expression);
int _la;
try {
setState(40);
_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,1,_ctx) ) {
case 1:
_localctx = new MultiplicationExpressionContext(_localctx);
enterOuterAlt(_localctx, 1);
{
setState(26);
atom();
setState(27);
match(MULT);
setState(28);
atom();
}
break;
case 2:
_localctx = new AdditiveExpressionContext(_localctx);
enterOuterAlt(_localctx, 2);
{
setState(30);
atom();
setState(31);
((AdditiveExpressionContext)_localctx).op = _input.LT(1);
_la = _input.LA(1);
if ( !(_la==ADD || _la==SUB) ) {
((AdditiveExpressionContext)_localctx).op = (Token)_errHandler.recoverInline(this);
}
else {
if ( _input.LA(1)==Token.EOF ) matchedEOF = true;
_errHandler.reportMatch(this);
consume();
}
setState(32);
atom();
}
break;
case 3:
_localctx = new ModuloExpressionContext(_localctx);
enterOuterAlt(_localctx, 3);
{
setState(34);
atom();
setState(35);
match(MOD);
setState(36);
atom();
}
break;
case 4:
_localctx = new UnaryNegateExpressionContext(_localctx);
enterOuterAlt(_localctx, 4);
{
setState(38);
match(SUB);
setState(39);
atom();
}
break;
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static class AtomContext extends ParserRuleContext {
public AtomContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_atom; }
public AtomContext() { }
public void copyFrom(AtomContext ctx) {
super.copyFrom(ctx);
}
}
public static class IntAtomContext extends AtomContext {
public TerminalNode INTEGER_LITERAL() { return getToken(KlangParser.INTEGER_LITERAL, 0); }
public IntAtomContext(AtomContext ctx) { copyFrom(ctx); }
}
public final AtomContext atom() throws RecognitionException {
AtomContext _localctx = new AtomContext(_ctx, getState());
enterRule(_localctx, 10, RULE_atom);
try {
_localctx = new IntAtomContext(_localctx);
enterOuterAlt(_localctx, 1);
{
setState(42);
match(INTEGER_LITERAL); match(INTEGER_LITERAL);
} }
} }
@@ -194,12 +392,18 @@ public class KlangParser extends Parser {
} }
public static final String _serializedATN = public static final String _serializedATN =
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\5\25\4\2\t\2\4\3"+ "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\n/\4\2\t\2\4\3\t"+
"\t\3\4\4\t\4\3\2\3\2\3\3\3\3\3\3\7\3\16\n\3\f\3\16\3\21\13\3\3\4\3\4\3"+ "\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\3\2\3\2\3\3\7\3\22\n\3\f\3\16\3\25"+
"\4\2\2\5\2\4\6\2\2\2\22\2\b\3\2\2\2\4\n\3\2\2\2\6\22\3\2\2\2\b\t\5\4\3"+ "\13\3\3\4\3\4\3\5\3\5\3\5\3\5\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6"+
"\2\t\3\3\2\2\2\n\17\5\6\4\2\13\f\7\4\2\2\f\16\5\6\4\2\r\13\3\2\2\2\16"+ "\3\6\3\6\3\6\3\6\5\6+\n\6\3\7\3\7\3\7\2\2\b\2\4\6\b\n\f\2\3\3\2\6\7\2"+
"\21\3\2\2\2\17\r\3\2\2\2\17\20\3\2\2\2\20\5\3\2\2\2\21\17\3\2\2\2\22\23"+ ",\2\16\3\2\2\2\4\23\3\2\2\2\6\26\3\2\2\2\b\30\3\2\2\2\n*\3\2\2\2\f,\3"+
"\7\3\2\2\23\7\3\2\2\2\3\17"; "\2\2\2\16\17\5\4\3\2\17\3\3\2\2\2\20\22\5\6\4\2\21\20\3\2\2\2\22\25\3"+
"\2\2\2\23\21\3\2\2\2\23\24\3\2\2\2\24\5\3\2\2\2\25\23\3\2\2\2\26\27\5"+
"\b\5\2\27\7\3\2\2\2\30\31\7\3\2\2\31\32\5\n\6\2\32\33\7\4\2\2\33\t\3\2"+
"\2\2\34\35\5\f\7\2\35\36\7\5\2\2\36\37\5\f\7\2\37+\3\2\2\2 !\5\f\7\2!"+
"\"\t\2\2\2\"#\5\f\7\2#+\3\2\2\2$%\5\f\7\2%&\7\b\2\2&\'\5\f\7\2\'+\3\2"+
"\2\2()\7\7\2\2)+\5\f\7\2*\34\3\2\2\2* \3\2\2\2*$\3\2\2\2*(\3\2\2\2+\13"+
"\3\2\2\2,-\7\t\2\2-\r\3\2\2\2\4\23*";
public static final ATN _ATN = public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray()); new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static { static {

View File

@@ -1,25 +1,44 @@
grammar Klang; grammar Klang;
parse parse
: multiplicativeExpr <EOF> : block <EOF>
; ;
multiplicativeExpr block
: unaryExpression (MULT unaryExpression)* : statement*
; ;
unaryExpression statement
: INTEGER_LITERAL : print
; ;
print
: PRINT expression SCOL
;
expression
: atom MULT atom #multiplicationExpression
| atom op=(ADD | SUB) atom #additiveExpression
| atom MOD atom #moduloExpression
| SUB atom #unaryNegateExpression
;
atom
: INTEGER_LITERAL #intAtom
;
PRINT: 'print';
SCOL: ';';
MULT: '*';
ADD: '+';
SUB: '-';
MOD: '%';
INTEGER_LITERAL INTEGER_LITERAL
: [0-9]+ : [0-9]+
; ;
MULT
: '*'
;
WS WS
: [ \t\r\n] -> skip : [ \t\r\n] -> skip
; ;

View File

@@ -0,0 +1,71 @@
package de.hsrm.compiler.Klang;
public class JSVisitor extends KlangBaseVisitor<Void> {
private final StringBuilder sb;
public JSVisitor(StringBuilder sb) {
this.sb = sb;
}
@Override
public Void visitStatement(KlangParser.StatementContext ctx) {
for (int i = 0; i < ctx.children.size(); i++) {
this.visit(ctx.children.get(i));
sb.append("\n");
}
return null;
}
@Override
public Void visitPrint(KlangParser.PrintContext ctx) {
sb.append("console.log(");
this.visit(ctx.expression());
sb.append(");");
return null;
}
@Override
public Void visitMultiplicationExpression(KlangParser.MultiplicationExpressionContext ctx) {
this.visit(ctx.atom(0));
sb.append(" * ");
this.visit(ctx.atom(1));
return null;
}
@Override
public Void visitAdditiveExpression(KlangParser.AdditiveExpressionContext ctx) {
this.visit(ctx.atom(0));
switch (ctx.op.getType()) {
case KlangParser.ADD:
sb.append(" + ");
break;
case KlangParser.SUB:
sb.append(" - ");
break;
}
this.visit(ctx.atom(1));
return null;
}
@Override
public Void visitModuloExpression(KlangParser.ModuloExpressionContext ctx) {
this.visit(ctx.atom(0));
sb.append(" % ");
this.visit(ctx.atom(1));
return null;
}
@Override
public Void visitUnaryNegateExpression(KlangParser.UnaryNegateExpressionContext ctx) {
sb.append("-");
this.visit(ctx.atom());
return null;
}
@Override
public Void visitIntAtom(KlangParser.IntAtomContext ctx) {
sb.append(ctx.getText());
return null;
}
}

View File

@@ -19,8 +19,9 @@ public class Klang {
KlangParser parser = new KlangParser(tokens); KlangParser parser = new KlangParser(tokens);
ParseTree tree = parser.parse(); // begin parsing at init rule ParseTree tree = parser.parse(); // begin parsing at init rule
Visitor visitor = new Visitor(); StringBuilder sb = new StringBuilder();
int result = visitor.visit(tree); JSVisitor visitor = new JSVisitor(sb);
System.out.println(result); visitor.visit(tree);
System.out.println(sb.toString());
} }
} }

View File

@@ -0,0 +1,55 @@
package de.hsrm.compiler.Klang;
public class Value {
public static Value VOID = new Value(new Object());
final Object value;
public Value(Object value) {
this.value = value;
}
public Integer asInteger() {
return (Integer)value;
}
public String asString() {
return String.valueOf(value);
}
public boolean isDouble() {
return value instanceof Double;
}
@Override
public int hashCode() {
if(value == null) {
return 0;
}
return this.value.hashCode();
}
@Override
public boolean equals(Object o) {
if(value == o) {
return true;
}
if(value == null || o == null || o.getClass() != value.getClass()) {
return false;
}
Value that = (Value)o;
return this.value.equals(that.value);
}
@Override
public String toString() {
return String.valueOf(value);
}
}

View File

@@ -1,19 +1,51 @@
package de.hsrm.compiler.Klang; package de.hsrm.compiler.Klang;
public class Visitor extends KlangBaseVisitor<Integer> { public class Visitor extends KlangBaseVisitor<Value> {
@Override
public Integer visitMultiplicativeExpr(KlangParser.MultiplicativeExprContext ctx) { @Override
int result = 1; public Value visitPrint(KlangParser.PrintContext ctx) {
Value value = this.visit(ctx.expression());
for (var expr: ctx.unaryExpression()) { System.out.println(value);
result *= this.visit(expr); return value;
} }
return result; @Override
} public Value visitMultiplicationExpression(KlangParser.MultiplicationExpressionContext ctx) {
Value left = this.visit(ctx.atom(0));
@Override Value right = this.visit(ctx.atom(1));
public Integer visitUnaryExpression(KlangParser.UnaryExpressionContext ctx) { return new Value(left.asInteger() * right.asInteger());
return Integer.parseInt(ctx.getText()); }
@Override
public Value visitAdditiveExpression(KlangParser.AdditiveExpressionContext ctx) {
Value left = this.visit(ctx.atom(0));
Value right = this.visit(ctx.atom(1));
switch (ctx.op.getType()) {
case KlangParser.ADD:
return new Value(left.asInteger() + right.asInteger());
case KlangParser.SUB:
return new Value(left.asInteger() - right.asInteger());
default:
throw new RuntimeException("Unknown operator for additive expression: "+ KlangParser.VOCABULARY.getDisplayName(ctx.op.getType()));
} }
}
@Override
public Value visitModuloExpression(KlangParser.ModuloExpressionContext ctx) {
Value left = this.visit(ctx.atom(0));
Value right = this.visit(ctx.atom(1));
return new Value(left.asInteger() % right.asInteger());
}
@Override
public Value visitUnaryNegateExpression(KlangParser.UnaryNegateExpressionContext ctx) {
Value value = this.visit(ctx.atom());
return new Value(-value.asInteger());
}
@Override
public Value visitIntAtom(KlangParser.IntAtomContext ctx) {
return new Value(Integer.parseInt(ctx.getText()));
}
} }