Difference between revisions of "Make your own compiler, interpreter, parser, or expression analyzer"

From Free Pascal wiki
m (link format unification)
m (added links)
Line 69: Line 69:
* [https://www.inf.ethz.ch/personal/wirth/CompilerConstruction/CompilerConstruction1.pdf Niklaus Wirth - Compiler construction]
* [https://www.inf.ethz.ch/personal/wirth/CompilerConstruction/CompilerConstruction1.pdf Niklaus Wirth - Compiler construction]
* [https://compilers.iecc.com/crenshaw/ Let's build a compiler]
* [https://compilers.iecc.com/crenshaw/ Let's build a compiler]
* [http://www.cs.man.ac.uk/~pjj/farrell/compmain.html Compiler Basics]
* [http://memphis.compilertools.net/interpreter.html Writing an interpreter]
* [http://memphis.compilertools.net/interpreter.html Writing an interpreter]
* [http://www.cs.man.ac.uk/~pjj/bnf/bnf.html Notations for context-free grammars]
[[Category:Compiler design]]
[[Category:Compiler design]]

Revision as of 15:03, 19 September 2018

FCL Passrc

FPC comes with a pascal parser in library form in the fcl-passrc package. This is not the main compiler parser, but it is the one used for fpdoc and pas2js.

Other FPC parser packages

fcl-xml is a FPC package that contains SAX XML and html parsers.

FPC also contains two expression parsers symbolic and TFPExpressionParser.

Lex and Yacc

Two of the oldest unix tools. Lex is a lexical analyser (token parser), and Yacc is a LALR parser generator. BNF notation is used as a formal way to express context free grammars. Code and grammar are mixed, so grammar is tied to implementation language.

Plex and Pyacc

Plex and Pyacc are pascal implementations of Lex and Yacc and they are part of your FreePascal distribution.

Lazarus Lex and Yacc

You can find unfortunately abandoned Lazarus Lex and Yacc here.


Gold is a free parsing system that you can use to develop your own programming languages, scripting languages and interpreters. It uses LALR parsing, and a mix of BNF notation, character sets and regular expressions for terminals to define language grammars. Code and grammar are separated, so grammar is not tied to implementation language. This means that the same grammar can be loaded into engines made in different programming languages.

Gold Parser Builder can be used to create, modify and test languages in Windows IDE which can also run on Wine. Command line tools are also available.

Gold Parser Builder can also be used as a parser code generator using internal templates (FreePascal included), but there are also 3rd party engines to process compiled grammars.

Gold Parser Builder has grammar editor with syntax highlighting, grammar generating wizard, test window to step through parsing of a sample source, templating system that can generate lexers/parsers or skeleton programs for various languages (including Delphi and FreePascal), import/export YACC/Bison, XML and HTML export, and interactive inspection of the compiled DFA and LALR tables.

There is a subjective feature comparison table of several parsers on Gold site, with special attention to Gold vs Yacc comparison.


SynFacilSyn is Lazarus cross-platform library that includes a SynEdit highlighter that also can work as a lexer because of its flexible syntax definition file. It's well documented and has been used in several projects like highlighter and lexer.

SynFacilSyn includes an adapter to be used in ATSynEdit.

The main advantage of using SynFacilSyn as lexer is that you can really see the tokens colored in SynEdit editor, without doing anything else. So if you define a lexer, you are defining a source highlighter too.

To define the syntax you can use an XML file, where the tokens are defined using tags and some RegEx constructions. There you can specify the token colors too, if you need it.

Syntax definition can be done programmatically too, so it's possible to change some syntax elements in runtime.

The highlighter of SynFacilSyn is light, fast and includes some useful methods to access the text content after the scan. Moreover includes some features to define recursive blocks ans sections so it can be used as a parser too.

SynFacilSyn is the base library for other tools that in group can be used to create powerful compilers/interpreters/IDE:

  • SynFacilCompletion - Scriptable Highlighter with code-completion for the SynEdit Component of Lazarus
  • SynFacilUtils Library with utilities to create editors using SynFacilSyn and SynFacilCompletion.
  • t-Xpres Framework to create compilers/interpreters based on SynFacilSyn. Includes lexer, parser, syntaxTree, expression evaluator and support to implement code generators or virtual machines.

The next programs have been created using this tools:





Useful BNF and EBNF tools

See also