Thread: Developing a parser

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    Developing a parser

    I want to write a C program that can parse input and give corresponding output like xml or html. Can someone also give a SMALL example as to how this works.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What's your input look like?

    There's only the usual bunch of functions to work with, fgetc(), fgets(), fread(), fscanf() etc etc.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    like perhaps i want this:
    the user inputs the <p>tag when ever he wants a new line written to a file like
    FILE *fp;
    fp=fopen("file.txt","a");
    RECIEVE INPUT
    PARSE FOR <p>'s
    OUTPUT TO fp
    fclose(fp);

    something like that, but I will define the tags (library of tags) later
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Lynux-Penguin
    like perhaps i want this:
    the user inputs the <p>tag when ever he wants a new line written to a file like
    FILE *fp;
    fp=fopen("file.txt","a");
    RECIEVE INPUT
    PARSE FOR <p>'s
    OUTPUT TO fp
    fclose(fp);

    something like that, but I will define the tags (library of tags) later
    Your output file is what exactly? Text? HTML? If it's just HTML, simply directly send the output. It's up to them to make sure their tags are correct. You can always set up "matched" vs "non-matched" or "paired" vs "non-paired" tags and count up the total when done saying "You have three unmatched <B> tags."

    I don't understand what your problem is. Don't you know how to use basic file input? Do you not know how to read input from a user, or what exactly is your problem?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I didn't want to post code
    basically
    I want the input from a user to be parsed for tags
    like webbrowsers do for HTML or some XML parsers, I can't find any example code anywhere, I am just curious as to the key behind parsers so that I may take advantage of it.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Um... It's not difficult:
    Code:
    while( c != EOF )
    {
        while( (c=fgetc(stdin))!= '<') )
        {
            if ( c != EOF )
                buffer[x++]=c;
        }
        while( (c=fgetc(stdin))!= '>') )
        {
            if ( c != EOF )
                tag[y++] = c;
        }
    }
    Vola. Parser.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    thanks, that helps a lot
    but, when is EOF reached by the user?
    is it when the return key is striked or what?
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's EOF. Typically this is CTRL+Z or CTRL+D or something similar, OS dependant. You could subsitute EOF for whatever you wanted.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    thanks again!
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Parser Help
    By Barnzey in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2005, 12:10 PM
  3. Calculator Parser
    By mrafcho001 in forum C++ Programming
    Replies: 1
    Last Post: 07-17-2005, 11:38 AM
  4. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 09:54 AM
  5. Parser - need help!
    By thelma in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:06 PM