Re: Writing a Parser
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Re: Writing a Parser




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Re: Writing a Parser  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-01-04 08:20 AM

Cesar A. K. Grossmann <cakgguard-usenet2004@yahoo.com.br> wrote:
> Jens.Toerring@physik.fu-berlin.de wrote: 
[vbcol=seagreen]
> My error here. I'm still having trouble to understand this BNF thing, so
> I will make a new try.

> I have only two kinds of lines in the files I'm trying to parse that
> matters to the program, the ones that describe an atomic item and the
> ones that describe a composite one:

> line := atomic_item | composite_item

> The first part is a line that explains an "atomic" item (one that has no
> parts):

> atomic_item := IDENTIFIER '=' NUMBER STRING STRING ';'

> and a line that identifies a "composite" item, one that is made of
> several quantities of other itens, that can be "atomic" or "composite" ones:[/vbco
l]
[vbcol=seagreen]
> composite_item := IDENTIFIER '=' STRING '{' item [, item]* '}' ';'[/vbcol
]
[vbcol=seagreen]
> The "item" part is a description of the itens that made the composite
> item. You have a number and a identifier, that can be a composite or an
> atomic one:

> item := NUMBER STRING

> I think that the "terminals" that need to be recognized by the parser
> are: NUMBER, IDENTIFIER, STRING and the separators '=', ',', ';', '{'
> and '}'. The others (item, composite_item, atomic_item, item) are
> internal to the parsing activity.


I am still a bit confused about what you want to do so let me
summarize how I understand you:

You want to accept lines only that have the form

IDENTIFIER '=' rhs ';'

where 'rhs' can be

rhs := ( NUMBER STRING STRING ) | ( STRING '{' item [ , item ] * '}
' )

and 'item' in turn is

item := NUMBER IDENTIFIER

and _not_

item := NUMBER STRING

At least that's what I arrived at, taking into account the examples

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

you gave in the first post (actually, the first of these lines still
does not fit your rules because it starts with a NUMBER and not with
a string, do you perhaps meant

rhs := ( NUMBER STRING STRING ) | ( NUMBER '{' item [ , item ] * '}
' )

instead?)

If I understand you correctly, the parser should look similar to

line:    IDENTIFIER EQUAL rhs SEMICOLON
;

rhs:     NUMBER STRING STRING
| STRING LBRACE items RBRACE
;

items:   item
| items COMMA item
;

item:    NUMBER IDENTIFIER
;
 
[vbcol=seagreen]
> The line above is described by two rules. The first one is

> composite_item := IDENTIFIER '=' STRING '{' item [, item]* '}' ';'[/vbcol
]
[vbcol=seagreen]
> and the second one is

> item := NUMBER STRING

I still don't see that for two reasons. First, I can't figure out how
you can have a NUMBER at the start on the right hand side (i.e. after
the equal sign), going by those two rules. Second, in that line the
'item' thingies seems to be made up from

item := NUMBER IDENTIFIER

- there are no single or double quotes around 'eggs' or 'bacon_strip',
so they don't seem to be STRINGs but IDENTIFIERs, and in the next
(now snipped) lines they appear on the left hand side, what makes
them look even more like IDENTIFIERs.

> STRING := \"[^\"]+\"
>
> But the lexer is becoming a little confused about the distinction
> between a IDENTIFIER and a STRING. I changed the '"' part by '(' and
> ')', and the lexer worked, but I think that enclosing strings in '"' is
> more logical, so I'm trying to change that back.

On this I don't dare to comment, not having seen the real code you
had trouble with. But basically identifying STRING type tokens by
having them enclosed in double quotes should work.

Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:26 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register