1

I develop a preprocessor transpiling subset of C statements into gcc _asm statements. For this project, I will be happy to reuse existing C statements parser written with any popular Haskell technology, or just start with some simple C subset paser in order to avoid redoing existing work.

Unfortunately, so far I found grammars just for everything but C. And while I can start with Parsec grammars for Java/Go, it seems that MegaParsec is preferable choice?

I can quickly develop grammar for small C subset, but ready-to-use grammar for larger C subset will allow me to completely skip the development of C statement parser and focus on the meat of project - asm code generation.

Eventually, it may turn into LLVM pass transpiling parts of C++ code, but for quick prototype, I prefer Haskell, especially if I can find ready-to-use parser.

Bulat
  • 2,435
  • 1
  • 15
  • 15
  • 3
    Here is ANSI C full grammar for yacc which is, as far as I know, compatible with Happy: https://www.lysator.liu.se/c/ANSI-C-grammar-y.html. I guess that is all that You need – bartop Jun 20 '18 at 13:26
  • Thank you, but it still require Haskell lexer plus AST-building code, f.e. I need to add to rule "Exp1 : Exp1 '+' Term" the following Haskell code building AST - "{ Plus $1 $3 }". May be, there are more ready-to-use-in-Haskell alternatives. – Bulat Jun 20 '18 at 13:55
  • 3
    Does https://hackage.haskell.org/package/language-c help? – arrowd Jun 20 '18 at 15:08
  • @arrowd thank you. It's a perfect fit!!! Hope it will be able to parse just a few statements rather than entire program. – Bulat Jun 20 '18 at 15:30
  • But I still look for more variants, just for completeness. And BTW, language-c based on Happy grammar, so I may just end up ripping it. – Bulat Jun 20 '18 at 15:37
  • I'm stupid! Just adding "{}" around statements will turn them into single statement for which we have a parser. So it can't be easier! – Bulat Jun 20 '18 at 15:45

1 Answers1

2

There is Haskell library parsing C99 grammar (and C11 partially):

https://hackage.haskell.org/package/language-c

Bulat
  • 2,435
  • 1
  • 15
  • 15