SlideShare uma empresa Scribd logo
1 de 17
ANTLR ANother Tool for Language Recognition www.antlr.org Jobson Ronan (jrjs) Renato Viana (rvf)
Motivação ,[object Object],[object Object],[object Object],[object Object]
Características ,[object Object],[object Object],[object Object],[object Object]
Aprendendo por exemplo ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Gramática ,[object Object],[object Object],class ExprParser extends Parser; expr:  mexpr ((PLUS|MINUS) mexpr)* ;  mexpr  :  atom (STAR atom)* ;  atom:  INT  |  LPAREN expr RPAREN  ;
Lexer class ExprLexer extends Lexer; options { k=2; // needed for newline junk charVocabulary='0000'..'007F'; // allow ascii } LPAREN: '(' ; RPAREN: ')' ; PLUS  : '+' ; MINUS : '-' ; STAR  : '*' ; INT  : ('0'..'9')+ ; WS  : ( ' ' | '' '' | '' | '' ) {$setType(Token.SKIP);} ;
Gerando ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Testando ,[object Object],import antlr.*; public class Main { public static void main(String[] args) throws Exception { ExprLexer lexer = new ExprLexer(System.in); ExprParser parser = new ExprParser(lexer); parser.expr(); } }
Avaliando Expressões ,[object Object],class ExprParser extends Parser; expr returns [int value=0] {int x;} :  value=mexpr ( PLUS x=mexpr  {value += x;} | MINUS x=mexpr {value -= x;}  )* ; mexpr returns [int value=0] {int x;} :  value=atom ( STAR x=atom {value *= x;} )* ; atom returns [int value=0] :  i:INT {value=Integer.parseInt(i.getText());} |  LPAREN value=expr RPAREN ;
Testando ,[object Object],import antlr.*; public class Main { public static void main(String[] args) throws Exception { ExprLexer lexer = new ExprLexer(System.in); ExprParser parser = new ExprParser(lexer); int x = parser.expr(); System.out.println(x); } }
Problemas ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Gerando ASTs ,[object Object],[object Object],[object Object],[object Object]
Alterações na gramática ,[object Object],[object Object],class ExprParser extends Parser; options { buildAST=true; } expr:  mexpr ((PLUS^|MINUS^) mexpr)* ; mexpr :  atom (STAR^ atom)* ; atom:  INT |  LPAREN! expr RPAREN! ;
Definindo TreeParser class ExprTreeParser extends TreeParser; options { importVocab=ExprParser; } expr returns [int r=0] { int a,b; } :  #(PLUS  a=expr b=expr)  {r = a+b;} |  #(MINUS a=expr b=expr)  {r = a-b;}  |  #(STAR  a=expr b=expr)  {r = a*b;} |  i:INT  {r = (int)Integer.parseInt(i.getText());} ;
Testando import antlr.*; import antlr.collections.*; public class Main { public static void main(String[] args) throws Exception {  ExprLexer lexer = new ExprLexer(System.in); ExprParser parser = new ExprParser(lexer); parser.expr(); AST t = parser.getAST(); System.out.println(t.toStringTree()); ExprTreeParser treeParser = new ExprTreeParser(); int x = treeParser.expr(t); System.out.println(x); }  }
Exercícios ,[object Object],[object Object],[object Object],[object Object],[object Object]
ANTLR ANother Tool for Language Recognition www.antlr.org Jobson Ronan (jrjs) Renato Viana (rvf)

Mais conteúdo relacionado

Mais procurados

LabMM4 (T13 - 12/13) - Funções
LabMM4 (T13 - 12/13) - FunçõesLabMM4 (T13 - 12/13) - Funções
LabMM4 (T13 - 12/13) - Funções
Carlos Santos
 
Biblioteca strings profª ms
Biblioteca strings profª msBiblioteca strings profª ms
Biblioteca strings profª ms
Joelsa Soares
 
013 programando em python - arquivos
013   programando em python - arquivos013   programando em python - arquivos
013 programando em python - arquivos
Leandro Barbosa
 
007 programando em python - funcoes
007   programando em python - funcoes007   programando em python - funcoes
007 programando em python - funcoes
Leandro Barbosa
 

Mais procurados (20)

Capítulo 4 listas.
Capítulo 4   listas.Capítulo 4   listas.
Capítulo 4 listas.
 
LabMM4 (T13 - 12/13) - Funções
LabMM4 (T13 - 12/13) - FunçõesLabMM4 (T13 - 12/13) - Funções
LabMM4 (T13 - 12/13) - Funções
 
Perl Moderno, dia5
Perl Moderno, dia5Perl Moderno, dia5
Perl Moderno, dia5
 
Funcao PHP
Funcao PHPFuncao PHP
Funcao PHP
 
Biblioteca strings profª ms
Biblioteca strings profª msBiblioteca strings profª ms
Biblioteca strings profª ms
 
Perl Moderno, dia4
Perl Moderno, dia4Perl Moderno, dia4
Perl Moderno, dia4
 
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
 
Aula4
Aula4Aula4
Aula4
 
Perl Moderno, dia2
Perl Moderno, dia2Perl Moderno, dia2
Perl Moderno, dia2
 
Programação Orientada a Objetos (POO) com PHP - Parte 1
Programação Orientada a Objetos (POO) com PHP - Parte 1Programação Orientada a Objetos (POO) com PHP - Parte 1
Programação Orientada a Objetos (POO) com PHP - Parte 1
 
Perl Moderno, dia3
Perl Moderno, dia3Perl Moderno, dia3
Perl Moderno, dia3
 
Variáveis
VariáveisVariáveis
Variáveis
 
013 programando em python - arquivos
013   programando em python - arquivos013   programando em python - arquivos
013 programando em python - arquivos
 
Android - Dicas de Performance
Android - Dicas de PerformanceAndroid - Dicas de Performance
Android - Dicas de Performance
 
007 programando em python - funcoes
007   programando em python - funcoes007   programando em python - funcoes
007 programando em python - funcoes
 
A Classe StringBuilder em Java
A Classe StringBuilder em JavaA Classe StringBuilder em Java
A Classe StringBuilder em Java
 
Python 02
Python 02Python 02
Python 02
 
Python e seus desafios
Python e seus desafiosPython e seus desafios
Python e seus desafios
 
Programação funcional no dia a dia
Programação funcional no dia a diaProgramação funcional no dia a dia
Programação funcional no dia a dia
 
Python 3.x - Ihh.. E agora ? Como faço ?
Python 3.x - Ihh.. E agora ? Como faço ?Python 3.x - Ihh.. E agora ? Como faço ?
Python 3.x - Ihh.. E agora ? Como faço ?
 

Semelhante a ANTLR-ANother Tool for Language Recognition

Ficha javacc
Ficha javaccFicha javacc
Ficha javacc
Æx Lynx
 
TDC 2011 Goiânia: Evolução da linguagem de programação JavaScript
TDC 2011 Goiânia: Evolução da linguagem de programação JavaScriptTDC 2011 Goiânia: Evolução da linguagem de programação JavaScript
TDC 2011 Goiânia: Evolução da linguagem de programação JavaScript
Rogério Moraes de Carvalho
 
55 New Things in Java 7 - Brazil
55 New Things in Java 7 - Brazil55 New Things in Java 7 - Brazil
55 New Things in Java 7 - Brazil
Stephen Chin
 

Semelhante a ANTLR-ANother Tool for Language Recognition (20)

JavaCC
JavaCCJavaCC
JavaCC
 
Ficha javacc
Ficha javaccFicha javacc
Ficha javacc
 
Java para iniciantes
Java para iniciantesJava para iniciantes
Java para iniciantes
 
Php
PhpPhp
Php
 
Um Mundo Java Sem XML
Um Mundo Java Sem XMLUm Mundo Java Sem XML
Um Mundo Java Sem XML
 
Javafx Introdução
Javafx IntroduçãoJavafx Introdução
Javafx Introdução
 
Linguagem R
Linguagem RLinguagem R
Linguagem R
 
TDC 2011 Goiânia: Evolução da linguagem de programação JavaScript
TDC 2011 Goiânia: Evolução da linguagem de programação JavaScriptTDC 2011 Goiânia: Evolução da linguagem de programação JavaScript
TDC 2011 Goiânia: Evolução da linguagem de programação JavaScript
 
Java Desktop
Java DesktopJava Desktop
Java Desktop
 
Java 06 Strings Arrays
Java 06 Strings ArraysJava 06 Strings Arrays
Java 06 Strings Arrays
 
teste
testeteste
teste
 
TechEd Brasil 2011: WEB 302 - Presente e futuro da linguagem de programação J...
TechEd Brasil 2011: WEB 302 - Presente e futuro da linguagem de programação J...TechEd Brasil 2011: WEB 302 - Presente e futuro da linguagem de programação J...
TechEd Brasil 2011: WEB 302 - Presente e futuro da linguagem de programação J...
 
55 New Things in Java 7 - Brazil
55 New Things in Java 7 - Brazil55 New Things in Java 7 - Brazil
55 New Things in Java 7 - Brazil
 
Linguagem de Programação PERL
Linguagem de Programação PERLLinguagem de Programação PERL
Linguagem de Programação PERL
 
Ruby - Criando código para máquinas e humanos
Ruby - Criando código para máquinas e humanosRuby - Criando código para máquinas e humanos
Ruby - Criando código para máquinas e humanos
 
Canivete shell
Canivete shellCanivete shell
Canivete shell
 
Canivete shell
Canivete shellCanivete shell
Canivete shell
 
Linguagem C - Strings
Linguagem C - StringsLinguagem C - Strings
Linguagem C - Strings
 
Pymordida0 Semana de computação da SOCIESC - 2008/10
Pymordida0 Semana de computação da SOCIESC - 2008/10Pymordida0 Semana de computação da SOCIESC - 2008/10
Pymordida0 Semana de computação da SOCIESC - 2008/10
 
Java8
Java8Java8
Java8
 

Mais de elliando dias

Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
elliando dias
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
elliando dias
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
elliando dias
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
elliando dias
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
elliando dias
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
elliando dias
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
elliando dias
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
elliando dias
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
elliando dias
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
elliando dias
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
elliando dias
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
elliando dias
 

Mais de elliando dias (20)

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slides
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structures
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
 
Geometria Projetiva
Geometria ProjetivaGeometria Projetiva
Geometria Projetiva
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
 
Ragel talk
Ragel talkRagel talk
Ragel talk
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
 
Minicurso arduino
Minicurso arduinoMinicurso arduino
Minicurso arduino
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
 
Rango
RangoRango
Rango
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makes
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
 

Último

Último (9)

Programação Orientada a Objetos - 4 Pilares.pdf
Programação Orientada a Objetos - 4 Pilares.pdfProgramação Orientada a Objetos - 4 Pilares.pdf
Programação Orientada a Objetos - 4 Pilares.pdf
 
ATIVIDADE 1 - LOGÍSTICA EMPRESARIAL - 52_2024.docx
ATIVIDADE 1 - LOGÍSTICA EMPRESARIAL - 52_2024.docxATIVIDADE 1 - LOGÍSTICA EMPRESARIAL - 52_2024.docx
ATIVIDADE 1 - LOGÍSTICA EMPRESARIAL - 52_2024.docx
 
ATIVIDADE 1 - SISTEMAS DISTRIBUÍDOS E REDES - 52_2024.docx
ATIVIDADE 1 - SISTEMAS DISTRIBUÍDOS E REDES - 52_2024.docxATIVIDADE 1 - SISTEMAS DISTRIBUÍDOS E REDES - 52_2024.docx
ATIVIDADE 1 - SISTEMAS DISTRIBUÍDOS E REDES - 52_2024.docx
 
ATIVIDADE 1 - GCOM - GESTÃO DA INFORMAÇÃO - 54_2024.docx
ATIVIDADE 1 - GCOM - GESTÃO DA INFORMAÇÃO - 54_2024.docxATIVIDADE 1 - GCOM - GESTÃO DA INFORMAÇÃO - 54_2024.docx
ATIVIDADE 1 - GCOM - GESTÃO DA INFORMAÇÃO - 54_2024.docx
 
Boas práticas de programação com Object Calisthenics
Boas práticas de programação com Object CalisthenicsBoas práticas de programação com Object Calisthenics
Boas práticas de programação com Object Calisthenics
 
ATIVIDADE 1 - CUSTOS DE PRODUÇÃO - 52_2024.docx
ATIVIDADE 1 - CUSTOS DE PRODUÇÃO - 52_2024.docxATIVIDADE 1 - CUSTOS DE PRODUÇÃO - 52_2024.docx
ATIVIDADE 1 - CUSTOS DE PRODUÇÃO - 52_2024.docx
 
Luís Kitota AWS Discovery Day Ka Solution.pdf
Luís Kitota AWS Discovery Day Ka Solution.pdfLuís Kitota AWS Discovery Day Ka Solution.pdf
Luís Kitota AWS Discovery Day Ka Solution.pdf
 
Padrões de Projeto: Proxy e Command com exemplo
Padrões de Projeto: Proxy e Command com exemploPadrões de Projeto: Proxy e Command com exemplo
Padrões de Projeto: Proxy e Command com exemplo
 
ATIVIDADE 1 - ESTRUTURA DE DADOS II - 52_2024.docx
ATIVIDADE 1 - ESTRUTURA DE DADOS II - 52_2024.docxATIVIDADE 1 - ESTRUTURA DE DADOS II - 52_2024.docx
ATIVIDADE 1 - ESTRUTURA DE DADOS II - 52_2024.docx
 

ANTLR-ANother Tool for Language Recognition

  • 1. ANTLR ANother Tool for Language Recognition www.antlr.org Jobson Ronan (jrjs) Renato Viana (rvf)
  • 2.
  • 3.
  • 4.
  • 5.
  • 6. Lexer class ExprLexer extends Lexer; options { k=2; // needed for newline junk charVocabulary='0000'..'007F'; // allow ascii } LPAREN: '(' ; RPAREN: ')' ; PLUS : '+' ; MINUS : '-' ; STAR : '*' ; INT : ('0'..'9')+ ; WS : ( ' ' | '' '' | '' | '' ) {$setType(Token.SKIP);} ;
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14. Definindo TreeParser class ExprTreeParser extends TreeParser; options { importVocab=ExprParser; } expr returns [int r=0] { int a,b; } : #(PLUS a=expr b=expr) {r = a+b;} | #(MINUS a=expr b=expr) {r = a-b;} | #(STAR a=expr b=expr) {r = a*b;} | i:INT {r = (int)Integer.parseInt(i.getText());} ;
  • 15. Testando import antlr.*; import antlr.collections.*; public class Main { public static void main(String[] args) throws Exception { ExprLexer lexer = new ExprLexer(System.in); ExprParser parser = new ExprParser(lexer); parser.expr(); AST t = parser.getAST(); System.out.println(t.toStringTree()); ExprTreeParser treeParser = new ExprTreeParser(); int x = treeParser.expr(t); System.out.println(x); } }
  • 16.
  • 17. ANTLR ANother Tool for Language Recognition www.antlr.org Jobson Ronan (jrjs) Renato Viana (rvf)