CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2005

    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:

    def myVar = 10;
    The lexer will split that into token like this:

    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?

  2. #2
    Join Date
    Apr 1999

    Re: Writing a Lexical Analyzer/Parser

    Quote Originally Posted by vivendi View Post
    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.


    Paul McKenzie
    Last edited by Paul McKenzie; October 6th, 2012 at 11:13 AM.

  3. #3
    Join Date
    Apr 2000
    Belgium (Europe)

    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.