Commit ef96a7ed by fukai

提交Antlr课程

parents
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.brilliance.whyf</groupId>
<artifactId>antlr-lession</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.9.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
grammar Calculator;
prog : stat+;
stat:
expr NEWLINE # print
| ID '=' expr NEWLINE # assign
| NEWLINE # blank
;
expr:
expr op=('*'|'/') expr # MulDiv
| expr op=('+'|'-') expr # AddSub
| INT # int
| ID # id
| '(' expr ')' # parenthese
;
MUL : '*' ;
DIV : '/' ;
ADD : '+' ;
SUB : '-' ;
ID : [a-zA-Z]+ ;
INT : [0-9]+ ;
NEWLINE :'\r'? '\n' ;
DELIMITER : ';';
WS : [ \t]+ -> skip;
\ No newline at end of file
package com.brilliance.whyf.calc.expand;
import com.brilliance.whyf.calc.gen.CalculatorBaseVisitor;
import com.brilliance.whyf.calc.gen.CalculatorParser;
import java.util.HashMap;
import java.util.Map;
public class CalculatorVisitorImpl extends CalculatorBaseVisitor<Integer> {
// 存储变量的值
private Map<String, Integer> variable;
public CalculatorVisitorImpl() {
variable = new HashMap<>();
}
// 当遇到print节点,计算出exrp的值,然后打印出来
@Override
public Integer visitPrint(CalculatorParser.PrintContext ctx) {
Integer result = ctx.expr().accept(this);
System.out.println(result);
return null;
}
// 分别获取子节点expr的值,然后做加减运算
@Override
public Integer visitAddSub(CalculatorParser.AddSubContext ctx) {
Integer param1 = ctx.expr(0).accept(this);
Integer param2 = ctx.expr(1).accept(this);
if (ctx.op.getType() == CalculatorParser.ADD) {
return param1 + param2;
}
else {
return param1 - param2;
}
}
// 分别获取子节点expr的值,然后做乘除运算
@Override
public Integer visitMulDiv(CalculatorParser.MulDivContext ctx) {
Integer param1 = ctx.expr(0).accept(this);
Integer param2 = ctx.expr(1).accept(this);
if (ctx.op.getType() == CalculatorParser.MUL) {
return param1 * param2;
}
else {
return param1 / param2;
}
}
// 当遇到int节点,直接返回数据
@Override
public Integer visitInt(CalculatorParser.IntContext ctx) {
return Integer.parseInt(ctx.getText());
}
// 当遇到Id节点,从变量表获取值
@Override
public Integer visitId(CalculatorParser.IdContext ctx) {
return variable.get(ctx.getText());
}
// 当遇到赋值语句,获取右边expr的值,然后将变量的值保存到variable集合
@Override
public Integer visitAssign(CalculatorParser.AssignContext ctx) {
String name = ctx.ID().getText();
Integer value = ctx.expr().accept(this);
variable.put(name, value);
return null;
}
}
token literal names:
null
'='
'('
')'
'*'
'/'
'+'
'-'
null
null
null
';'
null
token symbolic names:
null
null
null
null
MUL
DIV
ADD
SUB
ID
INT
NEWLINE
DELIMITER
WS
rule names:
prog
stat
expr
atn:
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 14, 45, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 3, 2, 6, 2, 10, 10, 2, 13, 2, 14, 2, 11, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 23, 10, 3, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 5, 4, 32, 10, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 7, 4, 40, 10, 4, 12, 4, 14, 4, 43, 11, 4, 3, 4, 2, 3, 6, 5, 2, 4, 6, 2, 4, 3, 2, 6, 7, 3, 2, 8, 9, 2, 48, 2, 9, 3, 2, 2, 2, 4, 22, 3, 2, 2, 2, 6, 31, 3, 2, 2, 2, 8, 10, 5, 4, 3, 2, 9, 8, 3, 2, 2, 2, 10, 11, 3, 2, 2, 2, 11, 9, 3, 2, 2, 2, 11, 12, 3, 2, 2, 2, 12, 3, 3, 2, 2, 2, 13, 14, 5, 6, 4, 2, 14, 15, 7, 12, 2, 2, 15, 23, 3, 2, 2, 2, 16, 17, 7, 10, 2, 2, 17, 18, 7, 3, 2, 2, 18, 19, 5, 6, 4, 2, 19, 20, 7, 12, 2, 2, 20, 23, 3, 2, 2, 2, 21, 23, 7, 12, 2, 2, 22, 13, 3, 2, 2, 2, 22, 16, 3, 2, 2, 2, 22, 21, 3, 2, 2, 2, 23, 5, 3, 2, 2, 2, 24, 25, 8, 4, 1, 2, 25, 32, 7, 11, 2, 2, 26, 32, 7, 10, 2, 2, 27, 28, 7, 4, 2, 2, 28, 29, 5, 6, 4, 2, 29, 30, 7, 5, 2, 2, 30, 32, 3, 2, 2, 2, 31, 24, 3, 2, 2, 2, 31, 26, 3, 2, 2, 2, 31, 27, 3, 2, 2, 2, 32, 41, 3, 2, 2, 2, 33, 34, 12, 7, 2, 2, 34, 35, 9, 2, 2, 2, 35, 40, 5, 6, 4, 8, 36, 37, 12, 6, 2, 2, 37, 38, 9, 3, 2, 2, 38, 40, 5, 6, 4, 7, 39, 33, 3, 2, 2, 2, 39, 36, 3, 2, 2, 2, 40, 43, 3, 2, 2, 2, 41, 39, 3, 2, 2, 2, 41, 42, 3, 2, 2, 2, 42, 7, 3, 2, 2, 2, 43, 41, 3, 2, 2, 2, 7, 11, 22, 31, 39, 41]
\ No newline at end of file
T__0=1
T__1=2
T__2=3
MUL=4
DIV=5
ADD=6
SUB=7
ID=8
INT=9
NEWLINE=10
DELIMITER=11
WS=12
'='=1
'('=2
')'=3
'*'=4
'/'=5
'+'=6
'-'=7
';'=11
// Generated from D:/Git/antlr-lession/src/main/java/com/brilliance/whyf/calc\Calculator.g4 by ANTLR 4.9.1
package com.brilliance.whyf.calc.gen;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.TerminalNode;
/**
* This class provides an empty implementation of {@link CalculatorListener},
* which can be extended to create a listener which only needs to handle a subset
* of the available methods.
*/
public class CalculatorBaseListener implements CalculatorListener {
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterProg(CalculatorParser.ProgContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitProg(CalculatorParser.ProgContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterPrint(CalculatorParser.PrintContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitPrint(CalculatorParser.PrintContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterAssign(CalculatorParser.AssignContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitAssign(CalculatorParser.AssignContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterBlank(CalculatorParser.BlankContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitBlank(CalculatorParser.BlankContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterMulDiv(CalculatorParser.MulDivContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitMulDiv(CalculatorParser.MulDivContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterAddSub(CalculatorParser.AddSubContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitAddSub(CalculatorParser.AddSubContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterParenthese(CalculatorParser.ParentheseContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitParenthese(CalculatorParser.ParentheseContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterId(CalculatorParser.IdContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitId(CalculatorParser.IdContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterInt(CalculatorParser.IntContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitInt(CalculatorParser.IntContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterEveryRule(ParserRuleContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitEveryRule(ParserRuleContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void visitTerminal(TerminalNode node) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void visitErrorNode(ErrorNode node) { }
}
\ No newline at end of file
// Generated from D:/Git/antlr-lession/src/main/java/com/brilliance/whyf/calc\Calculator.g4 by ANTLR 4.9.1
package com.brilliance.whyf.calc.gen;
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
/**
* This class provides an empty implementation of {@link CalculatorVisitor},
* which can be extended to create a visitor which only needs to handle a subset
* of the available methods.
*
* @param <T> The return type of the visit operation. Use {@link Void} for
* operations with no return type.
*/
public class CalculatorBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements CalculatorVisitor<T> {
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitProg(CalculatorParser.ProgContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitPrint(CalculatorParser.PrintContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitAssign(CalculatorParser.AssignContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitBlank(CalculatorParser.BlankContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitMulDiv(CalculatorParser.MulDivContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitAddSub(CalculatorParser.AddSubContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitParenthese(CalculatorParser.ParentheseContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitId(CalculatorParser.IdContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitInt(CalculatorParser.IntContext ctx) { return visitChildren(ctx); }
}
\ No newline at end of file
token literal names:
null
'='
'('
')'
'*'
'/'
'+'
'-'
null
null
null
';'
null
token symbolic names:
null
null
null
null
MUL
DIV
ADD
SUB
ID
INT
NEWLINE
DELIMITER
WS
rule names:
T__0
T__1
T__2
MUL
DIV
ADD
SUB
ID
INT
NEWLINE
DELIMITER
WS
channel names:
DEFAULT_TOKEN_CHANNEL
HIDDEN
mode names:
DEFAULT_MODE
atn:
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 14, 65, 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, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 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, 3, 8, 3, 9, 6, 9, 43, 10, 9, 13, 9, 14, 9, 44, 3, 10, 6, 10, 48, 10, 10, 13, 10, 14, 10, 49, 3, 11, 5, 11, 53, 10, 11, 3, 11, 3, 11, 3, 12, 3, 12, 3, 13, 6, 13, 60, 10, 13, 13, 13, 14, 13, 61, 3, 13, 3, 13, 2, 2, 14, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 14, 3, 2, 5, 4, 2, 67, 92, 99, 124, 3, 2, 50, 59, 4, 2, 11, 11, 34, 34, 2, 68, 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, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 3, 27, 3, 2, 2, 2, 5, 29, 3, 2, 2, 2, 7, 31, 3, 2, 2, 2, 9, 33, 3, 2, 2, 2, 11, 35, 3, 2, 2, 2, 13, 37, 3, 2, 2, 2, 15, 39, 3, 2, 2, 2, 17, 42, 3, 2, 2, 2, 19, 47, 3, 2, 2, 2, 21, 52, 3, 2, 2, 2, 23, 56, 3, 2, 2, 2, 25, 59, 3, 2, 2, 2, 27, 28, 7, 63, 2, 2, 28, 4, 3, 2, 2, 2, 29, 30, 7, 42, 2, 2, 30, 6, 3, 2, 2, 2, 31, 32, 7, 43, 2, 2, 32, 8, 3, 2, 2, 2, 33, 34, 7, 44, 2, 2, 34, 10, 3, 2, 2, 2, 35, 36, 7, 49, 2, 2, 36, 12, 3, 2, 2, 2, 37, 38, 7, 45, 2, 2, 38, 14, 3, 2, 2, 2, 39, 40, 7, 47, 2, 2, 40, 16, 3, 2, 2, 2, 41, 43, 9, 2, 2, 2, 42, 41, 3, 2, 2, 2, 43, 44, 3, 2, 2, 2, 44, 42, 3, 2, 2, 2, 44, 45, 3, 2, 2, 2, 45, 18, 3, 2, 2, 2, 46, 48, 9, 3, 2, 2, 47, 46, 3, 2, 2, 2, 48, 49, 3, 2, 2, 2, 49, 47, 3, 2, 2, 2, 49, 50, 3, 2, 2, 2, 50, 20, 3, 2, 2, 2, 51, 53, 7, 15, 2, 2, 52, 51, 3, 2, 2, 2, 52, 53, 3, 2, 2, 2, 53, 54, 3, 2, 2, 2, 54, 55, 7, 12, 2, 2, 55, 22, 3, 2, 2, 2, 56, 57, 7, 61, 2, 2, 57, 24, 3, 2, 2, 2, 58, 60, 9, 4, 2, 2, 59, 58, 3, 2, 2, 2, 60, 61, 3, 2, 2, 2, 61, 59, 3, 2, 2, 2, 61, 62, 3, 2, 2, 2, 62, 63, 3, 2, 2, 2, 63, 64, 8, 13, 2, 2, 64, 26, 3, 2, 2, 2, 7, 2, 44, 49, 52, 61, 3, 8, 2, 2]
\ No newline at end of file
// Generated from D:/Git/antlr-lession/src/main/java/com/brilliance/whyf/calc\Calculator.g4 by ANTLR 4.9.1
package com.brilliance.whyf.calc.gen;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.misc.*;
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
public class CalculatorLexer extends Lexer {
static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); }
protected static final DFA[] _decisionToDFA;
protected static final PredictionContextCache _sharedContextCache =
new PredictionContextCache();
public static final int
T__0=1, T__1=2, T__2=3, MUL=4, DIV=5, ADD=6, SUB=7, ID=8, INT=9, NEWLINE=10,
DELIMITER=11, WS=12;
public static String[] channelNames = {
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
};
public static String[] modeNames = {
"DEFAULT_MODE"
};
private static String[] makeRuleNames() {
return new String[] {
"T__0", "T__1", "T__2", "MUL", "DIV", "ADD", "SUB", "ID", "INT", "NEWLINE",
"DELIMITER", "WS"
};
}
public static final String[] ruleNames = makeRuleNames();
private static String[] makeLiteralNames() {
return new String[] {
null, "'='", "'('", "')'", "'*'", "'/'", "'+'", "'-'", null, null, null,
"';'"
};
}
private static final String[] _LITERAL_NAMES = makeLiteralNames();
private static String[] makeSymbolicNames() {
return new String[] {
null, null, null, null, "MUL", "DIV", "ADD", "SUB", "ID", "INT", "NEWLINE",
"DELIMITER", "WS"
};
}
private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames();
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
/**
* @deprecated Use {@link #VOCABULARY} instead.
*/
@Deprecated
public static final String[] tokenNames;
static {
tokenNames = new String[_SYMBOLIC_NAMES.length];
for (int i = 0; i < tokenNames.length; i++) {
tokenNames[i] = VOCABULARY.getLiteralName(i);
if (tokenNames[i] == null) {
tokenNames[i] = VOCABULARY.getSymbolicName(i);
}
if (tokenNames[i] == null) {
tokenNames[i] = "<INVALID>";
}
}
}
@Override
@Deprecated
public String[] getTokenNames() {
return tokenNames;
}
@Override
public Vocabulary getVocabulary() {
return VOCABULARY;
}
public CalculatorLexer(CharStream input) {
super(input);
_interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
}
@Override
public String getGrammarFileName() { return "Calculator.g4"; }
@Override
public String[] getRuleNames() { return ruleNames; }
@Override
public String getSerializedATN() { return _serializedATN; }
@Override
public String[] getChannelNames() { return channelNames; }
@Override
public String[] getModeNames() { return modeNames; }
@Override
public ATN getATN() { return _ATN; }
public static final String _serializedATN =
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\16A\b\1\4\2\t\2\4"+
"\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\4\n\t\n\4\13\t"+
"\13\4\f\t\f\4\r\t\r\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\3\b\3\t\6\t+\n\t\r\t\16\t,\3\n\6\n\60\n\n\r\n\16\n\61\3\13\5\13\65"+
"\n\13\3\13\3\13\3\f\3\f\3\r\6\r<\n\r\r\r\16\r=\3\r\3\r\2\2\16\3\3\5\4"+
"\7\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27\r\31\16\3\2\5\4\2C\\c|\3\2\62"+
";\4\2\13\13\"\"\2D\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\13"+
"\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2\2\23\3\2\2\2\2\25\3\2\2"+
"\2\2\27\3\2\2\2\2\31\3\2\2\2\3\33\3\2\2\2\5\35\3\2\2\2\7\37\3\2\2\2\t"+
"!\3\2\2\2\13#\3\2\2\2\r%\3\2\2\2\17\'\3\2\2\2\21*\3\2\2\2\23/\3\2\2\2"+
"\25\64\3\2\2\2\278\3\2\2\2\31;\3\2\2\2\33\34\7?\2\2\34\4\3\2\2\2\35\36"+
"\7*\2\2\36\6\3\2\2\2\37 \7+\2\2 \b\3\2\2\2!\"\7,\2\2\"\n\3\2\2\2#$\7\61"+
"\2\2$\f\3\2\2\2%&\7-\2\2&\16\3\2\2\2\'(\7/\2\2(\20\3\2\2\2)+\t\2\2\2*"+
")\3\2\2\2+,\3\2\2\2,*\3\2\2\2,-\3\2\2\2-\22\3\2\2\2.\60\t\3\2\2/.\3\2"+
"\2\2\60\61\3\2\2\2\61/\3\2\2\2\61\62\3\2\2\2\62\24\3\2\2\2\63\65\7\17"+
"\2\2\64\63\3\2\2\2\64\65\3\2\2\2\65\66\3\2\2\2\66\67\7\f\2\2\67\26\3\2"+
"\2\289\7=\2\29\30\3\2\2\2:<\t\4\2\2;:\3\2\2\2<=\3\2\2\2=;\3\2\2\2=>\3"+
"\2\2\2>?\3\2\2\2?@\b\r\2\2@\32\3\2\2\2\7\2,\61\64=\3\b\2\2";
public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static {
_decisionToDFA = new DFA[_ATN.getNumberOfDecisions()];
for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) {
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}
\ No newline at end of file
T__0=1
T__1=2
T__2=3
MUL=4
DIV=5
ADD=6
SUB=7
ID=8
INT=9
NEWLINE=10
DELIMITER=11
WS=12
'='=1
'('=2
')'=3
'*'=4
'/'=5
'+'=6
'-'=7
';'=11
// Generated from D:/Git/antlr-lession/src/main/java/com/brilliance/whyf/calc\Calculator.g4 by ANTLR 4.9.1
package com.brilliance.whyf.calc.gen;
import org.antlr.v4.runtime.tree.ParseTreeListener;
/**
* This interface defines a complete listener for a parse tree produced by
* {@link CalculatorParser}.
*/
public interface CalculatorListener extends ParseTreeListener {
/**
* Enter a parse tree produced by {@link CalculatorParser#prog}.
* @param ctx the parse tree
*/
void enterProg(CalculatorParser.ProgContext ctx);
/**
* Exit a parse tree produced by {@link CalculatorParser#prog}.
* @param ctx the parse tree
*/
void exitProg(CalculatorParser.ProgContext ctx);
/**
* Enter a parse tree produced by the {@code print}
* labeled alternative in {@link CalculatorParser#stat}.
* @param ctx the parse tree
*/
void enterPrint(CalculatorParser.PrintContext ctx);
/**
* Exit a parse tree produced by the {@code print}
* labeled alternative in {@link CalculatorParser#stat}.
* @param ctx the parse tree
*/
void exitPrint(CalculatorParser.PrintContext ctx);
/**
* Enter a parse tree produced by the {@code assign}
* labeled alternative in {@link CalculatorParser#stat}.
* @param ctx the parse tree
*/
void enterAssign(CalculatorParser.AssignContext ctx);
/**
* Exit a parse tree produced by the {@code assign}
* labeled alternative in {@link CalculatorParser#stat}.
* @param ctx the parse tree
*/
void exitAssign(CalculatorParser.AssignContext ctx);
/**
* Enter a parse tree produced by the {@code blank}
* labeled alternative in {@link CalculatorParser#stat}.
* @param ctx the parse tree
*/
void enterBlank(CalculatorParser.BlankContext ctx);
/**
* Exit a parse tree produced by the {@code blank}
* labeled alternative in {@link CalculatorParser#stat}.
* @param ctx the parse tree
*/
void exitBlank(CalculatorParser.BlankContext ctx);
/**
* Enter a parse tree produced by the {@code MulDiv}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
*/
void enterMulDiv(CalculatorParser.MulDivContext ctx);
/**
* Exit a parse tree produced by the {@code MulDiv}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
*/
void exitMulDiv(CalculatorParser.MulDivContext ctx);
/**
* Enter a parse tree produced by the {@code AddSub}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
*/
void enterAddSub(CalculatorParser.AddSubContext ctx);
/**
* Exit a parse tree produced by the {@code AddSub}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
*/
void exitAddSub(CalculatorParser.AddSubContext ctx);
/**
* Enter a parse tree produced by the {@code parenthese}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
*/
void enterParenthese(CalculatorParser.ParentheseContext ctx);
/**
* Exit a parse tree produced by the {@code parenthese}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
*/
void exitParenthese(CalculatorParser.ParentheseContext ctx);
/**
* Enter a parse tree produced by the {@code id}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
*/
void enterId(CalculatorParser.IdContext ctx);
/**
* Exit a parse tree produced by the {@code id}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
*/
void exitId(CalculatorParser.IdContext ctx);
/**
* Enter a parse tree produced by the {@code int}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
*/
void enterInt(CalculatorParser.IntContext ctx);
/**
* Exit a parse tree produced by the {@code int}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
*/
void exitInt(CalculatorParser.IntContext ctx);
}
\ No newline at end of file
// Generated from D:/Git/antlr-lession/src/main/java/com/brilliance/whyf/calc\Calculator.g4 by ANTLR 4.9.1
package com.brilliance.whyf.calc.gen;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.misc.*;
import org.antlr.v4.runtime.tree.*;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
public class CalculatorParser extends Parser {
static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); }
protected static final DFA[] _decisionToDFA;
protected static final PredictionContextCache _sharedContextCache =
new PredictionContextCache();
public static final int
T__0=1, T__1=2, T__2=3, MUL=4, DIV=5, ADD=6, SUB=7, ID=8, INT=9, NEWLINE=10,
DELIMITER=11, WS=12;
public static final int
RULE_prog = 0, RULE_stat = 1, RULE_expr = 2;
private static String[] makeRuleNames() {
return new String[] {
"prog", "stat", "expr"
};
}
public static final String[] ruleNames = makeRuleNames();
private static String[] makeLiteralNames() {
return new String[] {
null, "'='", "'('", "')'", "'*'", "'/'", "'+'", "'-'", null, null, null,
"';'"
};
}
private static final String[] _LITERAL_NAMES = makeLiteralNames();
private static String[] makeSymbolicNames() {
return new String[] {
null, null, null, null, "MUL", "DIV", "ADD", "SUB", "ID", "INT", "NEWLINE",
"DELIMITER", "WS"
};
}
private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames();
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
/**
* @deprecated Use {@link #VOCABULARY} instead.
*/
@Deprecated
public static final String[] tokenNames;
static {
tokenNames = new String[_SYMBOLIC_NAMES.length];
for (int i = 0; i < tokenNames.length; i++) {
tokenNames[i] = VOCABULARY.getLiteralName(i);
if (tokenNames[i] == null) {
tokenNames[i] = VOCABULARY.getSymbolicName(i);
}
if (tokenNames[i] == null) {
tokenNames[i] = "<INVALID>";
}
}
}
@Override
@Deprecated
public String[] getTokenNames() {
return tokenNames;
}
@Override
public Vocabulary getVocabulary() {
return VOCABULARY;
}
@Override
public String getGrammarFileName() { return "Calculator.g4"; }
@Override
public String[] getRuleNames() { return ruleNames; }
@Override
public String getSerializedATN() { return _serializedATN; }
@Override
public ATN getATN() { return _ATN; }
public CalculatorParser(TokenStream input) {
super(input);
_interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
}
public static class ProgContext extends ParserRuleContext {
public List<StatContext> stat() {
return getRuleContexts(StatContext.class);
}
public StatContext stat(int i) {
return getRuleContext(StatContext.class,i);
}
public ProgContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_prog; }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).enterProg(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).exitProg(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
if ( visitor instanceof CalculatorVisitor ) return ((CalculatorVisitor<? extends T>)visitor).visitProg(this);
else return visitor.visitChildren(this);
}
}
public final ProgContext prog() throws RecognitionException {
ProgContext _localctx = new ProgContext(_ctx, getState());
enterRule(_localctx, 0, RULE_prog);
int _la;
try {
enterOuterAlt(_localctx, 1);
{
setState(7);
_errHandler.sync(this);
_la = _input.LA(1);
do {
{
{
setState(6);
stat();
}
}
setState(9);
_errHandler.sync(this);
_la = _input.LA(1);
} while ( (((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__1) | (1L << ID) | (1L << INT) | (1L << NEWLINE))) != 0) );
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static class StatContext extends ParserRuleContext {
public StatContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_stat; }
public StatContext() { }
public void copyFrom(StatContext ctx) {
super.copyFrom(ctx);
}
}
public static class PrintContext extends StatContext {
public ExprContext expr() {
return getRuleContext(ExprContext.class,0);
}
public TerminalNode NEWLINE() { return getToken(CalculatorParser.NEWLINE, 0); }
public PrintContext(StatContext ctx) { copyFrom(ctx); }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).enterPrint(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).exitPrint(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
if ( visitor instanceof CalculatorVisitor ) return ((CalculatorVisitor<? extends T>)visitor).visitPrint(this);
else return visitor.visitChildren(this);
}
}
public static class BlankContext extends StatContext {
public TerminalNode NEWLINE() { return getToken(CalculatorParser.NEWLINE, 0); }
public BlankContext(StatContext ctx) { copyFrom(ctx); }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).enterBlank(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).exitBlank(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
if ( visitor instanceof CalculatorVisitor ) return ((CalculatorVisitor<? extends T>)visitor).visitBlank(this);
else return visitor.visitChildren(this);
}
}
public static class AssignContext extends StatContext {
public TerminalNode ID() { return getToken(CalculatorParser.ID, 0); }
public ExprContext expr() {
return getRuleContext(ExprContext.class,0);
}
public TerminalNode NEWLINE() { return getToken(CalculatorParser.NEWLINE, 0); }
public AssignContext(StatContext ctx) { copyFrom(ctx); }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).enterAssign(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).exitAssign(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
if ( visitor instanceof CalculatorVisitor ) return ((CalculatorVisitor<? extends T>)visitor).visitAssign(this);
else return visitor.visitChildren(this);
}
}
public final StatContext stat() throws RecognitionException {
StatContext _localctx = new StatContext(_ctx, getState());
enterRule(_localctx, 2, RULE_stat);
try {
setState(20);
_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,1,_ctx) ) {
case 1:
_localctx = new PrintContext(_localctx);
enterOuterAlt(_localctx, 1);
{
setState(11);
expr(0);
setState(12);
match(NEWLINE);
}
break;
case 2:
_localctx = new AssignContext(_localctx);
enterOuterAlt(_localctx, 2);
{
setState(14);
match(ID);
setState(15);
match(T__0);
setState(16);
expr(0);
setState(17);
match(NEWLINE);
}
break;
case 3:
_localctx = new BlankContext(_localctx);
enterOuterAlt(_localctx, 3);
{
setState(19);
match(NEWLINE);
}
break;
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static class ExprContext extends ParserRuleContext {
public ExprContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_expr; }
public ExprContext() { }
public void copyFrom(ExprContext ctx) {
super.copyFrom(ctx);
}
}
public static class MulDivContext extends ExprContext {
public Token op;
public List<ExprContext> expr() {
return getRuleContexts(ExprContext.class);
}
public ExprContext expr(int i) {
return getRuleContext(ExprContext.class,i);
}
public TerminalNode MUL() { return getToken(CalculatorParser.MUL, 0); }
public TerminalNode DIV() { return getToken(CalculatorParser.DIV, 0); }
public MulDivContext(ExprContext ctx) { copyFrom(ctx); }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).enterMulDiv(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).exitMulDiv(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
if ( visitor instanceof CalculatorVisitor ) return ((CalculatorVisitor<? extends T>)visitor).visitMulDiv(this);
else return visitor.visitChildren(this);
}
}
public static class AddSubContext extends ExprContext {
public Token op;
public List<ExprContext> expr() {
return getRuleContexts(ExprContext.class);
}
public ExprContext expr(int i) {
return getRuleContext(ExprContext.class,i);
}
public TerminalNode ADD() { return getToken(CalculatorParser.ADD, 0); }
public TerminalNode SUB() { return getToken(CalculatorParser.SUB, 0); }
public AddSubContext(ExprContext ctx) { copyFrom(ctx); }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).enterAddSub(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).exitAddSub(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
if ( visitor instanceof CalculatorVisitor ) return ((CalculatorVisitor<? extends T>)visitor).visitAddSub(this);
else return visitor.visitChildren(this);
}
}
public static class ParentheseContext extends ExprContext {
public ExprContext expr() {
return getRuleContext(ExprContext.class,0);
}
public ParentheseContext(ExprContext ctx) { copyFrom(ctx); }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).enterParenthese(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).exitParenthese(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
if ( visitor instanceof CalculatorVisitor ) return ((CalculatorVisitor<? extends T>)visitor).visitParenthese(this);
else return visitor.visitChildren(this);
}
}
public static class IdContext extends ExprContext {
public TerminalNode ID() { return getToken(CalculatorParser.ID, 0); }
public IdContext(ExprContext ctx) { copyFrom(ctx); }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).enterId(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).exitId(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
if ( visitor instanceof CalculatorVisitor ) return ((CalculatorVisitor<? extends T>)visitor).visitId(this);
else return visitor.visitChildren(this);
}
}
public static class IntContext extends ExprContext {
public TerminalNode INT() { return getToken(CalculatorParser.INT, 0); }
public IntContext(ExprContext ctx) { copyFrom(ctx); }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).enterInt(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof CalculatorListener ) ((CalculatorListener)listener).exitInt(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
if ( visitor instanceof CalculatorVisitor ) return ((CalculatorVisitor<? extends T>)visitor).visitInt(this);
else return visitor.visitChildren(this);
}
}
public final ExprContext expr() throws RecognitionException {
return expr(0);
}
private ExprContext expr(int _p) throws RecognitionException {
ParserRuleContext _parentctx = _ctx;
int _parentState = getState();
ExprContext _localctx = new ExprContext(_ctx, _parentState);
ExprContext _prevctx = _localctx;
int _startState = 4;
enterRecursionRule(_localctx, 4, RULE_expr, _p);
int _la;
try {
int _alt;
enterOuterAlt(_localctx, 1);
{
setState(29);
_errHandler.sync(this);
switch (_input.LA(1)) {
case INT:
{
_localctx = new IntContext(_localctx);
_ctx = _localctx;
_prevctx = _localctx;
setState(23);
match(INT);
}
break;
case ID:
{
_localctx = new IdContext(_localctx);
_ctx = _localctx;
_prevctx = _localctx;
setState(24);
match(ID);
}
break;
case T__1:
{
_localctx = new ParentheseContext(_localctx);
_ctx = _localctx;
_prevctx = _localctx;
setState(25);
match(T__1);
setState(26);
expr(0);
setState(27);
match(T__2);
}
break;
default:
throw new NoViableAltException(this);
}
_ctx.stop = _input.LT(-1);
setState(39);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input,4,_ctx);
while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) {
if ( _alt==1 ) {
if ( _parseListeners!=null ) triggerExitRuleEvent();
_prevctx = _localctx;
{
setState(37);
_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,3,_ctx) ) {
case 1:
{
_localctx = new MulDivContext(new ExprContext(_parentctx, _parentState));
pushNewRecursionContext(_localctx, _startState, RULE_expr);
setState(31);
if (!(precpred(_ctx, 5))) throw new FailedPredicateException(this, "precpred(_ctx, 5)");
setState(32);
((MulDivContext)_localctx).op = _input.LT(1);
_la = _input.LA(1);
if ( !(_la==MUL || _la==DIV) ) {
((MulDivContext)_localctx).op = (Token)_errHandler.recoverInline(this);
}
else {
if ( _input.LA(1)==Token.EOF ) matchedEOF = true;
_errHandler.reportMatch(this);
consume();
}
setState(33);
expr(6);
}
break;
case 2:
{
_localctx = new AddSubContext(new ExprContext(_parentctx, _parentState));
pushNewRecursionContext(_localctx, _startState, RULE_expr);
setState(34);
if (!(precpred(_ctx, 4))) throw new FailedPredicateException(this, "precpred(_ctx, 4)");
setState(35);
((AddSubContext)_localctx).op = _input.LT(1);
_la = _input.LA(1);
if ( !(_la==ADD || _la==SUB) ) {
((AddSubContext)_localctx).op = (Token)_errHandler.recoverInline(this);
}
else {
if ( _input.LA(1)==Token.EOF ) matchedEOF = true;
_errHandler.reportMatch(this);
consume();
}
setState(36);
expr(5);
}
break;
}
}
}
setState(41);
_errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input,4,_ctx);
}
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
unrollRecursionContexts(_parentctx);
}
return _localctx;
}
public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) {
switch (ruleIndex) {
case 2:
return expr_sempred((ExprContext)_localctx, predIndex);
}
return true;
}
private boolean expr_sempred(ExprContext _localctx, int predIndex) {
switch (predIndex) {
case 0:
return precpred(_ctx, 5);
case 1:
return precpred(_ctx, 4);
}
return true;
}
public static final String _serializedATN =
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\16-\4\2\t\2\4\3\t"+
"\3\4\4\t\4\3\2\6\2\n\n\2\r\2\16\2\13\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3"+
"\3\5\3\27\n\3\3\4\3\4\3\4\3\4\3\4\3\4\3\4\5\4 \n\4\3\4\3\4\3\4\3\4\3\4"+
"\3\4\7\4(\n\4\f\4\16\4+\13\4\3\4\2\3\6\5\2\4\6\2\4\3\2\6\7\3\2\b\t\2\60"+
"\2\t\3\2\2\2\4\26\3\2\2\2\6\37\3\2\2\2\b\n\5\4\3\2\t\b\3\2\2\2\n\13\3"+
"\2\2\2\13\t\3\2\2\2\13\f\3\2\2\2\f\3\3\2\2\2\r\16\5\6\4\2\16\17\7\f\2"+
"\2\17\27\3\2\2\2\20\21\7\n\2\2\21\22\7\3\2\2\22\23\5\6\4\2\23\24\7\f\2"+
"\2\24\27\3\2\2\2\25\27\7\f\2\2\26\r\3\2\2\2\26\20\3\2\2\2\26\25\3\2\2"+
"\2\27\5\3\2\2\2\30\31\b\4\1\2\31 \7\13\2\2\32 \7\n\2\2\33\34\7\4\2\2\34"+
"\35\5\6\4\2\35\36\7\5\2\2\36 \3\2\2\2\37\30\3\2\2\2\37\32\3\2\2\2\37\33"+
"\3\2\2\2 )\3\2\2\2!\"\f\7\2\2\"#\t\2\2\2#(\5\6\4\b$%\f\6\2\2%&\t\3\2\2"+
"&(\5\6\4\7\'!\3\2\2\2\'$\3\2\2\2(+\3\2\2\2)\'\3\2\2\2)*\3\2\2\2*\7\3\2"+
"\2\2+)\3\2\2\2\7\13\26\37\')";
public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static {
_decisionToDFA = new DFA[_ATN.getNumberOfDecisions()];
for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) {
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}
\ No newline at end of file
// Generated from D:/Git/antlr-lession/src/main/java/com/brilliance/whyf/calc\Calculator.g4 by ANTLR 4.9.1
package com.brilliance.whyf.calc.gen;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
/**
* This interface defines a complete generic visitor for a parse tree produced
* by {@link CalculatorParser}.
*
* @param <T> The return type of the visit operation. Use {@link Void} for
* operations with no return type.
*/
public interface CalculatorVisitor<T> extends ParseTreeVisitor<T> {
/**
* Visit a parse tree produced by {@link CalculatorParser#prog}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitProg(CalculatorParser.ProgContext ctx);
/**
* Visit a parse tree produced by the {@code print}
* labeled alternative in {@link CalculatorParser#stat}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitPrint(CalculatorParser.PrintContext ctx);
/**
* Visit a parse tree produced by the {@code assign}
* labeled alternative in {@link CalculatorParser#stat}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitAssign(CalculatorParser.AssignContext ctx);
/**
* Visit a parse tree produced by the {@code blank}
* labeled alternative in {@link CalculatorParser#stat}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitBlank(CalculatorParser.BlankContext ctx);
/**
* Visit a parse tree produced by the {@code MulDiv}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitMulDiv(CalculatorParser.MulDivContext ctx);
/**
* Visit a parse tree produced by the {@code AddSub}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitAddSub(CalculatorParser.AddSubContext ctx);
/**
* Visit a parse tree produced by the {@code parenthese}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitParenthese(CalculatorParser.ParentheseContext ctx);
/**
* Visit a parse tree produced by the {@code id}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitId(CalculatorParser.IdContext ctx);
/**
* Visit a parse tree produced by the {@code int}
* labeled alternative in {@link CalculatorParser#expr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitInt(CalculatorParser.IntContext ctx);
}
\ No newline at end of file
package com.brilliance.whyf.antlrv4.test;
import com.brilliance.whyf.calc.expand.CalculatorVisitorImpl;
import com.brilliance.whyf.calc.gen.CalculatorLexer;
import com.brilliance.whyf.calc.gen.CalculatorParser;
import com.brilliance.whyf.calc.gen.CalculatorVisitor;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.junit.Test;
public class TestExpre {
@Test
public void testExpr1()
{
String expression = "a = 12\n" +
"b = a * 4\n" +
"a + b\n";
CalculatorLexer lexer = new CalculatorLexer(CharStreams.fromString(expression));
CommonTokenStream tokens = new CommonTokenStream(lexer);
CalculatorParser parser = new CalculatorParser(tokens);
parser.setBuildParseTree(true);
ParseTree root = parser.prog();
CalculatorVisitor<Integer> visitor = new CalculatorVisitorImpl();
root.accept(visitor);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment