-
October 6th, 2012, 09:01 AM
#1
Writing a Lexical Analyzer/Parser
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:
The lexer will split that into token like this:
Code:
def -> keyword
myVar -> Identifier
= -> Operator
10 -> Number
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.
So what is exactly the next step i have to take?
-
October 6th, 2012, 11:03 AM
#2
Re: Writing a Lexical Analyzer/Parser
Originally Posted by vivendi
So what is exactly the next step i have to take?
The next step is too much to explain on a programming board. Better you get a book on compiler theory (The Dragon Book is one), as that describes in detail the lexical analysis, parser, syntactic analysis, and then finally code generation (the JavaScript generation). You need all of these to accomplish what you're doing.
More than likely, you would have built some sort of syntax tree after the lexical analysis, parsing, and then syntax analysis have been done. Then you have to write something ( a translator) that can read the syntax tree and convert what the syntax tree describes into JavaScript.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 6th, 2012 at 11:13 AM.
-
October 8th, 2012, 11:02 AM
#3
Re: Writing a Lexical Analyzer/Parser
Be prepared to do a LOT of research and programming. compiler technology is quite a broad subject and it's a highly technical bit of knowledge. It is not something you'd want to start with unless you are very familiar with programming as is.
As for the translator... This seems somewhat of a strange thing to do. A lot of effort to automate a process that is hard to automate to begin with. Especially if your target is javascript which is different from a technological approach to a language like C/C++.
If you want to learn about lexers and parsers in general then there are easier tasks than a programming language translator. Especially considering even "small" computer languages tend to have considerable grammars.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|