Re: Parser Help...



This sounds like a class assignment, is it? I don't want to give you too
much help, but I can give you a few pointers. Try googling for
"recursive-descent parser" - that's what I'm describing in this message, and
it's by far the easiest type to make by hand. I'm sure there are some good
beginning tutorials for this kindof thing on, say, flipcode or wherever.

But what you probably want to do is tokenise which is splitting the string
up into tokens, which are the individual "items" that have meaning and you
can interpret, and then run through those keeping track of your state,
building a tree as you go.

For example, tokens would be "typedef", "struct, "{", "float", ";" etc.
Each of these has symantic meanings - the "{" for example tells you you're
entering a block, the ";" is a statement terminator, the "typedef" that
you're defining a type built via one or more of the following tokens, etc.
You can read all those into your own internal format so, for example, you
might be not dealing with strings any more except for the ones that have
names (the variable / structure names below). Because you know what the
tokens mean, you know what to expect to be after them (if it's wrong, fire a
syntax error, just as you'd expect your compiler to). I'd suggest building
it with a whole heap of small functions to handle each bit, eg a function
that knows how to handle typedef that expects a type afterwards, and passes
on the rest of the tokens to a method that returns a type having done its
own thing including passing on to other small methods.

Hope that all makes sense and hangs together. I'm trying to tell you how to
these things work rather than giving you a code solution for this particular
problem.

Cheers,

David



"Adrian" <adrian99@xxxxxxxxx> wrote in message
news:47524054$1@xxxxxxxxxxxxxxxxxxxxxxxxx

Hello all,

I recently encountered a problem which I cannot seem to solve
correctly. I am building a simple parser, which can extract
information from a predefined structure of code and arrange it in
to a treeview for easy processing later.

Basically, when a user writes code for this language, I want to
be able to extract certain parts, for example, in this language
their is a case where the user can define a "struct", like so:


typedef struct
{
float structure;
vector[3] position;
} MyStructure;

The struct is declared via "typedef struct", the structure
contents is listed inside the curly brackets (the type, then the
name) and finally the name of the structure is declared last
outside the ending curly bracket.

How can I extract this information (it will be amounst other
code) and order it into a treeview (the structure's name being
the parent node, and all the "elements" as children (with their
hint or data as their type?)

Thanks in advance, this one has really got me stuck. I appreciate
any help ;)
Adrian


.



Relevant Pages

  • Parser Help...
    ... I am building a simple parser, which can extract ... Basically, when a user writes code for this language, I want to ... The struct is declared via "typedef struct", ... contents is listed inside the curly brackets (the type, ...
    (borland.public.delphi.non-technical)
  • Re: Tokenizer interface
    ... > From time to time I've found it advantageous to extract all ... For my purposes, I prefer to scan tokens one by one. ... deciding whether a character was or wasn't a delimiter - and it also ...
    (comp.lang.c)
  • Re: parsing VB code with a regex
    ... snippets. ... it might have to find all occurrences of ... between the curly brackets and the characters inside them. ... it must only find tokens that are not quoted and not ...
    (microsoft.public.dotnet.general)
  • Re: grabbing text between two tokens
    ... Basically the text I am trying to extract is embedded inside two ... use lib ... occurences of these strings that are tagged by these tokens. ...
    (perl.beginners)
  • Re: BASIC Tokens
    ... a line) and using tokens to flag the type of immediately following data (so ... typedef struct literal_s { ... char** string; ...
    (comp.sys.cbm)