<div dir="ltr"><div><div>This is a classic series of articles that show how to write a very simple compiler in Turbo Pascal. The fundamentals when it comes to scanning are the same:<br><br><a href="http://compilers.iecc.com/crenshaw/">http://compilers.iecc.com/crenshaw/</a><br><br></div>I've also written a BASIC implementation for Free Pascal and Lazarus. The scanner should be straightforward to understand:<br><br><a href="http://runtimelegend.com/rep/rbasic/artifact/2350e85c36a77e4d2d76adde23fd7d45731b5b22">http://runtimelegend.com/rep/rbasic/artifact/2350e85c36a77e4d2d76adde23fd7d45731b5b22</a><br><br></div><div>The compiler code shows how it can be used:<br><a href="http://runtimelegend.com/rep/rbasic/artifact/93859f52fd424edfc1e0d5dfd16a92ed8ac04855">http://runtimelegend.com/rep/rbasic/artifact/93859f52fd424edfc1e0d5dfd16a92ed8ac04855</a><br><br></div><div>But you may also find the formatter code simpler. Although it is a bit too simple:<br><a href="http://runtimelegend.com/rep/rbasic/artifact/f3e9fb2d1ed8e60d36b50754c2d9a7d7c109fc40">http://runtimelegend.com/rep/rbasic/artifact/f3e9fb2d1ed8e60d36b50754c2d9a7d7c109fc40</a><br></div><div><br></div>For general theory you can look on recursive descent parsers (they're the simplest to implement and AFAIK most compilers use them, either to build the token list or directly).<br><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 6, 2015 at 6:55 PM, silvioprog <span dir="ltr"><<a href="mailto:silvioprog@gmail.com" target="_blank">silvioprog@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello,<div><br></div><div>I'm planning to write three parsers, and googling, I found some entries talking about lexical parsers.</div><div><br></div><div>After that, I did a 'find in files' in FPC sources, and I found many parsers (eg: jsonparser (jsonscanner), JSParser (JSScanner), fpsqlparser (fpsqlscanner), PParser (PScanner), fpexprpars etc.) that use lexical scanner.</div><div><div><br></div><div>Below, three possible string that I need to parse:</div><div><br></div><div>1)</div><div><br></div><div>${someVariable} -- 4 tokens</div><div><br></div><div>or</div><div><br></div><div>${a + b} -- 8 tokens - 1 expression<br></div><div><br></div><div>or</div><div><br></div><div>${fn:lenght('abc') * 3} -- 11 tokens - 1 function - 1 expression<br></div><div><br></div><div>2)</div><div><br></div><div><div><c:forEach var="contact" items="${contactDao.list}"></div><div>    ${<a href="http://contato.name" target="_blank">contato.name</a>}, ${contato.email}</div><div></c:forEach></div></div><div><br></div><div>3)</div><div><br></div><div>contacts[0].name=abc</div><div>contacts[0].email=abc@def<br></div><div><div>contacts[1].name=def</div><div>contacts[2].email=def@ghi</div></div><div><br></div><div>So my parser will allow to register dynamic variables, functions (to be called via script) and plugins (to extend the parser).</div><div><br></div><div>However, I have a question: is there any article about 'how to write lexical parsers' using Object Pascal?</div><div><br></div><div>I need any material about this subject, and I'm very grateful for any tip.</div><div><br></div><div>Thanks in advance!</div><span class="HOEnZb"><font color="#888888"><div><br></div>-- <br><div>Silvio Clécio<br>My public projects - <a href="http://github.com/silvioprog" target="_blank">github.com/silvioprog</a></div>
</font></span></div></div>
<br>--<br>
_______________________________________________<br>
Lazarus mailing list<br>
<a href="mailto:Lazarus@lists.lazarus.freepascal.org">Lazarus@lists.lazarus.freepascal.org</a><br>
<a href="http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus" target="_blank">http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus</a><br>
<br></blockquote></div><br></div>