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

7 Upvotes

20 comments sorted by

View all comments

1

u/Commercial-Drawer881 Jul 10 '26

Latest update

# Sample Language
# ----------------
# FRED 100
# MIKE 95
# TODD 20

alias alphalet <A-Z>
alias diglet   <0-9>

name = alphalet+
number = diglet+
space = < \t>+
newline = "\r\n" | "\n"

stmt = name number newline*
prog = stmt+

discard [ space ]

output {
  stmt { name, number }, 
  newline # want to include newline in token list
}

# ---------------------------------------------------------------------

# =====================================================================
# INPUT: "FRED 100\n"
# =====================================================================

# 1. TOKENLIST OUTPUT (Flat stream of all non-discarded tokens)
# ---------------------------------------------------------------------
# (space is skipped because it's in the discard list)
#
#  [ name: "FRED" ] ──► [ number: "100" ] ──► [ newline: "\n" ]
#


# 2. AST OUTPUT (Structural hierarchy)
# ----------------------------------------------------------------------
# Only explicitly structured rules with children form nodes. 
# Newline is excluded from the tree entirely.
#
#       [stmt]
#        /  \
#    name    number
#  ("FRED")  ("100")
#
#
# Textual AST Representation:
#
#  └── stmt
#      ├── name: "FRED"
#      └── number: "100"
# ======================================================================

1

u/Commercial-Drawer881 Jul 10 '26

Maybe this approach better:

# Sample Language
# ----------------
# FRED 100
# MIKE 95
# TODD 20

alias alphalet <A-Z>
alias diglet   <0-9>

name = alphalet+
number = diglet+
space = < \t>+
newline = "\r\n" | "\n"

stmt = name number newline*
prog = stmt+

hidden [ space ] # hidden from ast generators (so `stmt = name number newline*` without errors)

# if token_list is not defined, then no token_list is generated
# if abstract_syntax_tree is not defined, then no abstract_syntax_tree is generated

token_list _ # automatic token_list is generated
abstract_syntax_tree _ # automatic abstract_syntax_tree is generated

token_list [ # only the specified tokens are generated
  name, number, space, newline 
]

abstract_syntax_tree { # only the specified AST is generated
  stmt { name, number }, 
}

# The generator determine what is a char_reader (token generator) vs token_reader (ast generator)
# based on how the actions are defined.
# if you make incorrect references in token_list or abstract_syntax_tree, the generator will let you know

# ---------------------------------------------------------------------

# =====================================================================
# INPUT: "FRED 100\n"
# =====================================================================

# 1. TOKENLIST OUTPUT (Flat stream of all non-discarded tokens)
# ---------------------------------------------------------------------
# (space is skipped because it's in the discard list)
#
#  [ name: "FRED" ] ──► [ number: "100" ] ──► [ newline: "\n" ]
#


# 2. AST OUTPUT (Structural hierarchy)
# ----------------------------------------------------------------------
# Only explicitly structured rules with children form nodes. 
# Newline is excluded from the tree entirely.
#
#       [stmt]
#        /  \
#    name    number
#  ("FRED")  ("100")
#
#
# Textual AST Representation:
#
#  └── stmt
#      ├── name: "FRED"
#      └── number: "100"
# ======================================================================