I think i finally understood parsing
-
Hi its now a while ago since i posted something here but i think i understood parsing.
So you just do this with a state machine.var = "lqqqqqqqqqqqq=2;"
command = " prit;"
ri = 0
i = 0
state2 = 0
state = 0
var_name = ""
var_val = ""
sec = True
while i< len(var):
if var[i].isalpha() and state == 0:state = 0 var_name = var_name + var[i] i = i + 1 #print (var_name) if var[i] == "=" and state == 0: i = i + 1 state = 2 if state == 2: var_val = var_val + var[i] i = i +1 if var[i] == ";": #print(var_name , var_val) break
Here is python code as an example but i assume this works in fuze as well with some changes
Now i just need to understand how abstract syntaxtrees work
-
Interesting. But there are many different parsing theories. From Recursive Descent Parsers to using DFAs (Deterministic Finite Automata). Your idea of using states can work for a parser but again it depends on the stage of the compiler or interpreter you're working on. For example within the lexing stage you'll mostly be dealing with lexemes and single characters such as:
";", "=", "+",etc.
As you already know about the lexer stage this will create tokens used by the parser.Concerning a state machine for your parser or the proper term DFA. I'd recommend using a state transition table. Here's the link for that for more information: https://en.wikipedia.org/wiki/State-transition_table
I've already used this for lexical analysis which checks for patterns in my HACK language which enables me to get error detection in case any line of code deviates from the patterns it knows. This also includes checking for variable names to make sure they do not start with a number. Ex123foo
.Abstract Syntax Trees require knowledge of using references or pointers. I have a function in Fuze that creates a Tree structure in the FUNCTION section of the forum if you'd like to experiment with those concepts in Fuze: https://fuzearena.com/forum/topic/1544/functions-for-tree-data-structures
-
@LucasJG25 thank you. So i would say i now found a way
-
@petermeisenstein Indeed, you're getting there. Keep up the good work!
-
@LucasJG25 Btw the Wikipedia article looks very complex and not sure if my level of english is enough for this. but thank you very much again for the ivestment of your time to lookup this article
-
@petermeisenstein It's cool. I'm not sure if you can change the language on that page or if you can translate it with google translate. But Happy to share my knowledge with you. Hope you keep posting your progress on the forums. It's been a bit lonely since I haven't been seeing anyone else posting ideas or concepts on parsing or compiler design. :D
-
@LucasJG25 One issue is that fuze or the existence of fuze or the reason why fuze exists is not to build such things like parsers or whatever. You can see this on the language it self because it is not offering things like regex for example. Fuze has a big advantage when it comes to games and graphical stuff
-
@petermeisenstein Though it's true that Fuze doesn't come with things like regex. These are language features that help make the process of compiler design much easier. Mostly in the lexer field. Regex is just a pattern matcher which is kind of what a DFA can do as well.
Fuze not having regex can make things a bit harder when dealing with projects which focus on compiler concepts. But that's the charm of Fuze. To make your own functions or add your own data structures.
I believe that great programmers stem from situation where they may be limited whether in hardware or software. We always come up with amazing solutions.
-
@LucasJG25 I get your point like to build things by your self. I usually like todo this as well without using much libraries and so on. But you need to think about it from a different way. Why should you do everything from nothing. Thats the point of most programming languages to use libraries. Imagine you would do everything from nothing on your pc as well. Writing code for communication with the hardware in x86 Assembly and so on. Sometimes its good to write things yourself but sometimes not.
-
@petermeisenstein Indeed. I too understand your point of view. But I'm just that kind of guy that loves the low level stuff. That's just my character :D
-
@LucasJG25 You know what would help me when you try to use the code snippet (rebuild it in fuze or python ) and then try to apply abstract syntax tree to it and show me the result. Best thing would be to code this as easy as possible. So when you have time and want to this is a challenge for you.
-
@petermeisenstein interesting! I have been thinking about getting ASTs (Abstract Syntax Trees) made in Fuze. Will take some time since I'm working on other projects currently. But once I have it made I'll post a link to the Share code here. :D
-
@LucasJG25 Okay please try to make it as simple as possible
-
@petermeisenstein Ok sounds good.