Pascal to C converter

15,117

Solution 1

Why do you want to do such a Pascal to C converter?

If you just want to run some Pascal programs, it is simpler to use (or improve) existing compilers like gpc, or Pascal to C translators, like e.g. p2c

If you want to convert hand-written Pascal code to humanly-readable (and improvable) C code, the task is much more difficult; in particular, you probably want to convert the indentation, the comments, keep the same names as much as possible -but avoiding clashes with system names- etc!

You always want to parse some abstract syntax tree, but the precise nature of these trees is different. Perhaps flex + bison or even ANTLR may or not be adequate (you can always write a hand-written parser). Also, error recovery may or not be important to you (aborting on the first syntax error is very easy; trying to make sense of an ill-written syntactically-incorrect Pascal source is quite hard).

If you want to build a toy Pascal compiler, consider using LLVM (or perhaps even GCC middle-end and back-ends)

Solution 2

You might want to take a look at "Translating Between Programming Languages Using A Canonical Representation And Attribute Grammar Inversion" and references therein.

Solution 3

The most common approach would be to build a parse tree in your front end, and then walk through that tree outputting the equivalent C in the back end. This gives you the flexibility to perform any reordering of declarations that's required (IIRC Pascal supports use before declaration, but C doesn't). If you're using flex for the scanner, tradition would dictate using bison for the parser, although there are alternatives. If you look, you can probably find a freely available Pascal syntax in the format expected by bison.

Solution 4

You have to know the Pascal grammar, the C grammar and built (design) a "something" (i.e. a grammar or an automata...) that can translate every Pascal rule in the corresponding C rule.

Than, once you have your tokenized stream, using some method like LR, you can find the semantic tree which correspond to the sequence of Pascal rule applied and convert every rule in the corresponding C rule (this can be easly done with Bison).

Pay attention that Pascal and C have not Context Free grammars, so more control will be necessary.

Share:
15,117
Ziem
Author by

Ziem

Android developer.

Updated on June 29, 2022

Comments

  • Ziem
    Ziem almost 2 years

    I'm writing program which translate Pascal to C and need some help. I started with scanner generator Flex. I defined some rules and created scanner which is working more or less ok. It breaks Pascal syntax into tokens, for now it's only printing what it found. But I have no idea what should I do next. Are there any articles or books covering this subject? What is the next step?

  • Ziem
    Ziem almost 12 years
    Because it's a university project.
  • Basile Starynkevitch
    Basile Starynkevitch almost 12 years
    Then, ask the teacher what is the exact puprose of it. Probably, to teach you what abstract syntax trees are really in practice. You might want to use ANTLR (if permitted to use such tools), or stick to bison + flex. Take care of source locations when parsing (you'll need to store them in your AST). And looking into the source code of p2c will teach you a lot!
  • Ziem
    Ziem almost 12 years
    We are learning about grammars, compilers, how they work, etc. What I learned, is that this problem is similar to compiler behaviour. So I think that is the purpose of creating this translator. Can you tell me something more about p2c? I know about pas2c but never heard about p2c.
  • Ziem
    Ziem almost 12 years
    Do you have some good materials showing how to do this with Bison?
  • Ziem
    Ziem almost 12 years
    Probably I will choose Bison.
  • Ziem
    Ziem almost 12 years
    If you have any other materials covering this problem, let me know.
  • Marco van de Voort
    Marco van de Voort almost 12 years
    Classically, Pascal parsers are built using recursive descent, not yacc/lex. Pascal is close to LL(1), so this is not that hard.
  • Marco van de Voort
    Marco van de Voort almost 12 years
    Pascal is strictly declaration before use, forward procedure have to be declared with "forward;" directive, and only pointer typedefinitions can reference undeclared symbols, which must resolve within the same typeblock though.
  • ykatchou
    ykatchou over 11 years
    p2c seems un-maintained since 1993... do you have other tools with the same purpose ?