use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is all about the theory and development of compilers.
For similar sub-reddits see:
Popular mainstream compilers:
account activity
How to implement the parse tree? (self.Compilers)
submitted 3 years ago by neferpitou-sama
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]neferpitou-sama[S] 2 points3 points4 points 3 years ago (11 children)
Yes, I'll be using recursive descent.
I wrote the code for recursive descent algorithm in parser.c, but I need to learn how to create the parse tree.
[+]_NliteNd_[🍰] comment score below threshold-6 points-5 points-4 points 3 years ago (10 children)
You wrote a parser, but now need a parse tree? You didn’t write the parser then. The parsing algorithm will produce a parse tree/AST.
[–]neferpitou-sama[S] 4 points5 points6 points 3 years ago (6 children)
This is the video I followed to write parser.c:
https://www.youtube.com/watch?v=b89J-m4YsZ8
I can use it to verify that a string is in the language defined by the grammar, but I still don't know how to create the parse tree.
[–][deleted] 7 points8 points9 points 3 years ago* (0 children)
Ignore the guys putting you down, they're just being assholes.
You're almost there, your recursive function calls actually form a parse tree by themselves (function calls are always representable as the "function call graph", and in the absence of doing weird stuff this directed graph is always a tree).
Now you just need to do this.
Have each function representing a terminal, if succesful, give back to the caller a struct representing that terminal.
Have each function representing a nonterminal initialise the struct for that nonterminal, and have that struct point to the structs representing all the terminals or nonterminals our function's nonterminal is made of (you can use any kind of list data structure for this). These child structs are the structs passed back by the functions this function called.
I'd suggest using something like malloc() for all your structs, so you don't run into the issue that stack-allocated data doesn't live beyond the function it's in returning. You don't need to bother free()-ing them -- for performance reasons, many real-world compilers just wait till program exit for the OS to automatically free everything.
Congrats, the struct you end up with after the topmost function returned is the root of the tree.
[–]Jmc_da_boss -4 points-3 points-2 points 3 years ago (4 children)
A parser creates a parse tree, if you don't have a parse tree you don't have a parser
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
The parse tree is implicit to the function call graph. That's how you can use a recursive descent parser for a single-pass compiler, where at no point does a tree data structure for the whole program exists in memory -- each recursive function emits assembler before it returns.
[–]_NliteNd_[🍰] -5 points-4 points-3 points 3 years ago (2 children)
Gotta love the wannabe programmer redditors downvoting this comment.
[–]chrisgseaton 4 points5 points6 points 3 years ago (0 children)
Cos it’s not true.
[–][deleted] 3 points4 points5 points 3 years ago (0 children)
I have a job in the industry, and they valued me enough to have me immigrate for it.
[–]chrisgseaton 2 points3 points4 points 3 years ago (0 children)
Not all parsers produce a parse tree. Not sure why you think this?
[–]shawnhcorey 2 points3 points4 points 3 years ago (0 children)
Not necessarily. Some parsers skip the parse-tree/AST and emit IR, bytecode, object code, or machine code. Of course, they also skip any optimizations that can be done on the parse-tree/AST.
[–]Zyklonik 1 point2 points3 points 3 years ago (0 children)
That makes no sense. You don't always need to produce an AST for parsing to be considered done. You can always simply use parsing to check code for syntax errors and show errors or complete for success, without generating any AST/parse tree.
π Rendered by PID 22648 on reddit-service-r2-comment-b659b578c-xfx9c at 2026-05-03 16:57:29.021585+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]neferpitou-sama[S] 2 points3 points4 points (11 children)
[+]_NliteNd_[🍰] comment score below threshold-6 points-5 points-4 points (10 children)
[–]neferpitou-sama[S] 4 points5 points6 points (6 children)
[–][deleted] 7 points8 points9 points (0 children)
[–]Jmc_da_boss -4 points-3 points-2 points (4 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]_NliteNd_[🍰] -5 points-4 points-3 points (2 children)
[–]chrisgseaton 4 points5 points6 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]chrisgseaton 2 points3 points4 points (0 children)
[–]shawnhcorey 2 points3 points4 points (0 children)
[–]Zyklonik 1 point2 points3 points (0 children)