Last active
December 15, 2016 15:06
-
-
Save lrlucena/f15afc95512bcc6f2930b4bf066e072c to your computer and use it in GitHub Desktop.
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 Linguagem1; | |
documento: bloco* ; | |
bloco: 'h1' Texto | |
| 'p' Texto | |
| form | |
; | |
form: 'form' '{' elemento* '}'; | |
elemento: Id '=' Texto | |
| 'input' Texto | |
; | |
// Lexer | |
Id: ('a'..'z')+; | |
Texto: '"' .*? '"'; | |
Espaco: [ \t\n\r] -> skip; |
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.plp.linguagem1; | |
import org.antlr.v4.runtime.ParserRuleContext; | |
import org.antlr.v4.runtime.tree.ParseTreeProperty; | |
import org.antlr.v4.runtime.tree.TerminalNode; | |
import br.edu.ifrn.plp.linguagem1.Linguagem1Parser.BlocoContext; | |
import br.edu.ifrn.plp.linguagem1.Linguagem1Parser.ElementoContext; | |
public class MeuListener extends Linguagem1BaseListener { | |
private final ParseTreeProperty<String> valores = new ParseTreeProperty<>(); | |
private String saida = ""; | |
public String getSaida() { | |
return saida; | |
} | |
@Override | |
public void exitDocumento(Linguagem1Parser.DocumentoContext ctx) { | |
String pagina = "<html>"; | |
for (BlocoContext filho : ctx.bloco()) { | |
final String valor = valores.get(filho); | |
pagina = pagina + valor; | |
} | |
pagina = pagina + "</html>"; | |
saida = pagina; | |
} | |
@Override | |
public void exitH1(Linguagem1Parser.H1Context ctx) { | |
final String valor = "<h1>" + ctx.Texto().getText() + "</h1>"; | |
valores.put(ctx, valor); | |
} | |
@Override | |
public void exitParagrafo(Linguagem1Parser.ParagrafoContext ctx) { | |
final String valor = "<p>" + ctx.Texto().getText() + "</p>"; | |
valores.put(ctx, valor); | |
} | |
@Override | |
public void exitFormulario(Linguagem1Parser.FormularioContext ctx) { | |
String form = "<form>"; | |
for (ElementoContext filho : ctx.elemento()) { | |
final String valor = valores.get(filho); | |
form = form + valor; | |
} | |
form = form + "</form>"; | |
valores.put(ctx, form); | |
} | |
@Override | |
public void exitParametro(Linguagem1Parser.ParametroContext ctx) { | |
String id = ctx.Id().getText(); // valores.get(ctx.Id()) | |
String texto = ctx.Id().getText(); | |
String valor = id + " = " + texto; | |
valores.put(ctx, valor); | |
} | |
@Override | |
public void exitInput(Linguagem1Parser.InputContext ctx) { | |
String texto = ctx.Texto().getText(); | |
String valor = "<input name='" + texto + "'>"; | |
valores.put(ctx, valor); | |
} | |
@Override | |
public void exitEveryRule(ParserRuleContext ctx) { | |
} | |
@Override | |
public void visitTerminal(TerminalNode node) { | |
} | |
} |
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.plp.linguagem1; | |
import org.antlr.v4.runtime.ParserRuleContext; | |
import org.antlr.v4.runtime.misc.NotNull; | |
import org.antlr.v4.runtime.tree.ParseTree; | |
import org.antlr.v4.runtime.tree.ParseTreeProperty; | |
import org.antlr.v4.runtime.tree.TerminalNode; | |
public class MinhaLinguagem extends linguagemBaseListener { | |
private final ParseTreeProperty<String> valores = new ParseTreeProperty<>(); | |
private String saida = ""; | |
public String getSaida() { | |
return saida; | |
} | |
} |
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.plp.linguagem1; | |
import org.antlr.v4.runtime.*; | |
import org.antlr.v4.runtime.tree.ParseTree; | |
import org.antlr.v4.runtime.tree.ParseTreeWalker; | |
public class Principal { | |
public static void main(String[] args) { | |
final ANTLRInputStream input = new ANTLRInputStream("h1 \"ola\"\npg \"TADS\""); | |
final Linguagem1Lexer lexer = new Linguagem1Lexer(input); | |
final CommonTokenStream tokens = new CommonTokenStream(lexer); | |
final Linguagem1Parser parser = new Linguagem1Parser(tokens); | |
final ParseTree tree = parser.documento(); | |
final ParseTreeWalker walker = new ParseTreeWalker(); | |
final MeuListener listener = new MeuListener(); | |
try { | |
walker.walk(listener, tree); | |
} catch (Exception e) { | |
} | |
final String saida = listener.getSaida(); | |
System.out.println(saida); | |
} | |
} |
Author
lrlucena
commented
Dec 15, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment