{"id":717,"date":"2008-02-18T12:04:39","date_gmt":"2008-02-18T17:04:39","guid":{"rendered":"http:\/\/www.bytebot.net\/blog\/archives\/2008\/02\/18\/an-introduction-to-antlr-sparse-notes"},"modified":"2008-02-18T12:04:42","modified_gmt":"2008-02-18T17:04:42","slug":"an-introduction-to-antlr-sparse-notes","status":"publish","type":"post","link":"http:\/\/www.bytebot.net\/blog\/archives\/2008\/02\/18\/an-introduction-to-antlr-sparse-notes","title":{"rendered":"An introduction to ANTLR (sparse notes)"},"content":{"rendered":"<p><i>I attended Clinton Roy&#8217;s excellent session titled <a href=\"http:\/\/linux.conf.au\/programme\/detail?TalkID=41\">An introduction to ANTLR: A parser toolkit for problems large and small<\/a>. Now that the <a href=\"http:\/\/mirror.linux.org.au\/pub\/linux.conf.au\/2008\/slides\/041-antlr_lca.pdf.bz2\">slides<\/a>, and the video (<a href=\"http:\/\/mirror.linux.org.au\/pub\/linux.conf.au\/2008\/Thu\/mel8-041a.ogg\">1<\/a>, <a href=\"http:\/\/mirror.linux.org.au\/pub\/linux.conf.au\/2008\/Thu\/mel8-041b.ogg\">2<\/a>) are online, I don&#8217;t know if my bits of notes are of any use (I made them while the tutorial was in progress), but they&#8217;re sitting on my desktop, and really should just get published. The files referenced below, you get via checking out the <a href=\"http:\/\/linux.conf.au\/wiki\/index.php?title=Antlr_Tutorial_Preparation\">Antlr Tutorial Preparation<\/a> wiki page.<\/i><\/p>\n<p><b>Why use Antlr?<\/b><br \/>To parse configuration files, syntax highlighting, Domain Specific Languages (DSL), interpreters, translation\/transformation.<\/p>\n<p>Generates easy to follow code, LL(*) parsing algorithm. Bison is more powerful than Antlr. Compined lexer, and parser generator. <\/p>\n<p>fun (int a, char b); &lt;&#8211; as you do LL, till you hit the &#8220;;&#8221;, you have no idea if you&#8217;re dealing with a function or a declaration. Of course, there are look-ahead LL parsers too. An LL3 parser, which can see 3 tokens ahead, you still can&#8217;t see ahead enough, till you hit the ;. This is why, there exists an LL(*) &#8211; pick the smallest look-ahead, your grammar would need<\/p>\n<p>Antlr, will help you get rid of using regular expressions.<\/p>\n<p>Island grammars &#8211; one language, inside another (like HTML, inside PHP, or Doxygen inside C) are supported by Antlr.<\/p>\n<p>Antlr Wiki is good, but hard to find things. Mailing list is great. The book by Terrence Parr is good, but out-dated, so go ahead, and get the online PDF version. A new cookbook\/recipe list is coming out soon.<\/p>\n<p>Using <a href=\"http:\/\/www.antlr.org\/works\/index.html\">AntlrWorks<\/a>. java -jar antlrworks.jar<\/p>\n<p>conffile.g parses a = 1, b = foo.<\/p>\n<p>IDENT&nbsp;&nbsp; :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (&#8216;_&#8217;|&#8217;a&#8217;..&#8217;z&#8217;|&#8217;A&#8217;..&#8217;Z&#8217;)(&#8216;_&#8217;|&#8217;a&#8217;..&#8217;z&#8217;|&#8217;A&#8217;..&#8217;Z&#8217;|&#8217;0&#8242;..&#8217;9&#8242;)*;<br \/>NUMBER&nbsp; :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;0&#8217;..&#8217;9&#8217;+;<br \/>WS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;\\r&#8217; | &#8216;\\n&#8217; | &#8216; &#8216; {$channel=HIDDEN;};<\/p>\n<p>The above are lexer rules. WS = whitespace. It reads from bottom up. White space, a number (0-9). IDENT will match foo, foo, foo1, but not 1foo (identifiers don&#8217;t start with numbers).<\/p>\n<p>{$channel=HIDDEN;} &lt;&#8211; IDENTs and NUMBERs get through the channel get through the parse. The whitespace, the parser sees them, but it will ignore them (i.e. hide them).<\/p>\n<p>[-(byte@hermione)-(pts\/6)-(11:21am:31\/01\/2008)-]<br \/>[-(\/tmp\/antlrworks)&gt; l<br \/>total 248<br \/>drwxrwxr-x&nbsp; 2 byte byte&nbsp;&nbsp; 4096 2008-01-31 11:20 .\/<br \/>drwxrwxrwt 44 root root 192512 2008-01-31 11:20 ..\/<br \/>-rw-rw-r&#8211;&nbsp; 1 byte byte&nbsp;&nbsp;&nbsp; 340 2008-01-31 11:20 conffile__.g<br \/>-rw-rw-r&#8211;&nbsp; 1 byte byte&nbsp;&nbsp; 8492 2008-01-31 11:20 conffileLexer.java<br \/>-rw-rw-r&#8211;&nbsp; 1 byte byte&nbsp;&nbsp; 4180 2008-01-31 11:20 conffileParser.java<br \/>-rw-rw-r&#8211;&nbsp; 1 byte byte&nbsp;&nbsp;&nbsp;&nbsp; 28 2008-01-31 11:20 conffile.tokens<\/p>\n<p>conffile__.g &#8211; lexer file<br \/>conffile.tokes &#8211; tokens<\/p>\n<p>CMinus.g takes input, which is a C program. Go to the interpreter, and you can then see the entire parse tree. Very impressive!<\/p>\n<p>Technorati Tags: <a class=\"performancingtags\" href=\"http:\/\/technorati.com\/tag\/lca08\" rel=\"tag\">lca08<\/a>, <a class=\"performancingtags\" href=\"http:\/\/technorati.com\/tag\/antlr\" rel=\"tag\">antlr<\/a>, <a class=\"performancingtags\" href=\"http:\/\/technorati.com\/tag\/antlrworks\" rel=\"tag\">antlrworks<\/a><\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-email\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-email sd-button share-icon\" href=\"mailto:?subject=%5BShared%20Post%5D%20An%20introduction%20to%20ANTLR%20%28sparse%20notes%29&body=http%3A%2F%2Fwww.bytebot.net%2Fblog%2Farchives%2F2008%2F02%2F18%2Fan-introduction-to-antlr-sparse-notes&share=email\" target=\"_blank\" title=\"Click to email a link to a friend\" data-email-share-error-title=\"Do you have email set up?\" data-email-share-error-text=\"If you&#039;re having problems sharing via email, you might not have email set up for your browser. You may need to create a new email yourself.\" data-email-share-nonce=\"d1c5795d9d\" data-email-share-track-url=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/02\/18\/an-introduction-to-antlr-sparse-notes?share=email\"><span>Email<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-717\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/02\/18\/an-introduction-to-antlr-sparse-notes?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-717\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/02\/18\/an-introduction-to-antlr-sparse-notes?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-717\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/02\/18\/an-introduction-to-antlr-sparse-notes?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>I attended Clinton Roy&#8217;s excellent session titled An introduction to ANTLR: A parser toolkit for problems large and small. Now that the slides, and the video (1, 2) are online, I don&#8217;t know if my bits of notes are of any use (I made them while the tutorial was in progress), but they&#8217;re sitting on [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-email\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-email sd-button share-icon\" href=\"mailto:?subject=%5BShared%20Post%5D%20An%20introduction%20to%20ANTLR%20%28sparse%20notes%29&body=http%3A%2F%2Fwww.bytebot.net%2Fblog%2Farchives%2F2008%2F02%2F18%2Fan-introduction-to-antlr-sparse-notes&share=email\" target=\"_blank\" title=\"Click to email a link to a friend\" data-email-share-error-title=\"Do you have email set up?\" data-email-share-error-text=\"If you&#039;re having problems sharing via email, you might not have email set up for your browser. You may need to create a new email yourself.\" data-email-share-nonce=\"d1c5795d9d\" data-email-share-track-url=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/02\/18\/an-introduction-to-antlr-sparse-notes?share=email\"><span>Email<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-717\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/02\/18\/an-introduction-to-antlr-sparse-notes?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-717\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/02\/18\/an-introduction-to-antlr-sparse-notes?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-717\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/02\/18\/an-introduction-to-antlr-sparse-notes?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[1],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4vJD-bz","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":538,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2007\/04\/27\/diggcom-scales-japanese-character-set-data-warehousing","url_meta":{"origin":717,"position":0},"title":"Digg.com scales; Japanese Character Set; Data Warehousing","date":"27\/4\/2007","format":false,"excerpt":"I missed a couple of talks that I'd really have liked to attend, for various reasons (probably the fact that at the MySQL conferences, staff also have a tonne of meetings and customers\/people to meet). Thanks to the great bloggers, I don't feel so bad for missing such talks. And\u2026","rel":"","context":"In &quot;MySQL&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":874,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2008\/07\/22\/memcached-and-mysql-webminar-from-a-web-20-company","url_meta":{"origin":717,"position":1},"title":"Memcached and MySQL: webminar from a Web 2.0 company","date":"22\/7\/2008","format":false,"excerpt":"At OSCON, Brian and Dormando gave their ever famous talk, Memcached and MySQL: Everything You Need To Know. I didn't attend the tutorial, but they assured me it was similar to what was given at the MySQL Conference 2008 (everything, but the very nice buttons dormando was giving out with\u2026","rel":"","context":"In &quot;MySQL&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3214,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2016\/05\/02\/april-2016-mariadb-server-related-worklog","url_meta":{"origin":717,"position":2},"title":"April 2016 MariaDB Server related worklog","date":"2\/5\/2016","format":false,"excerpt":"Planning for May MariaDB Corporation roadshows in London and Paris. The Paris edition is also one where Kentoku Shiba from Spiral Arms will make an appearance; we're also likely to plan several meetings around there too. Planning the MariaDB Berlin Meetup. Getting the speakers from our side. Ensuring that other\u2026","rel":"","context":"In &quot;Work&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2586,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2012\/11\/19\/percona-live-santa-clara-2013-tutorial-schedule-out","url_meta":{"origin":717,"position":3},"title":"Percona Live Santa Clara 2013 tutorial schedule out","date":"19\/11\/2012","format":false,"excerpt":"I didn't participate in 2012, but in 2013, I'm back on the conference committee for Percona Live Santa Clara. We have an awesome Program Chair in Shlomi Noach, and after much deliberation & commentary, we have a tutorials schedule out now. Expect that the rest of the conference content to\u2026","rel":"","context":"In &quot;MariaDB&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3280,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2016\/09\/08\/speaking-at-percona-live-europe-amsterdam","url_meta":{"origin":717,"position":4},"title":"Speaking at Percona Live Europe Amsterdam","date":"8\/9\/2016","format":false,"excerpt":"I'm happy to speak at Percona Live Europe Amsterdam 2016 again this year (just look at the awesome schedule). On my agenda: Monday Oct 3 Tutorial: Best Practices of MySQL High Availability Monday Oct 3 Tutorial: The Complete MariaDB Tutorial Wednesday Oct 5 Talk: Securing your MySQL\/MariaDB data with my\u2026","rel":"","context":"In &quot;MariaDB&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3134,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2015\/11\/29\/voting-for-the-percona-live-data-performance-conference-2016","url_meta":{"origin":717,"position":5},"title":"Voting for talks at the Percona Live Data Performance Conference 2016","date":"29\/11\/2015","format":false,"excerpt":"So this year the Percona Live conference has a new name \u2013 it is the \u201cData Performance Conference\u201d (presumably for a much broader appeal and the fact that Percona is now in the MongoDB world as well). And the next new thing to note? You have to go through a\u2026","rel":"","context":"In &quot;MariaDB&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/717"}],"collection":[{"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/comments?post=717"}],"version-history":[{"count":0,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/717\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/media?parent=717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/categories?post=717"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/tags?post=717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}