rbbarbosa / Petit

Petit programming language and compiler
BSD 3-Clause "New" or "Revised" License
8 stars 1 forks source link

Petit

Petit programming language and compiler

Example

Petit is an educational programming language for learning compilers. Here's an example:

factorial(integer n) =
    if n then n * factorial(n-1) else 1

Tutorials and exercises

  1. Tutorial I: Lexical analysis
  2. Tutorial II: Advanced lex features
  3. Tutorial III: Syntactic analysis
  4. Tutorial IV: Abstract syntax
  5. Tutorial V: Semantic analysis
  6. Tutorial VI: Code generation
  7. Solutions for most exercises

Grammar

program: IDENTIFIER '(' parameters ')' '=' expression
       | program IDENTIFIER '(' parameters ')' '=' expression

parameters: parameter
          | parameters ',' parameter

parameter: INTEGER IDENTIFIER
         | DOUBLE IDENTIFIER

arguments: expression
         | arguments ',' expression

expression: IDENTIFIER
          | NATURAL
          | DECIMAL
          | IDENTIFIER '(' arguments ')'
          | IF expression THEN expression ELSE expression
          | expression '+' expression
          | expression '-' expression
          | expression '*' expression
          | expression '/' expression
          | '(' expression ')'