r/Compilers • • Jul 07 '26

Building a Parser Generator!

Hi, I am creating a Parser Generator. It will have it's own unique syntax for grammar definition. This is what I am working on currently. I am sharing a draft of the syntax and asking if anyone is interested in sharing their feedbacks in the comments.

Is this syntax:

  • easy to read?
  • easy to understand?
  • sparks interest in you?
  • what stands out?
  • do you suggests any changes or additions?

UPDATED

I am settling on a syntax for the language. Here is a snippet:

# BASIC LANG
# ---------
# A 100
# B  25

parser {
    # action      ->  ast: (parent(children))
    (name number) -> (name(number)); # generate name with number as child of name
    (spc | nl) -> (); # no AST generated
}

name = <A:Z>+; # character sequence
number = <0> | <1:9> <0:9>+; # character sequence

# spc is built-in rule for space
# nl is built-in rule for nl

Full syntax is being worked on here: https://github.com/Algodal/Algodal_Text_Parser_Generator_Manual

I will be streaming code implementation of the parser generator on youtube: https://www.youtube.com/@RevnantRicko

8 Upvotes

21 comments sorted by

View all comments

1

u/Commercial-Drawer881 Aug 08 '26 edited Aug 09 '26

After doing some research and experimenting, I have settled on mostly on the following syntax design:

# BASIC LANG
# ---------
# A 100
# B  25

parser {
    # action      ->  ast: (node(children)...)
    (name number) -> (name(number)); # generate name with number as child of name
    (spc | nl) -> (); # no AST generated
}

name = <A:Z>+; # character sequence
number = <0> | <1:9> <0:9>+; # character sequence

# spc is built-in rule for space
# nl is built-in rule for newline
# ADVANCE LANG
# X = 23 + 5 * (6 - 4)
# X

# Character Sequence is defined with `=`
name = <A:Za:z> (<A:Za:z_0:9>)*;
number = <0> | <1:9> <0:9>+;

# Syntactic Analysis is defined with `:=`
expr := 
    (expr . "+" . expr) -> ("+"(expr expr)) |
    (expr . "-" . expr) -> ("-"(expr expr)) |
    (expr . "*" . expr) -> ("*"(expr expr)) |
    (expr . "/" . expr) -> ("/"(expr expr)) |
    ("(" . expr . ")") -> (expr)  | # anything not included in the AST specification is discarded
    number # default AST
;

## You can choose not to specify the AST and a default AST will be generated
## Example of what that will look like is specified below in comments
# expr := 
#    expr . "+" . expr |
#    expr . "-" . expr |
#    expr . "*" . expr |
#    expr . "/" . expr |
#    "(" . expr . ")"  |
#    number
#;

# Since it is a mix parser (lexing and syntactical analysis occurs at the same time) there is no
# luxury of discarding space tokens. Instead you can use `.` to represent generalized skip and define
# what is skipped.

. { # required if `.` is used in any grammar
    spc, nl # everywhere `.` is used in grammar all the actions defined in this list is called until
    # it can not be called anymore.
};

parser {
    (name . "=" . expr eol) -> ("="(name expr)); 
    name eol;
}

# eol is a built-in action for END-OF-LINE

# OPTIONALLY!!! Parsing Precidence can be set for actions
# if these are matched in the parsing, the following binding powers are applied.

# Binding Power
# Right-associative (LBP > RBP)
# Left-associative  (LBP < RBP)

bindpow { # this is completely optional (though important if you are parsing expressions)
    "-"  : (0, 12), # duplicates allowed if lbp is 0
    "!"  : (0, 12),
    "++" : (14, 0),
    "--" : (14, 0),
    "^"  : (11, 10),
    "*"  : (8, 9),
    "/"  : (8, 9),
    "+"  : (6, 7),
    "-"  : (6, 7),
    "<"  : (4, 5),
    "<=" : (4, 5),
    ">"  : (4, 5),
    ">=" : (4, 5),
    "==" : (2, 3),
    "!=" : (2, 3),
}
# CHALLENGE LANG
# X:
#   Y
#   Z
# A     X

# In special cases the parser generator via standard library or user may provide
# functions that allows for complex parsing that the syntaxical grammar can not 
# support.

# The parser generator standard library provide functions for python indentation.
# If the user wants haskel indentation, they can write their own functions.
# Functions are expected to be written externally in C (or any language binding 
# to C ABI and as long as the user can run the parser in that language's ecosystem).

name = <A:Za:z> (<A:Za:z_0:9>)*;
structure1 := name . ":" . indent (name newline)* dedent;
structure2 := name special_tab name;

# placeholder definition of actions (REQUIRED!)
indent = _; # indicates that the action grammar is defined externally through user customized code.
dedent = _;
special_tab = _;
newline = _;

# Reference linking of the custom action with user code. Actual linking is done outside DSL.
# User code is called on text where ever action is used to generate syntactic objects.
custom_action { 
    indent: "apm_py_indent",
    dedent: "apm_py_dedent",
    newline: "apm_py_newline",
    special_tab: "myfunc"
}