Results 1 to 3 of 3

Thread: Lexical analysis using stack implementation

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    228

    Lexical analysis using stack implementation

    My goal is to scan a file and check whether the given file is well-formed or not.

    for example

    Code:
    <test>
    <yeah>
    <yahoo> bla blabla </yahoo>
    </yeah>
    what ever <s> it is </s>
    <br/> test
    </test>

    i have parsed every single tag into the stack now I'm at the point where I have to check whether all the tag is paired except the tag without closing like <br/>

    now my stack look like the following
    [8] = </test>
    [7] = <br/>
    [6] = </s>
    [5] = <s>
    [4] = </yeah>
    [3] = </yahoo>
    [2] = <yahoo>
    [1] = <yeah>
    [0] = <test>
    .......
    and so on.

    Can you guys help me good algorithm to check whether or not all the tag is paired.
    I'm not expecting the code to make this happen and I'm sure you won't give that to me, I just need good algorithm or steps that can make this happen. Been thinking for quite sometime but I cant figure the easy way to do this.

    Thank you in advance
    Last edited by ayahnabunda; Sep 17th, 2008 at 06:54 AM.
    effort effort effort and still effort

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Lexical analysis using stack implementation

    Create another stack object.

    Itterate through your list of tags
    Every time you get an opening tag push it on the stack.
    Every time you get a closing tag make sure there's it's opening tag on the top of the stack (if it isn't then the file isn't well-formed). If there is a corisponding opening tag on the stack then pop it off.
    If you get a tag such as <br/> that appears on its own ignore it (or maybe do your own check specific to what you're parsing.

    When you get to the end of the list of tags then if there's nothing on the stack the file is ok, if there's stuff still on the stack this means there are still open tags, so the file isn't well-formed.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  3. #3
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Lexical analysis using stack implementation

    For your file listed the stack would have the following tags as you itterated through each one:

    -Nothing-
    Test
    Yeah, Test
    Yahoo, Yeah, Test
    Yeah, Test
    Test
    s, Test
    Test
    -Nothing-
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width