Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-03

How to write an eficient lexical scanner/parser?

16 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

Hello,

I'm planning to write three parsers, and googling, I found some entries talking about lexical parsers.

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.

Below, three possible string that I need to parse:

1)

${someVariable} -- 4 tokens

or

${a + b} -- 8 tokens - 1 expression

or

${fn:lenght('abc') * 3} -- 11 tokens - 1 function - 1 expression

2)

<c:forEach var="contact" items="${contactDao.list}"> ${contato.name}, ${contato.email}
</c:forEach>

3)

contacts[0].name=abc
contacts[0].email=abc at def
contacts[1].name=def
contacts[2].email=def at ghi

So my parser will allow to register dynamic variables, functions (to be called via script) and plugins (to extend the parser).

However, I have a question: is there any article about 'how to write lexical parsers' using Object Pascal?

I need any material about this subject, and I'm very grateful for any tip.

Thanks in advance!

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150306/4e518192/attachment.html>

Reply by email

Kostas Michalopoulos
to silvioprog

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:

http://compilers.iecc.com/crenshaw/

I've also written a BASIC implementation for Free Pascal and Lazarus. The scanner should be straightforward to understand:

http://runtimelegend.com/rep/rbasic/artifact/2350e85c36a77e4d2d76adde23fd7d45731b5b22

The compiler code shows how it can be used:
http://runtimelegend.com/rep/rbasic/artifact/93859f52fd424edfc1e0d5dfd16a92ed8ac04855

But you may also find the formatter code simpler. Although it is a bit too simple:
http://runtimelegend.com/rep/rbasic/artifact/f3e9fb2d1ed8e60d36b50754c2d9a7d7c109fc40

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).

silvioprog wrote 58 lines
Hello, I'm planning to write three parsers, and googling, I found some entries talking about lexical parsers. 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. Below, three possible string that I need to parse: 1) ${someVariable} -- 4 tokens or ${a + b} -- 8 tokens - 1 expression or ${fn:lenght('abc') * 3} -- 11 tokens - 1 function - 1 expression 2) <c:forEach var="contact" items="${contactDao.list}"> ${contato.name}, ${contato.email} </c:forEach> 3) contacts[0].name=abc contacts[0].email=abc at def contacts[1].name=def contacts[2].email=def at ghi So my parser will allow to register dynamic variables, functions (to be called via script) and plugins (to extend the parser). However, I have a question: is there any article about 'how to write lexical parsers' using Object Pascal? I need any material about this subject, and I'm very grateful for any tip. Thanks in advance! -- Silvio Cl?cio My public projects - github.com/silvioprog -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150306/f76a6548/attachment.html>

Reply by email

aradeonas
to Kostas Michalopoulos

Hi Silvio,

About this parsing subject maybe looking at BeniBela Xidel and InternetTools help you or talking to Benito Aurthur of them. He is a very good developer and kind person like you.
http://videlibri.sourceforge.net/xidel.html
http://www.benibela.de/sources_en.html#internettools

Ara

Kostas Michalopoulos wrote 86 lines
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: http://compilers.iecc.com/crenshaw/ I've also written a BASIC implementation for Free Pascal and Lazarus. The scanner should be straightforward to understand: http://runtimelegend.com/rep/rbasic/artifact/2350e85c36a77e4d2d76adde23fd7d45731b5b22 The compiler code shows how it can be used: http://runtimelegend.com/rep/rbasic/artifact/93859f52fd424edfc1e0d5dfd16a92ed8ac04855 But you may also find the formatter code simpler. Although it is a bit too simple: http://runtimelegend.com/rep/rbasic/artifact/f3e9fb2d1ed8e60d36b50754c2d9a7d7c109fc40 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). On Fri, Mar 6, 2015 at 6:55 PM, silvioprog wrote: > Hello, > > I'm planning to write three parsers, and googling, I found some > entries talking about lexical parsers. > > 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. > > Below, three possible string that I need to parse: > > 1) > > ${someVariable} -- 4 tokens > > or > > ${a + b} -- 8 tokens - 1 expression > > or > > ${fn:lenght('abc') * 3} -- 11 tokens - 1 function - 1 expression > > 2) > > <c:forEach var="contact" items="${contactDao.list}"> ${contato.name}, > ${contato.email} </c:forEach> > > 3) > > contacts[0].name=abc contacts[0].email=abc at def contacts[1].name=def > contacts[2].email=def at ghi > > So my parser will allow to register dynamic variables, functions (to > be called via script) and plugins (to extend the parser). > > However, I have a question: is there any article about 'how to write > lexical parsers' using Object Pascal? > > I need any material about this subject, and I'm very grateful for > any tip. > > Thanks in advance! > > > -- > > Silvio Cl?cio My public projects - github.com/silvioprog > > > -- > _______________________________________________ > Lazarus mailing list > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus > -- _________________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
http://www.fastmail.com - The professional email service -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150306/c0f7624e/attachment-0001.html>

