Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Antlr Conexaojava


Published on

Published in: Technology
  • Be the first to comment

Antlr Conexaojava

  1. 1. Playing on DSLs With ANTLR <ul><li>Fernando Meyer </li></ul><ul><li>[email_address] </li></ul>
  3. 3. Who am I <ul><li>Software Engineer at JBoss </li></ul><ul><li>JBoss Drools Core Developer - Working on BRMS design and implementation. </li></ul><ul><li>Language and Compilers researcher </li></ul><ul><li>fmeyer @ GUJ || #drools </li></ul>
  4. 4. DSL DSLs are generally very high level languages tailored to specific tasks. They are designed to make their users particularly effective in a specific domain. DSLs include a wide range of applications, many of which you might not consider languages. DSLs include data for mats, configuration file for mats, network protocols, text processing languages, protein patter ns, gene sequences, space probe control languages, and domain-specific programming languages.
  5. 5. Enterprise Usages ? <ul><li>insurance policy DSL developed internally in large insurance enterprise </li></ul><ul><li>combat simulation DSL (**) </li></ul><ul><li>salary calculation DSL </li></ul><ul><li>billing DSL </li></ul>
  6. 6. Rules DSL? try Drools
  7. 7. Advantages? <ul><li>DSLs allow solutions to be expressed in the idiom and at the level of abstraction of the problem domain. Consequently, domain experts themselves can understand, validate, modify, and often even develop DSL programs. </li></ul><ul><li>Self-documenting code. </li></ul><ul><li>DSLs enhance quality, productivity, reliability, maintainability, portability and reusability. </li></ul><ul><li>DSLs allow validation at the domain level. As long as the language constructs are safe any sentence written with them can be considered safe. </li></ul><ul><li>prefer Ruby, Python ... [*flexible languages] </li></ul>
  8. 8. Disadvantages <ul><li>Cost of designing, implementing, and maintaining a DSL. </li></ul><ul><li>Finding, setting, and maintaining proper scope. </li></ul><ul><li>Difficulty of balancing trade-offs between domain-specificity and general-purpose programming language constructs. </li></ul><ul><li>Potential loss of processor efficiency compared with hand-coded software. </li></ul><ul><li>Hard or impossible to debug. (*) </li></ul>
  9. 9. Antlr A sophisticated parser generator you can use to implement language interpreters, compilers, and other translators.
  10. 10. Low Complexity? recipe &quot;PBJ Sandwich&quot; ingredients &quot;two slices of bread&quot;, &quot;one heaping tablespoon of peanut butter&quot;, &quot;one teaspoon of jam&quot; instructions &quot;spread peanut butter...&quot;, &quot;spread jam...&quot;, &quot;place other slice...&quot; servings 1 prep_time &quot;2 minutes&quot;
  11. 11. <ul><li>don’t use ANTLR </li></ul><ul><li>prefer Ruby , Python ... [*flexible languages] </li></ul>
  12. 12. Overall Translation Data Flow
  13. 13. Compilation Process
  14. 14. Lexical analysis <ul><li>The specification of a programming language will include a set of rules, often expressed syntactically, specifying the set of possible character sequences that can form a token or lexeme. </li></ul>
  15. 15. ***A lexeme is an abstract unit of morphological analysis in linguistics, A token is a categorized block of text, usually consisting of indivisible characters known as lexemes. A lexical analyser processes lexemes to categorize them according to function, giving them meaning. This assignment of meaning is known as tokenization . A token can look like anything: English, gibberish symbols, anything; it just needs to be a useful part of the structured text.
  16. 16. Java programming language: sum=3+2; lexeme token type sum IDENT = ASSIGN_OP 3 NUMBER + ADD_OP 2 NUMBER ; SEMICOLON
  17. 17. Parsing parsing (more formally syntactic analysis) is the process of analyzing a sequence of tokens to determine its grammatical structure with respect to a given formal grammar.
  18. 18. Parsing transforms input text into a data structure (AST) x = y - z * 3
  19. 19. Parser is the component of a compiler that carries out this task.
  20. 20. Relationship Between Characters, Tokens, and ASTs
  21. 21. A State Machine That Generates Blues Lyrics
  22. 22. Understanding ANTLR Grammar Files <ul><li>Believe it or not but Antlr grammar files are built to be as close to your java source files as possible </li></ul>
  23. 23. The Grammar File
  24. 24. The rule section
  25. 25. ANTLRWorks Grammar Development Environment
  26. 26. Now ... Hands on Macs ! Download the sample app from
  27. 27. Questions? <ul><li>[email_address] </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>