473,769 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing a Parser

Hi

I'm trying to build a parser for a file I create. The file format is as
follow:

IDENTIFIER = NUMBER STRING STRING;

COMPOSITE = STRING { ITEM [, ITEM, ...] };

ITEM = NUMBER IDENTIFIER|COMP OSITE

A file example is as follow:

eggs_with_bacon = 1 { 2 eggs, 1 bacon_strip };
eggs = 0.02 "Egg" "Unit";
bagon_strip = 0.04 "Bacon strip" "Unit";

(My idea was to use it as means to calculate costs of buildings, so you
can detail it to the level of bricks and mortar, and get the total price
of the building, but it can be used as a means to calculate any kind of
costs).

I have already started building a parser, using bison/flex, with rules:

(flex rules)

[[:digit:]]+("."[[:digit:]]*)? return NUMBER;
[:alpha:][[:alnum:]_]* return IDENTIFIER;
\"[^\"]\" return STRING;
\= return EQUAL;
\{ return LBRACE;
\} return RBRACE;
\, return COMMA;
\; return SEMICOLON;
^#.*\n$ /* eat comments */
[.\n]+ /* eat this */

(end flex rules)

(bison rules)

line: /* empty */
| line description;

description: short-description
| long-description;

short-description: IDENTIFIER EQUAL NUMBER STRING STRING SEMICOLON;

long-description: IDENTIFIER EQUAL NUMBER LBRACE items RBRACE SEMICOLON;

items: item
| items COLON item;

item: NUMBER IDENTIFIER;

(end bison rules)

I added some dummy actions to the bison rules, just to debug it, and I'm
getting "parse error" after the first line is parsed. I think there are
something I'm missing, the parser should get to state 0 after reading
the first rule, but it is going to other state, and gives the 'parse
error'. Can someone tells me what is wrong with the rules?

TIA
P.S.: Sorry for the "engrish".
--
..O. Cesar A. K. Grossmann ICQ UIN: 35659423
...O http://www.LinuxByGrossmann.cjb.net/
OOO Quidquid Latine dictum sit, altum viditur
Nov 14 '05 #1
9 1833
Cesar A. K. Grossmann wrote:
Hi

I'm trying to build a parser for a file I create. The file format is as
follow:

IDENTIFIER = NUMBER STRING STRING;

<snip>

(flex rules)

I must admit that Flex is great, but in this newsgroup C rules!

Case

<snip>


Nov 14 '05 #2
Case wrote:

I must admit that Flex is great, but in this newsgroup C rules!


There is a better group for this kind of question? The other groups
where flex/bison or lex/yacc are cited are about "writing a compiler",
and I think that this is not the case.

TIA
--
..O. Cesar A. K. Grossmann ICQ UIN: 35659423
...O http://www.LinuxByGrossmann.cjb.net/
OOO Quidquid Latine dictum sit, altum viditur
Nov 14 '05 #3
Cesar A. K. Grossmann wrote:
Case wrote:

I must admit that Flex is great, but in this newsgroup C rules!

There is a better group for this kind of question? The other groups
where flex/bison or lex/yacc are cited are about "writing a compiler",
and I think that this is not the case.


You'll get better advice in those groups than here.
True, you will have little interest in many of the topics
discussed among compiler writers -- but parsing *is* a
topic they're likely to know a good deal about, and if
you're nice to them they may share that knowledge.

comp.lang.c is (in)famous for its brush-offs and its
(uneven) insistence on topicality. Please understand that
there's an excellent reason for this, to wit: C is a powerful
language with wide applicability. It can be used (sometimes
with extensions) to implement device drivers, keep track of
your bowling league scores, manage the restocking schedules
for supermarket shelves, write "Quake," solve differential
equations, implement PostScript interpreters, and, yes,
parse languages according to grammars. If all these topics
were welcomed in comp.lang.c, the group would become useless,
effectively indistinguishab le from alt.chat.anyold topic.

