Thread: need sth about parsing

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497

    need sth about parsing

    hello, im trying to make an emulator ! well the simplest form of the emulator has been coded so far , but it seems for the further development i need to know about parsing and you know that kind of stuff(parsing , scanning,tokens.. !)
    i just know some basic definitions about parsing, nothing more!
    so first i googled parsing! and found some related articles, but , those were for xml things! so again no luck !
    couldnt find a proper article concerning C++ programming!
    so i thought i could be a good idea to ask you for help.
    anything (comment, sample source code, article ,etc)is welcomed .

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What are you emulating/parsing?

    It makes a whole lot of difference.

    But essentially, a parser consists of two or three parts:
    a lexical analyzer/tokenizer that takes the input and produces tokens or lexemes.
    http://en.wikipedia.org/wiki/Lexical_analysis

    a syntactic/semantic analyzer that takes the tokens and "makes sense out of it" (which may be to generate an error, e.g. we found a token that doesn't belong here), e.g in C:
    Code:
    printf("Hello, World\n";)
    should give an error about either ; when it's not expected, or a ) expected before the ; .

    The output of the syntactic/semantic analyzer is something the parser proper can interpret (e.g binary tree of expressions, or a sequence of commands in some other way).

    More here:
    http://en.wikipedia.org/wiki/Parsing

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    double post edited! !!
    Last edited by Masterx; 11-05-2008 at 11:41 PM.

  4. #4
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    tanx dear Mats, as you may know, i started coding the Dietels exercise "The simpletron computer emulation " its ok now , a nice console app that serves that exercise .
    after that , it made me write an emulator for other lets say "Abstract" or conceptual Computer Systems, such as CLA, (a single adressed computer with octal base ,featuring a 16 bit Accumulator,a 9 bit programer Conter, 512 memory lines(each 16 bits), a 2^4 or 16 instructions, 2 flags of z and n (representing Zero and Negetive flag),and a 9 bit address line)
    Code:
     _________________________
    |    Opcode |  Address    |
    |___________|_____________|
        4 bit         9 bit
    ______________________________
    |  Opcode| M| I|X|  Address  |
    |________|__|__|_|___________|
              0   0  0
    MIX=000

    Code:
    HLT //halt the program , used to state the end of the program!. only one instance is allowed( you cant use halt twice!) 
    LDA M //Loades value from M
    STA  Y//Store the Accumultor value to Y
    SBA  Y             //subtracts Accumultor value from Y
    ORA Y            //Ors the value of y with the value stored in  accumultor and places the result in acumultor 
    ANA Y           //Ands the value of y with the value stored in  accumultor and places the result in acumultor 
    JMP  Y         //jump to Y
    JNG Y         //if the value stored in acumultor is negetive then jump to Y
    JZR Y         //if the value stored in acumultor is zero then jump to Y
    CLA            //zeros the accumulator
    INC          //increments the value stored in accumulator
    DEC        //decrements as above
    ADA Y     //Adds the value of Y with the value of accumulator
    RED Y    //Reads  users input and places it in Y
    WRI  Y  //print s the value of Y

    a sample code which can be executed on a CLA computer is as follows:
    //simple mode of addressing
    Code:
          ORG 9      //stating where to start               
    9.   RED 5                                                 
    10. RED 6                                               
    11. LDA 5                                               
    12. SBA 6                                              
    13. JNG 17                                              
    14. WRI 5                                              
    15. JMP 20                                             
    17. WRI 6                                              
    20. HLT                                                 
    
    -----------------
         Machine Language    
     1110   000  000  000  101 
     1110   000  000  000  110 
     0001   000  000  000  101 
     0100   000  000  000  110 
     1000   000  000  001  111 
     1111   000  000  000  101 
     0111   000  000  010  000
     1111   000  000  000  110 
     0000   000  000  000  000
    ----------------------------------------
    //Symbolic mode of addressing
    Code:
    ORG 100
    L1:LDA 1
         ANA  Mask
         DEC
         JNG  L3
    L2:LDA 1
         ADA  sum
         STA  sum
         LDA  count
         DEC
         JZR  stop
         STA  count
         LDA L1
         INC
         STA L1
         STA L2
         JMP L1
    stop:WRI sum
         HLT
    sum:000
    count:007
    Mask:001
    ---------------------------
    //Index-Indirect mode of addressing
    MIX
    0 1 1 == LDA IX 10 //loads a memory location which XR+10 points to
    and etc...(a couple of other addressing modes )
    -----------------------------

    "pretty much like simpletron" but this one has more stuffs such as: labels,different modes of addressng ( immediate /indirect modes,...) & alot more! (its like Assembly language to some extend ! .)
    so thats what im planing to code ! , at first i thought i could be accomplished by using if , and loops , ( it is possible i think)but using parsing would make it rather easy and also look professional and debuging the app could be more conviniet too
    so thats all .
    Last edited by Masterx; 11-06-2008 at 01:26 AM.

  5. #5
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    any help on parsing in C++ in highly appreciated
    plz some one help me !
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I'm not familiar with the C++ STL very well, but basically you want to split up everything separated by a ' ' (space) character or a ':' character. In C++ it knows that a token or term is separated by a space, or an operator, or someother syntactical mark. Just look through the documentation for a function with strings that "tokenizes" or "splits" strings. You'll need to specify the delimiting characters.

    With C-style string you could easily just loop through this process:

    Read until you find one of the delimiting characters, then create a string large enough for that token and copy it. Then the delimiting character would be the next token if it has any meaning, and you just loop through the process until you reach the end of the file. Then every meaningful part of your program is stored in memory - and you can process it syntactically as mentioned above.

    edit: Moved to the C++ forum

  7. #7
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by sean View Post
    I'm not familiar with the C++ STL very well, but basically you want to split up everything separated by a ' ' (space) character or a ':' character. In C++ it knows that a token or term is separated by a space, or an operator, or someother syntactical mark. Just look through the documentation for a function with strings that "tokenizes" or "splits" strings. You'll need to specify the delimiting characters.

    With C-style string you could easily just loop through this process:

    Read until you find one of the delimiting characters, then create a string large enough for that token and copy it. Then the delimiting character would be the next token if it has any meaning, and you just loop through the process until you reach the end of the file. Then every meaningful part of your program is stored in memory - and you can process it syntactically as mentioned above.

    edit: Moved to the C++ forum
    many thanks sean ,Ok i 'll do it this way and will tell you about it.
    again tanx, i really appreciate your answer .
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing Struct Objects to Threads
    By pobri19 in forum C Programming
    Replies: 4
    Last Post: 04-18-2009, 06:25 AM
  2. draw tree graph of yacc parsing
    By talz13 in forum C Programming
    Replies: 2
    Last Post: 07-23-2006, 01:33 AM
  3. Parsing a formula
    By ashkya in forum C Programming
    Replies: 2
    Last Post: 02-27-2006, 08:39 AM
  4. Parsing for Dummies
    By MisterWonderful in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2004, 05:31 PM
  5. I hate string parsing with a passion
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-19-2002, 07:30 PM