Shop OBEX P1 Docs P2 Docs Learn Events
Lexical Analyzer for Pascal for the C3 — Parallax Forums

Lexical Analyzer for Pascal for the C3

Mike GreenMike Green Posts: 23,101
edited 2011-06-23 14:56 in Propeller 1
I thought I would post this maybe as a source of code fragments for those that are interested. I put together a lexical analyzer for an extended Pascal compiler. It accepts input from a PS/2 keyboard and displays information on a TV display (using Cluso99's 1-pin drivers), keeps its working tables in the C3's SRAM, and writes its output (lexical tokens and symbol table) to C3 flash files. There's a C3 version of Kye's FATEngine, but it's not really used at this point. Eventually it will be used for the input file and any output files.

In this program, there are several symbol table lookup routines, one that uses a pre-defined table and one that uses a symbol table in SRAM. There's a numeric input routine that accepts hex constants ($00000000), binary constants (%00000000), and decimal constants including floating point. The analyzer accepts Pascal comments and C type line comments (using //). The analyzer uses a two character backup scheme so it can move ahead two characters in the input stream, but back up through one or both of them if it doesn't know what to do with what it finds.

I may polish this up in a couple of weeks and probably work on a parser later this summer. I have no idea whether I'll take this further than that, but here it is.

Comments

  • jmgjmg Posts: 15,185
    edited 2011-06-23 14:08
    Mike Green wrote: »
    I may polish this up in a couple of weeks and probably work on a parser later this summer. I have no idea whether I'll take this further than that, but here it is.

    Interesting. For those who want self-hosting, perhaps 'Turbo Pascal' on the Prop may yet arrive ?
    Esitmates on how large it would be after adding an Editor and Compiler/debug ?

    Any such work should 'common subset' with a widespread PC language, so that PC tools can be use too, for algorithm framework and testing,
    and I'd do more than one language - what you have sounds like it would split easily.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-06-23 14:56
    We're talking here about multiple phases. There would be a lexical analyzer that produces a symbol table and stream of lexemes, a parser that produces a tree and maybe does some constant folding (actually evaluates constant sub-expressions including some floating point). There would be a declaration phase that just processes declarations, assigns variable locations (including offsets to substructures), and may do string constant folding (eliminating duplicates). Last would be a code generation phase. In the interest of compact code, I may generate Spin bytecodes or something very close.

    We're not talking here about an IDE. Any text editor would be separate. I'd probably just start with the one that's part of Sphinx.

    The language we're talking about here is an extended Pascal with some string manipulation and ways to explicitly escape the strong typing of Pascal. The concepts and extensions I plan are based on Palm Pascal, a self-hosted 68xxx and ARM compiler for the PalmOS, now redone for Android.
Sign In or Register to comment.