[C]Parsing

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Post Reply
User avatar
Halifax
Sir Posts-A-Lot
Posts: 225
Joined: Mon 01 Jan, 2007 10:39 am
Location: Pennsylvania, US

[C]Parsing

Post by Halifax »

Alright I have been looking into parsing and things I have found a lot of information. Mostly it appears that bottom-up parsing is harder than top-down parsing, but top-down is easier.

I was wondering what some of you people who have parsed languages before used/think what's best?

One thing I don't get about bottom-up parsing is doing it with tables. If someone could explain this to me then that would help a lot.
User avatar
Dwedit
Maxcoderz Staff
Posts: 579
Joined: Wed 15 Dec, 2004 6:06 am
Location: Chicago!
Contact:

Post by Dwedit »

Find someone else's working parser then hack the crap out of it until you figure out how it works.
You know your hexadecimal output routine is broken when it displays the character 'G'.
User avatar
Halifax
Sir Posts-A-Lot
Posts: 225
Joined: Mon 01 Jan, 2007 10:39 am
Location: Pennsylvania, US

Post by Halifax »

:) That was exactly what I wasn't look forward to.
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Post by tr1p1ea »

Well, it really depends on what you are parsing. Are you writing a straight-up assembler or a compiler-of-sorts or something else?
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
User avatar
Halifax
Sir Posts-A-Lot
Posts: 225
Joined: Mon 01 Jan, 2007 10:39 am
Location: Pennsylvania, US

Post by Halifax »

Compiler of sorts ;) which I would not like to annouce at this moment, but you know what it is. I have heard and confirmed that GCC uses a top-down parser is the latest 4.x versions. So it seems like top-down is good enough for a semi-complicated language such as C.
User avatar
Dwedit
Maxcoderz Staff
Posts: 579
Joined: Wed 15 Dec, 2004 6:06 am
Location: Chicago!
Contact:

Post by Dwedit »

The tools you need to create parsers or compilers are Flex and Bison. But I've even taken a class about parsers, and still have no clue how to use them. I think that's where "Look at someone else's Flex and Bison code" comes into play.
You know your hexadecimal output routine is broken when it displays the character 'G'.
User avatar
Timendus
Calc King
Posts: 1729
Joined: Sun 23 Jan, 2005 12:37 am
Location: Netherlands
Contact:

Post by Timendus »

Usually you have a scanner that breaks the input up in tokens (which is what flex is used for) which are then passed on to the parser (which you can make with bison). The scanner will look for syntactic correctness and stuff, the parser will usually build an internal model of whatever you're parsing. When the model's complete, you can output it in another form (something you can pass on to an assembler, possibly :)), give statistics about it, or whatever you were planning to do.
http://clap.timendus.com/ - The Calculator Link Alternative Protocol
http://api.timendus.com/ - Make your life easier, leave the coding to the API
http://vera.timendus.com/ - The calc lover's OS
User avatar
Halifax
Sir Posts-A-Lot
Posts: 225
Joined: Mon 01 Jan, 2007 10:39 am
Location: Pennsylvania, US

Post by Halifax »

Alright I have heard of everything you guys have suggested. Flex, Bison, YACC, and all that stuff. Those tools output crap as the syntax becomes harder and more complex which is why GCC devs switched to a hand written parser for C in 4.x and a hand written parser for C++ in 3.x and up.

I don't want to use Lexer or Parser generators. I have got a hold on top-down parsing and wrote my first one yesterday for a dummy language and top-down simplifies parsing a lot, but Bottom-up with a table can put top-down parsing to shame I thinking.

Although I guess I will just try a recursive top-down parser if I can't learn bottom-up :/
Post Reply