{"id":7726,"date":"2023-09-11T14:53:10","date_gmt":"2023-09-11T11:53:10","guid":{"rendered":"https:\/\/new.intechcore.com\/?p=7726"},"modified":"2025-12-01T14:01:35","modified_gmt":"2025-12-01T11:01:35","slug":"development-of-an-own-programming-language-in-javaantlr-interpreter","status":"publish","type":"post","link":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/","title":{"rendered":"Developing your own programming language in Java+ANTLR: Interpreter"},"content":{"rendered":"<p>This is the first article in the series\u00a0\u201cDeveloping your own programming language in Java\u201d\u00a0which is aimed to show the full path of creating a language,\u00a0as well as writing and maintaining tools for it.\u00a0By the end of this article,\u00a0we will implement an interpreter with which it will be possible to execute programs in our language.\u00a0Any programming language has a syntax that needs to be converted into a data structure that is convenient for validation,\u00a0transformation and execution.\u00a0As a rule,\u00a0such a data structure is an\u00a0<strong>abstract syntax tree<\/strong>\u00a0(<a class=\"external-link\" href=\"https:\/\/en.wikipedia.org\/wiki\/Abstract_syntax_tree\" rel=\"nofollow\"><span class=\"caps\">AST<\/span><\/a>).\u00a0Each tree node represents a construct that occurs in the source code.\u00a0The source code is parsed by the parser,\u00a0and the output is\u00a0<span class=\"caps\">AST<\/span>.<\/p>\n<p>Languages have been developed for a long time,\u00a0and therefore at the moment we have quite a few mature tools,\u00a0including\u00a0<a class=\"external-link\" href=\"https:\/\/en.wikipedia.org\/wiki\/Compiler-compiler\" rel=\"nofollow\">parser generators<\/a>.\u00a0Parser generators take a description of the grammar of a particular language as input,\u00a0and as output we get parsers,\u00a0interpreters and compilers.<\/p>\n<p>In this article,\u00a0we will consider the\u00a0<a class=\"external-link\" href=\"https:\/\/en.wikipedia.org\/wiki\/ANTLR\" rel=\"nofollow\"><span class=\"caps\">ANTLR<\/span><\/a>\u00a0tool.\u00a0<span class=\"caps\">ANTLR<\/span>\u00a0is a utility that receives a grammar in the form\u00a0<a class=\"external-link\" href=\"https:\/\/en.wikipedia.org\/wiki\/Extended_Backus%E2%80%93Naur_form\" rel=\"nofollow\"><span class=\"caps\">EBNF<\/span><\/a>\u00a0as input,\u00a0and interfaces\/classes as output\u00a0(in our case,\u00a0this is java code)\u00a0for parsing programs.\u00a0The list of languages in which parsers are generated <a href=\"https:\/\/github.com\/antlr\/antlr4\/blob\/master\/doc\/targets.md\" target=\"_blank\" rel=\"noopener\">can be found\u00a0here<\/a>.<\/p>\n<h2 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Grammarexample\">Grammar example<\/h2>\n<p>Before moving on to real grammar,\u00a0let\u2019s try to describe some of the rules of a typical programming language in\u00a0words:<\/p>\n<ul>\n<li><code>VARIABLE<\/code>\u00a0is an\u00a0<code>IDENTIFIER<\/code><\/li>\n<li><code>DIGIT<\/code>\u00a0is one of the characters\u00a0<code>0<\/code>\u00a0<code>1<\/code>\u00a0<code>2<\/code>\u00a0<code>3<\/code>\u00a0<code>4<\/code>\u00a0<code>5<\/code>\u00a0<code>6<\/code>\u00a0<code>7<\/code>\u00a0<code>8<\/code>\u00a0<code>9<\/code><\/li>\n<li><code>NUMBER<\/code>\u00a0is one or more elements of type\u00a0<code>DIGIT<\/code><\/li>\n<li><code>EXPRESSION<\/code>\u00a0is\u00a0<code>NUMBER<\/code><\/li>\n<li><code>EXPRESSION<\/code>\u00a0is\u00a0<code>VARIABLE<\/code><\/li>\n<li><code>EXPRESSION<\/code>\u00a0is\u00a0<code>EXPRESSION<\/code>\u00a0\u2018+\u2019\u00a0<code>EXPRESSION<\/code><\/li>\n<li><code>EXPRESSION<\/code>\u00a0is \u2018(\u2018\u00a0<code>EXPRESSION<\/code>\u00a0\u2019)\u2019<\/li>\n<\/ul>\n<p>As you can see from this list,\u00a0a language grammar is a set of rules that can have recursive links.\u00a0Each rule can refer to itself or to any other rule.\u00a0<span class=\"caps\">ANTLR<\/span>\u00a0has many operators in its arsenal to describe such\u00a0rules.<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\">: marker of the beginning of the rule<br \/>; rule end marker<br \/><span style=\"color:#f97b58;\">|<\/span> alternative operator<br \/>.. range operator<br \/>~ denial<br \/>. any character<br \/><span style=\"color:#f97b58;\">=<\/span> assignment<br \/>(&#8230;) sub-rule<br \/>(&#8230;)<span style=\"color:#f97b58;\">*<\/span> repeating of a subrule 0 or more times<br \/>(&#8230;)<span style=\"color:#f97b58;\">+<\/span>  repeating of a subrule 1 or more times<br \/>(&#8230;)<span style=\"color:#f97b58;\">?<\/span> subrule, may be missing<br \/>{&#8230;} semantic actions (in the language used as the output &#8211; for example, Java)<br \/>[&#8230;] rule parameters<\/div>\n<h3 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-ANTLRruleexamples\"><span class=\"caps\">ANTLR<\/span>\u00a0rule examples<\/h3>\n<p>The following example describes the rules for integers and floating-point numbers:<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#ec5f66;font-style:italic;\">NUMBER<\/span> <span style=\"color:#f97b58;\">:<\/span> [0-9]<span style=\"color:#f97b58;\">+<\/span> ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">FLOAT<\/span> <span style=\"color:#f97b58;\">:<\/span> NUMBER <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">.<\/span><span style=\"color:#80b979;\">&#8216;<\/span> NUMBER ;<\/div>\n<p>It is very important to understand that the grammar describes only the syntax of the language on the basis of which the parser will be generated.\u00a0The parser will generate an\u00a0<span class=\"caps\">AST<\/span>,\u00a0with which help it will be possible to implement the semantics of the language.\u00a0In the previous example,\u00a0we set a rule for parsing an integer,\u00a0but did not describe how much memory the number occupies\u00a0(8 bits,\u00a016,\u00a0\u2026),\u00a0and whether the number is signed or unsigned.\u00a0For example,\u00a0in some programming languages you can start using a variable without declaring it.\u00a0You can also not declare the type of the variable;\u00a0in this case the type will be determined automatically at runtime.\u00a0All these rules of language semantics are not described in the grammar,\u00a0but are implemented in another part of the language.<\/p>\n<h3 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-ANTLRtokensandexpressions\"><span class=\"caps\">ANTLR<\/span>\u00a0tokens and expressions<\/h3>\n<p><span class=\"caps\">ANTLR<\/span>\u00a0grammar consists of two types of rules:\u00a0<strong>tokens<\/strong>\u00a0and\u00a0<strong>expressions<\/strong>,\u00a0which are used to define the structure of the grammar and parse\u00a0input.<\/p>\n<p><strong>Tokens<\/strong>\u00a0are rules that define individual lexical elements of the input language,\u00a0such as numbers,\u00a0identifiers,\u00a0operation signs,\u00a0and so on.\u00a0Each token corresponds to a certain type of token,\u00a0which is used for further processing by the parser.\u00a0The\u00a0<strong>lexical analyzer<\/strong>\u00a0scans the input text,\u00a0parses it into tokens,\u00a0and creates a sequence of tokens,\u00a0which are then passed to the\u00a0<strong>parser<\/strong>.\u00a0They are written in uppercase\u00a0(For example:\u00a0<code>NUMBER<\/code>,\u00a0<code>IDENTIFIER<\/code>).<\/p>\n<p><strong>Expressions<\/strong>\u00a0are rules that define the grammar structure of the input language.\u00a0They describe how tokens are related and how they can be combined into more complex structures.\u00a0Expressions can contain references to tokens as well as to other expressions.\u00a0Written in camelCase notation\u00a0(For example:\u00a0<code>expression<\/code>,\u00a0<code>functionDefinition<\/code>).<\/p>\n<p>Thus,\u00a0the difference between tokens and expressions in\u00a0<span class=\"caps\">ANTLR<\/span>\u00a0is that tokens define individual lexical elements of the input language and convert them into tokens,\u00a0while expressions define the structure of the grammar and describe how tokens are related to each other in more complex structures.<\/p>\n<h2 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-LanguageRequirements\">Language Requirements<\/h2>\n<p>Before you start implementing a language,\u00a0you need to determine the set of features it should support.\u00a0For our task,\u00a0for educational purposes,\u00a0we will use a simple grammar.\u00a0The language will support the following constructs:<\/p>\n<ul>\n<li>Variables (types\u00a0<code>String<\/code>,\u00a0<code>Long<\/code>,\u00a0<code>Double<\/code>);<\/li>\n<li>Assignment operator (=);<\/li>\n<li>Arithmetic operations\u00a0(+,\u00a0-,\u00a0*,\u00a0\/);<\/li>\n<li>Comparison operators (&gt;, &lt;, &gt;=, &lt;=, ==, !=);<\/li>\n<li>Conditional statements (if ,\u00a0else);<\/li>\n<li>Functions;<\/li>\n<li>Print to the console (built-in\u00a0<code>println<\/code>\u00a0operator).<\/li>\n<\/ul>\n<h2 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Grammar\">Grammar<\/h2>\n<p>And finally, a complete description of the grammar for the language:<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#c695c6;\">grammar<\/span> Jimple ;<\/p>\n<p><span style=\"color:#999999;\">\/\/ <\/span><span style=\"color:#999999;\">root <\/span><span style=\"color:#999999;\">rule <\/span><span style=\"color:#999999;\">of <\/span><span style=\"color:#999999;\">grammar<\/span><br \/><span style=\"color:#c695c6;font-style:italic;\">program<\/span><span style=\"color:#f97b58;\">:<\/span> (statement)<span style=\"color:#f97b58;\">*<\/span> EOF;<\/p>\n<p><span style=\"color:#999999;\">\/\/ <\/span><span style=\"color:#999999;\">list <\/span><span style=\"color:#999999;\">of <\/span><span style=\"color:#999999;\">possible <\/span><span style=\"color:#999999;\">statements<\/span><br \/><span style=\"color:#c695c6;font-style:italic;\">statement<\/span><span style=\"color:#f97b58;\">:<\/span> variableDeclaration<br \/>         <span style=\"color:#f97b58;\">|<\/span> assignment<br \/>         <span style=\"color:#f97b58;\">|<\/span> functionDefinition<br \/>         <span style=\"color:#f97b58;\">|<\/span> functionCall<br \/>         <span style=\"color:#f97b58;\">|<\/span> println<br \/>         <span style=\"color:#f97b58;\">|<\/span> return<br \/>         <span style=\"color:#f97b58;\">|<\/span> ifStatement<br \/>         <span style=\"color:#f97b58;\">|<\/span> blockStatement<br \/>         ;<\/p>\n<p><span style=\"color:#999999;\">\/\/ <\/span><span style=\"color:#999999;\">list <\/span><span style=\"color:#999999;\">of <\/span><span style=\"color:#999999;\">possible <\/span><span style=\"color:#999999;\">expressions<\/span><br \/><span style=\"color:#c695c6;font-style:italic;\">expression<\/span><span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">(<\/span><span style=\"color:#80b979;\">&#8216;<\/span> expression <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">)<\/span><span style=\"color:#80b979;\">&#8216;<\/span>                                      #parenthesisExpr<br \/>          <span style=\"color:#f97b58;\">|<\/span> left<span style=\"color:#f97b58;\">=<\/span>expression op<span style=\"color:#f97b58;\">=<\/span>(ASTERISK <span style=\"color:#f97b58;\">|<\/span> SLASH) right<span style=\"color:#f97b58;\">=<\/span>expression  #mulDivExpr<br \/>          <span style=\"color:#f97b58;\">|<\/span> left<span style=\"color:#f97b58;\">=<\/span>expression op<span style=\"color:#f97b58;\">=<\/span>(PLUS <span style=\"color:#f97b58;\">|<\/span> MINUS) right<span style=\"color:#f97b58;\">=<\/span>expression      #plusMinusExpr<br \/>          <span style=\"color:#f97b58;\">|<\/span> left<span style=\"color:#f97b58;\">=<\/span>expression compOperator right<span style=\"color:#f97b58;\">=<\/span>expression           #compExpr<br \/>          <span style=\"color:#f97b58;\">|<\/span> IDENTIFIER                                              #idExp<br \/>          <span style=\"color:#f97b58;\">|<\/span> NUMBER                                                  #numExpr<br \/>          <span style=\"color:#f97b58;\">|<\/span> DOUBLE_NUMBER                                           #doubleExpr<br \/>          <span style=\"color:#f97b58;\">|<\/span> STRING_LITERAL                                          #stringExpr<br \/>          <span style=\"color:#f97b58;\">|<\/span> functionCall                                            #funcCallExpr<br \/>          ;<\/p>\n<p><span style=\"color:#999999;\">\/\/ <\/span><span style=\"color:#999999;\">descriptions <\/span><span style=\"color:#999999;\">of <\/span><span style=\"color:#999999;\">individual <\/span><span style=\"color:#999999;\">expressions <\/span><span style=\"color:#999999;\">and <\/span><span style=\"color:#999999;\">statements<\/span><br \/><span style=\"color:#c695c6;font-style:italic;\">variableDeclaration<\/span><span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">var<\/span><span style=\"color:#80b979;\">&#8216;<\/span> IDENTIFIER <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">=<\/span><span style=\"color:#80b979;\">&#8216;<\/span> expression ;<\/p>\n<p><span style=\"color:#c695c6;font-style:italic;\">assignment<\/span><span style=\"color:#f97b58;\">:<\/span> IDENTIFIER <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">=<\/span><span style=\"color:#80b979;\">&#8216;<\/span> expression ;<\/p>\n<p><span style=\"color:#c695c6;font-style:italic;\">compOperator<\/span><span style=\"color:#f97b58;\">:<\/span> op<span style=\"color:#f97b58;\">=<\/span>(LESS <span style=\"color:#f97b58;\">|<\/span> LESS_OR_EQUAL <span style=\"color:#f97b58;\">|<\/span> EQUAL <span style=\"color:#f97b58;\">|<\/span> NOT_EQUAL <span style=\"color:#f97b58;\">|<\/span> GREATER <span style=\"color:#f97b58;\">|<\/span> GREATER_OR_EQUAL) ;<\/p>\n<p><span style=\"color:#c695c6;font-style:italic;\">println<\/span><span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">println<\/span><span style=\"color:#80b979;\">&#8216;<\/span> expression ;<\/p>\n<p><span style=\"color:#c695c6;font-style:italic;\">return<\/span><span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">return<\/span><span style=\"color:#80b979;\">&#8216;<\/span> expression ;<\/p>\n<p><span style=\"color:#c695c6;font-style:italic;\">blockStatement<\/span><span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">{<\/span><span style=\"color:#80b979;\">&#8216;<\/span> (statement)<span style=\"color:#f97b58;\">*<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">}<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<\/p>\n<p><span style=\"color:#c695c6;font-style:italic;\">functionCall<\/span><span style=\"color:#f97b58;\">:<\/span> IDENTIFIER <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">(<\/span><span style=\"color:#80b979;\">&#8216;<\/span> (expression (<span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">,<\/span><span style=\"color:#80b979;\">&#8216;<\/span> expression)<span style=\"color:#f97b58;\">*<\/span>)<span style=\"color:#f97b58;\">?<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">)<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<\/p>\n<p><span style=\"color:#c695c6;font-style:italic;\">functionDefinition<\/span><span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">fun<\/span><span style=\"color:#80b979;\">&#8216;<\/span> name<span style=\"color:#f97b58;\">=<\/span>IDENTIFIER <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">(<\/span><span style=\"color:#80b979;\">&#8216;<\/span> (IDENTIFIER (<span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">,<\/span><span style=\"color:#80b979;\">&#8216;<\/span> IDENTIFIER)<span style=\"color:#f97b58;\">*<\/span>)<span style=\"color:#f97b58;\">?<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">)<\/span><span style=\"color:#80b979;\">&#8216;<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">{<\/span><span style=\"color:#80b979;\">&#8216;<\/span> (statement)<span style=\"color:#f97b58;\">*<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">}<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<\/p>\n<p><span style=\"color:#c695c6;font-style:italic;\">ifStatement<\/span><span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">if<\/span><span style=\"color:#80b979;\">&#8216;<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">(<\/span><span style=\"color:#80b979;\">&#8216;<\/span> expression <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">)<\/span><span style=\"color:#80b979;\">&#8216;<\/span> statement  elseStatement<span style=\"color:#f97b58;\">?<\/span> ;<\/p>\n<p><span style=\"color:#c695c6;font-style:italic;\">elseStatement<\/span><span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">else<\/span><span style=\"color:#80b979;\">&#8216;<\/span> statement ;<\/p>\n<p><span style=\"color:#999999;\">\/\/ <\/span><span style=\"color:#999999;\">list <\/span><span style=\"color:#999999;\">of <\/span><span style=\"color:#999999;\">tokens<\/span><br \/><span style=\"color:#ec5f66;font-style:italic;\">IDENTIFIER<\/span>          <span style=\"color:#f97b58;\">:<\/span> [a-zA-Z_] [a-zA-Z_0-9]<span style=\"color:#f97b58;\">*<\/span> ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">NUMBER<\/span>              <span style=\"color:#f97b58;\">:<\/span> [0-9]<span style=\"color:#f97b58;\">+<\/span> ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">DOUBLE_NUMBER<\/span>       <span style=\"color:#f97b58;\">:<\/span> NUMBER <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">.<\/span><span style=\"color:#80b979;\">&#8216;<\/span> NUMBER ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">STRING_LITERAL<\/span>      <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">&#8220;<\/span><span style=\"color:#80b979;\">&#8216;<\/span> (~[&#8220;])<span style=\"color:#f97b58;\">*<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">&#8220;<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<\/p>\n<p><span style=\"color:#ec5f66;font-style:italic;\">ASTERISK<\/span>            <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">*<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">SLASH<\/span>               <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">\/<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">PLUS<\/span>                <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">+<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">MINUS<\/span>               <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">&#8211;<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<\/p>\n<p><span style=\"color:#ec5f66;font-style:italic;\">ASSIGN<\/span>              <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">=<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">EQUAL<\/span>               <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">==<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">NOT_EQUAL<\/span>           <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">!=<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">LESS<\/span>                <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">&lt;<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">LESS_OR_EQUAL<\/span>       <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">&lt;=<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">GREATER<\/span>             <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">><\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<br \/><span style=\"color:#ec5f66;font-style:italic;\">GREATER_OR_EQUAL<\/span>    <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">>=<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ;<\/p>\n<p><span style=\"color:#ec5f66;font-style:italic;\">SPACE<\/span>               <span style=\"color:#f97b58;\">:<\/span> [ \\r\\n\\t]<span style=\"color:#f97b58;\">+<\/span> -> skip;<br \/><span style=\"color:#ec5f66;font-style:italic;\">LINE_COMMENT<\/span>        <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">\/\/<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ~[\\n\\r]<span style=\"color:#f97b58;\">*<\/span> -> skip;<\/div>\n<p>As you have probably already guessed,\u00a0our language is called Jimple\u00a0(derived from Jvm simple).\u00a0It\u2019s probably worth explaining some points that may not be obvious when you first get acquainted with\u00a0<span class=\"caps\">ANTLR<\/span>.<\/p>\n<h3 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Labels\">Labels<\/h3>\n<p>op label has been used to describe the rules for some operations.\u00a0This allows us to use this label later as the name of a variable that will contain the value of the operator.\u00a0Theoretically,\u00a0we could avoid specifying labels,\u00a0but in this case,\u00a0we would have to write additional code to get the value of the operator from the parse\u00a0tree.<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#c695c6;font-style:italic;\">compOperator<\/span><span style=\"color:#f97b58;\">:<\/span> op<span style=\"color:#f97b58;\">=<\/span>(LESS <span style=\"color:#f97b58;\">|<\/span> LESS_OR_EQUAL <span style=\"color:#f97b58;\">|<\/span> EQUAL <span style=\"color:#f97b58;\">|<\/span> NOT_EQUAL <span style=\"color:#f97b58;\">|<\/span> GREATER <span style=\"color:#f97b58;\">|<\/span> GREATER_OR_EQUAL) ;<\/div>\n<h3 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Namedrulealternatives\">Named rule alternatives<\/h3>\n<p>In\u00a0<span class=\"caps\">ANTLR<\/span>,\u00a0by defining a rule with multiple alternatives,\u00a0a user can assign a name to every of them,\u00a0and then in the tree it will appear in a form of a separate processing node.\u00a0That is very convenient when you need to assign to the processing of every rule option a separate method.\u00a0It is important that the names must be given either to all alternatives or to none of them.\u00a0The following example demonstrates what it looks\u00a0like:<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#c695c6;font-style:italic;\">expression<\/span><span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">(<\/span><span style=\"color:#80b979;\">&#8216;<\/span> expression <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">)<\/span><span style=\"color:#80b979;\">&#8216;<\/span>  #parenthesisExpr<br \/>          <span style=\"color:#f97b58;\">|<\/span> IDENTIFIER          #idExp<br \/>          <span style=\"color:#f97b58;\">|<\/span> NUMBER              #numExpr<\/div>\n<p><span class=\"caps\">ANTLR<\/span>\u00a0will generate the following code:<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#c695c6;font-style:italic;\">interface<\/span> <span style=\"color:#f9ae58;\">JimpleVisitor<\/span><span style=\"color:#5fb4b4;\">&lt;<\/span><span style=\"color:#f9ae58;\">T<\/span><span style=\"color:#5fb4b4;\">><\/span> {<br \/>    <span style=\"color:#6699cc;font-style:italic;\">T<\/span> <span style=\"color:#5fb4b4;\">visitParenthesisExpr<\/span>(<span style=\"color:#6699cc;font-style:italic;\">ParenthesisExprContext<\/span> <span style=\"color:#f9ae58;\">ctx<\/span>)<span style=\"color:#ac7a68;\">;<\/span><\/p>\n<p>    <span style=\"color:#6699cc;font-style:italic;\">T<\/span> <span style=\"color:#5fb4b4;\">visitIdExp<\/span>(<span style=\"color:#6699cc;font-style:italic;\">IdExpContext<\/span> <span style=\"color:#f9ae58;\">ctx<\/span>)<span style=\"color:#ac7a68;\">;<\/span><\/p>\n<p>    <span style=\"color:#6699cc;font-style:italic;\">T<\/span> <span style=\"color:#5fb4b4;\">visitNumExpr<\/span>(<span style=\"color:#6699cc;font-style:italic;\">NumExprContext<\/span> <span style=\"color:#f9ae58;\">ctx<\/span>)<span style=\"color:#ac7a68;\">;<\/span><br \/>}<\/div>\n<h3 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Channels\">Channels<\/h3>\n<p><span class=\"caps\">ANTLR<\/span>\u00a0has such a construct as\u00a0<code>channels<\/code>.\u00a0Usually,\u00a0channels are used to work with comments,\u00a0but since in most cases we do not need to check for comments,\u00a0they must be discarded with\u00a0<code>-&gt; skip<\/code>,\u00a0that we have already used.\u00a0However,\u00a0there are cases when we need to interpret the meaning of comments or other constructs,\u00a0in this case you should use pipes.\u00a0<span class=\"caps\">ANTLR<\/span>\u00a0has already a built-in channel called\u00a0<code>HIDDEN<\/code>\u00a0that you can use,\u00a0or you can also declare your own channels for specific purposes.\u00a0Later,\u00a0by parsing the code,\u00a0you will be able to access these channels.<\/p>\n<h4 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Anexampleofdeclaringandusingachannel\">An example of declaring and using a channel<\/h4>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#c695c6;font-style:italic;\">channels<\/span> { MYLINECOMMENT }<\/p>\n<p><span style=\"color:#ec5f66;font-style:italic;\">LINE_COMMENT<\/span> <span style=\"color:#f97b58;\">:<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">\/\/<\/span><span style=\"color:#80b979;\">&#8216;<\/span> ~[rn]<span style=\"color:#f97b58;\">+<\/span> -> channel(MYLINECOMMENT) ;<\/div>\n<h3 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Fragments\">Fragments<\/h3>\n<p>Along with tokens,\u00a0<span class=\"caps\">ANTLR<\/span>\u00a0has such an element as\u00a0<code>fragments<\/code>.\u00a0Rules with a fragment prefix can only be called from other lexer rules.\u00a0They are not tokens per se.\u00a0In the following example,\u00a0we moved the definitions of numbers for different number systems into fragments.<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#ec5f66;font-style:italic;\">NUMBER<\/span><span style=\"color:#f97b58;\">:<\/span> DIGITS <span style=\"color:#f97b58;\">|<\/span> OCTAL_DIGITS <span style=\"color:#f97b58;\">|<\/span> HEX_DIGITS;<br \/><span style=\"color:#c695c6;\">fragment<\/span> DIGITS: <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">1<\/span><span style=\"color:#80b979;\">&#8216;<\/span>..<span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">9<\/span><span style=\"color:#80b979;\">&#8216;<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">0<\/span><span style=\"color:#80b979;\">&#8216;<\/span>..<span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">9<\/span><span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#f97b58;\">*<\/span>;<br \/><span style=\"color:#c695c6;\">fragment<\/span> OCTAL_DIGITS: <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">0<\/span><span style=\"color:#80b979;\">&#8216;<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">0<\/span><span style=\"color:#80b979;\">&#8216;<\/span>..<span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">7<\/span><span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#f97b58;\">+<\/span>;<br \/><span style=\"color:#c695c6;\">fragment<\/span> HEX_DIGITS: <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">0x<\/span><span style=\"color:#80b979;\">&#8216;<\/span> (<span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">0<\/span><span style=\"color:#80b979;\">&#8216;<\/span>..<span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">9<\/span><span style=\"color:#80b979;\">&#8216;<\/span> <span style=\"color:#f97b58;\">|<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">a<\/span><span style=\"color:#80b979;\">&#8216;<\/span>..<span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">f<\/span><span style=\"color:#80b979;\">&#8216;<\/span> <span style=\"color:#f97b58;\">|<\/span> <span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">A<\/span><span style=\"color:#80b979;\">&#8216;<\/span>..<span style=\"color:#80b979;\">&#8216;<\/span><span style=\"color:#80b979;\">F<\/span><span style=\"color:#80b979;\">&#8216;<\/span>)<span style=\"color:#f97b58;\">+<\/span>;<\/div>\n<p>Thus,\u00a0a number in any number system\u00a0(for example: \u201c123\u201d,\u00a0\u201c0762\u201d,\u00a0or\u00a0\u201c0xac1\u201d)\u00a0will be treated as a\u00a0<span class=\"caps\">NUMBER<\/span>\u00a0token,\u00a0not\u00a0<span class=\"caps\">DIGITS<\/span>,\u00a0<span class=\"caps\">OCTAL_DIGITS<\/span>,\u00a0or\u00a0<span class=\"caps\">HEX_DIGITS<\/span>.\u00a0Jimple does not use fragments.<\/p>\n<h2 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Tools\">Tools<\/h2>\n<p>Before we start generating the parser,\u00a0we need to set up the tools to work with\u00a0<span class=\"caps\">ANTLR<\/span>.\u00a0As is known,\u00a0a good and convenient tool makes half the success.\u00a0To accomplish that,\u00a0we need to download\u00a0<span class=\"caps\">ANTLR<\/span>\u00a0<a class=\"external-link\" href=\"https:\/\/www.antlr.org\/download.html\" rel=\"nofollow\">library<\/a>\u00a0and to write scripts in order to run it.\u00a0There are also maven\/gradle\/IntelliJ\u00a0<span class=\"caps\">IDEA<\/span>\u00a0plugins,\u00a0which we will not be using in this article,\u00a0but for productive development,\u00a0they can be found useful.<\/p>\n<p>We need the following scripts:<\/p>\n<p><strong>antlr4.sh script<\/strong><\/p>\n<div style=\"white-space:pre-wrap;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#6699cc;\">java<\/span> <span style=\"color:#5fb4b4;\">&#8211;<\/span><span style=\"color:#f9ae58;\">Xmx500M<\/span> <span style=\"color:#5fb4b4;\">&#8211;<\/span><span style=\"color:#f9ae58;\">cp<\/span> <span style=\"color:#5fb4b4;\">&#8220;<\/span><span style=\"color:#80b979;\">.<\/span><span style=\"color:#ac7a68;\">:<\/span><span style=\"color:#80b979;\">\/<\/span><span style=\"color:#80b979;\">usr<\/span><span style=\"color:#80b979;\">\/<\/span><span style=\"color:#80b979;\">lib<\/span><span style=\"color:#80b979;\">\/<\/span><span style=\"color:#80b979;\">antlr-4.12.0-complete.jar<\/span><span style=\"color:#5fb4b4;\">&#8220;<\/span> <span style=\"color:#80b979;\">org.antlr.v4.Tool<\/span> <span style=\"color:#5fb4b4;font-style:italic;\">$<\/span><span style=\"color:#ec5f66;font-style:italic;\">@<\/span><\/div>\n<h3><strong>Skript grun.sh<\/strong><\/h3>\n<div style=\"white-space:pre-wrap;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#6699cc;\">java<\/span> <span style=\"color:#5fb4b4;\">&#8211;<\/span><span style=\"color:#f9ae58;\">Xmx500M<\/span> <span style=\"color:#5fb4b4;\">&#8211;<\/span><span style=\"color:#f9ae58;\">cp<\/span> <span style=\"color:#5fb4b4;\">&#8220;<\/span><span style=\"color:#80b979;\">.<\/span><span style=\"color:#ac7a68;\">:<\/span><span style=\"color:#80b979;\">\/<\/span><span style=\"color:#80b979;\">usr<\/span><span style=\"color:#80b979;\">\/<\/span><span style=\"color:#80b979;\">lib<\/span><span style=\"color:#80b979;\">\/<\/span><span style=\"color:#80b979;\">antlr-4.12.0-complete.jar<\/span><span style=\"color:#5fb4b4;\">&#8220;<\/span> <span style=\"color:#80b979;\">org.antlr.v4.gui.TestRig<\/span> <span style=\"color:#5fb4b4;font-style:italic;\">$<\/span><span style=\"color:#ec5f66;font-style:italic;\">@<\/span><\/div>\n<h3><strong>grun.sh script<\/strong><\/h3>\n<div style=\"white-space:pre-wrap;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#6699cc;\">antlr4.sh<\/span> <span style=\"color:#80b979;\">Jimple.g4<\/span> <span style=\"color:#5fb4b4;\">&#8211;<\/span><span style=\"color:#f9ae58;\">package<\/span> <span style=\"color:#80b979;\">org.jimple.lang<\/span> <span style=\"color:#5fb4b4;\">&#8211;<\/span><span style=\"color:#f9ae58;\">visitor<\/span><\/div>\n<h2 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Parsergeneration\">Parser generation<\/h2>\n<p>Save the grammar in the file\u00a0<code>Jimple.g4<\/code>.\u00a0Next,\u00a0run the script like\u00a0this:<\/p>\n<div style=\"white-space:pre-wrap;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#6699cc;\">antlr4.sh<\/span> <span style=\"color:#80b979;\">Jimple.g4<\/span> <span style=\"color:#5fb4b4;\">&#8211;<\/span><span style=\"color:#f9ae58;\">package<\/span> <span style=\"color:#80b979;\">org.jimple.lang<\/span> <span style=\"color:#5fb4b4;\">&#8211;<\/span><span style=\"color:#f9ae58;\">visitor<\/span><\/div>\n<\/pre>\n<p><code>-package<\/code>\u00a0option allows you to specify the java\u00a0<code>package<\/code>\u00a0in which the code will be generated.\u00a0The\u00a0<code>-visitor<\/code>\u00a0option allows you to generate a\u00a0<code>JimpleVisitor<\/code>\u00a0interface that implements the\u00a0<a class=\"external-link\" href=\"https:\/\/en.wikipedia.org\/wiki\/Visitor_pattern\" rel=\"nofollow\">Visitor<\/a>\u00a0pattern.<\/p>\n<p>After successful execution of the script,\u00a0several files will appear in the current directory:\u00a0<code>JimpleParser.java<\/code>,\u00a0<code>JimpleLexer.java<\/code>,\u00a0<code>JimpleListener.java<\/code>,\u00a0<code>JimpleVisitor.java<\/code>.\u00a0The first two files contain the generated parser and lexer code,\u00a0respectively.\u00a0The other two files contain interfaces for working with the parse tree.\u00a0In this article,\u00a0we will use the\u00a0<code>JimpleVisitor<\/code>\u00a0interface,\u00a0more specifically\u00a0<code>JimpleBaseVisitor<\/code>\u00a0\u2014\u00a0this one is also a generated class that implements the\u00a0<code>JimpleVisitor<\/code>\u00a0interface and contains implementations of all methods.\u00a0It allows us to override only the methods we\u00a0need.<\/p>\n<h2 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-InterpreterImplementation\">Interpreter Implementation<\/h2>\n<p>Finally,\u00a0we have reached the most interesting part\u00a0\u2014\u00a0the implementation of the interpreter.\u00a0Although in this article we will not analyze the issue of checking the code for errors,\u00a0some interpretation errors will still be implemented.\u00a0First of all,\u00a0let\u2019s create a\u00a0<code>JimpleInterpreter<\/code>\u00a0class with an\u00a0<code>eval<\/code>\u00a0method,\u00a0which will take a string with the code for Jimple as input.\u00a0Next,\u00a0we need to parse the source code into tokens using\u00a0<code>JimpleLexer<\/code>,\u00a0then to create an\u00a0<span class=\"caps\">AST<\/span>\u00a0tree using\u00a0<code>JimpleParser<\/code>.<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#c695c6;font-style:italic;\">class<\/span> <span style=\"color:#f9ae58;\">JimpleInterpreter<\/span> {<br \/>    <span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#6699cc;font-style:italic;\">Object<\/span> <span style=\"color:#5fb4b4;\">eval<\/span>(<span style=\"color:#ec5f66;\">final<\/span> <span style=\"color:#6699cc;font-style:italic;\">String<\/span> <span style=\"color:#f9ae58;\">input<\/span>) {<br \/>        <span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">parse <\/span><span style=\"color:#999999;\">initial <\/span><span style=\"color:#999999;\">code <\/span><span style=\"color:#999999;\">on <\/span><span style=\"color:#999999;\">tokens<\/span><br \/>        <span style=\"color:#ec5f66;\">final<\/span> <span style=\"color:#6699cc;font-style:italic;\">JimpleLexer<\/span> lexer <span style=\"color:#f97b58;\">=<\/span> <span style=\"color:#c695c6;\">new<\/span> <span style=\"color:#6699cc;font-style:italic;\">JimpleLexer<\/span>(<span style=\"color:#6699cc;font-style:italic;\">CharStreams<\/span><span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">fromString<\/span>(input))<span style=\"color:#ac7a68;\">;<\/span><br \/>        <span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">create <\/span><span style=\"color:#999999;\">AST <\/span><span style=\"color:#999999;\">tree<\/span><br \/>        <span style=\"color:#ec5f66;\">final<\/span> <span style=\"color:#6699cc;font-style:italic;\">JimpleParser<\/span> parser <span style=\"color:#f97b58;\">=<\/span> <span style=\"color:#c695c6;\">new<\/span> <span style=\"color:#6699cc;font-style:italic;\">JimpleParser<\/span>(<span style=\"color:#c695c6;\">new<\/span> <span style=\"color:#6699cc;font-style:italic;\">CommonTokenStream<\/span>(lexer))<span style=\"color:#ac7a68;\">;<\/span><br \/>        <span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">create <\/span><span style=\"color:#999999;\">an <\/span><span style=\"color:#999999;\">object <\/span><span style=\"color:#999999;\">class <\/span><span style=\"color:#999999;\">JimpleInterpreterVisitor<\/span><br \/>        <span style=\"color:#ec5f66;\">final<\/span> <span style=\"color:#6699cc;font-style:italic;\">JimpleInterpreterVisitor<\/span> interpreterVisitor <span style=\"color:#f97b58;\">=<\/span> <span style=\"color:#c695c6;\">new<\/span> <span style=\"color:#6699cc;font-style:italic;\">JimpleInterpreterVisitor<\/span>(<span style=\"color:#c695c6;\">new<\/span> <span style=\"color:#6699cc;font-style:italic;\">JimpleContextImpl<\/span>(stdout))<span style=\"color:#ac7a68;\">;<\/span><br \/>        <span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">run <\/span><span style=\"color:#999999;\">interpreter<\/span><br \/>        <span style=\"color:#c695c6;\">return<\/span> interpreterVisitor<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">visitProgram<\/span>(parser<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">program<\/span>())<span style=\"color:#ac7a68;\">;<\/span><br \/>    }<br \/>}<\/div>\n<p>We have a syntax tree.\u00a0Let\u2019s add semantics with the\u00a0<code>JimpleInterpreterVisitor<\/code>\u00a0class we wrote,\u00a0which will bypass the\u00a0<span class=\"caps\">AST<\/span>\u00a0by calling up the appropriate methods.\u00a0Since the root rule of our grammar is the\u00a0<code>program<\/code>\u00a0rule\u00a0(see\u00a0<code>program: (statement)* EOF<\/code>\u00a0above),\u00a0the tree traversal starts from it.\u00a0To accomplish this,\u00a0we call the\u00a0<code>visitProgram<\/code>\u00a0method implemented by default on the\u00a0<code>JimpleInterpreterVisitor<\/code>\u00a0object,\u00a0to the input of which we give an object of the\u00a0<code>ProgramContext<\/code>\u00a0class.\u00a0The\u00a0<span class=\"caps\">ANTLR<\/span>\u00a0implementation consists of calling the\u00a0<code>visitChildren (RuleNode node)<\/code>\u00a0that traverses all the descendant items of the given tree node,\u00a0calling the\u00a0<code>visit<\/code>\u00a0method on each of\u00a0them.<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">code <\/span><span style=\"color:#999999;\">generated <\/span><span style=\"color:#999999;\">by <\/span><span style=\"color:#999999;\">ANTLR<\/span><br \/><span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#c695c6;font-style:italic;\">class<\/span> <span style=\"color:#f9ae58;\">JimpleBaseVisitor<\/span><span style=\"color:#5fb4b4;\">&lt;<\/span><span style=\"color:#f9ae58;\">T<\/span><span style=\"color:#5fb4b4;\">><\/span> <span style=\"color:#ec5f66;\">extends<\/span> <span style=\"color:#5fb4b4;font-style:italic;\">AbstractParseTreeVisitor<\/span><span style=\"color:#5fb4b4;\">&lt;<\/span><span style=\"color:#6699cc;font-style:italic;\">T<\/span><span style=\"color:#5fb4b4;\">><\/span> <span style=\"color:#ec5f66;\">implements<\/span> <span style=\"color:#5fb4b4;font-style:italic;\">JimpleVisitor<\/span><span style=\"color:#5fb4b4;\">&lt;<\/span><span style=\"color:#6699cc;font-style:italic;\">T<\/span><span style=\"color:#5fb4b4;\">><\/span> {<br \/>    <span style=\"color:#5fb4b4;\">@<\/span><span style=\"color:#6699cc;\">Override<\/span><br \/>    <span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#6699cc;font-style:italic;\">T<\/span> <span style=\"color:#5fb4b4;\">visitProgram<\/span>(<span style=\"color:#6699cc;font-style:italic;\">JimpleParser<\/span><span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;font-style:italic;\">ProgramContext<\/span> <span style=\"color:#f9ae58;\">ctx<\/span>) {<br \/>        <span style=\"color:#c695c6;\">return<\/span> <span style=\"color:#6699cc;\">visitChildren<\/span>(ctx)<span style=\"color:#ac7a68;\">;<\/span><br \/>    }<\/p>\n<p>    <span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">other <\/span><span style=\"color:#999999;\">methods <\/span><span style=\"color:#999999;\">omitted <\/span><span style=\"color:#999999;\">for <\/span><span style=\"color:#999999;\">brevity<\/span><br \/>}<\/div>\n<p>As you can see,\u00a0<code>JimpleBaseVisitor<\/code>\u00a0is a generic class for which you need to define the type of processing for each node.\u00a0In our case,\u00a0this is the\u00a0<code>Object<\/code>\u00a0class,\u00a0since expressions can return values of different types.\u00a0Typically,\u00a0an\u00a0<code>expression<\/code>\u00a0must return a value,\u00a0while a\u00a0<code>statement<\/code>\u00a0returns nothing\u00a0\u2013\u00a0this is their difference.\u00a0In case of statement,\u00a0we can return\u00a0<code>null<\/code>.\u00a0However,\u00a0in order not to accidentally encounter\u00a0<code>NullPointerException<\/code>,\u00a0instead of\u00a0<code>null<\/code>\u00a0we will return an object of type\u00a0<code>Object<\/code>,\u00a0which is globally defined in the\u00a0<code>JimpleInterpreter<\/code>\u00a0class:<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#ec5f66;\">static<\/span> <span style=\"color:#ec5f66;\">final<\/span> <span style=\"color:#6699cc;font-style:italic;\">Object<\/span> <span style=\"color:#f9ae58;\">VOID<\/span> <span style=\"color:#f97b58;\">=<\/span> <span style=\"color:#c695c6;\">new<\/span> <span style=\"color:#6699cc;font-style:italic;\">Object<\/span>()<span style=\"color:#ac7a68;\">;<\/span><\/div>\n<p><code>JimpleInterpreterVisitor<\/code>\u00a0class extends class\u00a0<code>JimpleBaseVisitor<\/code>,\u00a0overriding only the methods we are interested in.\u00a0Let\u2019s consider the implementation of the built-in\u00a0<code>println<\/code>\u00a0operator,\u00a0which is described in the grammar as\u00a0<code>println: 'println' expression ;<\/code>.\u00a0The first thing we need to complete is to evaluate the\u00a0<code>expression<\/code>,\u00a0for this we need to call the\u00a0<code>visit<\/code>\u00a0method and pass the\u00a0<code>expression<\/code>\u00a0object from the current\u00a0<code>PrintlnContext<\/code>\u00a0into it.\u00a0In the\u00a0<code>visitPrintln<\/code>\u00a0method,\u00a0we are absolutely not interested in a calculation process of an expression;\u00a0the corresponding method is responsible for calculating each rule\u00a0(context).\u00a0For example,\u00a0the\u00a0<code>visitStringExpr<\/code>\u00a0method is used to evaluate a string literal.<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#c695c6;font-style:italic;\">class<\/span> <span style=\"color:#f9ae58;\">JimpleInterpreterVisitor<\/span> <span style=\"color:#ec5f66;\">extends<\/span> <span style=\"color:#5fb4b4;font-style:italic;\">JimpleBaseVisitor<\/span><span style=\"color:#5fb4b4;\">&lt;<\/span><span style=\"color:#6699cc;font-style:italic;\">Object<\/span><span style=\"color:#5fb4b4;\">><\/span> { <br \/>    <span style=\"color:#5fb4b4;\">@<\/span><span style=\"color:#6699cc;\">Override<\/span><br \/>    <span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#6699cc;font-style:italic;\">Object<\/span> <span style=\"color:#5fb4b4;\">visitPrintln<\/span>(<span style=\"color:#ec5f66;\">final<\/span> <span style=\"color:#6699cc;font-style:italic;\">JimpleParser<\/span><span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;font-style:italic;\">PrintlnContext<\/span> <span style=\"color:#f9ae58;\">ctx<\/span>) {<br \/>        <span style=\"color:#ec5f66;\">final<\/span> <span style=\"color:#6699cc;font-style:italic;\">Object<\/span> result <span style=\"color:#f97b58;\">=<\/span> <span style=\"color:#6699cc;\">visit<\/span>(ctx<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">expression<\/span>())<span style=\"color:#ac7a68;\">;<\/span><br \/>        <span style=\"color:#6699cc;font-style:italic;\">System<\/span><span style=\"color:#f97b58;\">.<\/span>out<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">println<\/span>(result)<span style=\"color:#ac7a68;\">;<\/span><br \/>        <span style=\"color:#c695c6;\">return<\/span> <span style=\"color:#c695c6;\">VOID<\/span><span style=\"color:#ac7a68;\">;<\/span><br \/>    }<\/p>\n<p>    <span style=\"color:#5fb4b4;\">@<\/span><span style=\"color:#6699cc;\">Override<\/span><br \/>    <span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#6699cc;font-style:italic;\">Object<\/span> <span style=\"color:#5fb4b4;\">visitStringExpr<\/span>(<span style=\"color:#ec5f66;\">final<\/span> <span style=\"color:#6699cc;font-style:italic;\">JimpleParser<\/span><span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;font-style:italic;\">StringExprContext<\/span> <span style=\"color:#f9ae58;\">ctx<\/span>) {<br \/>        <span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">return <\/span><span style=\"color:#999999;\">string <\/span><span style=\"color:#999999;\">literal<\/span><br \/>        <span style=\"color:#c695c6;\">return<\/span> <span style=\"color:#6699cc;\">cleanStringLiteral<\/span>(ctx<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;font-style:italic;\">STRING_LITERAL<\/span>()<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">getText<\/span>())<span style=\"color:#ac7a68;\">;<\/span><br \/>    }<\/p>\n<p>    <span style=\"color:#ec5f66;\">private<\/span> <span style=\"color:#6699cc;font-style:italic;\">String<\/span> <span style=\"color:#5fb4b4;\">cleanStringLiteral<\/span>(<span style=\"color:#ec5f66;\">final<\/span> <span style=\"color:#6699cc;font-style:italic;\">String<\/span> <span style=\"color:#f9ae58;\">literal<\/span>) {<br \/>        <span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">clear <\/span><span style=\"color:#999999;\">string <\/span><span style=\"color:#999999;\">from <\/span><span style=\"color:#999999;\">quotation <\/span><span style=\"color:#999999;\">marks<\/span><br \/>        <span style=\"color:#c695c6;\">return<\/span> literal<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">length<\/span>() <span style=\"color:#f97b58;\">><\/span> <span style=\"color:#f9ae58;\">1<\/span> <span style=\"color:#f97b58;\">?<\/span> literal<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">substring<\/span>(<span style=\"color:#f9ae58;\">1<\/span><span style=\"color:#ac7a68;\">,<\/span> literal<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">length<\/span>() <span style=\"color:#f97b58;\">&#8211;<\/span> <span style=\"color:#f9ae58;\">1<\/span>) <span style=\"color:#f97b58;\">:<\/span> literal<span style=\"color:#ac7a68;\">;<\/span><br \/>    }<\/p>\n<p>    <span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">other <\/span><span style=\"color:#999999;\">methods <\/span><span style=\"color:#999999;\">omitted <\/span><span style=\"color:#999999;\">for <\/span><span style=\"color:#999999;\">brevity<\/span><br \/>}<\/div>\n<p>By implementing only these methods,\u00a0the interpreter already supports\u00a0<code>println<\/code>\u00a0and string literals,\u00a0which allows us to execute\u00a0<code>println \"Hello, Jimple!\"<\/code>\u00a0code.<\/p>\n<h3 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Runinterpreter\">Run interpreter<\/h3>\n<p>To launch the interpreter,\u00a0you need to create a standard main method,\u00a0which after some small checks with the use of the\u00a0<code>JimpleInterpreter<\/code>\u00a0class,\u00a0will run our\u00a0code:<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#c695c6;font-style:italic;\">class<\/span> <span style=\"color:#f9ae58;\">MainApp<\/span> {<br \/>    <span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#ec5f66;\">static<\/span> <span style=\"color:#c695c6;font-style:italic;\">void<\/span> <span style=\"color:#5fb4b4;\">main<\/span>(<span style=\"color:#6699cc;font-style:italic;\">String<\/span><span style=\"color:#ec5f66;\">[]<\/span> <span style=\"color:#f9ae58;\">args<\/span>) {<br \/>        <span style=\"color:#c695c6;\">if<\/span> (args<span style=\"color:#f97b58;\">.<\/span>length <span style=\"color:#f97b58;\">&lt;<\/span> <span style=\"color:#f9ae58;\">1<\/span>) {<br \/>            <span style=\"color:#6699cc;font-style:italic;\">System<\/span><span style=\"color:#f97b58;\">.<\/span>out<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">println<\/span>(<span style=\"color:#5fb4b4;\">&#8220;<\/span><span style=\"color:#80b979;\">usage: <\/span><span style=\"color:#80b979;\">jimple <\/span><span style=\"color:#80b979;\">input.jimple<\/span><span style=\"color:#5fb4b4;\">&#8220;<\/span>)<span style=\"color:#ac7a68;\">;<\/span><br \/>            <span style=\"color:#6699cc;font-style:italic;\">System<\/span><span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">exit<\/span>(<span style=\"color:#f9ae58;\">1<\/span>)<span style=\"color:#ac7a68;\">;<\/span><br \/>        }<\/p>\n<p>        <span style=\"color:#6699cc;font-style:italic;\">Path<\/span> path <span style=\"color:#f97b58;\">=<\/span> <span style=\"color:#6699cc;font-style:italic;\">Paths<\/span><span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">get<\/span>(args[<span style=\"color:#f9ae58;\">0<\/span>])<span style=\"color:#ac7a68;\">;<\/span><br \/>        <span style=\"color:#c695c6;\">if<\/span> (<span style=\"color:#f97b58;\">!<\/span><span style=\"color:#6699cc;font-style:italic;\">Files<\/span><span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">exists<\/span>(path)) {<br \/>            <span style=\"color:#6699cc;font-style:italic;\">System<\/span><span style=\"color:#f97b58;\">.<\/span>err<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">println<\/span>(<span style=\"color:#5fb4b4;\">&#8220;<\/span><span style=\"color:#80b979;\">File <\/span><span style=\"color:#80b979;\">not <\/span><span style=\"color:#80b979;\">found: <\/span><span style=\"color:#5fb4b4;\">&#8220;<\/span> <span style=\"color:#f97b58;\">+<\/span> path)<span style=\"color:#ac7a68;\">;<\/span><br \/>            <span style=\"color:#6699cc;font-style:italic;\">System<\/span><span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">exit<\/span>(<span style=\"color:#f9ae58;\">1<\/span>)<span style=\"color:#ac7a68;\">;<\/span><br \/>        }<\/p>\n<p>        <span style=\"color:#c695c6;\">new<\/span> <span style=\"color:#6699cc;font-style:italic;\">JimpleInterpreter<\/span>()<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">eval<\/span>(path)<span style=\"color:#ac7a68;\">;<\/span><br \/>    }<br \/>}<\/div>\n<h3 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Implementationdetails\">Implementation details<\/h3>\n<p>There is no need to provide the entire implementation code of the interpreter,\u00a0a link to the source code can be found at the end of the article.\u00a0However,\u00a0I want to examine some interesting details.<\/p>\n<p>As already mentioned,\u00a0the interpreter is based on the Visitor pattern,\u00a0which visits the nodes of the\u00a0<span class=\"caps\">AST<\/span>\u00a0tree and executes the corresponding instructions.\u00a0In the process of code execution in the current context it appears that new identifiers\u00a0(names of variables and\/or functions)\u00a0need to be stored somewhere.\u00a0To do this,\u00a0we will write a\u00a0<code>JimpleContext<\/code>\u00a0class that will store not only these identifiers,\u00a0but also the current context for executing nested code blocks and functions,\u00a0since a local variable and\/or function parameter must be deleted after leaving their\u00a0scope.<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#5fb4b4;\">@<\/span><span style=\"color:#6699cc;\">Override<\/span><br \/><span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#6699cc;font-style:italic;\">Object<\/span> <span style=\"color:#5fb4b4;\">handleFunc<\/span>(<span style=\"color:#6699cc;font-style:italic;\">FunctionSignature<\/span> <span style=\"color:#f9ae58;\">func<\/span><span style=\"color:#ac7a68;\">,<\/span> <span style=\"color:#6699cc;font-style:italic;\">List<\/span><span style=\"color:#5fb4b4;\">&lt;<\/span><span style=\"color:#6699cc;font-style:italic;\">String<\/span><span style=\"color:#5fb4b4;\">><\/span> <span style=\"color:#f9ae58;\">parameters<\/span><span style=\"color:#ac7a68;\">,<\/span> <span style=\"color:#6699cc;font-style:italic;\">List<\/span><span style=\"color:#5fb4b4;\">&lt;<\/span><span style=\"color:#6699cc;font-style:italic;\">Object<\/span><span style=\"color:#5fb4b4;\">><\/span> <span style=\"color:#f9ae58;\">arguments<\/span><span style=\"color:#ac7a68;\">,<\/span> <span style=\"color:#6699cc;font-style:italic;\">FunctionDefinitionContext<\/span> <span style=\"color:#f9ae58;\">ctx<\/span>) {<br \/>    <span style=\"color:#6699cc;font-style:italic;\">Map<\/span><span style=\"color:#5fb4b4;\">&lt;<\/span><span style=\"color:#6699cc;font-style:italic;\">String<\/span><span style=\"color:#ac7a68;\">,<\/span> <span style=\"color:#6699cc;font-style:italic;\">Object<\/span><span style=\"color:#5fb4b4;\">><\/span> variables <span style=\"color:#f97b58;\">=<\/span> <span style=\"color:#c695c6;\">new<\/span> <span style=\"color:#6699cc;font-style:italic;\">HashMap<\/span><span style=\"color:#5fb4b4;\">&lt;><\/span>(parameters<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">size<\/span>())<span style=\"color:#ac7a68;\">;<\/span><br \/>    <span style=\"color:#c695c6;\">for<\/span> (<span style=\"color:#c695c6;font-style:italic;\">int<\/span> i <span style=\"color:#f97b58;\">=<\/span> <span style=\"color:#f9ae58;\">0<\/span><span style=\"color:#ac7a68;\">;<\/span> i <span style=\"color:#f97b58;\">&lt;<\/span> parameters<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">size<\/span>()<span style=\"color:#ac7a68;\">;<\/span> i<span style=\"color:#f97b58;\">++<\/span>) {<br \/>        variables<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">put<\/span>(parameters<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">get<\/span>(i)<span style=\"color:#ac7a68;\">,<\/span> arguments<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">get<\/span>(i))<span style=\"color:#ac7a68;\">;<\/span><br \/>    }<br \/>    <span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">create <\/span><span style=\"color:#999999;\">a <\/span><span style=\"color:#999999;\">new <\/span><span style=\"color:#999999;\">function <\/span><span style=\"color:#999999;\">scope <\/span><span style=\"color:#999999;\">and <\/span><span style=\"color:#999999;\">push <\/span><span style=\"color:#999999;\">it <\/span><span style=\"color:#999999;\">onto <\/span><span style=\"color:#999999;\">the <\/span><span style=\"color:#999999;\">stack<\/span><br \/>    context<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">pushCallScope<\/span>(variables)<span style=\"color:#ac7a68;\">;<\/span><\/p>\n<p>    <span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">execution <\/span><span style=\"color:#999999;\">of <\/span><span style=\"color:#999999;\">function <\/span><span style=\"color:#999999;\">expressions <\/span><span style=\"color:#999999;\">omitted <\/span><span style=\"color:#999999;\">for <\/span><span style=\"color:#999999;\">brevity<\/span><br \/>    <span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">remove <\/span><span style=\"color:#999999;\">the <\/span><span style=\"color:#999999;\">scope <\/span><span style=\"color:#999999;\">of <\/span><span style=\"color:#999999;\">function <\/span><span style=\"color:#999999;\">parameters <\/span><span style=\"color:#999999;\">from <\/span><span style=\"color:#999999;\">the <\/span><span style=\"color:#999999;\">stack<\/span><br \/>    context<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">popCallScope<\/span>()<span style=\"color:#ac7a68;\">;<\/span><\/p>\n<p>    <span style=\"color:#c695c6;\">return<\/span> functionResult<span style=\"color:#ac7a68;\">;<\/span><br \/>}<\/div>\n<p>In our language,\u00a0a variable stores a value of a type that is determined at runtime.\u00a0Further,\u00a0in the following instructions,\u00a0this type may change.\u00a0Thus,\u00a0we got a dynamically typed language.\u00a0However,\u00a0some checking of types is still present in cases where performing the operation is pointless.\u00a0For example,\u00a0a number cannot be divided by a string.<\/p>\n<h4 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Whydoweneedtwopasses?\">Why do we need two passes?<\/h4>\n<p>The original version of the interpreter was to implement a method for each rule.\u00a0For example,\u00a0if a function declaration processing method finds a function with the same name\u00a0(and number of parameters)\u00a0in the current context,\u00a0then an exception is thrown,\u00a0otherwise the function is added to the current context.\u00a0The function call method works the same way.\u00a0If the function is not found,\u00a0then an exception is thrown,\u00a0otherwise the function is called.\u00a0This approach works,\u00a0but it does not allow you to call the function before it is defined.\u00a0For example,\u00a0the following code will not\u00a0work:<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#c695c6;font-style:italic;\">var<\/span> result <span style=\"color:#f97b58;\">=<\/span> <span style=\"color:#f9ae58;\">9<\/span> <span style=\"color:#f97b58;\">+<\/span> <span style=\"color:#f9ae58;\">10<\/span><\/p>\n<p>println <span style=\"color:#5fb4b4;\">&#8220;<\/span><span style=\"color:#80b979;\">Result <\/span><span style=\"color:#80b979;\">is <\/span><span style=\"color:#5fb4b4;\">&#8220;<\/span> <span style=\"color:#f97b58;\">+<\/span> <span style=\"color:#6699cc;\">add<\/span>(result<span style=\"color:#ac7a68;\">,<\/span> <span style=\"color:#f9ae58;\">34<\/span>)<\/p>\n<p>fun <span style=\"color:#6699cc;\">add<\/span>(a<span style=\"color:#ac7a68;\">,<\/span> b) {<br \/>    return a <span style=\"color:#f97b58;\">+<\/span> b<br \/>}<\/div>\n<p>In this case,\u00a0we have two approaches.\u00a0The first one is to require functions to be defined before they are used\u00a0(not very convenient for users of the language).\u00a0The second approach is to perform two passes.\u00a0The first pass is needed for finding out all the functions that have been defined in the code.\u00a0And the second is directly for executing the code.\u00a0In my implementation,\u00a0I chose the second approach.\u00a0The implementation of the\u00a0<code>visitFunctionDefinition<\/code>\u00a0method should be moved to a separate class that extends the generated class\u00a0<code>JimpleBaseVisitor&lt;T&gt;<\/code>\u00a0that is already known to\u00a0us.<\/p>\n<div style=\"white-space:pre;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#999999;\">\/\/<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">class <\/span><span style=\"color:#999999;\">finds <\/span><span style=\"color:#999999;\">all <\/span><span style=\"color:#999999;\">functions <\/span><span style=\"color:#999999;\">in <\/span><span style=\"color:#999999;\">the <\/span><span style=\"color:#999999;\">code <\/span><span style=\"color:#999999;\">and <\/span><span style=\"color:#999999;\">registers <\/span><span style=\"color:#999999;\">them <\/span><span style=\"color:#999999;\">in <\/span><span style=\"color:#999999;\">the <\/span><span style=\"color:#999999;\">context<\/span><br \/><span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#c695c6;font-style:italic;\">class<\/span> <span style=\"color:#f9ae58;\">FunctionDefinitionVisitor<\/span> <span style=\"color:#ec5f66;\">extends<\/span> <span style=\"color:#5fb4b4;font-style:italic;\">JimpleBaseVisitor<\/span><span style=\"color:#5fb4b4;\">&lt;<\/span><span style=\"color:#6699cc;font-style:italic;\">Object<\/span><span style=\"color:#5fb4b4;\">><\/span> {<br \/>    <span style=\"color:#ec5f66;\">private<\/span> <span style=\"color:#6699cc;font-style:italic;\">JimpleContext<\/span> context<span style=\"color:#ac7a68;\">;<\/span><br \/>    <span style=\"color:#ec5f66;\">private<\/span> <span style=\"color:#6699cc;font-style:italic;\">FunctionCallHandler<\/span> handler<span style=\"color:#ac7a68;\">;<\/span><\/p>\n<p>    <span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#5fb4b4;\">FunctionDefinitionVisitor<\/span>(<span style=\"color:#6699cc;font-style:italic;\">JimpleContext<\/span> <span style=\"color:#f9ae58;\">context<\/span><span style=\"color:#ac7a68;\">,<\/span> <span style=\"color:#6699cc;font-style:italic;\">FunctionCallHandler<\/span> <span style=\"color:#f9ae58;\">handler<\/span>) {<br \/>        <span style=\"color:#ec5f66;font-style:italic;\">this<\/span><span style=\"color:#f97b58;\">.<\/span>context <span style=\"color:#f97b58;\">=<\/span> context<span style=\"color:#ac7a68;\">;<\/span><br \/>        <span style=\"color:#ec5f66;font-style:italic;\">this<\/span><span style=\"color:#f97b58;\">.<\/span>handler <span style=\"color:#f97b58;\">=<\/span> handler<span style=\"color:#ac7a68;\">;<\/span><br \/>    }<\/p>\n<p>    <span style=\"color:#5fb4b4;\">@<\/span><span style=\"color:#6699cc;\">Override<\/span><br \/>    <span style=\"color:#ec5f66;\">public<\/span> <span style=\"color:#6699cc;font-style:italic;\">Object<\/span> <span style=\"color:#5fb4b4;\">visitFunctionDefinition<\/span>(<span style=\"color:#6699cc;font-style:italic;\">JimpleParser<\/span><span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;font-style:italic;\">FunctionDefinitionContext<\/span> <span style=\"color:#f9ae58;\">ctx<\/span>) {<br \/>        <span style=\"color:#6699cc;font-style:italic;\">String<\/span> name <span style=\"color:#f97b58;\">=<\/span> ctx<span style=\"color:#f97b58;\">.<\/span>name<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">getText<\/span>()<span style=\"color:#ac7a68;\">;<\/span><br \/>        <span style=\"color:#6699cc;font-style:italic;\">List<\/span><span style=\"color:#5fb4b4;\">&lt;<\/span><span style=\"color:#6699cc;font-style:italic;\">String<\/span><span style=\"color:#5fb4b4;\">><\/span> parameters <span style=\"color:#f97b58;\">=<\/span> ctx<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;font-style:italic;\">IDENTIFIER<\/span>()<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">stream<\/span>()<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">skip<\/span>(<span style=\"color:#f9ae58;\">1<\/span>)<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">map<\/span>(<span style=\"color:#6699cc;font-style:italic;\">ParseTree<\/span><span style=\"color:#f97b58;\">::<\/span><span style=\"color:#6699cc;\">getText<\/span>)<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">toList<\/span>()<span style=\"color:#ac7a68;\">;<\/span><br \/>        <span style=\"color:#c695c6;font-style:italic;\">var<\/span> funcSig <span style=\"color:#f97b58;\">=<\/span> <span style=\"color:#c695c6;\">new<\/span> <span style=\"color:#6699cc;font-style:italic;\">FunctionSignature<\/span>(name<span style=\"color:#ac7a68;\">,<\/span> parameters<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">size<\/span>())<span style=\"color:#ac7a68;\">;<\/span><br \/>        context<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">registerFunction<\/span>(funcSig<span style=\"color:#ac7a68;\">,<\/span> (<span style=\"color:#f9ae58;\">func<\/span><span style=\"color:#ac7a68;\">,<\/span> <span style=\"color:#f9ae58;\">args<\/span>) <span style=\"color:#c695c6;font-style:italic;\">-><\/span> handler<span style=\"color:#f97b58;\">.<\/span><span style=\"color:#6699cc;\">handleFunc<\/span>(func<span style=\"color:#ac7a68;\">,<\/span> parameters<span style=\"color:#ac7a68;\">,<\/span> args<span style=\"color:#ac7a68;\">,<\/span> ctx))<span style=\"color:#ac7a68;\">;<\/span><br \/>        <span style=\"color:#c695c6;\">return<\/span> <span style=\"color:#c695c6;\">VOID<\/span><span style=\"color:#ac7a68;\">;<\/span><br \/>    }<br \/>}<\/div>\n<p>Now we have a class that we can use before executing the interpreter class directly.\u00a0It will fill our context with the definitions of all the functions that we will call in the interpreter class.<\/p>\n<h2 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-WhatdoesanASTlooklike?\">What does an\u00a0<span class=\"caps\">AST<\/span>\u00a0look\u00a0like?<\/h2>\n<p>In order to visualize the\u00a0<span class=\"caps\">AST<\/span>,\u00a0you need to use the\u00a0<code>grun<\/code>\u00a0utility\u00a0(see above).\u00a0To do this,\u00a0run\u00a0<code>grun<\/code>\u00a0with the parameters\u00a0<code>Jimple program -gui<\/code>\u00a0(the first parameter is the name of the grammar,\u00a0the second is the name of the rule).\u00a0As a result,\u00a0a window with the\u00a0<span class=\"caps\">AST<\/span>\u00a0tree will be opened.\u00a0Before executing this utility,\u00a0it is important to compile the code generated by\u00a0<span class=\"caps\">ANTLR<\/span>.<\/p>\n<div style=\"white-space:pre-wrap;font-family:Monospace;color:#333333;background-color:#f8f8f8;-moz-tab-size:2;tab-size:2;border:1px solid #E5E5E5;padding:3px;margin: 6px 0 6px 0;font-size:12px;\"><span style=\"color:#999999;\">#<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">generate <\/span><span style=\"color:#999999;\">a <\/span><span style=\"color:#999999;\">parser<\/span><br \/><span style=\"color:#6699cc;\">antlr4.sh<\/span> <span style=\"color:#80b979;\">Jimple.g4<\/span><br \/><span style=\"color:#999999;\">#<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">compile <\/span><span style=\"color:#999999;\">the <\/span><span style=\"color:#999999;\">generated <\/span><span style=\"color:#999999;\">code<\/span><br \/><span style=\"color:#6699cc;\">javac<\/span> <span style=\"color:#5fb4b4;\">&#8211;<\/span><span style=\"color:#f9ae58;\">cp<\/span> <span style=\"color:#5fb4b4;\">&#8220;<\/span><span style=\"color:#80b979;\">.<\/span><span style=\"color:#ac7a68;\">:<\/span><span style=\"color:#80b979;\">\/<\/span><span style=\"color:#80b979;\">usr<\/span><span style=\"color:#80b979;\">\/<\/span><span style=\"color:#80b979;\">lib<\/span><span style=\"color:#80b979;\">\/<\/span><span style=\"color:#80b979;\">jvm<\/span><span style=\"color:#80b979;\">\/<\/span><span style=\"color:#80b979;\">antlr-4.12.0-complete.jar<\/span><span style=\"color:#5fb4b4;\">&#8220;<\/span> <span style=\"color:#80b979;\">Jimple<\/span><span style=\"color:#c695c6;\">*<\/span><span style=\"color:#80b979;\">.java<\/span><br \/><span style=\"color:#999999;\">#<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">run <\/span><span style=\"color:#999999;\">grun<\/span><br \/><span style=\"color:#6699cc;\">grun.sh<\/span> <span style=\"color:#80b979;\">Jimple<\/span> <span style=\"color:#80b979;\">program<\/span> <span style=\"color:#5fb4b4;\">&#8211;<\/span><span style=\"color:#f9ae58;\">gui<\/span><br \/><span style=\"color:#999999;\">#<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">enter <\/span><span style=\"color:#999999;\">code: <\/span><span style=\"color:#999999;\">`println <\/span><span style=\"color:#999999;\">&#8220;Hello, <\/span><span style=\"color:#999999;\">Jimple!&#8221;`<\/span><br \/><span style=\"color:#999999;\">#<\/span><span style=\"color:#999999;\"> <\/span><span style=\"color:#999999;\">press <\/span><span style=\"color:#999999;\">Ctrl+D <\/span><span style=\"color:#999999;\">(<\/span><span style=\"color:#999999;\">Linux) <\/span><span style=\"color:#999999;\">or <\/span><span style=\"color:#999999;\">Ctrl+Z <\/span><span style=\"color:#999999;\">(<\/span><span style=\"color:#999999;\">Windows)<\/span><\/div>\n<p>For the Jimple code\u00a0<code>println \"Hello, Jimple!\"<\/code>,\u00a0the following\u00a0<span class=\"caps\">AST<\/span>\u00a0will be generated:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-10144 size-full\" src=\"https:\/\/intechcore.com\/wp-content\/uploads\/2023\/09\/hello_jimple_ast.png\" alt=\"AST\" width=\"426\" height=\"517\" srcset=\"https:\/\/intechcore.com\/wp-content\/uploads\/2023\/09\/hello_jimple_ast.png 426w, https:\/\/intechcore.com\/wp-content\/uploads\/2023\/09\/hello_jimple_ast-247x300.png 247w\" sizes=\"auto, (max-width: 426px) 100vw, 426px\" \/><\/p>\n<h2 id=\"Articledraft(EN)DevelopingourownprogramminglanguageinJava+ANTLR:interpreter-Summary\">Summary<\/h2>\n<p>In this article you have got acquainted with such concepts as lexical and parser analyzers.\u00a0We used the\u00a0<span class=\"caps\">ANTLR<\/span>\u00a0tool to generate such analyzers.\u00a0You have also learned how to write\u00a0<span class=\"caps\">ANTLR<\/span>\u00a0grammar.\u00a0As a result,\u00a0we created a simple language,\u00a0namely,\u00a0developed an interpreter for it.\u00a0As a bonus,\u00a0we managed to visualize the\u00a0<span class=\"caps\">AST<\/span>.<\/p>\n<p>The entire source code of the interpreter <a href=\"https:\/\/github.com\/intechcore\/articles\/tree\/main\/antlr\/JimpleLang\" target=\"_blank\" rel=\"noopener\">can be found link<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the first article in the series\u00a0\u201cDeveloping your own programming language in Java\u201d\u00a0which is aimed to show the full path of creating a language,\u00a0as well as writing and maintaining tools for it.\u00a0By the end of this article,\u00a0we will implement an interpreter with which it will be possible to execute programs in our language.\u00a0Any programming &#8230;<\/p>\n","protected":false},"author":3,"featured_media":7449,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[63,59,60,61,62,64,65,66],"class_list":{"0":"post-7726","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-articles","8":"tag-abstgract-syntax-tree-en","9":"tag-antlr-en","10":"tag-ast-en","11":"tag-ebnf-en","12":"tag-java-en","13":"tag-lexical-analyzer-en","14":"tag-parser-generator-en","15":"tag-token-en","16":"anons"},"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Intechcore GmbH - Developing your own programming language in Java+ANTLR: Interpreter<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Intechcore GmbH - Developing your own programming language in Java+ANTLR: Interpreter\" \/>\n<meta property=\"og:description\" content=\"This is the first article in the series\u00a0\u201cDeveloping your own programming language in Java\u201d\u00a0which is aimed to show the full path of creating a language,\u00a0as well as writing and maintaining tools for it.\u00a0By the end of this article,\u00a0we will implement an interpreter with which it will be possible to execute programs in our language.\u00a0Any programming ...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/\" \/>\n<meta property=\"og:site_name\" content=\"Intechcore GmbH\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-11T11:53:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-12-01T11:01:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/intechcore.com\/wp-content\/uploads\/2024\/09\/technology-785742_1920-1024x683-960x640-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"960\" \/>\n\t<meta property=\"og:image:height\" content=\"640\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ruslan Absaliamov\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ruslan Absaliamov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/\"},\"author\":{\"name\":\"Ruslan Absaliamov\",\"@id\":\"https:\/\/intechcore.com\/en\/#\/schema\/person\/366b8eb5c4abbccd8a17d7328dc0a2e6\"},\"headline\":\"Developing your own programming language in Java+ANTLR: Interpreter\",\"datePublished\":\"2023-09-11T11:53:10+00:00\",\"dateModified\":\"2025-12-01T11:01:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/\"},\"wordCount\":3010,\"publisher\":{\"@id\":\"https:\/\/intechcore.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/intechcore.com\/wp-content\/uploads\/2024\/09\/technology-785742_1920-1024x683-960x640-1.jpg\",\"keywords\":[\"abstgract syntax tree\",\"ANTLR\",\"AST\",\"EBNF\",\"Java\",\"lexical analyzer\",\"parser generator\",\"token\"],\"articleSection\":[\"Articles\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/\",\"url\":\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/\",\"name\":\"Intechcore GmbH - Developing your own programming language in Java+ANTLR: Interpreter\",\"isPartOf\":{\"@id\":\"https:\/\/intechcore.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/intechcore.com\/wp-content\/uploads\/2024\/09\/technology-785742_1920-1024x683-960x640-1.jpg\",\"datePublished\":\"2023-09-11T11:53:10+00:00\",\"dateModified\":\"2025-12-01T11:01:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#primaryimage\",\"url\":\"https:\/\/intechcore.com\/wp-content\/uploads\/2024\/09\/technology-785742_1920-1024x683-960x640-1.jpg\",\"contentUrl\":\"https:\/\/intechcore.com\/wp-content\/uploads\/2024\/09\/technology-785742_1920-1024x683-960x640-1.jpg\",\"width\":960,\"height\":640,\"caption\":\"Tastatur close-up\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/intechcore.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Articles\",\"item\":\"https:\/\/intechcore.com\/en\/articles\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Developing your own programming language in Java+ANTLR: Interpreter\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/intechcore.com\/en\/#website\",\"url\":\"https:\/\/intechcore.com\/en\/\",\"name\":\"Intechcore GmbH - Software Development Company\",\"description\":\"Technology Leading Software Development Company\",\"publisher\":{\"@id\":\"https:\/\/intechcore.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/intechcore.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/intechcore.com\/en\/#organization\",\"name\":\"Intechcore GmbH\",\"alternateName\":\"Software Development Company\",\"url\":\"https:\/\/intechcore.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/intechcore.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/new.intechcore.com\/wp-content\/uploads\/2024\/09\/favicon.png\",\"contentUrl\":\"https:\/\/new.intechcore.com\/wp-content\/uploads\/2024\/09\/favicon.png\",\"width\":64,\"height\":64,\"caption\":\"Intechcore GmbH\"},\"image\":{\"@id\":\"https:\/\/intechcore.com\/en\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/intechcore.com\/en\/#\/schema\/person\/366b8eb5c4abbccd8a17d7328dc0a2e6\",\"name\":\"Ruslan Absaliamov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/intechcore.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/9095380f9a38f10fce91553dcefa1b20f0a83ced000c99ab4f17343f92557a10?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/9095380f9a38f10fce91553dcefa1b20f0a83ced000c99ab4f17343f92557a10?s=96&d=mm&r=g\",\"caption\":\"Ruslan Absaliamov\"},\"url\":\"https:\/\/intechcore.com\/en\/author\/ruslan-absaliamov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Intechcore GmbH - Developing your own programming language in Java+ANTLR: Interpreter","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/","og_locale":"en_US","og_type":"article","og_title":"Intechcore GmbH - Developing your own programming language in Java+ANTLR: Interpreter","og_description":"This is the first article in the series\u00a0\u201cDeveloping your own programming language in Java\u201d\u00a0which is aimed to show the full path of creating a language,\u00a0as well as writing and maintaining tools for it.\u00a0By the end of this article,\u00a0we will implement an interpreter with which it will be possible to execute programs in our language.\u00a0Any programming ...","og_url":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/","og_site_name":"Intechcore GmbH","article_published_time":"2023-09-11T11:53:10+00:00","article_modified_time":"2025-12-01T11:01:35+00:00","og_image":[{"width":960,"height":640,"url":"https:\/\/intechcore.com\/wp-content\/uploads\/2024\/09\/technology-785742_1920-1024x683-960x640-1.jpg","type":"image\/jpeg"}],"author":"Ruslan Absaliamov","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Ruslan Absaliamov","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#article","isPartOf":{"@id":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/"},"author":{"name":"Ruslan Absaliamov","@id":"https:\/\/intechcore.com\/en\/#\/schema\/person\/366b8eb5c4abbccd8a17d7328dc0a2e6"},"headline":"Developing your own programming language in Java+ANTLR: Interpreter","datePublished":"2023-09-11T11:53:10+00:00","dateModified":"2025-12-01T11:01:35+00:00","mainEntityOfPage":{"@id":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/"},"wordCount":3010,"publisher":{"@id":"https:\/\/intechcore.com\/en\/#organization"},"image":{"@id":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#primaryimage"},"thumbnailUrl":"https:\/\/intechcore.com\/wp-content\/uploads\/2024\/09\/technology-785742_1920-1024x683-960x640-1.jpg","keywords":["abstgract syntax tree","ANTLR","AST","EBNF","Java","lexical analyzer","parser generator","token"],"articleSection":["Articles"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/","url":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/","name":"Intechcore GmbH - Developing your own programming language in Java+ANTLR: Interpreter","isPartOf":{"@id":"https:\/\/intechcore.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#primaryimage"},"image":{"@id":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#primaryimage"},"thumbnailUrl":"https:\/\/intechcore.com\/wp-content\/uploads\/2024\/09\/technology-785742_1920-1024x683-960x640-1.jpg","datePublished":"2023-09-11T11:53:10+00:00","dateModified":"2025-12-01T11:01:35+00:00","breadcrumb":{"@id":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#primaryimage","url":"https:\/\/intechcore.com\/wp-content\/uploads\/2024\/09\/technology-785742_1920-1024x683-960x640-1.jpg","contentUrl":"https:\/\/intechcore.com\/wp-content\/uploads\/2024\/09\/technology-785742_1920-1024x683-960x640-1.jpg","width":960,"height":640,"caption":"Tastatur close-up"},{"@type":"BreadcrumbList","@id":"https:\/\/intechcore.com\/en\/development-of-an-own-programming-language-in-javaantlr-interpreter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/intechcore.com\/en\/"},{"@type":"ListItem","position":2,"name":"Articles","item":"https:\/\/intechcore.com\/en\/articles\/"},{"@type":"ListItem","position":3,"name":"Developing your own programming language in Java+ANTLR: Interpreter"}]},{"@type":"WebSite","@id":"https:\/\/intechcore.com\/en\/#website","url":"https:\/\/intechcore.com\/en\/","name":"Intechcore GmbH - Software Development Company","description":"Technology Leading Software Development Company","publisher":{"@id":"https:\/\/intechcore.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/intechcore.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/intechcore.com\/en\/#organization","name":"Intechcore GmbH","alternateName":"Software Development Company","url":"https:\/\/intechcore.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/intechcore.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/new.intechcore.com\/wp-content\/uploads\/2024\/09\/favicon.png","contentUrl":"https:\/\/new.intechcore.com\/wp-content\/uploads\/2024\/09\/favicon.png","width":64,"height":64,"caption":"Intechcore GmbH"},"image":{"@id":"https:\/\/intechcore.com\/en\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/intechcore.com\/en\/#\/schema\/person\/366b8eb5c4abbccd8a17d7328dc0a2e6","name":"Ruslan Absaliamov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/intechcore.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/9095380f9a38f10fce91553dcefa1b20f0a83ced000c99ab4f17343f92557a10?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9095380f9a38f10fce91553dcefa1b20f0a83ced000c99ab4f17343f92557a10?s=96&d=mm&r=g","caption":"Ruslan Absaliamov"},"url":"https:\/\/intechcore.com\/en\/author\/ruslan-absaliamov\/"}]}},"_links":{"self":[{"href":"https:\/\/intechcore.com\/en\/wp-json\/wp\/v2\/posts\/7726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/intechcore.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intechcore.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intechcore.com\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/intechcore.com\/en\/wp-json\/wp\/v2\/comments?post=7726"}],"version-history":[{"count":19,"href":"https:\/\/intechcore.com\/en\/wp-json\/wp\/v2\/posts\/7726\/revisions"}],"predecessor-version":[{"id":10150,"href":"https:\/\/intechcore.com\/en\/wp-json\/wp\/v2\/posts\/7726\/revisions\/10150"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/intechcore.com\/en\/wp-json\/wp\/v2\/media\/7449"}],"wp:attachment":[{"href":"https:\/\/intechcore.com\/en\/wp-json\/wp\/v2\/media?parent=7726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intechcore.com\/en\/wp-json\/wp\/v2\/categories?post=7726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intechcore.com\/en\/wp-json\/wp\/v2\/tags?post=7726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}