{"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":"https:\/\/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","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","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-717","post","type-post","status-publish","format-standard","hentry","category-general"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p4vJD-bz","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/comments?post=717"}],"version-history":[{"count":0,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/717\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/media?parent=717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/categories?post=717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/tags?post=717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}