470,870 Members | 1,452 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

Re: newbee help : parser in C


"vaib" <va************@gmail.comwrote in message
news:26**********************************@f24g2000 prh.googlegroups.com...
hi all . i am trying to build a parser in ANSI C . I made a lexical
analyser in the same language a while ago . It was a very simple lexer
that tokenised a simple C source code text file - basically a very
simple lexer . I had made use of hash tables for the data structure .
Now i want to know how to build a parser . I'll let you know what i
know about parser building - i'll need a grammar written in the form
of regular expressions , i would need to implement something called a
parse tree in a data structure of my choice and so on.
I don't know the formal approach to these things but I haven't come across
an RE grammar before, not for an entire language anyway.

The usual approach if you're not using external tools is to program using
'recursive descent' or top-down, whatever the term is. In this case the
grammar is built-in to the code. You will need a tokeniser too, which it
seems you already have.

Then you have all you need and top-down is really easy! But you have to
decide what the output will be, and typically this will be some tree
representation of the syntax.

I don't have any /simple/ examples however (and they're not in C). But if
you post a simple grammar example (which I can understand as I don't know
RE), then maybe I can give a bit more help.
--
Bartc
Jun 27 '08 #1
4 2753
On Jun 20, 3:41*pm, "Bartc" <b...@freeuk.comwrote:
"vaib" <vaibhavpang...@gmail.comwrote in message

news:26**********************************@f24g2000 prh.googlegroups.com...
hi all . i am trying to build a parser in ANSI C . I made a lexical
analyser in the same language a while ago . It was a very simple lexer
that tokenised a simple C source code text file - basically a very
simple lexer . I had made use of hash tables for the data structure .
Now i want to know how to build a parser . I'll let you know what i
know about parser building - i'll need a grammar written in the form
of regular expressions , i would need to implement something called a
parse tree in a data structure of my choice and so on.

I don't know the formal approach to these things but I haven't come across
an RE grammar before, not for an entire language anyway.

The usual approach if you're not using external tools is to program using
'recursive descent' or top-down, whatever the term is. In this case the
grammar is built-in to the code. You will need a tokeniser too, which it
seems you already have.

Then you have all you need and top-down is really easy! But you have to
decide what the output will be, and typically this will be some tree
representation of the syntax.

I don't have any /simple/ examples however (and they're not in C). But if
you post a simple grammar example (which I can understand as I don't know
RE), then maybe I can give a bit more help.

--
Bartc
all right . thank you so much . i really find people here who help me
all the time .
Jun 27 '08 #2
vaib wrote:
"Bartc" <b...@freeuk.comwrote:
>"vaib" <vaibhavpang...@gmail.comwrote in message
>>hi all . i am trying to build a parser in ANSI C . I made a
lexical analyser in the same language a while ago . It was a
very simple lexer that tokenised a simple C source code text
file - basically a very simple lexer . I had made use of hash
tables for the data structure .

Now i want to know how to build a parser . I'll let you know
what i know about parser building - i'll need a grammar written
in the form of regular expressions , i would need to implement
something called a parse tree in a data structure of my choice
and so on.

I don't know the formal approach to these things but I haven't
come across an RE grammar before, not for an entire language
anyway.
.... snip ...
>
all right . thank you so much . i really find people here who
help me all the time .
I suggest you look at the source for the portable Pascal compiler,
available on the ISO Pascal pages. You can find out about that on
comp.lang.pascal.ansi-iso newsgroup. It uses recursive descent.

Similar techniques will teach you much more than just using lex and
yacc. The first step is a useful lexer, that separates out
reserved words, identifiers, numerics, char symbols, etc. There is
no need for hash tables here. When you get to things that develop
symbol tables etc. that is another matter, and I recommend looking
at my hashlib, available at:

<http://cbfalconer.home.att.net/download/hashlib.zip>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #3
On Jun 26, 2:47*am, CBFalconer <cbfalco...@yahoo.comwrote:
vaib wrote:
"Bartc" <b...@freeuk.comwrote:
"vaib" <vaibhavpang...@gmail.comwrote in message
>hi all . i am trying to build a parser in ANSI C . I made a
lexical analyser in the same language a while ago . It was a
very simple lexer that tokenised a simple C source code text
file - basically a very simple lexer . I had made use of hash
tables for the data structure .
>Now i want to know how to build a parser . I'll let you know
what i know about parser building - i'll need a grammar written
in the form of regular expressions , i would need to implement
something called a parse tree in a data structure of my choice
and so on.
I don't know the formal approach to these things but I haven't
come across an RE grammar before, not for an entire language
anyway.

... snip ...
all right . thank you so much . i really find people here who
help me all the time .

I suggest you look at the source for the portable Pascal compiler,
available on the ISO Pascal pages. *You can find out about that on
comp.lang.pascal.ansi-iso newsgroup. *It uses recursive descent.

Similar techniques will teach you much more than just using lex and
yacc. *The first step is a useful lexer, that separates out
reserved words, identifiers, numerics, char symbols, etc. *There is
no need for hash tables here. *When you get to things that develop
symbol tables etc. that is another matter, and I recommend looking
at my hashlib, available at:

* *<http://cbfalconer.home.att.net/download/hashlib.zip>

--
*[mail]: Chuck F (cbfalconer at maineline dot net)
*[page]: <http://cbfalconer.home.att.net>
* * * * * * Try the download section.

** Posted fromhttp://www.teranews.com**
thank you for replying . right now i am reading 'lex and yacc' . i
plan to use yacc for generating a parser and then read its code ( ie ,
the generated parser's code ) and then continue my parser construction
study from the 'dragon book' . then i hope to have a clear picture in
my mind as to what 'exactly' has to be done in parser coding . am i
right in my approach ?? i generally avoid reading the code of the
already stable compilers sice it takes a lot of time for me to do that
and to figure out the complete code (...even of the front-end ) . do
you think my approach is right or is there a speedier way to it ?
thank you again for replying .
Jun 27 '08 #4
vaib wrote:
CBFalconer <cbfalco...@yahoo.comwrote:
.... snip ...
>
>I suggest you look at the source for the portable Pascal compiler,
available on the ISO Pascal pages. You can find out about that on
comp.lang.pascal.ansi-iso newsgroup. It uses recursive descent.

Similar techniques will teach you much more than just using lex and
yacc. The first step is a useful lexer, that separates out
reserved words, identifiers, numerics, char symbols, etc. There is
no need for hash tables here. When you get to things that develop
symbol tables etc. that is another matter, and I recommend looking
at my hashlib, available at:

<http://cbfalconer.home.att.net/download/hashlib.zip>

thank you for replying . right now i am reading 'lex and yacc'. i
plan to use yacc for generating a parser and then read its code (ie,
the generated parser's code) and then continue my parser construction
study from the 'dragon book'. then i hope to have a clear picture in
my mind as to what 'exactly' has to be done in parser coding. am i
right in my approach ?? i generally avoid reading the code of the
already stable compilers sice it takes a lot of time for me to do
that and to figure out the complete code (...even of the front-end).
do you think my approach is right or is there a speedier way to it?
I am darned if I know. I went through this process about 30 to 40
years ago, so my memory of it is dim.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Newbee | last post: by
3 posts views Thread by Himanshu Garg | last post: by
2 posts views Thread by Newbee Adam | last post: by
2 posts views Thread by Martin Hvidberg | last post: by
5 posts views Thread by consternation | last post: by
5 posts views Thread by Antoninus Twink | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.