Recent

Author Topic: Formal grammar for FPC 3.0.0  (Read 7727 times)

simone

  • Hero Member
  • *****
  • Posts: 573
Formal grammar for FPC 3.0.0
« on: August 28, 2016, 05:10:53 pm »
I'm searching for a formal grammar (i.e. BNF or EBNF) for the object-pascal language version supported by FPC 3.0.0. If this does not already exist, I might try to update (and then share) old grammars for Delphi 7 available on the web (in particular that of the GOLD parser). In this case I would to verify correctness and completeness of my work. There exists a set of source codes used as test case for verification purposes in development of FPC? Thanks in advance.
« Last Edit: August 28, 2016, 05:29:31 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Formal grammar for FPC 3.0.0
« Reply #1 on: August 28, 2016, 05:58:03 pm »
There is none, but you can use the syntax diagram in official documentation as a starting point, building pieces by pieces. FPC test cases are in the tests folder of the source archive. Do note that it doesn't yet pass 100%. On my x86_64-linux systems, it results in:
Code: [Select]
Total = 7131 (19:7112)
Total number of compilations = 4372 (11:4361)
Successfully compiled = 3297
Successfully failed = 1064
Compilation failures = 6
Compilation that did not fail while they should = 5
Total number of runs = 2759 (8:2751)
Successful runs = 2751
Failed runs = 8
Number units compiled = 139
Number program that should not be run = 396
Number of skipped tests = 428
Number of skipped graph tests = 10
Number of skipped interactive tests = 31
Number of skipped known bug tests = 6
Number of skipped tests for other versions = 4
Number of skipped tests for other cpus = 203
Number of skipped tests for other targets = 174
Roughly 99.73% passed.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Formal grammar for FPC 3.0.0
« Reply #2 on: August 28, 2016, 06:10:18 pm »
For correctness and completeness verification the compiler itself has already some very powerful features.
These are tied-in to the high level language layer that abstracts away from cpu and os.
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Formal grammar for FPC 3.0.0
« Reply #3 on: August 28, 2016, 08:55:29 pm »
I'm searching for a formal grammar (i.e. BNF or EBNF) for the object-pascal language version supported by FPC 3.0.0. If this does not already exist, I might try to update (and then share) old grammars for Delphi 7 available on the web (in particular that of the GOLD parser). In this case I would to verify correctness and completeness of my work. There exists a set of source codes used as test case for verification purposes in development of FPC? Thanks in advance.

Afaik there is none, and the Delphi 7 grammars from the delphi 7 documentation are meant for documentation purposes, and not 100% correct (for D7) either.

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: Formal grammar for FPC 3.0.0
« Reply #4 on: August 29, 2016, 12:59:29 am »
I have done some, but in is not as easy as you think, because of FPC's various compiler modes. So I limited my grammar to only the {$mode objfpc} compiler mode, because that is the only mode I use in development. It was originally based on the Delphi 7 grammar available on the internet, but I have made many changes, syntax fixes and applied some FPC 2.6.4 syntax. I haven't updated it to FPC 3.0.0 yet, and haven't touched generics syntax.

You can find the latest grammar here... See the last part of the file:

  https://github.com/graemeg/fpGUI/blob/master/docs/fpc_lang_ref.ipf
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Formal grammar for FPC 3.0.0
« Reply #5 on: August 29, 2016, 09:24:36 am »
We produced a usable but still not complete Delphi 2006 grammar once at the bank internally based on a Delphi 7 grammar (not the one from the documentation).
I will see if I am allowed to have it back - if it still exists - and publish that.
Its main drawback is that it doesn't parse some legal but exotic constructs like ^M escaping and has trouble with advanced records.

Btw there is also codewrench, parses more language features but I didn't check the quality. Can't feed it with grammars though. It is based on the old mwPasParser, just like Castalia.
https://sourceforge.net/projects/codewrench/

Castalia is 99%+ correct in code recognition up to D2009, but again, you can't feed it a grammar.
I use the castalia stuff a lot, specifically since it is the only opensource one that can parse KOL and is very fast.

I think Graeme suggests a good approach, though:
Because of the complexity it is better to focus on separate grammars for the different modes.

[edit]
Another grammar that looks promising:
http://www.davidghoyle.co.uk/WordPress/?page_id=1389
« Last Edit: August 29, 2016, 09:41:37 am by Thaddy »
Specialize a type, not a var.

simone

  • Hero Member
  • *****
  • Posts: 573
Re: Formal grammar for FPC 3.0.0
« Reply #6 on: August 31, 2016, 09:04:44 am »
Thanks to all. Just a suggestion... To novices interested in compiler construction, I recommends to read the old but good tutorial available here: http://compilers.iecc.com/crenshaw/. The author shows, using a smooth step-by-step approach and avoiding teoretical complication as far as possible, how to build a simple but realistic compiler with Turbo Pascal, able to produce object code in 680x0 assembler. A revisited version of the above tutorial is available here: http://www.pp4s.co.uk/main/tu-trans-comp-jc-intro.html. The update mainly concerns the object code produced, that in this case is in 80x86 assembler, and some minor aesthetic improvements. Since literature about this specialistic but important topic is heavily C-language oriented, in my opinion this tutorial is a very nice and exception.
« Last Edit: August 31, 2016, 09:06:40 am by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Formal grammar for FPC 3.0.0
« Reply #7 on: August 31, 2016, 12:08:17 pm »
Thanks to all. Just a suggestion... To novices interested in compiler construction, I recommends to read the old but good tutorial available here: http://compilers.iecc.com/crenshaw/.


Yes, I'm aware of that tutorial
 

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Formal grammar for FPC 3.0.0
« Reply #8 on: August 31, 2016, 12:31:58 pm »
Nice to see a Raspberry Pi version
Specialize a type, not a var.

 

TinyPortal © 2005-2018