Last active
December 2, 2019 04:21
-
-
Save lrlucena/b150cba803ddab1005d3 to your computer and use it in GitHub Desktop.
Calculadora usando ANTLR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grammar Calculadora; | |
prog: expr; | |
expr: expr op=('*'|'/') expr # OpBin | |
| expr op=('+'|'-') expr # OpBin | |
| '(' expr ')' # par | |
| INT # num | |
; | |
INT : ('0'..'9')+ ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.edu.ifrn.calculadora; | |
import org.antlr.v4.runtime.misc.NotNull; | |
import org.antlr.v4.runtime.tree.ParseTree; | |
import org.antlr.v4.runtime.tree.ParseTreeProperty; | |
public class MinhaCalculadora extends CalculadoraBaseListener { | |
ParseTreeProperty<Integer> values = new ParseTreeProperty<>(); | |
public void setValue(ParseTree node, int value) { | |
values.put(node, value); | |
} | |
public int getValue(ParseTree node) { | |
return values.get(node); | |
} | |
@Override | |
public void exitPar(@NotNull CalculadoraParser.ParContext ctx) { | |
int valor = getValue(ctx.expr()); | |
setValue(ctx, valor); | |
} | |
@Override | |
public void exitNum(@NotNull CalculadoraParser.NumContext ctx) { | |
int valor = Integer.parseInt(ctx.INT().getText()); | |
setValue(ctx, valor); | |
} | |
private int resposta; | |
@Override | |
public void exitProg(@NotNull CalculadoraParser.ProgContext ctx) { | |
resposta = getValue(ctx.expr()); | |
} | |
@Override | |
public void exitOpBin(@NotNull CalculadoraParser.OpBinContext ctx) { | |
int esq = getValue(ctx.expr(0)); | |
int dir = getValue(ctx.expr(1)); | |
final int valor; | |
switch (ctx.op.getText()) { | |
case "*": | |
valor = esq * dir; | |
break; | |
case "/": | |
valor = esq / dir; | |
break; | |
case "+": | |
valor = esq + dir; | |
break; | |
default: | |
valor = esq - dir; | |
} | |
setValue(ctx, valor); | |
} | |
public int resultado() { | |
return resposta; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.edu.ifrn.calculadora; | |
import org.antlr.v4.runtime.misc.NotNull; | |
import org.antlr.v4.runtime.tree.ParseTree; | |
import org.antlr.v4.runtime.tree.ParseTreeProperty; | |
public class MinhaCalculadoraJava extends CalculadoraBaseListener { | |
ParseTreeProperty<String> values = new ParseTreeProperty<>(); | |
public void setValue(ParseTree node, String value) { | |
values.put(node, value); | |
} | |
public String getValue(ParseTree node) { | |
return values.get(node); | |
} | |
@Override | |
public void exitPar(@NotNull CalculadoraParser.ParContext ctx) { | |
String valor = getValue(ctx.expr()); | |
setValue(ctx, valor); | |
} | |
@Override | |
public void exitNum(@NotNull CalculadoraParser.NumContext ctx) { | |
setValue(ctx, ctx.INT().getText()); | |
} | |
@Override | |
public void exitProg(@NotNull CalculadoraParser.ProgContext ctx) { | |
resposta = "public class Calculadora {\n" | |
+ " public static void main(String ... arg) {\n" | |
+ " System.out.println(" + getValue(ctx.expr()) + ");\n" | |
+ " }\n" + "}"; | |
} | |
@Override | |
public void exitOpBin(@NotNull CalculadoraParser.OpBinContext ctx) { | |
String esq = getValue(ctx.expr(0)); | |
String dir = getValue(ctx.expr(1)); | |
final String valor; | |
switch (ctx.op.getText()) { | |
case "*": | |
valor = "(" + esq + " * " + dir + ")"; | |
break; | |
case "/": | |
valor = "(" + esq + " / " + dir + ")"; | |
break; | |
case "+": | |
valor = "(" + esq + " + " + dir + ")"; | |
break; | |
default: | |
valor = "(" + esq + " - " + dir + ")"; | |
} | |
setValue(ctx, valor); | |
} | |
private String resposta; | |
public String resultado() { | |
return resposta; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.edu.ifrn.calculadora; | |
import java.io.IOException; | |
import org.antlr.v4.runtime.ANTLRInputStream; | |
import org.antlr.v4.runtime.CommonTokenStream; | |
import org.antlr.v4.runtime.tree.ParseTree; | |
import org.antlr.v4.runtime.tree.ParseTreeWalker; | |
public class Principal { | |
private static ParseTree parse(String programa) { | |
final ANTLRInputStream input = new ANTLRInputStream(programa); | |
final CalculadoraLexer lexer = new CalculadoraLexer(input); | |
final CommonTokenStream tokens = new CommonTokenStream(lexer); | |
final CalculadoraParser parser = new CalculadoraParser(tokens); | |
return parser.prog(); | |
} | |
public static void main(String... args) throws IOException { | |
String programa = "1+2*(3-4)*5/2"; | |
final ParseTree tree = parse(programa); | |
final ParseTreeWalker walker = new ParseTreeWalker(); | |
// Percorrendo a árvore para avaliar o programa | |
final MinhaCalculadora listener = new MinhaCalculadora(); | |
walker.walk(listener, tree); | |
int resultado = listener.resultado(); | |
// Percorrendo a árvore para gerar um programa em Java | |
final MinhaCalculadoraJava listenerJava = new MinhaCalculadoraJava(); | |
walker.walk(listenerJava, tree); | |
String resultadoJava = listenerJava.resultado(); | |
System.out.println( programa); | |
System.out.println("========"); | |
System.out.println(resultado); | |
System.out.println("========"); | |
System.out.println(resultadoJava); | |
} | |
} |
https://www.antlr.org/
@kabuto356
LINUX
$ cd /usr/local/lib
$ wget https://www.antlr.org/download/antlr-4.7.2-complete.jar
$ export CLASSPATH=".:/usr/local/lib/antlr-4.7.2-complete.jar:$CLASSPATH"
$ alias antlr4='java -jar /usr/local/lib/antlr-4.7.2-complete.jar'
$ alias grun='java org.antlr.v4.gui.TestRig'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
y cuales son comando para corrrer el preograma antlr