{"id":46,"date":"2008-10-14T18:48:21","date_gmt":"2008-10-14T16:48:21","guid":{"rendered":"http:\/\/floris.briolas.nl\/floris\/?p=46"},"modified":"2014-10-17T15:10:17","modified_gmt":"2014-10-17T13:10:17","slug":"antlr-common-pittfals","status":"publish","type":"post","link":"https:\/\/floris.briolas.nl\/floris\/2008\/10\/antlr-common-pittfals\/","title":{"rendered":"ANTLR common pitfalls"},"content":{"rendered":"<p>Personally I think ANTLR is a great tool, It has a steep learning curve and it has a few quirks. I hope the description here will help you find your problem, understand it and help you fix the issue.<\/p>\n<p><strong>Recognition problems<\/strong><\/p>\n<p>Recognizes number as &#8216;1234&#8217; but not as &#8216;1&#8217;:<br \/>\nExample:<\/p>\n<pre name=\"code\" class=\"antlr\">\r\ngrammar number;\r\n\r\nnumber :\tINT;\r\nDIGIT\t:\t'0'..'9';\r\nINT\t:\tDIGIT+;\r\n<\/pre>\n<p>Explanation, the input such as &#8216;1&#8217; or &#8216;4&#8217; is just one char, it will be recognized as a &#8216;DIGIT&#8217;, not as an &#8216;INT&#8217;. You have two options :<br \/>\n&#8211; delete the DIGIT rule, and rewrite the INT rule as &#8221; INT:&#8217;0&#8242;..&#8217;9&#8242;; &#8221; (works)<br \/>\n&#8211; place the &#8216;fragment&#8217; keyword in front of DIGIT. DIGIT will not be seen as a token.<\/p>\n<p><strong>Grammar Check Errors<\/strong><\/p>\n<p>The following token definitions are unreachable: INT (AntlrWorks 1.1.7)<br \/>\nThe following token definitions can never be matched because prior tokens match the same input: INT (AntlrWorks 1.2) (AntlrWorks 1.3) <\/p>\n<p>Example:<\/p>\n<pre name=\"code\" class=\"antlr\">\r\ngrammar number;\r\n\r\nDIGIT:\t'0'..'9';\r\nINT\t:\tDIGIT;\r\n<\/pre>\n<p>What has INT to offer? As is it is a redundant rule. Probably you meant more than one number (thus matching 43) so make it INT : DIGIT+;<\/p>\n<p><strong>Another possibility:<\/strong><\/p>\n<pre name=\"code\" class=\"antlr\">\r\nfactExpression:\tFact fact;\r\nfact \t:\tID;\r\n\r\npropertyExpression:\tfact Property property;\r\n\r\nproperty:\tID;\r\n\r\nNEWLINE\t:\t'\\r'?'\\n';\r\nWS\t:\t(' ' | '\\t' | '\\n' | '\\r') { skip(); };\r\n\r\nFact\t: 'There is ' ARTICLE \/\/added extra space since it needs to be there\r\n;\r\n\r\nProperty\r\n:\t'has '  ARTICLE \/\/added extra space since it needs to be there\r\n;\r\n\r\nID \t:\t('a'..'z'|'A'..'Z')+;\r\n\r\nARTICLE\r\n:\t('a'|'an')\r\n;\r\n<\/pre>\n<p>Here the error is &#8220;The following token&#8230;match the same input: ARTICLE&#8221;. ID can both match &#8216;a&#8217; and &#8216;an&#8217;, but in this case ARTICLE is more important. Flip the ID and ARTICLE rule, and it&#8217;s fine.<br \/>\n<span style=\"background-color: #99CCFF;\"><strong><a href=\"http:\/\/floris.briolas.nl\/floris\/?p=256\">More on Ambiguous rules.<\/a><\/strong><\/span><\/p>\n<hr \/>\n<p>syntax error: codegen: :0:0: unexpected end of subtree (AntlrWorks 1.1.7 &amp; 1.2)<br \/>\nExample:<\/p>\n<pre name=\"code\" class=\"antlr\">\r\ngrammar number;\r\n\/\/number \t:\tINT | FLOAT;\r\nDIGIT\t :\t'0'..'9';\r\nINT\t:\tDIGIT+;\r\nFLOAT\t:\tDIGIT* '.' DIGIT+;\r\n<\/pre>\n<p>You are working with a mixed grammar (both LEXER and PARSER).<br \/>\nYou did not include any parser rules, please do so. Uncomment the &#8216;number&#8217; parser rule.<br \/>\n<strong>Another possibility:<\/strong> is that the last line of your grammar is comment, just move it.<\/p>\n<p>*Updated 10-nov-2009 : Added internal link to explain more on Ambiguous rules.;<br \/>\n*Updated 13-oct-2009 : Syntax Highlighting;<br \/>\n*Updated 25-jan-2010 : Small updates<br \/>\n*Updated 17-ock-2014 : Ending support for antlr 3. Thank you, it was fun!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Personally I think ANTLR is a great tool, It has a steep learning curve and it has a few quirks. I hope the description here will help you find your problem, understand it and help you fix the issue. Recognition problems Recognizes number as &#8216;1234&#8217; but not as &#8216;1&#8217;: Example: grammar number; number : INT; [&hellip;]<\/p>\n","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_newsletter_access":"","_jetpack_newsletter_tier_id":0,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[3],"tags":[6],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p61yPs-K","_links":{"self":[{"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/posts\/46"}],"collection":[{"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":19,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/posts\/46\/revisions\/141"}],"wp:attachment":[{"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}