... and that's why comp.lang.c tries to avoid "domain-
specific" discussion: There would be far too much of it to
sift through. It would drown out our sober discussions
of how (rather than "where") to apply the language, not to
mention the flame fests and insult wars that are the group's
*real* raison d'etre.

--
Er*********@sun .com

Nov 14 '05 #4
Cesar A. K. Grossmann <ca************ ******@yahoo.co m.br> spoke thus:
There is a better group for this kind of question? The other groups
where flex/bison or lex/yacc are cited are about "writing a compiler",
and I think that this is not the case.


comp.unix.progr ammer, maybe...?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5
Eric Sosman <Er*********@su n.com> wrote in message news:<40******* *******@sun.com >...
<snip>
of how (rather than "where") to apply the language, not to
mention the flame fests and insult wars that are the group's
*real* raison d'etre.


Really? Are you trying to ignite a flame fest yourself?

Hmmm. That would be an interesting one, a flame fest regarding
whether or not the raison d'etre of c.l.c is flame fests.

On second thought, though, there's fertile enough ground discussing
hypothetical C implementations written by brilliant, delusional mental
patients on hardware with destructive-read magnetic core memory with
integer trap representations that transform entire continents into
radioactive wastelands...
Mark F. Haigh
mf*****@sbcglob al.net
Nov 14 '05 #6
Christopher Benson-Manica wrote:
Cesar A. K. Grossmann <ca************ ******@yahoo.co m.br> spoke thus:

There is a better group for this kind of question? The other groups
where flex/bison or lex/yacc are cited are about "writing a compiler",
and I think that this is not the case.

comp.unix.progr ammer, maybe...?


comp.compilers is probably better, if only for the moderator.

--
rh
Nov 14 '05 #7
Cesar A. K. Grossmann wrote:
Case wrote:

I must admit that Flex is great, but in this newsgroup C rules!
There is a better group for this kind of question?


comp.compilers.
The other groups
where flex/bison or lex/yacc are cited are about "writing a compiler",
and I think that this is not the case.


It is nevertheless true that comp.compilers will handle questions about
flex/bison & family. (And grammars.)

And the other obvious choice is comp.unix.progr ammer.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 14 '05 #8
"Cesar A. K. Grossmann" <ca************ ******@yahoo.co m.br> wrote in message news:<2k******* ****@uni-berlin.de>...
Hi

I'm trying to build a parser for a file I create. The file format is as
follow:

IDENTIFIER = NUMBER STRING STRING;

COMPOSITE = STRING { ITEM [, ITEM, ...] };

ITEM = NUMBER IDENTIFIER|COMP OSITE

A file example is as follow:

eggs_with_bacon = 1 { 2 eggs, 1 bacon_strip };
eggs = 0.02 "Egg" "Unit";
bagon_strip = 0.04 "Bacon strip" "Unit";

(My idea was to use it as means to calculate costs of buildings, so you
can detail it to the level of bricks and mortar, and get the total price
of the building, but it can be used as a means to calculate any kind of
costs).

I have already started building a parser, using bison/flex, with rules:

(flex rules)

[[:digit:]]+("."[[:digit:]]*)? return NUMBER;
[:alpha:][[:alnum:]_]* return IDENTIFIER;
\"[^\"]\" return STRING;
\= return EQUAL;
\{ return LBRACE;
\} return RBRACE;
\, return COMMA;
\; return SEMICOLON;
^#.*\n$ /* eat comments */
[.\n]+ /* eat this */

(end flex rules)

(bison rules)

line: /* empty */
| line description;

description: short-description
| long-description;

short-description: IDENTIFIER EQUAL NUMBER STRING STRING SEMICOLON;

long-description: IDENTIFIER EQUAL NUMBER LBRACE items RBRACE SEMICOLON;

items: item
| items COLON item;

item: NUMBER IDENTIFIER;

(end bison rules)

