Thread: Parsing code.

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32

    Parsing code.

    I started working on a small programming language as a spare time project and I have created most of the functions (for input, output, variables, etc) except for the first step (ie, parsing the language and splitting it into tokens which my functions can use)

    I've been having a few problems parsing the code. I honestly am not sure how to create a function to parse different strings.

    For example:
    Code:
    int x = 3 + 3;
    //parsed as: TYPE = "int", VAR = "x", VALUE = "3 + 3"
    double y;
    //parsed as: TYPE = "double", VAR = "y", VALUE = NULL
    splitting the code doesn't work as well as I want as it will split the string differently depending on how the code was typed.

    For example:
    Code:
    int x=3+3;
    //when split with a space, you get "int" and "x=3+3;"
    int x = 3 + 3 ;
    //when split with a space, you get "int", "x", "=", "3", "+", "3", ";"
    Long story short, am I just doing something wrong, or is there a different way to parse code which I haven't learned?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >int x=3+3;
    >//when split with a space, you get "int" and "x=3+3;"

    Maybe you should split the assignment symbol first, then you get "int x" and "3+3;" no matter really how it's typed.

    >int x = 3 + 3 ;
    >//when split with a space, you get "int", "x", "=", "3", "+", "3", ";"

    Then you can re-enter the "int x" and "3+3;" tokens to get that. Or whatever you really want.

    There's also this Parsing a String into Tokens Using strcspn 1 - C which will help you keep important parts of the language.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    This is actually a huge topic. You are going to want to google "lexical analysis". To get you started you may find some use with:
    the LEX and YACC page.
    Programming Language Design
    Algorithmic Languages and Compilers

    The last two are courses however they contain many useful links to free online books and software.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well who wants to write a compiler anyway?!

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by whiteflags View Post
    Well who wants to write a compiler anyway?!
    Haha....well evidently the OP does. That is until he realizes you have to be a masochist to actually want to make one.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32
    To be fair, its an interpreter, not a compiler. Compilers require even more code and even more time.

    Thanks for all the links though, I'll check them all out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CSV parsing - Can you look at my code?
    By spadez in forum C Programming
    Replies: 2
    Last Post: 02-05-2009, 06:57 AM
  2. parsing C code
    By lordmule in forum Linux Programming
    Replies: 1
    Last Post: 07-07-2008, 02:25 AM
  3. Code Parsing in C/C++
    By sgies in forum C Programming
    Replies: 8
    Last Post: 02-18-2008, 01:23 PM
  4. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  5. code for parsing emails
    By ranj in forum C++ Programming
    Replies: 1
    Last Post: 08-08-2003, 08:10 AM