Reply by email

Anthony Walter
to silvioprog: I'm planning to write three parsers, and googling, I found some entries talking about…

Here is something I originally wrote in 2001. I had a product briefly for converting pascal code into documentation and it quite fast. Do with it as you will.

http://pastebin.com/qXJdHwGM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150306/12d71d9e/attachment.html>

Reply by email

silvioprog
to Kostas Michalopoulos: This is a classic series of articles that show how to write a very simple compiler in…

On Fri, Mar 6, 2015 at 3:15 PM, Kostas Michalopoulos <

Show quoted text 27 lines
wrote: 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: http://compilers.iecc.com/crenshaw/ I've also written a BASIC implementation for Free Pascal and Lazarus. The scanner should be straightforward to understand: http://runtimelegend.com/rep/rbasic/artifact/2350e85c36a77e4d2d76adde23fd7d45731b5b22 The compiler code shows how it can be used: http://runtimelegend.com/rep/rbasic/artifact/93859f52fd424edfc1e0d5dfd16a92ed8ac04855 But you may also find the formatter code simpler. Although it is a bit too simple: http://runtimelegend.com/rep/rbasic/artifact/f3e9fb2d1ed8e60d36b50754c2d9a7d7c109fc40 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).

I downloaded the PDF with all articles.

Thank you very much!

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150310/6ae0594b/attachment.html>

Reply by email

silvioprog
to aradeonas: About this parsing subject maybe looking at BeniBela Xidel and InternetTools help you or…
aradeonas wrote 10 lines
Hi Silvio, About this parsing subject maybe looking at BeniBela Xidel and InternetTools help you or talking to Benito Aurthur of them. He is a very good developer and kind person like you. http://videlibri.sourceforge.net/xidel.html http://www.benibela.de/sources_en.html#internettools Ara

I think that it uses regex as parser. I'll check when I have a free time. Thank you!

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150310/e789931d/attachment.html>

Reply by email

silvioprog
to Anthony Walter: Here is something I originally wrote in 2001. I had a product briefly for converting…
Anthony Walter wrote 6 lines
Here is something I originally wrote in 2001. I had a product briefly for converting pascal code into documentation and it quite fast. Do with it as you will. http://pastebin.com/qXJdHwGM

Nice parser. I had some ideas after seeing your code. Thank you!

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150310/c5386f18/attachment-0001.html>

Reply by email

leledumbo
to silvioprog: I'm planning to write three parsers, and googling, I found some entries talking about…
However, I have a question: is there any article about 'how to write

lexical parsers' using Object Pascal?

First, you need to differentiate them correctly. Lexical scanner (or simply lexer) and parser, not lexical parser :)

I've written some simple languages, generating GraphViz, LLVM or C code, using both self learning reference from the almighty Prof. Niklaus Wirth's articles, Jack Crenshaw's, the legendary dragon book and many others plus my compiler course in college.

