<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>grammar &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<atom:link href="https://blog.sephiroth.it/tag/grammar/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.sephiroth.it</link>
	<description>programmer by pure chance</description>
	<lastBuildDate>Sat, 19 Jun 2010 14:20:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://blog.sephiroth.it/wp-content/uploads/2019/03/cropped-sepy-big-32x32.png</url>
	<title>grammar &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<link>https://blog.sephiroth.it</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>An experience with antlr, java and python</title>
		<link>https://blog.sephiroth.it/2006/12/01/an-experience-with-antlr-java-and-python/</link>
					<comments>https://blog.sephiroth.it/2006/12/01/an-experience-with-antlr-java-and-python/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Fri, 01 Dec 2006 19:09:11 +0000</pubDate>
				<category><![CDATA[general]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[antlr]]></category>
		<category><![CDATA[asdt]]></category>
		<category><![CDATA[bison]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[ecma]]></category>
		<category><![CDATA[gnu flex]]></category>
		<category><![CDATA[grammar]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[profiler]]></category>
		<category><![CDATA[psyco]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=187</guid>

					<description><![CDATA[I just wanted to share a little experience with generating an AS3 parser using antlr and python.I was trying first to create the parser using...
]]></description>
										<content:encoded><![CDATA[<p>I just wanted to share a little experience with generating an AS3 parser using <a href="http://www.antlr.org">antlr</a> and <a href="http://www.python.org">python</a>.<br />
I was trying first to create the parser using GNU <a title="gnu flex" href="http://www.gnu.org/software/flex/" target="_blank">Flex</a> and <a title="gnu bison" href="http://www.gnu.org/software/bison/" target="_blank">Bison</a> in C, probably the best way for a very performancing parser.<br />
Yeah, that&#8217;s right.. but looking at the antlr syntax I realized that&#8217;s easier and easier.<br />
Moreover I start using this very useful <a title="antlrstudio" href="http://www.placidsystems.com/antlrstudio.aspx" target="_blank">eclipse plugin</a> for antlr debugging which made my life easier!</p>
<p>The grammar file I created is a compromise between the <a title="as3.g" href="http://www.google.com/codesearch?hl=it&amp;q=+file:%5C.g%24+as3.g+show:gFeBx72kxIk:XKFG9i76Jiw:mSlmdOLa2XQ&amp;sa=N&amp;cd=1&amp;ct=rc&amp;cs_p=:pserver:anonymous%40aseclipseplugin.cvs.sourceforge.net:/cvsroot/aseclipseplugin+org.asdt.astview&amp;cs_f=src/org/asdt/core/internal/antlr/as3.g#a0" target="_blank">asdt grammar</a> file and the <a title="ECMA-262 specifications" href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" target="_blank">ECMA-262 grammar</a> specification.</p>
<p>Once finished working on my eclipse project I&#8217;ve managed to parse succesfully all the adobe corelibs files using this java test file:<br />
<code lang="java">package org.sepy.core.parsers.as3;<br />
import java.io.File;<br />
import java.io.FileInputStream;<br />
import java.io.FileNotFoundException;<br />
import antlr.CommonAST;<br />
import antlr.RecognitionException;<br />
import antlr.TokenStreamException;<br />
public class Application {<br />
public static void main(String argv[])<br />
{<br />
if(argv.length &gt; 0)<br />
{<br />
File file = new File(argv[0]);<br />
if(file.exists())<br />
{<br />
FileInputStream is = null;<br />
try {<br />
is = new FileInputStream(file);<br />
} catch (FileNotFoundException e) {<br />
// TODO Auto-generated catch block<br />
e.printStackTrace();<br />
}<br />
AS3Lexer L = new AS3Lexer(is);<br />
AS3Parser P = new AS3Parser(L);<br />
try {<br />
P.compilationUnit();<br />
} catch (RecognitionException e) {<br />
// TODO Auto-generated catch block<br />
System.out.println(" line=" + e.line + ", column="+ e.column);<br />
System.out.println(e.getMessage());<br />
e.printStackTrace(System.err);<br />
} catch (TokenStreamException e) {<br />
// TODO Auto-generated catch block<br />
System.out.println(" line=" + L.getLine() + ", column="+ L.getColumn());<br />
System.out.println(L.getGuessInfo());<br />
System.out.println(e.getMessage());<br />
e.printStackTrace(System.err);<br />
}<br />
CommonAST.setVerboseStringConversion(false, P.getTokenNames());<br />
CommonAST ast = (CommonAST) P.getAST();<br />
System.out.println("Tree:");<br />
System.out.println(ast.toStringTree());<br />
}<br />
}<br />
}<br />
}</code><br />
Ok, done that I decided to export the grammar file for python (thanks to antrl python export feature).<br />
Everything works fine also for python, but I realized that the python script were so much slower than the java one!<br />
<code lang="python">import sys<br />
import antlr<br />
import AS3Parser<br />
import AS3Lexer<br />
L = AS3Lexer.Lexer(filename);<br />
P = AS3Parser.Parser(L);<br />
P.setFilename(filename)<br />
try:<br />
   P.compilationUnit();<br />
   ast = P.getAST();<br />
except:<br />
   pass<br />
</code><br />
On a 75Kb actionscript file the python script took about 7 seconds to run, while the java application only 2 seconds. I know python interpreter caould be slower than many other languages, but I never thought so much slower.<br />
So I run the python <a href="http://docs.python.org/lib/module-hotshot.html" target="_blank">hotshot profiler</a> to see which could be the bottleneck in the python script and I found most of the problems were due to unuseless antlr (the python module) method&#8217;s calls.<br />
After making corrections to the antlr.py file the same script took exactly half of the time. Now 3 seconds. Wow 🙂<br />
But not fast enough.<br />
So I enabled for the antlr python script <a href="http://psyco.sourceforge.net/" target="_blank">psyco</a> module and this time the same script took <strong>only 1.6 seconds.</strong><br />
Now the python script is fast enough, even if I&#8217;m sure I can make more optimizations in the antlr module&#8230;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2006/12/01/an-experience-with-antlr-java-and-python/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
