Skip to content

Latest commit

 

History

History
55 lines (43 loc) · 1.3 KB

File metadata and controls

55 lines (43 loc) · 1.3 KB

FeGen: a Python-embedded DSL and a framework for rapid prototyping DSL Compiler

FeGen is designed to bridge syntax definition and MLIR code generation. It enables developers to define both grammatical rules and semantic bindings within FeGen DSL, streamlining the process of prototype compiler construction.

This repository shows the implementation of FeGen.

How to use?

  1. Define Your Grammar Class
from FeGen import *

class MyGrammar(FeGenGrammar):
    def __init__(self):
        super().__init__()
  1. Add Lexer and Parser Methods to Your Class
    @lexer
    def Number(self):
        """
        Number ::= [1-9][0-9]*|[0-9]
        """
        return newTerminalRule(regular_expr("[1-9][0-9]*|[0-9]"))
      
    ...
    
    @parser
    def module(self):
        """
        module: structDefine* funcDefine+
        """
        r_one_or_more = one_or_more(self.function_definition(), self.NEWLINE()) 
        r = newParserRule(r_one_or_more)
        funcs = r_one_or_more.get_attr("funcop", flatten=True)
        self.themodule = builtin.ModuleOp(funcs) # xDSL
        return r
    ...
  1. Create Instance and Get Start
...
g = MyGrammar()
mylexer = g.lexer()
myparser = g.parser(mylexer, start="module")
cstroot = myparser.parse(code)
cstroot.visit()
themodule = g.themodule
...