I really really suggest Prof. Niklaus Wirth's one
(http://www.inf.ethz.ch/personal/wirth/, click the Compiler Construction link) as it's concise, simple, doesn't use stupid generator (i.e.: easy to implement by hand) while covering most important parts of the subject. While he didn't use or stress or even discourage the usage of abstract syntax tree, he did explain it that's good enough to understand.

Here's what I've written in the past and is publicly available:
- https://bitbucket.org/leledumbo/erd-maker (generates GraphViz code for ERD visualization)
- https://bitbucket.org/leledumbo/linguc (generates LLVM code that can be compiled as a library for a mathematically provable language focusing in database driven application)

I don't use the direct code generation approach anymore as I found the abstract syntax tree to be usable for more than just code generation.

Reply by email

Mehmet Erol Sanliturk
to leledumbo
leledumbo wrote 41 lines
> However, I have a question: is there any article about 'how to write lexical parsers' using Object Pascal? First, you need to differentiate them correctly. Lexical scanner (or simply lexer) and parser, not lexical parser :) I've written some simple languages, generating GraphViz, LLVM or C code, using both self learning reference from the almighty Prof. Niklaus Wirth's articles, Jack Crenshaw's, the legendary dragon book and many others plus my compiler course in college. I really really suggest Prof. Niklaus Wirth's one (http://www.inf.ethz.ch/personal/wirth/, click the Compiler Construction link) as it's concise, simple, doesn't use stupid generator (i.e.: easy to implement by hand) while covering most important parts of the subject. While he didn't use or stress or even discourage the usage of abstract syntax tree, he did explain it that's good enough to understand. Here's what I've written in the past and is publicly available: - https://bitbucket.org/leledumbo/erd-maker (generates GraphViz code for ERD visualization) - https://bitbucket.org/leledumbo/linguc (generates LLVM code that can be compiled as a library for a mathematically provable language focusing in database driven application) I don't use the direct code generation approach anymore as I found the abstract syntax tree to be usable for more than just code generation. -- View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-How-to-write-an-eficient-lexical-scanner-parser-tp4041002p4041083.html Sent from the Free Pascal - Lazarus mailing list . -- _______________________________________________

When there is NO license information in your repositories , this means that

"NO one can use them ."

with respect to copy right laws or conventions .

Thank you very much .

Mehmet Erol Sanliturk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150311/ab39cfc1/attachment.html>

Reply by email

Lukasz Sokol
to Mehmet Erol Sanliturk
Mehmet Erol Sanliturk wrote

[...]

Show quoted text 8 lines
When there is NO license information in your repositories , this means that "NO one can use them ." with respect to copy right laws or conventions .

Pointer/Keyword/URL please? I presume for when it matters, one would ask the author first...?

Show quoted text 5 lines
Thank you very much . Mehmet Erol Sanliturk

el es

Reply by email

leledumbo
to Mehmet Erol Sanliturk: On Wed, Mar 11, 2015 at 2:38 AM, leledumbo When there is NO license information in your…
Show quoted text 5 lines
When there is NO license information in your repositories , this means that "NO one can use them ." with respect to copy right laws or conventions .

No idea with what's default by law or convention, but when I give no license, consider it public domain.

Reply by email

Virgo Pärna
to leledumbo
leledumbo wrote
No idea with what's default by law or convention, but when I give no license, consider it public domain.

By default author has copyright and sole right to redistribute. And that's what applies, when nothing else is declared.

Virgo P?rna

Reply by email

Mark Morgan Lloyd
to leledumbo: No idea with what's default by law or convention, but when I give no license, consider it…
leledumbo wrote 8 lines
> When there is NO license information in your repositories , this means that > > "NO one can use them ." > > with respect to copy right laws or conventions . No idea with what's default by law or convention, but when I give no license, consider it public domain.

I'd imagine that application of the law would vary depending on jurisdiction, and it might be unintentionally affected by the stated policy of wherever the files are posted.

However the /correct/ thing to do would be to look at the import list and to adopt whatever license is implied by the libraries and documentation sources that are referenced. If a body of code imports (only) libraries which conform to any of the recognised free (as in speech) licenses, then an unqualified "no one can use them" is not a reasonable conclusion.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Mehmet Erol Sanliturk
to Virgo Pärna: By default author has copyright and sole right to redistribute. And that's what applies,…
Virgo P?rna wrote 18 lines
On Wed, 11 Mar 2015 08:18:35 -0700 (MST), leledumbo < > wrote: > > No idea with what's default by law or convention, but when I give no > license, consider it public domain. > By default author has copyright and sole right to redistribute. And that's what applies, when nothing else is declared. -- Virgo P?rna -- _______________________________________________

Yes , the correct understanding is the above sentence . Without explicit declaration , nothing can be assumed "implicitly" that is permitted , i.e. , permission requires to be explicit .

Mehmet Erol Sanliturk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150311/74fa8d10/attachment.html>

Reply by email

Mehmet Erol Sanliturk
to Lukasz Sokol: [...] Pointer/Keyword/URL please? I presume for when it matters, one would ask the author…
Lukasz Sokol wrote 29 lines
On 11/03/15 11:57, Mehmet Erol Sanliturk wrote: > > [...] > > > When there is NO license information in your repositories , this means that > > "NO one can use them ." > > with respect to copy right laws or conventions . > Pointer/Keyword/URL please? I presume for when it matters, one would ask the author first...? > > Thank you very much . > > Mehmet Erol Sanliturk > el es -- _______________________________________________

http://en.wikipedia.org/wiki/Copyright
http://en.wikipedia.org/wiki/Copyright_infringement http://en.wikipedia.org/wiki/Public_domain

http://en.wikipedia.org/wiki/Software_license
http://en.wikipedia.org/wiki/Public_domain_software http://en.wikipedia.org/wiki/Software_copyright
http://en.wikipedia.org/wiki/License-free_software

"Personal" use is a different concept , because no one may know that a copy is used whether usage is permitted or not for a copyrighted work .

Problem arises when "Redistribution" or ( "Derivative" works are "Published" ) .

One is the following :

http://www.acm.org/publications/policies/softwarecrnotice

Some of these collected software published before a year in US , is in "Public Domain" in the US with respect to a law ( I do not remember the year exactly but it is around 1970 ) .
ACM has denied this "Public Domain" concept by countering it with "Copyright Law" :

More restrictive rules are effective even they ( restrictive and permissive ) are in the laws for the copyright holder .

In summary , with respect to copyright laws ( there are minor differences in countries as related to Bern Convention agreement exceptions adopted ) anything is not explicitly mentioned is assumed to be not permitted .

Mehmet Erol Sanliturk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150311/01bb1ea2/attachment-0001.html>

Reply by email

silvioprog
to leledumbo: lexical parsers' using Object Pascal? First, you need to differentiate them correctly.…
leledumbo wrote 31 lines
> However, I have a question: is there any article about 'how to write lexical parsers' using Object Pascal? First, you need to differentiate them correctly. Lexical scanner (or simply lexer) and parser, not lexical parser :) I've written some simple languages, generating GraphViz, LLVM or C code, using both self learning reference from the almighty Prof. Niklaus Wirth's articles, Jack Crenshaw's, the legendary dragon book and many others plus my compiler course in college. I really really suggest Prof. Niklaus Wirth's one (http://www.inf.ethz.ch/personal/wirth/, click the Compiler Construction link) as it's concise, simple, doesn't use stupid generator (i.e.: easy to implement by hand) while covering most important parts of the subject. While he didn't use or stress or even discourage the usage of abstract syntax tree, he did explain it that's good enough to understand. Here's what I've written in the past and is publicly available: - https://bitbucket.org/leledumbo/erd-maker (generates GraphViz code for ERD visualization) - https://bitbucket.org/leledumbo/linguc (generates LLVM code that can be compiled as a library for a mathematically provable language focusing in database driven application) I don't use the direct code generation approach anymore as I found the abstract syntax tree to be usable for more than just code generation.

Buddy, thanks alot for that information. I'll take a look at this! =)

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150313/c29140a7/attachment.html>

Reply by email