I added some dummy actions to the bison rules, just to debug it, and I'm
getting "parse error" after the first line is parsed. I think there are
something I'm missing, the parser should get to state 0 after reading
the first rule, but it is going to other state, and gives the 'parse
error'. Can someone tells me what is wrong with the rules?

TIA
P.S.: Sorry for the "engrish".

Also consider posting to the flex mailing list he*******@gnu.o rg.

That said, I'm not sure its got anyone awake on the other end these days.
Nov 14 '05 #9
Rob Thorpe wrote:

Also consider posting to the flex mailing list he*******@gnu.o rg.


ftp://mail.gnu.org/help-flex/2004-05

That list is full of SPAM. But thanks to all of you.

[]s
--
..O. Cesar A. K. Grossmann ICQ UIN: 35659423
...O http://www.LinuxByGrossmann.cjb.net/
OOO Quidquid Latine dictum sit, altum viditur
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5286
by: Karalius, Joseph | last post by:
Can anyone explain what is happening here? I haven't found any useful info on Google yet. Thanks in advance. mmagnet:/home/jkaralius/src/zopeplone/Python-2.3.5 # make gcc -pthread -c -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -I. -I./Include -DPy_BUILD_CORE -o Modules/python.o Modules/python.c gcc -pthread -c -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -I. -I./Include -DPy_BUILD_CORE -o...
0
273
by: darin dimitrov | last post by:
I am looking for an implementation of a multipart content parser for ..NET (http://www.faqs.org/rfcs/rfc2388.html). I suppose that the HttpWebRequest class uses such a parser in order to extract the parameters and uploaded files from the request stream. Correct me if I am wrong but these classes are intended for an internal of the framework. So my question is what would be the easiest way to implement such a parser in .NET ? Thanks,...
6
1805
by: Jan Danielsson | last post by:
Hello all, I guess this is a question for people who have written a parser. Does an XML parser ever need to be recursive? I mean like: &fo&bar;o; I know this particular example is in the XML specs, and it says that it will not happen. But are there some really wild constructions that
7
2441
by: beza1e1 | last post by:
I'm writing a parser for english language. This is a simple function to identify, what kind of sentence we have. Do you think, this class wrapping is right to represent the result of the function? Further parsing then checks isinstance(text, Declarative). ------------------- class Sentence(str): pass class Declarative(Sentence): pass class Question(Sentence): pass class Command(Sentence): pass
4
2810
by: siddharthkhare | last post by:
Hi All, I need to parse certain text from a paragraph (like 20 lines). I know the exact tags that I am looking for. my approach is to define a xml (config) file that defines what tag I am looking for and corresponding regular expression to search for the pattern. Xml file will also have a way to say what should be the pervious tag
59
3476
by: riva | last post by:
I am developing a compression program. Is there any way to write a data to file in the form of bits, like write bit 0 then bit 1 and then bit 1 and so on ....
1
1350
by: Matthew Wilson | last post by:
I'm working on two coroutines -- one iterates through a huge stream, and emits chunks in pieces. The other routine takes each chunk, then scores it as good or bad and passes that score back to the original routine, so it can make a copy of the stream with the score appended on. I have the code working, but it just looks really ugly. Here's a vastly simplified version. One function yields some numbers, and the other function tells me...
3
1771
by: Kinokunya | last post by:
Hi guys, My group and I will be working on our final year project, the scope to do a program/web-based application similar areas of functionalities like the PyLint and PyChecker; a Python syntax checker. We have no Python background, equipped only with some knowledge of Java and Dot net. We did some research on PyLint and found out that there are 2 common modules that PyLint & PyChecker are using, namely logilab-astng and
1
3862
by: Mudcat | last post by:
In short what I'm trying to do is read a document using an xml parser and then upload that data back into a database. I've got the code more or less completed using xml.etree.ElementTree for the parser and dbi/ odbc for my db connection. To fix problems with unicode I built a work-around by mapping unicode characters to equivalent ascii characters and then encoding everything to ascii. That allowed me to build the application and debug...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.