Hi, i'm trying to learn more about how Lexical Analyzers/Parsers work. I haven't coded any classes yet, because i'm not really sure how the entire process from a Lexer to working code goes.
My goal is to write a simple made up programming language and translate that to another language, like Javascript. The first thing i have to do is give the code to a Lexical Analyzer.
The lexer will split the source into tokens and assign a label to it. So suppose i have the following code:
That's basically as far as i understand what a lexer does. How can i translate the tokens to a language like Javascript?? From what i understand i need to write a Parser class. But i couldn't find any info on what that class exactly does.
LR parsers are too dificult to write by hand so they are generated by tools which transform a grammar into C/C++/Java... code: http://en.wikipedia.org/wiki/LR_parser
A parser is responsible for applying the semantics (meaning) of your language.
A simple command line parser generally parses one token at a time.
A more complex language needs to build parse trees and generally uses a state machine to reduce sequences of tokens into intermediate productions, and then reduce the intermediate productions into a single (final) production; i.e. the complete program.
Expressing your language in BNF is usefull exercise in understanding how to parse the language.
If you language is non-trivial, you might take a look at Bison, which is an open source parser generator. Given the BNF for a language, it wiill generate a parser for that language. You still have to write the code that "applies" each production using the tokens on the right side and produces the left hand side of each BNF statement.
Thanks, i see i still have to go through alot of resources before i can fully understand how the entire process goes. Eventhough my parser won't be that complex in the end. But its still a good idea to know what possibilities are out there.
I also started reading the Dragon Book. It's an old book, but still recommended by alot of others.